├── .github
├── FUNDING.yml
├── build
└── workflows
│ └── release.yml
├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── bios.z80
├── c
├── globals.h
├── handlers.c
├── inventory.c
├── items.c
├── main.c
├── util.c
└── world.c
├── encrypt.c
├── game.z80
├── prn.z80
└── version.z80
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 | github: skx
3 | custom: https://steve.fi/donate/
4 |
--------------------------------------------------------------------------------
/.github/build:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | apt-get update
4 |
5 | which pasmo || apt-get install -y pasmo
6 | which gcc || apt-get install -y build-essential
7 | which make || apt-get install -y make
8 |
9 | make release game-spectrum
10 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | on: release
2 | name: Handle Release
3 | jobs:
4 | upload:
5 | name: Upload release
6 | runs-on: ubuntu-latest
7 | steps:
8 | - uses: actions/checkout@master
9 | - name: Generate the artifacts
10 | uses: skx/github-action-build@master
11 | - name: Upload
12 | uses: skx/github-action-publish-binaries@master
13 | env:
14 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
15 | with:
16 | args: "*.com *.tap"
17 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | lighthouse
2 | encrypt
3 | *.tap
4 | *.com
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 |
294 | Copyright (C)
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | , 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # Our version comes from the GITHUB_REF environmental variable,
2 | # if not set then we're running from "unreleased-git"
3 | VERSION := $(or ${GITHUB_REF},${GITHUB_REF},"unreleased-git")
4 |
5 |
6 | all: lighthouse game-cpm game-spectrum
7 |
8 |
9 | # Build the C version
10 | lighthouse: c/handlers.c c/inventory.c c/items.c c/main.c c/world.c c/util.c c/globals.h
11 | gcc -o lighthouse -Os -Wall -Wextra -Werror c/handlers.c c/inventory.c c/items.c c/main.c c/world.c c/util.c
12 |
13 |
14 | # Clean our generated output
15 | clean:
16 | rm -f lighthouse *.com *.tap encrypt || true
17 |
18 |
19 | # Build the encryption helper
20 | encrypt: encrypt.c
21 | gcc -o encrypt -Wall -Werror encrypt.c
22 |
23 |
24 | # Format our C-code
25 | format:
26 | astyle --style=allman -A1 --indent=spaces=4 --break-blocks --pad-oper --pad-header --unpad-paren --max-code-length=200 c/*.c c/*.h
27 |
28 |
29 | # version.z80 will contain the last part of the string $VERSION
30 | version:
31 | echo "DB \"$$(echo ${VERSION} | awk -F/ '{print $$NF}' )\"" > version.z80
32 |
33 |
34 |
35 | # build the game for CP/M
36 | game-cpm: game.z80 bios.z80 version Makefile
37 | pasmo --equ ENTRYPOINT=100h --equ ENCRYPT_STRINGS=0 --equ SPECTRUM=0 game.z80 lihouse.com
38 |
39 | # build the game for CP/M with encrypted strings/code
40 | game-cpm-encrypted: game.z80 bios.z80 version Makefile encrypt
41 | pasmo --equ ENTRYPOINT=100h --equ ENCRYPT_STRINGS=1 --equ SPECTRUM=0 game.z80 lihouse.com
42 | ./encrypt lihouse.com lihousex.com
43 | rm lihouse.com
44 |
45 |
46 |
47 | # build the game for the ZX Spectrum
48 | game-spectrum: game.z80 bios.z80 version Makefile
49 | pasmo --tapbas --equ ENTRYPOINT=32768 --equ ENCRYPT_STRINGS=0 --equ SPECTRUM=1 game.z80 lihouse.tap
50 |
51 | # build the game for the ZX Spectrum with encrypted strings
52 | game-spectrum-encrypted: game.z80 bios.z80 version Makefile encrypt
53 | pasmo --tapbas --equ ENTRYPOINT=32768 --equ ENCRYPT_STRINGS=1 --equ SPECTRUM=1 game.z80 lihouse.tap
54 | ./encrypt -crc lihouse.tap lihousex.tap
55 | rm lihouse.tap
56 |
57 |
58 | # Build the game for release - with strings encrypted
59 | release: version
60 | make game-cpm-encrypted
61 | make game-cpm
62 | make game-spectrum-encrypted
63 | make game-spectrum
64 |
65 |
66 | # Run the CP/M version via runcpm
67 | run-cpm: game-cpm
68 | ~/cpm/cpm lihouse
69 |
70 | # Run the CP/M version via runcpm, with the encrypted version of the code
71 | run-cpm-encrypted: game-cpm-encrypted
72 | ~/cpm/cpm lihouseX
73 |
74 | # Run the spectrum version
75 | run-spectrum: game-spectrum
76 | xspect -quick-load -load-immed -tap lihouse.tap
77 |
78 | # Run the spectrum version, with strings encrypted
79 | run-spectrum-encrypted: game-spectrum-encrypted
80 | xspect -quick-load -load-immed -tap lihousex.tap
81 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # The Lighthouse of Doom
2 |
3 | This repository contain a simple text-based adventure game, implemented
4 | twice, once in portable C, and once in Z80 assembly language.
5 |
6 | The Z80 version of the game will run under the CP/M operating system, and the humble 48k ZX Spectrum.
7 |
8 | My intention was to write a simple text-based adventure game to run under CP/M. Starting large projects in Z80 assembly language from scratch is a bit of a daunting prospect, so I decided to code the game in C first, so that I could get the design right, and avoid getting stuck in too many low-level details initially. Later I ported to the Spectrum, because it seemed like a fun challenge for myself!
9 |
10 | Quick links within this README file:
11 |
12 | * [Play Online](#play-online)
13 | * [Plot of the game](#game-plot)
14 | * [C Implementation](#c-implementation)
15 | * [Z80 Implementation](#z80-implementation)
16 | * [Z80 Changes](#z80-changes)
17 | * [Memory Map](#memory-map)
18 | * [Compiling & running it](#compiling--running-it)
19 | * [Downloading It](#downloading-it)
20 | * [Bugs?](#bugs)
21 |
22 |
23 |
24 | ## Play Online
25 |
26 | Thanks to the excellent [jsspeccy](https://github.com/gasman/jsspeccy3) ZX Spectrum emulator you can play this game with your browser here:
27 |
28 | * [https://steve.fi/software/lighthouse/](https://steve.fi/software/lighthouse/)
29 |
30 |
31 |
32 | ## Game Plot
33 |
34 | * You're inside a lighthouse (trapped? doesn't really matter I guess).
35 | * You see a boat on the horizon, heading your way.
36 | * But "oh no!", the lighthouse is dark.
37 | * The boat will surely crash if you don't turn on the main light.
38 |
39 | The game is over, when you either fix the light, or find another solution.
40 |
41 | If you do not achieve victory within a turn-limit the boat runs aground, and
42 | death will consume you all. (It is a _very_ big boat!)
43 |
44 |
45 |
46 | ## C Implementation
47 |
48 | The implementation is mostly concerned with creating the correct series of
49 | data-structures, which are essentially arrays of objects. Because if we
50 | can make the game table-based we simplify the coding that needs to be
51 | done - we don't need to write per-object handlers anywhere, we can just
52 | add pointers to tables/structures.
53 |
54 | The C implementation defines most of the important things in the file [globals.h](c/globals.h) such as:
55 |
56 | * The structure to define a location.
57 | * The structure to define an object.
58 |
59 | The game-state itself is stored in a couple of global variables, there isn't
60 | too much state to care about:
61 |
62 | * The current location (i.e. index into location-table).
63 | * A list of any items you're carrying.
64 | * The number of turns you've taken.
65 | * Incremented by one each time you enter a command, be it recognized or not.
66 | * Whether you won/lost.
67 |
68 |
69 |
70 | ## Z80 Implementation
71 |
72 | The Z80 implementation is based upon [the C-implementation](c/), with a few small changes.
73 |
74 | The implementation uses a simple set of structures:
75 |
76 | * A command-table to map input-commands to handlers.
77 | * An item-table to store details about each object in the game.
78 | * A person table to store telephone messages.
79 |
80 | The main implementation can be found in the file [game.z80](game.z80),
81 | but because we support two targets (CP/M 2.x and the ZX Spectrum) there
82 | is a small amount of platform-specific code found in [bios.z80](bios.z80).
83 |
84 | The `Makefile` should build everything appropriately for both systems,
85 | defining `SPECTRUM`, and `ENTRYPOINT` as appropriate.
86 |
87 |
88 | ### Z80 Changes
89 |
90 | * Along the way I realized that having fixed inventory slots made the coding more of a challenge, so I made the location of each object a property of the object itself.
91 | * The Z80 version has more easter-eggs (Try typing "`xyzzy`" a few times).
92 | * There is rudimentary support for text-wrapping.
93 | * Enter `WRAP 80` to wrap output around column 80.
94 | * Enter `WRAP` to view the current wrapping value.
95 | * There are __two__ victory conditions.
96 | * The game can be built with the text-strings, and game code, protected by simple XOR encryption:
97 | * This stops users from looking through the binary for hints.
98 | * Run `make release` to build both "normal" and "protected" versions of the release.
99 | * The encrypted versions of the games have an X suffix in their filenames.
100 |
101 |
102 | ### Memory Map
103 |
104 | Roughly speaking the game consists of two parts:
105 |
106 | * The driver/game which is about 3k of code.
107 | * The text of the game along with the state of the world (flags, items carried, etc), which is approximately 9k.
108 |
109 | All told the binaries for both CP/M and the ZX Spectrum are approximately 14k.
110 |
111 | The layout in memory is basically the same for both variants, however the starting address is different:
112 |
113 | * CP/M
114 | * Game loaded between the range 256-14000
115 | * [0x0100-0x3500]
116 | * Copy of state made to 53248 / 0xD000
117 | * ZX Spectrum
118 | * Game loaded between the range 32768-46000
119 | * [0x5000-0xB400]
120 | * Copy of state made to 53248 - 0xD000
121 |
122 | When the game starts a copy of the "state" is made to `0xD000` before anything has been modified, and then when the game is started that state is copied back to where it is used. This consists of all content between `per_game_state_start` and the end of the file. This has the location, flags, and similar, as well as all the static-text which will not change between runs. However copying this region was easier than just copying, or otherwise resetting, the state that changes during play.
123 |
124 | No doubt things will change over time, however the (undocumented) `BIOS` command will show you rough sizes for the various parts of the game.
125 |
126 |
127 |
128 | ## Compiling & Running It
129 |
130 | Ensure you have the `pasmo` assembler installed, and then use the supplied Makefile to compile the game.
131 |
132 | Running `make` will generate the default targets, if you wish to build only individual things then :
133 |
134 | * `make game-cpm` to build a normal CP/M version.
135 | * `make game-cpm-encrypted` to build an encrypted CP/M version.
136 | * `make game-spectrum` to build the ZX Spectrum version.
137 | * `make game-spectrum-encrypted` to build the encrypted ZX Spectrum version.
138 | * `make lighthouse` will build the C-game for Linux
139 | * `make release` will build both versions of the CP/M and ZX Spectrum release.
140 |
141 | If you have a working golang system you can run the game under this trivial CP/M emulator:
142 |
143 | * https://github.com/skx/cpmulator
144 |
145 |
146 |
147 | ## Downloading It
148 |
149 | If you look on our [release page](https://github.com/skx/lighthouse-of-doom/releases/) you can find the latest stable build.
150 |
151 | * For CP/M download `lihouse.com` to your system, and then run `LIHOUSE` to launch it.
152 | * `lihousex.com` is the encrypted version.
153 | * For the ZX Spectrum download `lihouse.tap` to your system, and then launch in your favourite emulator.
154 | * `lihousex.tap` is the encrypted version.
155 |
156 |
157 |
158 | ## Bugs?
159 |
160 | Report any bugs as you see them:
161 |
162 | * A crash of the game is a bug.
163 | * Bad spelling, grammar, or broken punctuation are also bugs.
164 | * Getting into a zombie-state where winning or losing are impossible is a bug.
165 |
166 |
167 |
168 | Steve
169 |
--------------------------------------------------------------------------------
/bios.z80:
--------------------------------------------------------------------------------
1 | ; bios.z80
2 | ;
3 | ; This file contains the common primitives we use for interfacing
4 | ; with CP/M or the ZX Spectrum (48k).
5 | ;
6 | ; We don't use many primitives, but we do need some facilities from
7 | ; the operating system, or the Spectrum ROM:
8 | ;
9 | ; Pause for a keypress.
10 | ;
11 | ; Clear the screen.
12 | ;
13 | ; Output a string.
14 | ;
15 | ; Allow the user to enter input
16 | ;
17 | ; Pause for a "small delay"
18 | ;
19 | ; The routines here will work for both systems, and contain the only
20 | ; platform-specific code we need.
21 | ;
22 |
23 | IF SPECTRUM
24 | MAX_INPUT_LENGTH: EQU 30
25 | ELSE
26 | MAX_INPUT_LENGTH: EQU 78
27 | ENDIF
28 |
29 | ;
30 | ; Initialization routine for the BIOS, if needed
31 | ; {{
32 | bios_init:
33 |
34 | IF SPECTRUM
35 | ld a,2
36 | call 5633 ; ROM_OPEN_CHANNEL
37 | call c_chan
38 | ld a,4
39 | call 5633 ; ROM_OPEN_CHANNEL
40 |
41 | ENDIF
42 | ret
43 | ; }}
44 |
45 | ;
46 | ; Delay for "a short while".
47 | ;
48 | ; This is used for showing "dots" in the SLEEP command, or when the
49 | ; grue is threatening you..
50 | ;
51 | ; {{
52 | bios_delay:
53 |
54 | IF SPECTRUM
55 | PUSH_ALL
56 | ld b,4
57 | wait:
58 | halt
59 | djnz wait
60 | POP_ALL
61 | ret
62 |
63 | ELSE
64 | ; Random values found by experimentation with my own system
65 | ld hl,0xffff / 8
66 | hl_delay_loop:
67 | inc hl
68 | ld de, 0xffff - 10
69 | de_delay_loop:
70 | inc de
71 | ld a, d
72 | or e
73 | jr nz, de_delay_loop
74 | ld a,h
75 | or l
76 | jr nz, hl_delay_loop
77 | ret
78 |
79 | ENDIF
80 | ;; }}
81 |
82 |
83 |
84 | ;
85 | ; Clear the screen.
86 | ;
87 | ; {{
88 | bios_clear_screen:
89 |
90 | IF SPECTRUM
91 | PUSH_ALL
92 | ld a,2
93 | call 0x1601 ; ROM_OPEN_CHANNEL
94 | call 0x0DAF ; ROM_CLS
95 |
96 | ;
97 | ; The code above will clear the screen, however we use a custom
98 | ; printing routine which has its own notion of the X,Y / AT
99 | ; coordinates to use.
100 | ;
101 | ; We explicitly reset those here, otherwise the screen will be clear,
102 | ; but printing will start at whatever spot it finished the previous
103 | ; time something was output.
104 | ;
105 | xor a
106 | ld hl,atflg
107 | ld (hl),a
108 | ld hl, row
109 | ld (hl),a
110 | ld hl, col
111 | ld (hl),a
112 | POP_ALL
113 | ret
114 |
115 | ELSE
116 | ld de, cpm_clear_screen_msg
117 | call bios_output_string
118 | ret
119 |
120 | cpm_clear_screen_msg:
121 | db 27, "[2J" ; "clear"
122 | db 27, "[H" ; "home"
123 | db "$"
124 | ENDIF
125 | ; }}
126 |
127 | ;
128 | ; Await a keypress. The pressed-key will be returned in the A-register.
129 | ;
130 | ; {{
131 | bios_await_keypress:
132 |
133 | IF SPECTRUM
134 | push hl
135 | ld hl, 0x5C08 ; LASTK - Set by the Spectrum ROM
136 | ld a,255
137 | ld (hl),a
138 | await_key:
139 | cp (hl)
140 | jr z,await_key
141 |
142 | ; get the key, and return it.
143 | ld a,(hl)
144 | pop hl
145 | ret
146 | ELSE
147 | ld c, 0x01
148 | call 0x0005
149 | ret
150 | ENDIF
151 | ; }}
152 |
153 |
154 | ;
155 | ; Output a single character, stored in the E register.
156 | ;
157 | ; {{
158 | bios_output_character:
159 | PUSH_ALL
160 | IF SPECTRUM
161 | ; output the character
162 | ld a,e
163 | RST 0x10
164 | ELSE
165 | ld c, 0x02
166 | call 0x005
167 | ENDIF
168 | POP_ALL
169 | ret
170 | ; }}
171 |
172 | ;
173 | ; Output a string, terminated by "$".
174 | ;
175 | ; The address of the string is stored in DE.
176 | ;
177 | ; To handle wrapping we process the string character by character,
178 | ; this is inefficient, but works fine for this use-case.
179 | ;
180 | ; {{
181 | bios_output_string:
182 |
183 | IF SPECTRUM
184 | PUSH_ALL
185 | ld a,4
186 | call 5633 ; ROM_OPEN_CHANNEL
187 | POP_ALL
188 | ENDIF
189 |
190 | PUSH_ALL
191 |
192 | ; DE has the string
193 | ; We'll work on HL, and use BC to keep track of the width
194 | ; of the string we've printed thus-far
195 | push de
196 | pop hl
197 |
198 | ; length will be stored here
199 | print_reset_line:
200 | ld bc, 0
201 |
202 | print_char_loop:
203 | ; get a character
204 | ld a,(hl)
205 | inc hl
206 | inc bc
207 |
208 | ; end of printing?
209 | cp '$'
210 | jr z, end_of_print
211 |
212 | ; linefeed? Reset our printing width
213 | cp 0x0d
214 | jr nz, not_linefeed
215 | ld e, 0x0d
216 | call bios_output_character
217 | IF SPECTRUM
218 | ELSE
219 | ld e, 0x0a
220 | call bios_output_character
221 | ENDIF
222 | jr print_reset_line
223 |
224 | not_linefeed:
225 | ; We'll ignore the newline
226 | cp 0x0a
227 | jr z, print_char_loop
228 |
229 | ; is it a space?
230 | cp ' '
231 | jr nz, print_continues
232 |
233 | ; if our length is bigger than the width we force
234 | ; a newline, otherwise we keep going
235 | push af
236 | push hl
237 | ld hl, WRAP_LOCATION
238 | ld a,(hl)
239 | pop hl
240 | cp c
241 | jr c, too_wide
242 | pop af
243 |
244 | print_continues:
245 | ld e,a
246 | call bios_output_character
247 | jr print_char_loop
248 |
249 | too_wide:
250 | pop af
251 |
252 | PUSH_ALL
253 | ld e, 0x0d
254 | call bios_output_character
255 | IF SPECTRUM
256 | ELSE
257 | ld e, 0x0a
258 | call bios_output_character
259 | ENDIF
260 | POP_ALL
261 | jr print_reset_line
262 | end_of_print:
263 | POP_ALL
264 | ret
265 | ; }}
266 |
267 | ;
268 | ; Prompt the user for a line of input.
269 | ;
270 | ; The way this works is you pass the address of a region of memory,
271 | ; in the DE register. The first byte is the length of the buffer.
272 | ;
273 | ; On return the second byte of the buffer will be populated by the
274 | ; amount of text which was read, then the input itself:
275 | ;
276 | ; DE points to the memory buffer;
277 | ;
278 | ; 0x00 - buffer size
279 | ; 0x01 - read-length
280 | ; 0x02 ... char..
281 | ; {{
282 | bios_read_input:
283 | IF SPECTRUM
284 | PUSH_ALL
285 |
286 | ; Move the cursor to the bottom of the screen, and display the prompt
287 | PUSH_ALL
288 | call move_to_bottom
289 | ld e, ">"
290 | call bios_output_character
291 | POP_ALL
292 |
293 | push de
294 | pop hl ; buffer -> hl
295 |
296 | inc hl ; skip to the returned size
297 | inc de
298 |
299 | xor a
300 | ld (hl),a ; size is now zero
301 | inc hl ; point to the start of the character buffer
302 |
303 | read_input_again:
304 | ; read a char
305 | call bios_await_keypress
306 |
307 | ; return? Then we're done
308 | cp 13
309 | jp z, read_input_over
310 |
311 | ; delete? remove the last character
312 | cp 12
313 | jr z, backspace
314 |
315 | ; escape? trash the input and start again.
316 | cp 7
317 | jr z, reset_line
318 |
319 | ; too many characters?
320 | push de
321 | push af
322 | ld a,(de) ; get the count of characters we've entered
323 | cp MAX_INPUT_LENGTH ; ignore input if too long
324 | jr nc,too_many_characters
325 | jr z, too_many_characters
326 | pop af
327 | pop de
328 | jr continue_input
329 |
330 | too_many_characters:
331 | ; too many characters, cleanup stack and ignore the character
332 | pop af
333 | pop de
334 | jr read_input_again
335 |
336 | continue_input:
337 |
338 | ; display the new character
339 | push de
340 | ld e,a
341 | call bios_output_character
342 | pop de
343 |
344 | ; add the character to the buffer
345 | ld (hl),a
346 | inc hl
347 |
348 | ; increase the input count
349 | ld a,(de)
350 | inc a
351 | ld (de),a
352 |
353 | ; repeat
354 | jr read_input_again
355 |
356 | backspace:
357 | ; remove size of the line by one
358 | ; unless it was empty
359 | ld a,(de)
360 | cp 0
361 | jr z, read_input_again
362 | dec a
363 | ld (de),a
364 |
365 | ; remove character
366 | dec hl
367 | ld (hl), 0x00
368 |
369 | PUSH_ALL
370 |
371 | ; move cursor to start
372 | call move_to_bottom
373 |
374 | ; output spaces - to overwrite what was previously input
375 | ;
376 | ; NOTE: We assume our input was a single line.
377 | ld b, MAX_INPUT_LENGTH+1
378 | ld e, " "
379 | spaces:
380 | call bios_output_character
381 | djnz spaces
382 |
383 | ; move to the bottom, and show the prompt again
384 | call move_to_bottom
385 |
386 | ; show the prompt
387 | ld e, ">"
388 | call bios_output_character
389 | POP_ALL
390 |
391 | ; save registers
392 | push hl
393 | push de
394 | push bc
395 |
396 | ; is the input line empty? If so return,
397 | ; cleaning up the stack.
398 | ld a,(de)
399 | cp 0
400 | jr nz, non_empty
401 | pop bc
402 | pop de
403 | pop hl
404 | jr read_input_again
405 | non_empty:
406 | ; OK we're gonna show the input the user
407 | ; entered.
408 | ld b,a
409 | push de
410 | pop hl
411 | inc hl
412 | loopy:
413 | ld e,(hl)
414 | call bios_output_character
415 | inc hl
416 | djnz loopy
417 | pop bc
418 | pop de
419 | pop hl
420 | jp read_input_again
421 |
422 | reset_line:
423 | ; clear the screen, and restart the input process
424 | call bios_clear_screen
425 | POP_ALL
426 | jp bios_read_input
427 |
428 | read_input_over:
429 | ; move the cursor to the top of the screen, and
430 | ; prepare for output again.
431 | call bios_clear_screen
432 |
433 | POP_ALL
434 | ret
435 |
436 | move_to_bottom:
437 | ld a,22 ; AT code.
438 | rst 16 ; Set the cursor-coord
439 | ld a,21 ; vertical coord.
440 | rst 16 ; set the vertical coord
441 | xor a ; horizontal position.
442 | rst 16 ; set the horizontal coord.
443 | ret
444 | ELSE
445 | push de
446 | ld de, prompt_message
447 | call bios_output_string
448 | pop de
449 | ld c, 0x0A
450 | call 0x005
451 | ret
452 | prompt_message:
453 | db 0x0a, 0x0d,">$"
454 | ENDIF
455 | ; }}
456 |
--------------------------------------------------------------------------------
/c/globals.h:
--------------------------------------------------------------------------------
1 | #ifndef _GLOBALS
2 | #define _GLOBALS
3 |
4 | // Maximum named items in a room
5 | #define MAX_ITEMS_PER_ROOM 5
6 |
7 | // The maximum number of items you can carry
8 | #define MAX_INV 5
9 |
10 | // The number of rooms
11 | #define MAX_ROOMS 4
12 |
13 | // Unused function argument
14 | #define UNUSED(x) (void)(x)
15 |
16 | // Handler for an input/command function.
17 | typedef void (*actionPtr)(char *input);
18 |
19 | // Handler for "using" an object
20 | typedef void (*usePtr)(char *item);
21 |
22 | // Handler to use to "take" an object
23 | typedef void (*takePtr)(int id);
24 |
25 | // Handler to use to "drop" an object
26 | typedef void (*dropPtr)(int id);
27 |
28 | // This structure contains the details of a single location.
29 | //
30 | // Our "game world" is made up of a collection of these locations.
31 | typedef struct location
32 | {
33 | // one-line description of the location.
34 | char desc[100];
35 |
36 | // extended description of the location.
37 | char edesc[512];
38 |
39 | // have we seen this? If so we show the short-description
40 | // of the location when we enter it.
41 | int seen;
42 |
43 | // Items present in this location
44 | int items[MAX_ITEMS_PER_ROOM];
45 |
46 | } location_t;
47 |
48 |
49 |
50 | // This structure contains all the properties and details of a single item.
51 | typedef struct object
52 | {
53 | // The name of the item
54 | char name[17];
55 |
56 | // A one-line description of the item, shown when a room is entered.
57 | char desc[100];
58 |
59 | // Extended description, used for the output of "EXAMINE XXX".
60 | char edesc[255];
61 |
62 | // function to call when this item is used.
63 | usePtr use;
64 |
65 | // function to call when this item is used, while in the player's
66 | // possession. (`use` is only when an item is used in the same location.)
67 | usePtr use_carried;
68 |
69 | // Function to call when the play picks up this object.
70 | takePtr get_fn;
71 |
72 | // Function to call when the play drops up this object.
73 | dropPtr drop_fn;
74 |
75 | } object_t;
76 |
77 |
78 |
79 | // Word contains a command the user can type
80 | typedef struct word
81 | {
82 | // The name of the command the user will enter
83 | char name[10];
84 |
85 | // Help information for this command, if present.
86 | char txt[32];
87 |
88 | // Is this hidden? If so it is not shown in the output
89 | // of the help-command.
90 | int hidden;
91 |
92 | // The handler to invoke when that is typed
93 | actionPtr ptr;
94 | } word_t;
95 |
96 |
97 | // Game state
98 | enum gameState { Playing, Won, Dead };
99 |
100 | // The player's location
101 | extern int location;
102 |
103 | // Count of elapsed turns
104 | extern int turn;
105 |
106 | // The player's inventory
107 | extern int inv[MAX_INV];
108 |
109 | // Dead? Won?
110 | extern enum gameState state;
111 |
112 | // world locations
113 | extern location_t world[];
114 |
115 | // World items
116 | extern object_t items[];
117 |
118 | // Built-in commands
119 | extern word_t dictionary[];
120 |
121 |
122 | // Forward declarations
123 |
124 | // utils.c
125 | char *object_from_input(char *input);
126 |
127 | // handlers.c
128 | void cls_fn(char *input);
129 | void call_fn(char *input);
130 | void down_fn(char *input);
131 | void drop_fn(char *input);
132 | void examine_fn(char *input);
133 | void get_fn(char *input);
134 | void help_fn(char *input);
135 | void inventory_fn(char *input);
136 | void language_fn(char *input);
137 | void look_fn(char *input);
138 | void magic_fn(char *input);
139 | void open_fn(char *input);
140 | void quit_fn(char *input);
141 | void up_fn(char *input);
142 | void use_fn(char *input);
143 |
144 |
145 | // items.c
146 | int is_object_present(char *name);
147 | void location_add_item(char *name, int location);
148 | void location_remove_item(char *name, int location);
149 | void get_book(int id);
150 | void get_torch(int id);
151 | void get_torch_lit(int id);
152 | void use_torch(char *txt);
153 | void use_torch_carried(char *txt);
154 | void get_mirror(int id);
155 | void get_mirror_broken(int id);
156 | void get_rug(int id);
157 | void use_mirror(char *txt);
158 | void use_mirror_broken(char *txt);
159 | void get_generator(int id);
160 | void use_generator(char *txt);
161 | void use_generator_carried(char *txt);
162 | void use_telephone(char *txt);
163 | void drop_book(int id);
164 | void drop_mirror(int id) ;
165 | void drop_mirror_broken(int id) ;
166 | void drop_torch(int id) ;
167 | void drop_torch_lit(int id) ;
168 | void drop_generator(int id);
169 |
170 | // inventory.c
171 | int inventory_has_item(char *name);
172 | void inventory_take_item(char *name);
173 | void inventory_drop_item(char *name);
174 |
175 | #endif
176 |
--------------------------------------------------------------------------------
/c/handlers.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "globals.h"
6 |
7 |
8 |
9 | // Word will have a term the user can enter, and a function-pointer
10 | // of code to invoke.
11 | //
12 | // If a word is "hidden:1" it is not shown in the output of help.
13 | word_t dictionary[] =
14 | {
15 | { name: "DOWN", txt: "Descend the stairs", ptr: down_fn },
16 | { name: "DROP", txt: "Drop an item", ptr: drop_fn },
17 | { name: "EXAMINE", txt: "Examine an object or item", ptr: examine_fn },
18 | { name: "GET", txt: "Take an item", ptr: get_fn },
19 | { name: "HELP", txt: "Show some help", ptr: help_fn },
20 | { name: "INVENTORY", txt: "See what you're carrying", ptr: inventory_fn },
21 | { name: "LOOK", txt: "Look at your surroundings", ptr: look_fn },
22 | { name: "OPEN", txt: "Open an item", ptr: open_fn },
23 | { name: "QUIT", txt: "Quit the game", ptr: quit_fn },
24 | { name: "UP", txt: "Climb the stairs", ptr: up_fn },
25 | { name: "USE", txt: "Use an item", ptr: use_fn },
26 |
27 | // Synonyms
28 | { name: "TAKE", hidden: 1, ptr: get_fn },
29 | { name: "PICKUP", hidden: 1, ptr: get_fn },
30 | { name: "READ", hidden: 1, ptr: examine_fn },
31 | { name: "EXIT", hidden: 1, ptr: quit_fn },
32 | { name: "BYE", hidden: 1, ptr: quit_fn },
33 |
34 | // Hidden-commands
35 | { name: "CLS", hidden: 1, ptr: cls_fn},
36 |
37 | // Easter-Eggs
38 | { name: "CALL", hidden: 1, ptr: call_fn},
39 | { name: "DIAL", hidden: 1, ptr: call_fn},
40 | { name: "PHONE", hidden: 1, ptr: call_fn},
41 | { name: "FUCK", hidden: 1, ptr: language_fn},
42 | { name: "SHIT", hidden: 1, ptr: language_fn},
43 | { name: "XYZZY", hidden: 1, ptr: magic_fn},
44 |
45 | // Abbreviations
46 | { name: "L", hidden: 1, ptr: look_fn },
47 | { name: "INV", hidden: 1, ptr: inventory_fn },
48 | { name: "I", hidden: 1, ptr: inventory_fn },
49 | { name: "U", hidden: 1, ptr: up_fn },
50 | { name: "D", hidden: 1, ptr: down_fn },
51 | { name: "", ptr: NULL },
52 | };
53 |
54 |
55 | void cls_fn(char *input)
56 | {
57 | UNUSED(input);
58 |
59 | printf("\e[1;1H\e[2J");
60 | }
61 |
62 | void call_fn(char *input)
63 | {
64 | if (!is_object_present("telephone"))
65 | {
66 | printf("There is no telephone here!\n");
67 | return;
68 | }
69 |
70 | if ((strstr(input, "999") != NULL) ||
71 | (strstr(input, "911") != NULL) ||
72 | (strstr(input, "POLICE") != NULL))
73 | {
74 | printf("Trying to call the police was a smart move, but Adventure Bay\n"
75 | "does not have a well-funded police-service.\n");
76 | return;
77 | }
78 |
79 | if ((strstr(input, "PAW") != NULL) &&
80 | (strstr(input, "PATROL") != NULL))
81 | {
82 | printf("The Paw Patrol are already on a roll, and cannot be disturbed.\n");
83 | return;
84 | }
85 |
86 | }
87 |
88 | void inventory_fn(char *input)
89 | {
90 | UNUSED(input);
91 |
92 | int found = 0;
93 |
94 | for (int i = 0; i < MAX_INV; i++)
95 | {
96 |
97 | // this slot is not empty?
98 | if (inv[i] != -1)
99 | {
100 | found = 1;
101 | }
102 | }
103 |
104 | if (found)
105 | {
106 | printf("You are carrying:\n");
107 |
108 | for (int i = 0; i < MAX_INV; i++)
109 | {
110 |
111 | // this slot is not empty?
112 | int item = inv[i];
113 |
114 | if (item != -1)
115 | {
116 | printf("\t%s\n", items[item].desc);
117 | }
118 | }
119 | }
120 | else
121 | {
122 | printf("You are not carrying anything.\n");
123 | }
124 | }
125 |
126 | void language_fn(char *input)
127 | {
128 | UNUSED(input);
129 |
130 | printf("Such bad language!\n");
131 | }
132 |
133 | void magic_fn(char *input)
134 | {
135 | UNUSED(input);
136 |
137 | static int count = 0;
138 | count++;
139 |
140 | if (count == 1)
141 | {
142 | printf("Magic happens.\n");
143 | }
144 | else if (count == 2)
145 | {
146 | printf("Magic intensifies.\n");
147 | }
148 | else if (count == 3)
149 | {
150 | printf("The sensation of magic screaming through your veins gives you a heady rush.\n");
151 | }
152 | else if (count == 4)
153 | {
154 | printf("You couldn't draw the line, could you?\n");
155 | printf("The magic flooding your body is too powerful, and you're finding it impossible\n"
156 | "to breath. With a wail of frustration you topple backwards.\n"
157 | "\nYou're dead.\n");
158 | state = Dead;
159 | }
160 | }
161 |
162 | void look_fn(char *input)
163 | {
164 | printf("You are in %s\n\n", world[location].desc);
165 |
166 | // First time we enter a room we show the full
167 | // description.
168 | //
169 | // Or if the user types "LOOK"
170 | int full = 0;
171 |
172 | if (world[location].seen == 0 ||
173 | strcmp(input, "LOOK") == 0)
174 | {
175 | full = 1;
176 | }
177 |
178 | // We've shown the short description, return unless
179 | // we're showing everything
180 | if (!full)
181 | return;
182 |
183 | printf("%s\n", world[location].edesc);
184 |
185 | int found = 0;
186 |
187 | for (int i = 0; i < MAX_ITEMS_PER_ROOM; i++)
188 | {
189 | if (world[location].items[i] != -1)
190 | {
191 | found = 1;
192 | }
193 | }
194 |
195 | if (found)
196 | {
197 | printf("You see:\n");
198 |
199 | for (int i = 0; i < MAX_ITEMS_PER_ROOM; i++)
200 | {
201 | int id = world[location].items[i];
202 |
203 | if (id != -1)
204 | {
205 | if (strlen(items[id].desc) > 0)
206 | printf("\t%s\n", items[id].desc);
207 | }
208 | }
209 | }
210 |
211 | world[location].seen = 1;
212 | }
213 |
214 | void quit_fn(char *input)
215 | {
216 | UNUSED(input);
217 |
218 | printf("Game over man, game over!\n");
219 | exit(0);
220 | }
221 |
222 | void help_fn(char *input)
223 | {
224 | UNUSED(input);
225 |
226 | int dict = 0;
227 | printf("The following commands are available:\n");
228 |
229 | while (strlen(dictionary[dict].name) > 0)
230 | {
231 | if (dictionary[dict].hidden != 1)
232 | {
233 | printf("\t%s\n\t %s\n", dictionary[dict].name, dictionary[dict].txt);
234 | }
235 |
236 | dict++;
237 | }
238 | }
239 |
240 | void up_fn(char *input)
241 | {
242 |
243 | // basement - no torch
244 | if (location == 4)
245 | {
246 |
247 | if ((rand() % 10) >= 6)
248 | {
249 | printf("You cannot see where you're going, but it seems like the\n"
250 | "smell is getting stronger\n");
251 | return;
252 | }
253 | else
254 | {
255 | printf("The smell is definitely closer now.\n");
256 | printf("You panic, and fall to the ground.\n");
257 | printf("The darkness is a mercy, as the grue attacks..\n");
258 | printf("Too bad the ship will crash; more food for the grue though.\n");
259 | state = Dead;
260 | }
261 | }
262 |
263 | // basement - with torch
264 | if (location == 3)
265 | {
266 | location = 2;
267 | look_fn(input);
268 | return;
269 | }
270 |
271 | // ground
272 | if (location == 2)
273 | {
274 | location = 1;
275 | look_fn(input);
276 | return;
277 | }
278 |
279 | // middle
280 | if (location == 1)
281 | {
282 | location = 0;
283 | look_fn(input);
284 | return;
285 | }
286 |
287 | // top
288 | printf("You cannot go up from here!\n");
289 | }
290 |
291 | void down_fn(char *input)
292 | {
293 | if (location == 0)
294 | {
295 | location = 1;
296 | look_fn(input);
297 | return;
298 | }
299 |
300 | if (location == 1)
301 | {
302 | location = 2;
303 | look_fn(input);
304 | return;
305 | }
306 |
307 | if (location == 2)
308 | {
309 |
310 | // we're on the ground floor
311 | // if the trapdoor is open you can go down
312 | if (is_object_present("trapdoor-open"))
313 | {
314 |
315 | if (inventory_has_item("torch-lit"))
316 | {
317 | location = 3;
318 | look_fn(input);
319 | }
320 | else
321 | {
322 | location = 4;
323 | look_fn(input);
324 | }
325 |
326 | return;
327 |
328 | }
329 | }
330 |
331 | if (location == 4)
332 | {
333 | if ((rand() % 10) >= 6)
334 | {
335 | printf("You cannot see where you're going, but it seems like the\n"
336 | "smell is getting stronger\n");
337 | return;
338 | }
339 | else
340 | {
341 | printf("The smell is definitely closer now.\n");
342 | printf("You panic, and fall to the ground.\n");
343 | printf("The darkness is a mercy, as the grue attacks..\n");
344 | printf("Too bad the ship will crash; more food for the grue though.\n");
345 | state = Dead;
346 | }
347 | }
348 |
349 | printf("You cannot go down from here!\n");
350 | }
351 |
352 | // examine an object
353 | void examine_fn(char *input)
354 | {
355 | // We'll be called with "EXAMINE XXXX" - so we need to find
356 | // the item.
357 | char *itm = object_from_input(input);
358 |
359 | if (itm == NULL)
360 | {
361 | printf("You need to tell me what to examine!\n");
362 | return;
363 | }
364 |
365 | //
366 | // Right see if this item is in the user's possession
367 | //
368 | for (int i = 0; i < MAX_INV; i++)
369 | {
370 |
371 | // The item
372 | int carried = inv[i];
373 |
374 | // Is the item present? With the same name?
375 | //
376 | // Here we cap the comparison length to allow "mirror" to
377 | // match both "mirror" and "mirror-broken".
378 | //
379 | if (carried != -1 && strncmp(itm, items[carried].name, strlen(itm)) == 0)
380 | {
381 | if (strlen(items[carried].edesc) > 0)
382 | {
383 | printf("%s\n", items[carried].edesc);
384 | }
385 | else
386 | {
387 | printf("You see nothing special.\n");
388 | }
389 |
390 | free(itm);
391 | return;
392 | }
393 | }
394 |
395 | //
396 | // Look for the item in the environment
397 | //
398 | for (int i = 0; i < MAX_ITEMS_PER_ROOM; i++)
399 | {
400 |
401 | // The item
402 | int present = world[location].items[i];
403 |
404 | // Is this slot full of something?
405 | if (present != -1)
406 | {
407 | // Here we cap the comparison length to allow "mirror" to
408 | // match both "mirror" and "mirror-broken".
409 | if (strncmp(itm, items[present].name, strlen(itm)) == 0)
410 | {
411 | if (strlen(items[present].edesc) > 0)
412 | {
413 | printf("%s\n", items[present].edesc);
414 | }
415 | else
416 | {
417 | printf("You see nothing special.\n");
418 | }
419 |
420 | free(itm);
421 | return;
422 | }
423 | }
424 | }
425 |
426 | printf("It doesn't look like that item is present, or in your inventory!\n");
427 | free(itm);
428 | return;
429 |
430 | }
431 |
432 | // get