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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
GiveMoney::
ld a, 3
call AddMoney
ld bc, MaxMoney
ld a, 3
call CompareMoney
jr z, .not_maxed_out
jr c, .not_maxed_out
ld hl, MaxMoney
ld a, [hli]
ld [de], a
inc de
ld a, [hli]
ld [de], a
inc de
ld a, [hli]
ld [de], a
scf
ret
.not_maxed_out
and a
ret
MaxMoney:
dt MAX_MONEY
TakeMoney::
ld a, 3
call SubtractMoney
jr nc, .okay
; leave with 0 money
xor a
ld [de], a
inc de
ld [de], a
inc de
ld [de], a
scf
ret
.okay
and a
ret
CompareMoney::
ld a, 3
CompareFunds:
; a: number of bytes
; bc: start addr of amount (big-endian)
; de: start addr of account (big-endian)
push hl
push de
push bc
ld h, b
ld l, c
ld c, 0
ld b, a
.loop1
dec a
jr z, .done
inc de
inc hl
jr .loop1
.done
and a
.loop2
ld a, [de]
sbc [hl]
jr z, .okay
inc c
.okay
dec de
dec hl
dec b
jr nz, .loop2
jr c, .set_carry
ld a, c
and a
jr .skip_carry
.set_carry
ld a, 1
and a
scf
.skip_carry
pop bc
pop de
pop hl
ret
SubtractMoney:
ld a, 3
SubtractFunds:
; a: number of bytes
; bc: start addr of amount (big-endian)
; de: start addr of account (big-endian)
push hl
push de
push bc
ld h, b
ld l, c
ld b, a
ld c, 0
.loop
dec a
jr z, .done
inc de
inc hl
jr .loop
.done
and a
.loop2
ld a, [de]
sbc [hl]
ld [de], a
dec de
dec hl
dec b
jr nz, .loop2
pop bc
pop de
pop hl
ret
AddMoney:
ld a, 3
AddFunds:
; a: number of bytes
; bc: start addr of amount (big-endian)
; de: start addr of account (big-endian)
push hl
push de
push bc
ld h, b
ld l, c
ld b, a
.loop1
dec a
jr z, .done
inc de
inc hl
jr .loop1
.done
and a
.loop2
ld a, [de]
adc [hl]
ld [de], a
dec de
dec hl
dec b
jr nz, .loop2
pop bc
pop de
pop hl
ret
GiveCoins::
ld a, 2
ld de, wCoins
call AddFunds
ld a, 2
ld bc, .maxcoins
call CompareFunds
jr c, .not_maxed
ld hl, .maxcoins
ld a, [hli]
ld [de], a
inc de
ld a, [hli]
ld [de], a
scf
ret
.not_maxed
and a
ret
.maxcoins
bigdw MAX_COINS
TakeCoins::
ld a, 2
ld de, wCoins
call SubtractFunds
jr nc, .okay
; leave with 0 coins
xor a
ld [de], a
inc de
ld [de], a
scf
ret
.okay
and a
ret
CheckCoins::
ld a, 2
ld de, wCoins
jp CompareFunds
|