-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwildwest.asm
More file actions
628 lines (565 loc) · 13.1 KB
/
wildwest.asm
File metadata and controls
628 lines (565 loc) · 13.1 KB
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
; Project made by Yoav Shai.
; The image files should be 320x200, 256 color BMPs.
; When I wrote this thing, only God and I knew how it worked.
; Today? He stands alone.
IDEAL
MODEL small
STACK 100h
DATASEG
played db 0 ; used to flag whether we should show insturctions or not
fails db 0 ; used for main menu fail counting
oneSec db ?
loopUntil db ?
waitSeconds db ?
waitMilli db ?
; ------------------------------ SCORE PRINTING -------------------------------
wPointsCountMsg db 13,10,'W key has ','$' ; 13, 10 maybe?
upPointsCountMsg db 13,10,'Up key has ','$',13,10
pointsMsg db ' points. $'
lineDown db 13,10,'$'
upPointsCount db 0
wPointsCount db 0
divisorTable db 10,1,0
tieMsg db 13,10,'It is a tie!', 13, 10, '$'
upWonMsg db 13,10,'Up key won!', 13, 10, '$'
wWonMsg db 13,10,'W key won!', 13, 10, '$'
upNoPointsMsg db 13,10,'Up key has no points at all! :( $'
wNoPointsMsg db 13,10,'W key has no points at all! :( $'
haventPlayedMsg db 13,10,'Seems like you did not bother playing at all :( $'
; ------------------------------------ KEYS -----------------------------------
enterkey equ 1Ch
downarrow equ 50h
uparrow equ 48h
wkey equ 11h
spacebar equ 39h
esckey equ 1h
; ----------------------------- BMP PRINTING VARS -----------------------------
bmp_test db 'test.bmp',0
filehandle dw ?
currentlyopen dw ? ; used to store the file that's currently open
openflag db ? ; 1 = a file is open. 0 = a file isn't open.
Header db 54 dup (0)
Palette db 256*4 dup (0)
ScrLine db 320 dup (0)
ErrorMsg db 'Error opening a file, bleep bloop.',13,10,'$'
; ---------------------------------- IMAGES -----------------------------------
bmp_mainMenu db 'mainmenu.bmp',0
bmp_onefail db 'onef.bmp',0
bmp_twofails db 'twof.bmp',0
bmp_threefails db 'threef.bmp',0
bmp_fourfails db 'fourf.bmp',0
bmp_fivefails db 'fivef.bmp',0
bmp_mainMenuQuitSelected db 'quit.bmp',0
bmp_mainMenuPlaySelected db 'play.bmp',0
bmp_gameInstructions db 'instr.bmp',0
bmp_gameInstructionsTwo db 'instrtwo.bmp',0
bmp_countdownthree db 'three.bmp',0
bmp_countdowntwo db 'two.bmp',0
bmp_countdownone db 'one.bmp',0
bmp_notyet db 'notyet.bmp',0
bmp_shoot db 'shoot.bmp',0
bmp_upWon db 'upwon.bmp',0
bmp_wWon db 'wwon.bmp',0
bmp_getready db 'getready.bmp',0
CODESEG ; --------------------------- CODE ------------------------------------
; ------------------------------ SCORE PRINTING -------------------------------
proc printNumber
push ax
push bx
push dx
mov bx, offset divisorTable
nextDigit:
xor ah, ah
div [byte ptr bx] ; al - quotinent, ah - remainder, ax/bx scores/divtable
add al, '0'
call PrintCharacter
mov al, ah
add bx, 1
cmp [byte ptr bx], 0
jne nextDigit
pop dx
pop bx
pop ax
ret
endp printNumber
proc printCharacter
push ax
push dx
mov ah, 2
mov dl, al
int 21h
pop dx
pop ax
ret
endp printCharacter
; -------------------------- END SCORE PRINTING -----------------------------
proc WaitForKeypress ; ah = scancode, al = ASCII
mov ah, 0
int 16h
cmp ah, 1h
je escpressed
ret
escpressed:
call SwitchToText
mov ax, 4c00h
int 21h
endp WaitForKeypress
proc WaitAnyKeypress
push ax
mov ah, 0
int 16h
cmp ah, 1h ; esc key
je escpressed
pop ax
ret
endp WaitAnyKeypress
proc GetKeypress ;scancode in ah, ascii in al
WaitingLoop:
mov ah, 1
int 16h
jz WaitingLoop
mov ah, 0 ; if we've reached this point, there's a key in the buffer
int 16h ; let's read it!
ret
endp GetKeypress
proc PushAll ; not used
pop di
push ax
push bx
push cx
push dx
push si
push di
ret
endp PushAll
proc PopAll ; not used
pop di
pop si
pop dx
pop cx
pop bx
pop ax
push di
ret
endp PopAll
proc SwitchToGraphic
push ax
mov ax, 13h
int 10h
pop ax
ret
endp SwitchToGraphic
proc SwitchToText
push ax
mov ax, 0002h
int 10h
pop ax
ret
endp SwitchToText
proc OpenFile ; file offset should be in dx
mov ah, 3Dh
xor al, al
; mov dx, offset filename
int 21h
jc openerror
mov [filehandle], ax
mov [currentlyopen], ax
mov [openflag], 1
ret
openerror:
mov dx, offset ErrorMsg
mov ah, 9h
int 21h
ret
endp OpenFile
proc CloseFile ; can't have too many files open
mov ah, 3Eh
mov bx, [currentlyopen]
int 21h
mov [openflag], 0
ret
endp CloseFile
proc ReadHeader
mov ah, 3fh
mov bx, [filehandle]
mov cx, 54
mov dx, offset Header
int 21h
ret
endp ReadHeader
proc ReadPalette
mov ah, 3fh
mov cx, 400h
mov dx, offset palette
int 21h
ret
endp ReadPalette
proc CopyPal
mov si, offset Palette
mov cx, 256
mov dx, 3C8h
mov al, 0
out dx, al
inc dx
PalLoop:
mov al, [si+2]
shr al, 2
out dx, al
mov al, [si+1]
shr al, 2
out dx, al
mov al, [si]
shr al, 2
out dx, al
add si, 4
loop PalLoop
ret
endp CopyPal
proc CopyBitmap
mov ax, 0A000h
mov es, ax
mov cx, 200
PrintBMPLoop:
push cx
mov di, cx
shl cx, 6
shl di, 8
add di, cx
mov ah, 3fh
mov cx, 320
mov dx, offset ScrLine
int 21h
cld
mov ax, 320
mov si, offset ScrLine
rep movsb
pop cx
loop PrintBMPLoop
ret
endp CopyBitmap
proc PrintBMPFile ;you should have the bmp's offset in dx
cmp [openflag], 0
je nofileopen
call CloseFile
nofileopen:
call OpenFile
call ReadHeader
call ReadPalette
call CopyPal
call CopyBitmap
ret
endp PrintBMPFile
proc WaitASecond
push ax
push cx
push dx
mov ah, 2Ch
int 21h ; get time. seconds in dh, milliseconds in dl.
cmp dh, 59
je resetSeconds
afterResetSeconds:
mov [waitSeconds], dh
mov [waitMilli], dl
WaitASecondLoop:
mov ah, 1
int 16h
jnz somethingWasPressed ; needed to check if they pressed ESC
mov ah, 2Ch
int 21h ; get time
mov ch, dh
sub ch, 2
cmp ch, [waitSeconds]
je doneWaiting ; in case when testing ms were very high
cmp dh, [waitSeconds]
je WaitASecondLoop ; if we're at the same second, check again later
cmp dl, [waitMilli]
jl WaitASecondLoop
doneWaiting:
pop dx
pop cx
pop ax
ret
resetSeconds:
sub dh, 60
jmp afterResetSeconds
somethingWasPressed:
mov ah, 0
int 16h
cmp ah, esckey
jne WaitASecondLoop
call SwitchToText
mov ax, 4C00h
int 21h
endp WaitASecond
proc WaitASecondOld ; completely halts the program for a second.
push ax
push cx
push dx
mov cx, 0Fh
mov dx, 4240h
mov ah, 86h
int 15h
pop dx
pop cx
pop ax
ret
endp WaitASecondOld
proc GetScancode ; The scan code will get placed in al
WaitForScancodeLoop:
in al, 64h
cmp al, 10b ; Is the buffer empty?
je WaitForScancodeLoop
in al, 60h
cmp al, 1 ; ESC button
je killme
ret
killme:
call SwitchToText
mov ax, 4c00h
int 21h
endp GetScancode
start:
mov ax, @data
mov ds, ax
call SwitchToGraphic
mainMenu: ; Main menu section. This will let the players pick 'play' or 'quit'
mov dx, offset bmp_mainMenu ; MAIN MENU bmp file
call PrintBMPFile
xor cx, cx ; cx will later act as the fail counter
mainMenuSelect:
call WaitForKeypress ; returns scancode in al
cmp ah, uparrow
je mainMenuPlaySelected
cmp ah, downarrow
je mainMenuQuitSelected
; jmp mainMenuSelect ; uncomment to enable no fails mode - DEBUG
mainMenuFail: ; if the code has reached this point, it means the user has
; typed an invalid key. We will count his fails and fuck him up.
mov dl, [fails]
inc dl
mov [fails], dl
; now we will see how many times user has failed, punish him appropriately
cmp [fails], 1
je onefail
cmp [fails], 2
je twofails
cmp [fails], 3
je threefails
cmp [fails], 4
je fourfails
cmp [fails], 5
je fivefails ; the last straw
onefail:
mov dx, offset bmp_onefail ; press one of the arrow buttons ya dip
call PrintBMPFile
jmp mainMenuSelect
twofails:
mov dx, offset bmp_twofails ; either the up or down keys
call PrintBMPFile
jmp mainMenuSelect
threefails:
mov dx, offset bmp_threefails ; holy shit, are you fucking retarded
call PrintBMPFile
jmp mainMenuSelect
fourfails:
mov dx, offset bmp_fourfails ; do it ONE more time.
call PrintBMPFile
jmp mainMenuSelect
fivefails:
mov dx, offset bmp_fivefails ; done playing, FUCK OFF message
call PrintBMPFile
call WaitASecond
jmp exit
mainMenuPlaySelected:
mov dx, offset bmp_mainMenuPlaySelected
call PrintBMPFile
mainMenuPlaySelectedTwo:
call WaitForKeypress
cmp ah, downarrow
je mainMenuQuitSelected
cmp ah, enterkey
je beforePlaying_Bridge
cmp ah, spacebar
je beforePlaying_Bridge
jmp mainMenuPlaySelectedTwo
mainMenuQuitSelected:
mov dx, offset bmp_mainMenuQuitSelected
call PrintBMPFile
mainMenuQuitSelectedTwo:
call WaitForKeypress
cmp ah, uparrow
je mainMenuPlaySelected
cmp ah, spacebar
je QuitAndShowScores
cmp ah, enterkey
jne mainMenuQuitSelectedTwo ; no need in printing again
; code reaching this point means the user has pressed enter.
; we shall display the scores and quit the game.
QuitAndShowScores:
call SwitchToText
printWPoints:
cmp [wPointsCount], 0
je wNoPoints
mov dx, offset wPointsCountMsg
mov ah, 9
int 21h
xor ax, ax
mov al, [wPointsCount]
call printNumber ; we now have w's score printed
mov dx, offset pointsMsg
mov ah, 9
int 21h
jmp printUpPoints
wNoPoints:
cmp [upPointsCount], 0
je haventPlayed
mov dx, offset wNoPointsMsg
mov ah, 9
int 21h
; moving here means w has no points and up has
; so we don't need to jump
printUpPoints:
cmp [upPointsCount], 0
je upNoPoints
mov dx, offset upPointsCountMsg
mov ah, 9
int 21h
xor ax, ax
mov al, [upPointsCount]
call printNumber
mov dx, offset pointsMsg
mov ah, 9
int 21h
jmp whoWon
upNoPoints:
mov dx, offset upNoPointsMsg
mov ah, 9
int 21h
jmp whoWon
beforePlaying_Bridge:
jmp beforePlaying
; was too far for one jump
haventPlayed:
mov dx, offset haventPlayedMsg
mov ah, 9
int 21h
mov ax, 4C00h ; exit the program
int 21h
whoWon:
; let's print a blank line first
mov dx, offset lineDown
mov ah, 9
int 21h
; checking who won
mov al, [upPointsCount]
mov ah, [wPointsCount]
cmp al, ah
jg printUpWonMsg
je printTieMsg
; reaching this point means W won
mov dx, offset wWonMsg
printBeforeQuitting:
mov ah, 9
int 21h
mov ax, 4C00h ; quitting here.
int 21h
printUpWonMsg:
mov dx, offset upWonMsg
jmp printBeforeQuitting
printTieMsg:
mov dx, offset tieMsg
jmp printBeforeQuitting
beforePlaying: ; no need in showing the instructions a second time
cmp [played], 1
je playTheGame
gameInstructions:
mov dx, offset bmp_gameInstructions
call PrintBMPFile
call WaitASecond ; we don't want them to accidentally skip this.
gameInstructionsAfterWait:
mov dx, offset bmp_gameInstructionsTwo
call PrintBMPFile
call WaitAnyKeypress
playTheGame:
mov dx, offset bmp_getready
call PrintBMPFile
; call WaitASecond ; give them a second to get ready
; mov dx, offset bmp_countdownthree
; call PrintBMPFile
; call WaitASecond
; mov dx, offset bmp_countdowntwo
; call PrintBMPFile
; call WaitASecond
; mov dx, offset bmp_countdownone
; call PrintBMPFile
; call WaitASecond
; mov dx, offset bmp_notyet
; --------------------------- uncomment these lines for countdown -------------
randomNumber:
mov ah, 2Ch
int 21h ; time is now in ch:cl:dh:dl
and dl, 00000111b ; max is 7
inc dl ; making sure they have at least one second
add dl, dh ; we want to loop until currsec+rand[1-7]
cmp dl, 59
jle SaveLoopUntil ; if it won't flow, no reason to change anything
sub dl, 60 ; the timer will flow to the next minute, so we'll adjust accordingly
SaveLoopUntil:
mov [loopUntil], dl
; wait for a new second to start the timer
mov ah, 2Ch ; get curr time
int 21h
mov [oneSec], dh ; save current second
WaitForTick:
int 21h
cmp dh, [oneSec]
je WaitForTick ; as long as we are in the same second, keep looping.
LoopWhileChecking:
mov ah, 2Ch
int 21h ; get time, seonds in dh
cmp dh, [loopUntil]
je ShootingAllowed
mov ah, 1
int 16h ; read keyboard status
jz LoopWhileChecking
mov ah, 0
int 16h ; we now have the key
cmp ah, uparrow
je wWon
cmp ah, wkey
je upWon
cmp ah, esckey
je exit
loop LoopWhileChecking
; if we've reached this point, it means the times is done. Players should be able
; to shoot now.
ShootingAllowed:
mov dx, offset bmp_shoot
call PrintBMPFile
scanwhopressed:
; waiting for input, no need for complex loops now
call WaitForKeypress
cmp ah, uparrow
je upWon
cmp ah, wkey
je wWon
jmp scanwhopressed
wWon:
mov [played], 1
add [wPointsCount], 1
mov dx, offset bmp_wWon
call PrintBMPFile
call WaitASecond
jmp mainMenuPlaySelected
upWon:
mov [played], 1
add [upPointsCount], 1
mov dx, offset bmp_upWon
call PrintBMPFile
call WaitASecond
jmp mainMenuPlaySelected
exit:
call SwitchToText
mov ax, 4c00h
int 21h
END start ; finito la comedia