-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance.c
134 lines (118 loc) · 3.49 KB
/
balance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* StickGBC by Matt Comben is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
* To view a copy of this license, visit
* http://creativecommons.org/licenses/by-nc-nd/4.0/.
*/
// Methods to manipulate 32-bit balance
#include "main.h"
#include "balance.h"
/*
* has_money
*
* Whether the player's balance exceeds a certain amount (or is high enough for a purchase)
*
* @param amount_h Upper 16-bit value of the amount to check
* @param amount_l Lower 16-bit value of the amount to check
*
* @returns 1 if the user has enough balance. 0 if not.
*/
BOOLEAN has_money(UINT16 amount_h, UINT16 amount_l) NONBANKED
{
UINT16 remainder = amount_h;
// If lower balance doesn't contain enough,
// add 1 to required amount in higher int
if (amount_l > game_state.balance[0U])
{
remainder = amount_h + 1U;
}
// If higher balance int doesn't
// contain enough, return 0
if (remainder > game_state.balance[1U])
{
return 0U;
}
// Otherwise, return 1
return 1U;
}
/*
* remove_money
*
* Removes a 32-bit value from the player's balance
*
* @param amount_h Upper 16-bit value of the amount to remove
* @param amount_l Lower 16-bit value of the amount to remove
*
* @returns 1 if the amount was removed. 0 if not.
*/
BOOLEAN remove_money(UINT16 amount_h, UINT16 amount_l) NONBANKED
{
UINT16 remainder = amount_h;
// If lower balance doesn't contain enough,
// add 1 to required amount in higher int
if ((game_state.balance[0U] - amount_l) > game_state.balance[0U])
{
remainder = amount_h + 1U;
}
// If higher balance int doesn't
// contain enough, return 0
if ((game_state.balance[1U] - remainder) > game_state.balance[1U])
{
// Check for overflow
return 0U;
}
// Otherwise, remove the money and return:
// If the lower did not require carrying,
// remove it from the lower balance
if (amount_h == remainder)
{
game_state.balance[0] -= amount_l;
}
else
{
// Else, remove the current balance from lower from
// balance, reset and remove the remainder
amount_l -= game_state.balance[0];
game_state.balance[0] = 0xFFFFU - amount_l;
}
// Remove the upper balance
game_state.balance[1] -= remainder;
return 1U;
}
/*
* add_money
*
* Adds a 32-bit value to the player's balance
*
* @param amount_h Upper 16-bit value of the amount to add
* @param amount_l Lower 16-bit value of the amount to add
*/
void add_money(UINT16 amount_h, UINT16 amount_l) NONBANKED
{
UINT16 overflow = amount_h;
// Check for overflow in lower
if ((game_state.balance[0] + amount_l) < game_state.balance[0])
{
// Check for possible overflow of amount_h
if (overflow == 0xFFFFU)
{
game_state.balance[0] = 0xFFFFU;
game_state.balance[1] = 0xFFFFU;
return;
}
overflow += 1U;
// Make the lower amount the difference after the overflow and set lower balance
// to 0
amount_l = amount_l - (0xFFFFU - game_state.balance[0]);
game_state.balance[0] = 0U;
}
// Check for overflow in higher
if ((game_state.balance[1] + overflow) < game_state.balance[1])
{
game_state.balance[0] = 0xFFFFU;
game_state.balance[1] = 0xFFFFU;
return;
}
// Increase lower int of balance
game_state.balance[0] += amount_l;
game_state.balance[1] += overflow;
}