Debian Codegraph @

terps/scott/ai_uk/robinofsherwood.c

raw symbols SCIP
1 //
2 // robinofsherwood.c
3 // part of ScottFree, an interpreter for adventures in Scott Adams format
4 //
5 // Created by Petter Sjölund on 2022-01-10.
6 //
7
8 #include <ctype.h>
9 #include <string.h>
10
11 #include "robinofsherwood.h"
12
13 #include "decompresstext.h"
14 #include "sagadraw.h"
15 #include "sagagraphics.h"
16 #include "scott.h"
17
18 extern Image *images;
19 extern uint8_t screenchars[768][8];
20
21 static uint8_t *forest_images = NULL;
22
23 #define WATERFALL_ANIMATION_RATE 15
24
25 void animate_lightning(int stage);
26
27 void SherwoodAction(int p)
28 {
29 event_t ev;
30
31 switch (p) {
32 case 0:
33 // Flash animation
34 AnimationFlag = 1;
35 glk_request_timer_events(15);
36
37 while (AnimationFlag < 11) {
38 glk_select(&ev);
39 if (ev.type == evtype_Timer) {
40 AnimationFlag++;
41 animate_lightning(AnimationFlag);
42 }
43 }
44 break;
45 case 1:
46 DrawImage(0); /* Herne */
47 Display(Bottom, "\n%s\n", sys[HIT_ENTER]);
48 showing_closeup = 1;
49 HitEnter();
50 Items[39].Location = 79;
51 Look();
52 break;
53 case 2:
54 // Climbing tree in forest
55 SavedRoom = MyLoc;
56 MyLoc = 93;
57 Look();
58 break;
59 default:
60 fprintf(stderr, "Unhandled SherwoodAction %d!\n", p);
61 break;
62 }
63 }
64
65 int IsForestLocation(void) { return (MyLoc >= 11 && MyLoc <= 73); }
66
67 #define TREES 0
68 #define BUSHES 1
69
70 void draw_sherwood(int loc)
71 {
72 glk_window_clear(Graphics);
73 int subimage_index = 0;
74
75 for (int i = 0; i < loc - 11; i++) {
76 // BUSHES type images are made up of 5 smaller images
77 int skip = 5;
78 if (forest_images[subimage_index] < 128)
79 // Those with bit 7 unset have 10 (trees only) or 11 (trees with path)
80 skip = 11;
81 subimage_index += skip;
82 }
83
84 int forest_type = TREES;
85 int subimages;
86
87 // if bit 7 of the image index is set then this image is BUSHES
88 if (forest_images[subimage_index] >= 128) {
89 forest_type = BUSHES;
90 subimages = 5;
91 // if the last subimage value is 255, there is no path
92 } else if (forest_images[subimage_index + 10] == 0xff) {
93 // Trees only
94 subimages = 10;
95 } else {
96 // Trees with path
97 subimages = 11;
98 }
99
100 int xpos = 0, ypos = 0, image_number;
101
102 for (int i = 0; i < subimages; i++) {
103 if (forest_type == TREES) {
104 if (i >= 8) {
105 if (i == 8) { // Undergrowth
106 ypos = 7;
107 xpos = 0;
108 } else if (i == 9) { // Bottom path
109 ypos = 10;
110 xpos = 0;
111 } else { // Forward path
112 ypos = 7;
113 xpos = 12;
114 }
115 } else { // Trees (every tree image is 4 characters wide)
116 ypos = 0;
117 xpos = i * 4;
118 }
119 }
120
121 image_number = forest_images[subimage_index++] & 127;
122
123 DrawSagaPictureAtPos(image_number, xpos, ypos);
124
125 if (forest_type == BUSHES) {
126 xpos += images[image_number].width;
127 }
128 }
129 }
130
131 void animate_waterfall(int stage)
132 {
133 RectFill(88, 16, 48, 64, white_colour);
134 for (int line = 2; line < 10; line++) {
135 for (int col = 11; col < 17; col++) {
136 for (int i = 0; i < 8; i++)
137 for (int j = 0; j < 8; j++)
138 if ((screenchars[col + line * 32][i] & (1 << j)) != 0) {
139 int ypos = line * 8 + i + stage;
140 if (ypos > 79)
141 ypos = ypos - 64;
142 PutPixel(col * 8 + j, ypos, blue_colour);
143 }
144 }
145 }
146 }
147
148 void animate_waterfall_cave(int stage)
149 {
150 RectFill(248, 24, 8, 64, white_colour);
151 for (int line = 3; line < 11; line++) {
152 for (int i = 0; i < 8; i++)
153 for (int j = 0; j < 8; j++)
154 if ((screenchars[31 + line * 32][i] & (1 << j)) != 0) {
155 int ypos = line * 8 + i + stage;
156 if (ypos > 87)
157 ypos = ypos - 64;
158 PutPixel(248 + j, ypos, blue_colour);
159 }
160 }
161 }
162
163 void animate_lightning(int stage)
164 {
165 // swich blue and bright yellow
166 if (palchosen == C64B)
167 SwitchPalettes(6, 7);
168 else {
169 SwitchPalettes(1, 14);
170 SwitchPalettes(9, 6);
171 }
172 DrawSagaPictureNumber(77);
173 if (stage == 11) {
174 glk_request_timer_events(0);
175 } else if (stage == 3) {
176 glk_request_timer_events(700);
177 } else {
178 glk_request_timer_events(40);
179 }
180 }
181
182 void RobinOfSherwoodLook(void)
183 {
184 if (!IsForestLocation()) {
185 if (Rooms[MyLoc].Image == 255) {
186 CloseGraphicsWindow();
187 } else {
188 DrawImage(Rooms[MyLoc].Image);
189 for (int ct = 0; ct <= GameHeader.NumItems; ct++)
190 if (Items[ct].Image) {
191 if ((Items[ct].Flag & 127) == MyLoc && Items[ct].Location == MyLoc) {
192 DrawImage(Items[ct].Image);
193 }
194 }
195 }
196 }
197
198 if (MyLoc == 82) // Dummy room where exit from Up a tree goes
199 MyLoc = SavedRoom;
200 if (MyLoc == 93) // Up a tree
201 for (int i = 0; i < GameHeader.NumItems; i++)
202 if (Items[i].Location == 93)
203 Items[i].Location = SavedRoom;
204 if (MyLoc == 7 && Items[62].Location == 7) // Left bedroom, open treasure chest
205 DrawImage(70);
206 if (IsForestLocation()) {
207 OpenGraphicsWindow();
208 draw_sherwood(MyLoc);
209
210 if (Items[36].Location == MyLoc) {
211 //"Gregory the tax collector with his horse and cart"
212 DrawImage(15); // Horse and cart
213 DrawImage(3); // Sacks of grain
214 }
215 if (Items[60].Location == MyLoc || Items[77].Location == MyLoc) {
216 // "A serf driving a horse and cart"
217 DrawImage(15); // Horse and cart
218 DrawImage(12); // Hay
219 }
220 if (MyLoc == 73) {
221 // Outlaws camp
222 DrawImage(36); // Campfire
223 }
224 }
225
226 if (MyLoc == 86 || MyLoc == 79) {
227 glk_request_timer_events(WATERFALL_ANIMATION_RATE);
228 }
229 }
230
231 void UpdateRobinOfSherwoodAnimations(void)
232 {
233 AnimationFlag++;
234 if (AnimationFlag > 63)
235 AnimationFlag = 0;
236 if (MyLoc == 86 || MyLoc == 79 || MyLoc == 84) {
237 /* If we're in room 84, the stone circle, we just */
238 /* want the timer to not switch off */
239 if (MyLoc == 86) {
240 animate_waterfall(AnimationFlag);
241 } else if (MyLoc == 79) {
242 animate_waterfall_cave(AnimationFlag);
243 }
244 } else {
245 glk_request_timer_events(0);
246 }
247 }
248
249 void LoadExtraSherwoodData(int c64)
250 {
251 // room images
252
253 int offset = file_baseline_offset + ((c64 == 1) ? 0x1ffd : 0x3d99);
254
255 uint8_t *ptr= SeekToPos(entire_file, offset);
256
257 if (ptr == 0)
258 return;
259
260 int ct;
261 Room *rp = Rooms;
262
263 for (ct = 0; ct <= GameHeader.NumRooms; ct++) {
264 rp->Image = *(ptr++);
265 rp++;
266 if (ct == 10) {
267 for (int i = 0; i < 63; i++) {
268 rp++;
269 ct++;
270 }
271 }
272 }
273
274 // rooms
275
276 ct = 0;
277 rp = Rooms;
278
279 offset = file_baseline_offset + ((c64 == 1) ? 0x402e : 0x5b7e);
280
281 ptr = SeekToPos(entire_file, offset);
282 if (ptr == 0)
283 return;
284
285 do {
286 rp->Text = DecompressText(ptr, ct);
287 *(rp->Text) = tolower(*(rp->Text));
288 ct++;
289 if (ct == 11) {
290 for (int i = 0; i < 61; i++) {
291 rp++;
292 rp->Text = "in Sherwood Forest";
293 }
294 }
295 rp++;
296 } while (ct < 33);
297
298 if (c64 == 1) {
299 SysMessageType messagekey[] = {
300 NORTH,
301 SOUTH,
302 EAST,
303 WEST,
304 UP,
305 DOWN,
306 EXITS,
307 YOU_SEE,
308 YOU_ARE,
309 HIT_ENTER,
310 YOU_CANT_GO_THAT_WAY,
311 OK,
312 WHAT_NOW,
313 HUH,
314 YOU_HAVE_IT,
315 TAKEN,
316 DROPPED,
317 YOU_HAVENT_GOT_IT,
318 INVENTORY,
319 YOU_DONT_SEE_IT,
320 THATS_BEYOND_MY_POWER,
321 DIRECTION,
322 YOURE_CARRYING_TOO_MUCH,
323 PLAY_AGAIN,
324 RESUME_A_SAVED_GAME,
325 YOU_CANT_DO_THAT_YET,
326 I_DONT_UNDERSTAND,
327 NOTHING
328 };
329
330 for (int i = 0; i < 26; i++) {
331 sys[messagekey[i]] = system_messages[i];
332 }
333
334 sys[HIT_ENTER] = system_messages[30];
335 sys[WHAT] = system_messages[13];
336
337 } else {
338 SysMessageType messagekey[] = {
339 BAD_DATA,
340 OK,
341 OK,
342 PLAY_AGAIN,
343 I_DONT_UNDERSTAND,
344 YOU_CANT_DO_THAT_YET,
345 HUH,
346 DIRECTION,
347 YOU_HAVENT_GOT_IT,
348 YOU_HAVE_IT,
349 YOU_DONT_SEE_IT,
350 THATS_BEYOND_MY_POWER,
351 YOU_CANT_GO_THAT_WAY,
352 YOU_ARE,
353 YOU_SEE,
354 YOU_SEE,
355 EXITS,
356 INVENTORY,
357 NOTHING,
358 WHAT_NOW,
359 HIT_ENTER,
360 YOURE_CARRYING_TOO_MUCH,
361 RESUME_A_SAVED_GAME
362 };
363
364 for (int i = 0; i < 23; i++) {
365 sys[messagekey[i]] = system_messages[i];
366 }
367
368 sys[WHAT] = sys[HUH];
369 }
370 sys[EXITS_DELIMITER] = " ";
371 sys[MESSAGE_DELIMITER] = ". ";
372
373 offset = file_baseline_offset + ((c64 == 1) ? 0x2300 : 0x3b6e);
374
375 ptr = SeekToPos(entire_file, offset);
376 if (ptr == 0)
377 return;
378
379 int cells = 555;
380 forest_images = MemAlloc(cells);
381
382 for (int i = 0; i < cells; i++)
383 forest_images[i] = *(ptr++);
384
385 }