-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.asm
581 lines (481 loc) · 12.3 KB
/
functions.asm
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
; FUNCTIONS
check_level_n_upgrade:
;param ax if == 0 means reset levels to level 1
test ax, ax ;
JE .level1
; check if all the enemies are dead, if yes level up
mov ax, 0
mov bx, enemies
mov cx, N_ENEMIES
.check_all_enemy_dead:
or ax, [bx + Player.draw]
add bx, QUANTA_PLAYER_SIZE
loop .check_all_enemy_dead
test ax, ax
JE .not_end
ret
.not_end:
inc byte [level] ; level up
cmp byte [level], 1
JE .level1
cmp byte [level], 2
JE .level2
cmp byte [level], 3
JE .level3
; if level 3 is cleared, you won the game
mov word bp, won_game
mov cx, won_game_len
call write_string
JMP exit
;
; init each levels elements, and ship attributes
.level1:
mov byte [level], 1
mov byte [player + Player.fire_rate], 2
mov byte [player + Player.bullet_index], 0
mov byte [player + Player.move_speed], 1
mov byte [player + Player.bullet_color], 0x03
; get level background
LEA ax, [stone_image0]
mov [stone_image], ax
mov cx, N_ENEMIES
mov bx, enemies
.draw_enemy_ship1:
mov byte [bx + Player.fire_rate], 2
mov byte [bx + Player.bullet_index], 0
mov byte [bx + Player.bullet_color], 0x02
add bx, QUANTA_PLAYER_SIZE
loop .draw_enemy_ship1
; load map
LEA bx, [map0]
mov [map], bx
call draw_map
; load enemy ship
LEA bx, [enemy_ship_image0]
mov [enemy_ship_image], bx
; load player ship
LEA bx, [player_ship_image0]
mov [player_ship_image], bx
ret
.level2:
mov byte [player + Player.fire_rate], 2
mov byte [player + Player.bullet_index], 0
mov byte [player + Player.move_speed], 2
mov byte [player + Player.bullet_color], 0x03
; get level background
LEA ax, [stone_image1]
mov [stone_image], ax
mov cx, N_ENEMIES
mov bx, enemies
.draw_enemy_ship2:
mov byte [bx + Player.fire_rate], 5
mov byte [bx + Player.bullet_index], 0
mov byte [bx + Player.bullet_color], 0x04
add bx, QUANTA_PLAYER_SIZE
loop .draw_enemy_ship2
LEA bx, [map1]
mov [map], bx
call draw_map
LEA bx, [enemy_ship_image1]
mov [enemy_ship_image], bx
LEA bx, [player_ship_image1]
mov [player_ship_image], bx
JMP .ret
.level3:
mov byte [player + Player.fire_rate], 2
mov byte [player + Player.bullet_index], 0
mov byte [player + Player.move_speed], 3
; get level background
LEA ax, [stone_image1]
mov [stone_image], ax
mov cx, N_ENEMIES
mov bx, enemies
.draw_enemy_ship3:
mov byte [bx + Player.fire_rate], 7
mov byte [bx + Player.bullet_index], 0
mov byte [bx + Player.bullet_color], 0x04
add bx, QUANTA_PLAYER_SIZE
loop .draw_enemy_ship3
LEA bx, [map2]
mov [map], bx
call draw_map
LEA bx, [enemy_ship_image2]
mov [enemy_ship_image], bx
LEA bx, [player_ship_image2]
mov [player_ship_image], bx
JMP .ret
.ret:
; show level up msg
mov word bp, lvlup_msg
mov cx, lvlup_msg_len
cli
call write_string
MOV CX, 20 ;0FH
MOV DX, FRAME_DELAY
mov ax, 0x8600
int 0x15
sti
ret
; handle keystrokes
keyboard_isr:
pusha
in al, 0x60
test al, 0x80
JNE .ret
test al, al
JE .ret
xor cx, cx
mov cl, [player + Player.move_speed]
cmp al, 0x13 ; 'r'
JZ .reset
; cmp al, 0x21 ; 'f'
; JZ .pause
cmp al, 0x11 ; 'w'
JZ .move_up
cmp al, 0x1E ; 'a'
JZ .move_left
cmp al, 0x1F ; 's'
JZ .move_down
cmp al, 0x20 ; 'd'
JZ .move_right
JMP .ret
.reset:
; dont reset if player ship is alive
mov bl, [player + Player.draw]
test bl, bl
JNZ .ret
mov ax, 0
call check_level_n_upgrade
mov al, 0x20
out 0x20, al
popa
JMP forever_loop
; .pause:
; hlt
mov word bp, lvlup_msg
mov cx, lvlup_msg_len
call write_string
JMP .ret
.move_down:
cmp word [player+Player.ship_y], HEIGHT - CUSTOM_IMAGE_SIZE
JE .ret
inc word [player+Player.ship_y]
loop .move_down
JMP .ret
.move_left:
cmp word [player+Player.ship_x], 0
JE .ret
dec word [player+Player.ship_x]
loop .move_left
JMP .ret
.move_up:
cmp word [player+Player.ship_y], 0
JE .ret
dec word [player+Player.ship_y]
loop .move_up
JMP .ret
.move_right:
cmp word [player+Player.ship_x], WIDTH - CUSTOM_IMAGE_SIZE
JE .ret
inc word [player+Player.ship_x]
loop .move_right
JMP .ret
.ret:
mov al, 0x20
out 0x20, al
popa
iret
; move bullets in their direction
move_bullets:
; param bx has struct player
; param dx has bullet direction
mov di, 0
cmp di, [bx + Player.bullet_index] ; nothing in array
JE .ret
cmp si, [bx + Player.bullet_index] ; iterated over entire array
JGE .ret
cmp dx, 0
JE .sub_
add [bx + Player.bullet_xy + si], word WIDTH * BULLET_LENGTH*2 ; check word bounds
JMP .add_
.sub_:
sub [bx + Player.bullet_xy + si], word WIDTH * BULLET_LENGTH *2 ; check word bounds
.add_:
JC .remove_from_array ; either of add/sub ops is out of bounds
mov ax, word WIDTH * HEIGHT ; check resolution bounds
cmp ax, [bx + Player.bullet_xy + si]
JNC .dont_remove_from_array
.remove_from_array:
; remove current si from array if it is OOB
mov di, [bx + Player.bullet_index]
mov di, [bx + Player.bullet_xy + di - 2]
mov [bx + Player.bullet_xy +si], di
sub word [bx + Player.bullet_index], 2
;
.dont_remove_from_array:
add word si, 2
JMP move_bullets
;
.ret:
ret
;
draw_ship:
; pusha
; param bx stores structure
; param dx stores image
; param di makes color == BG_COLOR if it is BG COLOR
; dont draw ship if ship is dead
cmp byte [bx + Player.draw], 0
JNE .draw
ret
.draw:
mov cx, di
xor di, di
add di, [bx + Player.ship_x]
mov ax, [bx + Player.ship_y]
call draw_image
; add bullet!!
cmp byte [bx + Player.draw], 0
JE .draw_ship_WO_fire
; bullets in the airspace should be less that fire rate
mov ax, [bx + Player.fire_rate]
cmp word [bx + Player.bullet_index], ax
JGE .draw_ship_WO_fire
add di, CUSTOM_IMAGE_SIZE/2 + WIDTH *CUSTOM_IMAGE_SIZE/2
mov si, 0
.redundant_bullet_check_loop:
; dont add bullet to array if already present
cmp [bx + Player.bullet_xy + si], di
JE .draw_ship_WO_fire
add si, 2
cmp si, [bx + Player.bullet_index]
JLE .redundant_bullet_check_loop
; store bullet loc and increase index
mov si, [bx + Player.bullet_index]
add word [bx + Player.bullet_index], 2
.draw_ship_WO_fire:
mov word [bx + Player.bullet_xy + si], di
ret
draw_image:
; param di has x
; param ax has y
; param cx is BG color then dont draw
; param dx has image struc
imul ax, WIDTH
add di, ax
push di
;
push cx
cmp cx, BG_COLOR
mov cx, 400
mov si, dx
mov dx, 0
JE .ret
CLD;
; copy the pixel from image to it's place on the screen
.loop:
pop ax
push ax
lodsb ; load byte from [si] to al, inc si
cmp dx, CUSTOM_IMAGE_SIZE ; works for 20x20 images
JL .same_row
add di, WIDTH
sub di, dx
xor dx, dx
.same_row:
CLD
inc dx
cmp al, BG_COLOR
JNE .stos
mov al, [es:di]
.stos:
stosb
loop .loop
.ret:
pop di
pop di
sti
ret
; Draw bullets
draw_bullets:
; param bx contain player struc
mov si, 0
cmp si, [bx + Player.bullet_index]
JZ .ret
.loop:
mov di, [bx + Player.bullet_xy + si]
mov dx, BULLET_LENGTH
.make_bullet:
cmp di, 0
JE .dont_make_bullet
mov al, [bx + Player.bullet_color]
mov [es:di], al
.dont_make_bullet:
sub di, WIDTH
dec dx
cmp dx, 0
JNZ .make_bullet
add si, 2
cmp si, [bx + Player.bullet_index]
JL .loop
.ret:
ret
fill_screen:
; uncomment to use a fixed color as BG
; xor di, di
; mov cx, WIDTH*HEIGHT
; mov al, 20 ;0x01 ;BG_COLOR
; rep stosb
; mov si, stone_image
; JMP .ret
; draw_image:
; param di has x
; param ax has y
; param cx is BG color then dont draw
; param dx has image struc
mov ax, 0
.draw_stones_y:
mov di, 0 ;CUSTOM_IMAGE_SIZE
.draw_stones_x:
mov dx, [stone_image]
mov cx, 0xFFFF
pusha
call draw_image
popa
add di, CUSTOM_IMAGE_SIZE
CMP di, WIDTH-CUSTOM_IMAGE_SIZE
JLE .draw_stones_x
add ax, CUSTOM_IMAGE_SIZE
cmp ax, HEIGHT-CUSTOM_IMAGE_SIZE
JLE .draw_stones_y
.ret:
ret
bullet_hit:
; checks if a bullet is hit
; si param stores, player struc of attacker
; bx param stores, player struc of victim
; returns di, 0 == no hit, 1 == hit
pusha
push bp
; check if no bullets
mov bp, 0
cmp bp, [si + Player.bullet_index]
JNE .bullet_loop
pop bp
mov di, 0xFFFF
popa
ret ; no_hit
; find x and y coord of each bullet and find overlap with victim
; by checking each boundry of the ship
.bullet_loop:
mov ax, [si + Player.bullet_xy + bp] ;, WIDTH ; ax Y dx X
mov cx, WIDTH
div cx
;ax has y, dx has x
mov cx, [bx + Player.ship_x]
cmp dx, cx
JL .try_next_bullet
add cx, CUSTOM_IMAGE_SIZE
cmp dx, cx
JG .try_next_bullet
mov cx, [bx + Player.ship_y]
cmp ax, cx
JL .try_next_bullet
add cx, CUSTOM_IMAGE_SIZE
cmp ax, cx
JG .try_next_bullet
pop bp
mov di, BG_COLOR
; bx ship is dead
mov byte [bx + Player.draw], 0
popa
ret ; hit
.try_next_bullet:
add bp, 2
cmp bp, [si + Player.bullet_index]
JL .bullet_loop
pop bp
mov di, 0xFFFF
popa
ret ; no hit
draw_map:
; read map structure and place ship location accordingly
; reads map.bin, which is a 8 times scaled down version of the actual VGA pixel space
push bp
; to make player as center of the frame (unused code)
; find player
; mov si, [map] ; will store player location in map
; mov cx, WIDTH * HEIGHT / 64
; .find_player_loop:
; lodsb
; cmp al, 'P'
; JE .break_player_loop
; loop .find_player_loop
; .break_player_loop:
; sub si, ((HEIGHT/8)+1) * (WIDTH/8) / 2
mov dx, 0 ; x counter
mov bx, 0 ; y counter
mov di, 0 ; enemy ship counter
mov si, [map]
mov cx, WIDTH*HEIGHT / 64 ; scaled map size
.loop:
cmp dx, WIDTH/8 ; + 1
JNE .continue
mov dx, 0
inc bx
.continue:
lodsb
shl bx, 3
shl dx, 3
cmp al, 'E'
JNE .check_player
; dec si
; mov byte [si], ' '
; inc si
; place enemy ship
mov [di + enemies + Player.ship_x], dx
mov [di + enemies + Player.ship_y], bx
mov byte [di + enemies + Player.draw], 1
add di, QUANTA_PLAYER_SIZE
inc bp
.check_player:
cmp al, 'P'
JNE .loop_
; place player ship
mov [player + Player.ship_x], dx
mov [player + Player.ship_y], bx
mov byte [player + Player.draw], 1
.loop_:
; revert to scaled map pixel space
shr bx, 3
shr dx, 3
inc dx
loop .loop
.ret:
pop bp
ret
write_string:
; param bp, msg
; param cx, len
pusha
push cx
xor di, di
mov cx, WIDTH*HEIGHT
mov al, 0 ;0x01 ;BG_COLOR
rep stosb
pop cx
mov bx, 0x0004
mov dx, 40 ; text width
sub dx, cx
shr dx, 1
mov dh, 25/2 ; text height
push es
; hide cursor
xor ax, ax
mov es, ax
mov ah, 0x13
mov al, 1
int 10h
pop es ; restore es to graphic memory
popa
ret