This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgame.z80
More file actions
3716 lines (3117 loc) · 97.2 KB
/
Copy pathgame.z80
File metadata and controls
3716 lines (3117 loc) · 97.2 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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;
; LIGHTHOUSE OF DOOM!!!
;
; Steve Kemp
; -
; https://steve.kemp.fi/
; https://github.com/skx/lighthouse-of-doom
;
;
; This is a port of the lighthouse-of-doom game from C to Z80 assembly, such
; that it will run under CP/M 2.x and the 48k ZX Spectrum.
;
; Although the game has been ported that process has resulted in some changes
; to the text, and the command-handlers. If you've played the C game, and
; completed it, there will be no significant new surprises.
;
; Rather than having a global player-inventory store, and a place
; in each location for object storage we've instead make the location
; a property of each item.
;
;
; Known deficiencies / bugs
; -------------------------
;
; HELP command doesn't show description next to command.
;
;
; Naming / Code Layout
; --------------------
;
; I've tried to be consistent with naming-schemes, but of course some
; code was added in an ad-hoc fashion:
;
; * All functions with a _command suffix are command-handlers
; These are invoked as a result of the user typing the appropriate
; string. e.g. User types "EXAMINE" we run "examine_function"
;
; * All object-specific functions have a _fn suffix.
; e.g. Custom code runs when you "TAKE RUG" is take_rug_fn
;
; If you're using Emacs, or similar editor, you should be able to fold
; or collapse sections of the code via the markers "; {" and "; }"
;
; Game over if you don't win in this many turns
MAX_TURN_LIMIT: EQU 100
; How many turns before the ship warning
REMINDER_TURN_COUNT: EQU 10
; Maximum number of turns you can survive being near a grue
MAX_GRUE_EXPOSURE: EQU 3
; Later we have an object-table, containing entries for all of the
; objects which are present in the game. Some of these objects are
; real in the sense that they can be used/taken. Others exist only
; so they may be examined.
;
; Each object has a description, a state, and a location, along with
; a collection of dedicated handler-functions.
;
; Here we define some state attributes for a couple of special objects
; which eases maintenance.
;
; 1. Torch may be lit or unlit.
TORCH_STATE_UNLIT: EQU 0
TORCH_STATE_LIT: EQU 1
; 2. The trapdoor may be invisible, open, or closed.
; [The objects have an invisible state, but we can't use that here.]
TRAPDOOR_STATE_INVISIBLE: EQU 0
TRAPDOOR_STATE_CLOSED: EQU 1
TRAPDOOR_STATE_OPEN: EQU 2
; 3. The mirror may be normal, or broken
MIRROR_OK: EQU 0
MIRROR_BROKEN: EQU 1
;
; Each item also contains a byte which holds the "location" (i.e. index
; into the location-map). Two locations are special:
;
; - A location of 0xff means the player is carrying the item.
;
; - A location of 0xfe means the item is available, but invisible.
;
; These are defined here for clarity.
;
ITEM_CARRIED: EQU 0xff
ITEM_INVISIBLE: EQU 0xfe
;********************************************************************
; Macros
;********************************************************************
; {{
;
; Simple macro to inject "L" into our input buffer, and call the LOOK handler.
;
MACRO FAKE_LOOK
ld hl, INPUT_BUFFER+2
ld (hl), 'L'
call look_function
ENDM
;
; Simple macro to push all (important) registers.
;
MACRO PUSH_ALL
push af
push bc
push de
push hl
ENDM
;
; Simple macro to pop all (important) registers.
;
MACRO POP_ALL
pop hl
pop de
pop bc
pop af
ENDM
; }}
;********************************************************************
; Entry-point and initial setup
;********************************************************************
; {{
;
; Entry-point of CP/M binaries is 0x100, as the zero-page, or PSP,
; is located before that:
;
; https://en.wikipedia.org/wiki/Zero_page_(CP/M)
;
; Entry-point for the ZX Spectrum port is 32768, chosen randomly.
;
ORG ENTRYPOINT
;
; Our game can be compiled with optional "encryption", which uses
; a simple scheme to hide the strings in our binary.
;
; If this support is used we'll have to restore our contents, or
; the game won't be playable.
;
; To compile the binary with encryption you need to define the
; `ENCRYPT_STRINGS` symbol, and then post-process the binary
; with `encrypt.c`.
;
IF ENCRYPT_STRINGS
ld hl, ENC ; start of region to unmangle
ld de, ( end_of_source - ENC ) ; length to unmangle.
ld c, '%' ; starting scrambling key (XOR)
enc_loop:
ld a,(hl) ; get the byte
xor c ; modify it
ld (hl),a ; store it back
inc hl ; point to the next byte
dec de ; decrease count of remaining bytes to process.
inc c ; bump key
ld a, d ; done?
or e
jr nz,enc_loop ; no, then repeat
jr enc_end ; Jump over our marker to the start of the game.
;
; Everything after here will be encrypted.
;
ENC: DB "SKX"
enc_end:
ENDIF
; Call system-specific setup routine before we do anything else.
call bios_init
; Here we're going to copy our game-state to the end of RAM
;
; We do this so that everything "resets" if the player dies/wins
; and then chooses to play again when prompted.
;
; We don't know for sure how big the data is, but we assume it'll
; fit from 0xD000..
ld hl, per_game_state_start
ld de, 0xD000
ld bc, end_of_source - per_game_state_start
ldir
;; }}
;********************************************************************
; Main game-loop
;********************************************************************
; {{
game_start:
; Reset the state of the the inventory, the game-world, etc.
;
; This is pointless at the start of the game, as nothing is
; modified, but we jump here if the user chooses to replay the
; game, so we have it here for that reason.
ld hl, 0xD000
ld de, per_game_state_start
ld bc, end_of_source - per_game_state_start
ldir
; Clear the screen
call bios_clear_screen
; Present the game intro-text.
; Set the wrapping to be 60 characters to ensure there's no weirdness
ld hl, WRAP_LOCATION
ld a, (hl)
push af
ld (hl), 60
ld de, usage_message
call bios_output_string
pop af
ld (hl),a
; Pause for input here.
call bios_await_keypress
call bios_clear_screen
; Show the starting-location.
FAKE_LOOK
; The start of our game loop.
;
; Here we basically read a line of input, process it, then
; attempt to call the appropriate handler. This repeats until
; the game is over, one way or another.
;
; We upper-case all input to allow easier matching to our
; command/dispatch-table.
game_loop:
; Have we exceeded the turn-count?
ld hl, TURN_COUNT
ld a, (hl)
cp MAX_TURN_LIMIT
jr c,game_loop_continue
ld hl, PLAYER_DEAD_FLAG
inc (hl)
game_loop_continue:
; Did the player die of a magic-overdoes
ld hl, PLAYER_MAGIC_OVERDOSE_FLAG
ld a, (hl)
cp 1
jp z, play_again
; Is the player dead?
ld hl, PLAYER_DEAD_FLAG
ld a, (hl)
cp 1
jr nz,not_dead
; The player is dead. Oops.
ld de, PLAYER_DEAD_MESSAGE
call bios_output_string
call turns_function
jr_play_again:
jp play_again
not_dead:
; Did the player win?
ld hl, PLAYER_WON_FLAG
ld a, (hl)
cp 1
jr nz, not_won
; The player won. Yay!
ld de, PLAYER_WON_MESSAGE
call bios_output_string
call turns_function
jr jr_play_again
not_won:
; Erase our input buffer.
ld hl, INPUT_BUFFER+2
ld b, MAX_INPUT_LENGTH
call erase_buffer
; Read a line of input from the player.
ld de, INPUT_BUFFER
call bios_read_input
; Every ten turns we show a message about the ship.
ld hl, SHIP_WARNING
dec (hl) ; decrease
jr nz, ship_warning_over ; nope? Do nothing
ld (hl), REMINDER_TURN_COUNT ; reset the count
ld de, ship_closer_msg ; show the message
call bios_output_string
call show_dots
call bios_clear_screen
ship_warning_over:
; If the input was zero-length (i.e. User pressed return) ignore it.
ld hl, INPUT_BUFFER+1
ld a,(hl)
cp 0
jr z, not_won
; Convert the input to upper-case.
ld b,a
inc hl
call uppercase_buffer
; Remove leading whitespace, by shifting input down
call filter_input_leading_whitespace
; Remove trailing whitespace, by replacing with nulls.
call filter_input_trailing_whitespace
; Take the input from the user, and see if we have a registered
; command with that name. If we do we can invoke it, but if not we've
; been given something we don't understand.
ld hl, INPUT_BUFFER+2
; We have two kinds of commands:
; 1. Global
; 2. Per-Item
;
; Per-item command are processed before the global ones,
; so that they can be invoked first.
;
call input_second_term
; did that fail?
cp 0
jr nz, process_global_command
; ok find the item, if that failed then
; again we continue with the global item.
call get_item_by_name
cp 0
jr nz, process_global_command
; now we should find IX points to the item
; is there a global command handler?
ld e,(IX + ITEM_TABLE_CUSTOM_ACTION_OFFSET)
ld d,(IX + ITEM_TABLE_CUSTOM_ACTION_OFFSET+1)
; DE points to the table. If it is null we have
; no per-item table for this object
ld a,d
or e
jr z, process_global_command
; At this point DE points to the table of per-item
; commands, and HL points to the object.
;
; We don't really want the object any more, instead
; we want to look at the verb at the start of our
; input buffer.
ld hl, INPUT_BUFFER+2
call find_command_for_custom_table
cp 255
jr z, process_global_command
; OK we found an item in the custom handler
; we're gonna call it.
call JP_HL
; since we found a custom command skip the global
; handling, and proceed to run our regular actions
jr regular_turn_actions
process_global_command:
; Otherwise we look for a global command
ld hl, INPUT_BUFFER+2
call find_command
cp 255
jr z, no_handler
push hl
; Call the handler
pop hl
call JP_HL
regular_turn_actions:
; Valid command - increase turn count
ld hl, TURN_COUNT
inc (hl)
; Did we get eaten?
call maybe_grue_death
; And restart the command-loop
jr jr_game_loop
no_handler:
; Show the user that their command wasn't understood.
ld de, invalid_msg
call bios_output_string
; Did we get eaten?
call maybe_grue_death
; Return to the start of our game-loop.
jr_game_loop:
jp game_loop
; }}
;********************************************************************
; Include our custom printing routine
;********************************************************************
include "prn.z80"
;********************************************************************
; Utility functions
;********************************************************************
; {{
; compare strings stored in HL + DE for equality. Length is stored in B
; Returns NZ if they're not equal.
CompareStringsWithLength:
ld a,(de)
cp (hl)
ret nz
inc hl
inc de
djnz CompareStringsWithLength
xor a
ret
;
; Helper function, to allow indirection.
;
JP_HL:
jp (hl)
;
; Show some significant dots
;
show_dots:
; width of the screen
ld b, MAX_INPUT_LENGTH - 1
dot_loop:
PUSH_ALL
call bios_delay
ld e, '.'
call bios_output_character
POP_ALL
djnz dot_loop
ret
; Multiply two 8-bit values together.
; INPUT: THE VALUES IN REGISTER B EN C
; OUTPUT: HL = B * C
; CHANGES: AF,DE,HL,B
;
multiply:
LD HL,0
LD A,B
OR A
RET Z
LD D,0
LD E,C
LOOP: ADD HL,DE
DJNZ LOOP
RET
;
; Output number helpers - used for reporting the number of turns
; you have taken, as well as showing the wrap-value via "WRAP".
;
DispHL:
ld bc,-10000
call Num1
ld bc,-1000
call Num1
ld bc,-100
call Num1
ld c,-10
call Num1
ld c,-1
Num1: ld a,'0'-1
Num2: inc a
add hl,bc
jr c,Num2
sbc hl,bc
PUSH_ALL
ld e, a
call bios_output_character
POP_ALL
ret
show_a_register:
PUSH_ALL
ld h,0
ld l,a
call DispHL
POP_ALL
ret
;Input:
; DE points to the string
;Outputs:
; HL is the result
; A is the 8-bit value of the number
; DE points to the byte after the number
;Destroys:
; BC
; if the string is non-empty, BC is HL/10
string_to_uint16:
ld hl,0
string_to_uint16_loop:
ld a,(de)
sub 30h
cp 10
ret nc
inc de
ld b,h
ld c,l
add hl,hl
add hl,hl
add hl,bc
add hl,hl
add a,l
ld l,a
jr nc,string_to_uint16_loop
inc h
jr string_to_uint16_loop
;
; Helper function, fill a region of memory with zero characters
;
; HL will point to the buffer.
; B will have the length
erase_buffer:
xor a
erase_buffer_loop:
ld (hl),a
inc hl
djnz erase_buffer_loop
ret
;
; Helper function. Upper-case the given string.
;
; HL will point to the buffer.
; B will have the length
;
uppercase_buffer:
ld a, (hl)
cp 'a'-1
jr c, uppercase_ok
cp 'z'+1
jr nc, uppercase_ok
sub 32
ld (hl),a
uppercase_ok:
inc hl
djnz uppercase_buffer
ret
;; INPUT_BUFFER+2 contains the user input
;; The length is in INPUT_BUFFER+1
;;
filter_input_leading_whitespace:
; is the first character a space?
ld a,(INPUT_BUFFER+2)
cp ' '
; nope? We're done
ret nz
; get the length of the input into BC
ld a,(INPUT_BUFFER+1)
xor b
ld c, a
; Move the buffer down one byte.
ld hl, INPUT_BUFFER+3
ld de, INPUT_BUFFER+2
ldir
; repeat until no more leading spaces are found
jr filter_input_leading_whitespace
;; INPUT_BUFFER+2 contains the user input
;; The length is in INPUT_BUFFER+1
;;
filter_input_trailing_whitespace:
;; Get the input length in BC
xor b
ld a,(INPUT_BUFFER+1)
ld c,a
;; Add to the start of the buffer, because
;; we will process the text in reverse.
ld hl, INPUT_BUFFER+1
add hl,bc
overwrite_space_loop:
;; get the charactor
ld a, (hl)
;; is it a space? If not return
cp ' '
ret nz
;; overwrite with a zero, and work backwards
ld (hl),0
dec hl
jr overwrite_space_loop
;********************************************************************
; Utility Functions For Game
;********************************************************************
; {{
;
; Ask the user if they wish to play again
;
play_again:
ld de, play_again_msg
call bios_output_string
call bios_await_keypress
cp 'y'
jp z, game_start
cp 'Y'
jp z, game_start
cp 'n'
jr z, play_again_no
cp 'N'
jr z, play_again_no
call bios_clear_screen
jr play_again
; User chose "N" to "play again?"
play_again_no:
ld de, play_again_no_msg
call bios_output_string
IF SPECTRUM
pop de
ret
ELSE
ld c, 0x0
call 0x0005
ret
ENDIF
; This will be a major part of our code so it is perhaps a little more
; verbose than it wants to be.
;
; Find the entry in the command-table. If we find it return the address
; of the routine to call in `HL`.
;
; On entry `HL` should contain the pointer to the input buffer to look
; for.
;
; On exit A contains 0 on success, with HL pointing to the handler to invoke,
; or A contains 255 on failure.
find_command:
ld de,command_table
find_command_for_custom_table:
;
; Entries are arranged like so:
; $LENGTH "ASCII" $HIDDEN ADDR1 ADDR2
;
; Where $LENGTH is the length of the ASCII command-name.
;
; So:
;
; 4, "QUIT", 0, quit_fn_ptr
; 3, "CLS", 1, cls_fn_ptr
; 0,
;
; "HIDDEN" is used solely to decide whether the command should be
; included in the output of "HELP".
;
find_command_again:
push de
ld a, (de) ; length of string
cp 0x00 ; zero? We've hit the end of the table and not found
jr nz, cont ; a match - return failure
pop de
ld a,255
ret
cont:
ld b, a ; B will contain the length
inc de ; DE point to the string itself
push hl
call CompareStringsWithLength
jr z,find_command_found
pop hl
; ok we didn't find a match with this command-table entry.
; restore DE which points to this entry's start
pop de
ld a, (de) ; get the length of the command-name
inc de ; point to the command-name
ld b,a ; bump the pointer past the ASCII string, in a loop
find_command_skip:
inc de
djnz find_command_skip
inc de ; bump past the hidden-flag
inc de ; bump past the addr1-byte
inc de ; bump past the addr2-byte
jr find_command_again ; loop back to try the next table entry.
find_command_found:
; drop hl
pop hl
; restore the pointer to the start of this command-table entry.
pop de
; Once again we need to bump past the length, and the ASCII string
; itself. Now we know we've got a match we want to get the address
; of the handler to invoke.
ld a, (de) ; get the length
inc de ; move to the start of the string
ld b,a
find_command_skip2: ; skip $LENGTH-bytes
inc de
djnz find_command_skip2
inc de ; skip the hidden-flag
ld a, (de) ; set l
ld l,a
inc de
ld a, (de) ; set h
ld h,a
xor a ; indicate success
ret ; return with the function-pointer in HL
;
; Helper function. Are ANY items present in this room?
;
; This is just used to know whether to print the "You can see .." message
;
; Return "A == 0" if so.
; Return "A == FF" if not
;
items_are_present:
; Point to the item-table, and prepare to loop over the items
ld IX, items
ld DE, ITEM_ENTRY_LENGTH
ld b, ITEM_COUNT
look_item_loop_test:
; Get the location of the object in C
ld c, (IX+ITEM_TABLE_LOCATION_OFFSET)
; Set A to the current location.
ld hl, CURRENT_LOCATION
ld a,(hl)
; Is the item in the room?
cp c
jr nz, item_not_present_loop
; Get the description of the object in HL.
ld l, (IX+ITEM_TABLE_DESCRIPTION_OFFSET)
ld h, (IX+ITEM_TABLE_DESCRIPTION_OFFSET+1)
; OK we have an item present. Is the description empty?
; if so that's a hidden object and it doesn't count.
ld a, h
or l
jr z, item_not_present_loop
xor a
ret
item_not_present_loop:
; Move to the next entry
add IX,DE
djnz look_item_loop_test
ld a, 0xff
ret
;
; Helper function to return an item, by name.
;
; This is used a lot because the item-entries contain a series of
; pointers for various purposes - handlers to be invoked for object-specific
; behaviour, the location of the object within our world-map, and
; a byte representing per-item state.
;
; On entry the name of the item to be found will be stored in HL.
;
; On exit A will be 00 if all is OK, and IX will point to the item.
; On exit A will be ff if there was a failure.
;
get_item_by_name:
; erase our temporary buffer, keeping the HL register
; with the name of the item to find safe.
push hl
ld hl, TMP_BUFFER
ld b, TMP_BUFFER_LEN
call erase_buffer
pop hl
; copy the name of the item into the temporary-buffer
ld de, TMP_BUFFER+1
ld b,0
gibn_copy_loop:
ld a,(hl)
cp 0
jr z, gibn_copy_complete
ld (de),a
inc de
inc hl
inc b
jr gibn_copy_loop
gibn_copy_complete:
; prefix the temporary buffer with the length of the item
ld hl, TMP_BUFFER
ld (hl),b
; Point to our table of items
ld ix, items
ld b, ITEM_COUNT
gibn_item_loop:
; Get the name of the object in DE - that's at the start of the item.
ld e,(IX)
ld d,(IX + 1)
; Now get the location of the current object.
ld a, (IX+ITEM_TABLE_LOCATION_OFFSET)
; Is this object carried?
cp ITEM_CARRIED
jr z, gibn_item_carried
; Is this object available (but hidden)?
cp ITEM_INVISIBLE
jr z, gibn_item_available
; Is this object in the current location?
push hl
ld hl, CURRENT_LOCATION
ld c,(hl)
pop hl
cp c
jr z, gibn_item_present
gibn_continue:
; OK we didn't find the object, so we need to try looking at
; the next table entry
ld DE, ITEM_ENTRY_LENGTH
add ix, DE
djnz gibn_item_loop
; At this point we've looked at all objects in the table, and
; we didn't find a match.
ld a, 0xff
ret
gibn_item_present:
gibn_item_carried:
gibn_item_available:
push bc ; preserve b for our loop
; OK we've found an item that is "present" or "available".
;
; Does this object have the name of the item we're actually
; supposed to be looking for? If so then we've found it
; and we can return our IX register (which points to the
; start of the object in the item-table).
;
; If the name doesn't match we loop around to examine the
; next item in the table.
ld hl, TMP_BUFFER ; Get the length from our buffer
ld b, (hl)
inc hl ; Now HL points to the input
; DE points to the name of the current item.
; B contains the length
call CompareStringsWithLength
jr z, gibn_found_it
pop bc
jr gibn_continue
gibn_found_it:
; We found the item. Drop the BC register, which we saved
; to preserve our loop-index.
pop bc
xor a
ret
;
; Helper function. Are we at risk of grue?
;
; If our location is in the dark-place we are at risk of being eaten.
;
; In the C implementation there was a random number test, here we decide
; that if you enter three valid turns within the dark place then you
; will die
;
maybe_grue_death:
; Are we in the dark-place?
ld hl, CURRENT_LOCATION
ld a,(hl)
cp 4
ret nz
; Get the grue (!)
ld hl, item_15_name
call get_item_by_name
cp 0
ret nz
; Once we've found the item IX will point to the entry
;
; Get the item state using that index register, after increasing it.
inc (IX + ITEM_TABLE_STATE_OFFSET)
ld a,(IX + ITEM_TABLE_STATE_OFFSET)
cp MAX_GRUE_EXPOSURE
jr nc, death_by_grue_grue
call show_dots
ld de, grue_message
call bios_output_string
ret
death_by_grue_grue:
ld de, you_were_eaten
call bios_output_string
ld hl,PLAYER_DEAD_FLAG
inc (hl)
call turns_function
jp play_again
ret
;
; Helper function. Is the player carrying something?
;
; This is used in the inventory function to decide whether to show the list
; of items, with header, or "You're carrying nothing".
;
; Return "A == 0" if so.
; Return "A == FF" if not.
;
player_has_objects:
; Point to the item-table, and prepare to loop over the items
ld IX, items
ld DE, ITEM_ENTRY_LENGTH