Debian Codegraph @

main.cpp

raw symbols SCIP
1 /*
2 * PROPRIETARY INFORMATION. This software is proprietary to POWDER
3 * Development, and is not to be reproduced, transmitted, or disclosed
4 * in any way without written permission.
5 *
6 * Produced by: Jeff Lait
7 *
8 * POWDER Development
9 *
10 * NAME: main.cpp ( POWDER Library, C++ )
11 *
12 * COMMENTS:
13 * Good old main.
14 *
15 * This is where the main game loop resides. It is also where
16 * a lot of the UI interaction has ended up. Code here, almost
17 * by definition, should be refactored somewhere else.
18 */
19
20 #include "mygba.h"
21 #include <stdio.h>
22 #include <ctype.h>
23
24 #include "gfxengine.h"
25 #include "gfx/all_bitmaps.h"
26 #include "artifact.h"
27 #include "encyc_support.h"
28 #include "control.h"
29 #include "glbdef.h"
30 #include "creature.h"
31 #include "map.h"
32 #include "assert.h"
33 #include "msg.h"
34 #include "item.h"
35 #include "itemstack.h"
36 #include "input.h"
37 #include "sramstream.h"
38 #include "victory.h"
39 #include "mobref.h"
40 #include "speed.h"
41 #include "piety.h"
42 #include "hiscore.h"
43 #include "stylus.h"
44 #include "rooms/allrooms.h"
45
46 #ifdef USE_VIRTUAL_SCREEN
47 #include "thread.h"
48 #endif
49
50 #if defined(USING_SDL) || defined(USING_DS)
51 #include <time.h>
52 #endif
53
54 // Defines whether we are in map testing mode.
55 //#define MAPTEST
56
57 // MULTIBOOT
58
59 // The following comment is preserved to provide sufficinet irony
60 // to avoid the danger of code anemia.
61 /*
62 ----------------------------------
63 This is a simple mapping application...
64 ----------------------------------
65 */
66
67 #ifdef USING_SDL
68 #define TILEWIDTH (gfx_gettilewidth())
69 #define TILEHEIGHT (gfx_gettileheight())
70 #else
71 #define TILEWIDTH 8
72 #define TILEHEIGHT 8
73 #endif
74
75 //
76 // No one likes autoprompt. Boohoo!
77 // But I have used it for a year now, so screw the user!
78 // In the end, it turned out to be a keyboard centric issue. Those
79 // lacking keyboards love autoprompt, those with hate. Thankfully
80 // this is a compile time issue.
81 //
82 #ifdef HAS_KEYBOARD
83 bool glbAutoPrompt = false;
84 #else
85 bool glbAutoPrompt = true;
86 #endif
87 bool glbSuppressAutoClimb = false;
88
89 // Do we allow the user to tap on the screen to move?
90 bool glbScreenTap = true;
91
92 #ifdef iPOWDER
93 bool glbActionBar = false;
94 #else
95 bool glbActionBar = true;
96 #endif
97
98 bool glbColouredFont = true;
99 bool glbFinishedASave = false;
100 bool glbSafeWalk = false;
101 bool glbToggleSafeWalk = false;
102 bool glbSwap = false;
103 bool glbToggleSwap = false;
104 bool glbJump = false;
105 bool glbToggleJump = false;
106 int glbAutoRunDX = 0;
107 int glbAutoRunDY = 0;
108 bool glbAutoRunEnabled = false;
109 bool glbAutoRunOpenSpace = false;
110 bool glbHasJustSearched = false;
111 int glbSearchCount = 0;
112
113 extern int glbMapCount;
114 extern int glbMobCount;
115 extern int glbItemCount;
116
117 ACTION_NAMES glb_actionbuttons[NUM_BUTTONS];
118
119 #ifdef USE_VIRTUAL_SCREEN
120 // Note the default of 0 actually is rather sensible here
121 int glb_spellgreylist[NUM_SPELLS];
122 #endif
123
124 #define STRIPLENGTH 50
125
126 // Specifies the default.
127 const ACTION_NAMES glb_globaldefaultactionstrip[STRIPLENGTH] =
128 {
129 ACTION_HELP,
130 ACTION_NONE,
131 ACTION_NONE,
132 ACTION_NONE,
133 ACTION_NONE,
134 ACTION_NONE,
135 ACTION_NONE,
136 ACTION_NONE,
137 ACTION_NONE,
138 ACTION_NONE,
139 ACTION_NONE,
140 ACTION_NONE,
141 ACTION_NONE,
142 ACTION_NONE,
143 ACTION_OPTIONS,
144 // Bottom row.
145 ACTION_LOOK,
146 ACTION_PICKUP,
147 ACTION_CLIMB,
148 ACTION_ZAP,
149 ACTION_FIRE,
150 ACTION_EAT,
151 ACTION_CLOSE,
152 ACTION_PRAY,
153 ACTION_SWAP,
154 ACTION_NAME,
155 ACTION_MINIMAP,
156 ACTION_JUMP,
157 ACTION_COMMAND,
158 ACTION_INVENTORY,
159 ACTION_VERBLIST,
160 // Left
161 ACTION_NONE,
162 ACTION_NONE,
163 ACTION_NONE,
164 ACTION_NONE,
165 ACTION_NONE,
166 ACTION_NONE,
167 ACTION_NONE,
168 ACTION_NONE,
169 ACTION_NONE,
170 ACTION_NONE,
171 // Right
172 ACTION_NONE,
173 ACTION_NONE,
174 ACTION_NONE,
175 ACTION_NONE,
176 ACTION_NONE,
177 ACTION_NONE,
178 ACTION_NONE,
179 ACTION_NONE,
180 ACTION_NONE,
181 ACTION_NONE,
182 };
183
184
185 u8 glb_globalactionstrip[STRIPLENGTH];
186
187 u8 *
188 getGlobalActionStrip()
189 {
190 return glb_globalactionstrip;
191 }
192
193 // This is not remappable so is fixed.
194 const ACTION_NAMES glb_inventoryactionstrip[STRIPLENGTH] =
195 {
196 // Top
197 ACTION_FAVORITE,
198 ACTION_NONE,
199 ACTION_NONE,
200 ACTION_NONE,
201 ACTION_NONE,
202 ACTION_NONE,
203 ACTION_NONE,
204 ACTION_NONE,
205 ACTION_NONE,
206 ACTION_NONE,
207 ACTION_NONE,
208 ACTION_NONE,
209 ACTION_NONE,
210 ACTION_NONE,
211 ACTION_NONE,
212 // Bottom
213 ACTION_EQUIP,
214 ACTION_DEQUIP,
215 ACTION_DIP,
216 ACTION_QUAFF,
217 ACTION_READ,
218 ACTION_ZAP,
219 ACTION_EAT,
220 ACTION_DROP,
221 ACTION_EXAMINE,
222 ACTION_QUIVER,
223 ACTION_THROW,
224 ACTION_SPLITSTACK,
225 ACTION_NAME,
226 ACTION_INVENTORY,
227 ACTION_SORT,
228 // Left
229 ACTION_NONE,
230 ACTION_NONE,
231 ACTION_NONE,
232 ACTION_NONE,
233 ACTION_NONE,
234 ACTION_NONE,
235 ACTION_NONE,
236 ACTION_NONE,
237 ACTION_NONE,
238 ACTION_NONE,
239 // Right
240 ACTION_NONE,
241 ACTION_NONE,
242 ACTION_NONE,
243 ACTION_NONE,
244 ACTION_NONE,
245 ACTION_NONE,
246 ACTION_NONE,
247 ACTION_NONE,
248 ACTION_NONE,
249 ACTION_NONE,
250 };
251
252 void
253 attemptunlock()
254 {
255 #ifdef iPOWDER
256 gfx_pager_addtext(
257 "POWDER is not like many similar looking games. It "
258 "is entirely reasonable for you to spend the next "
259 "year actively playing it and still not reach "
260 "dungeon level 15."
261 );
262 gfx_pager_newline(); gfx_pager_newline();
263 gfx_pager_addtext(
264 "This is not because of an arduous grind! You could win a game "
265 "of POWDER in three hours of play time. It is because "
266 "POWDER is hard, unforgiving, and a heck of a lot of fun."
267 );
268 gfx_pager_newline(); gfx_pager_newline();
269 gfx_pager_addtext(
270 "To explore deeper than dungeon level 15, however, you "
271 "must unlock POWDER. To help you reach those levels, as an "
272 "added bonus, when unlocked, you gain access to the "
273 "powerful Wish command that allows you to summon any item or "
274 "creature. Finally, unlocking will enable the stress "
275 "test."
276 );
277 gfx_pager_newline();
278 gfx_pager_display();
279
280 if (!hamfake_allowunlocks())
281 {
282 gfx_pager_addtext(
283 "You have disabled in-app purchases. Please re-enable "
284 "and try again."
285 );
286 gfx_pager_newline();
287 gfx_pager_display();
288
289 return;
290 }
291
292 if (!gfx_yesnomenu("Unlock POWDER now?"))
293 {
294 return;
295 }
296
297 hamfake_buttonreq(6);
298
299 // This may take a while, let the user know it is asynchronous.
300 gfx_pager_addtext(
301 "Accessing the store..."
302 );
303 gfx_pager_newline(); gfx_pager_newline();
304 gfx_pager_addtext(
305 "This may take a while. System pop-ups will take you through "
306 "the rest of the process. In the meantime, we return you to "
307 "your regularly scheduled game."
308 );
309 gfx_pager_newline();
310 gfx_pager_display();
311 #endif
312 }
313
314 ACTION_NAMES
315 getInventoryActionStrip(int button)
316 {
317 return glb_inventoryactionstrip[button];
318 }
319
320 void
321 resetDefaultActionStrip()
322 {
323 int i;
324
325 // Initialize the button strip.
326 for (i = 0; i < STRIPLENGTH; i++)
327 {
328 glb_globalactionstrip[i] = action_packStripButton(glb_globaldefaultactionstrip[i]);
329 }
330 }
331
332
333 const BUTTONS glb_mapbuttons[] =
334 {
335 BUTTON_A,
336 BUTTON_B,
337 BUTTON_R,
338 BUTTON_L,
339 #ifdef HAS_XYBUTTON
340 BUTTON_X,
341 BUTTON_Y,
342 #endif
343 NUM_BUTTONS
344 };
345
346 char
347 getActionChar(ACTION_NAMES action)
348 {
349 return glb_actiondefs[action].hint[0];
350 }
351
352 void
353 hideSideActionBar()
354 {
355 #ifdef HAS_STYLUS
356 int x, sx, sy;
357 if (glbActionBar)
358 {
359 for (x = 0; x < STRIPLENGTH; x++)
360 {
361 action_indexToOverlayPos(x, sx, sy);
362
363 if (sx == -1 || sx == 29)
364 {
365 hamfake_enablesprite(x+1, false);
366 }
367 }
368 }
369 #endif
370 }
371
372 bool
373 getMask(u8 val, int mask)
374 {
375 if (val & mask)
376 return true;
377 return false;
378 }
379
380 u8
381 setMask(bool state, int mask)
382 {
383 if (state)
384 return mask;
385 return 0;
386 }
387
388 void
389 loadOptions(SRAMSTREAM &is)
390 {
391 u8 val;
392 int i;
393
394 // Load our action key mappings.
395 for (i = 0; glb_mapbuttons[i] != NUM_BUTTONS; i++)
396 {
397 is.readRaw((char *) &val, 1);
398 glb_actionbuttons[glb_mapbuttons[i]] =
399 (ACTION_NAMES) val;
400 }
401
402 is.readRaw((char *) &val, 1);
403
404 glbAutoPrompt = getMask(val, 1);
405 glbActionBar = getMask(val, 2);
406 glbOpaqueTiles = getMask(val, 4);
407 glbColouredFont = getMask(val, 8);
408 glbGender = getMask(val, 16);
409 glbSafeWalk = getMask(val, 32);
410 // We save the opposite sense so people get the expected for
411 // versions that didn't save it.
412 glbScreenTap = !getMask(val, 64);
413
414 is.readRaw((char *) &val, 1);
415 int pset, sset;
416 pset = val & 0xf;
417 sset = val >> 4;
418 // Verify the tileset is legal.
419 if (pset >= NUM_TILESETS - !hamfake_extratileset())
420 pset = 0;
421 if (sset >= NUM_TILESETS - !hamfake_extratileset())
422 sset = 0;
423 gfx_settilesetmode(0, pset);
424 gfx_settilesetmode(1, sset);
425
426 // Load our current font
427 is.readRaw((char *) &val, 1);
428 if (val >= NUM_FONTS)
429 val = 0;
430 gfx_switchfonts(val);
431
432 is.readRaw(glbAvatarName, 23);
433 glbAvatarName[23] = 0;
434 }
435
436 void
437 saveOptions(SRAMSTREAM &os)
438 {
439 u8 val;
440 int i;
441
442 // Save our current action key mappings.
443 for (i = 0; glb_mapbuttons[i] != NUM_BUTTONS; i++)
444 {
445 val = glb_actionbuttons[glb_mapbuttons[i]];
446 os.writeRaw((const char *) &val, 1);
447 }
448
449 val = 0;
450
451 val |= setMask(glbAutoPrompt, 1);
452 val |= setMask(glbActionBar, 2);
453 val |= setMask(glbOpaqueTiles, 4);
454 val |= setMask(glbColouredFont, 8);
455 val |= setMask(glbGender, 16);
456 val |= setMask(glbSafeWalk, 32);
457 val |= setMask(!glbScreenTap, 64);
458 glbSuppressAutoClimb = false;
459
460 os.writeRaw((const char *) &val, 1);
461
462 // Save our current tileset
463 val = gfx_gettilesetmode(0) | (gfx_gettilesetmode(1) << 4);
464 os.writeRaw((const char *) &val, 1);
465 val = gfx_getfont();
466 os.writeRaw((const char *) &val, 1);
467
468 // Not necessarily null terminated!
469 os.writeRaw(glbAvatarName, 23);
470 }
471
472 bool
473 makeAWish(MOB *avatar)
474 {
475 bool didsomething = false;
476
477 const char *topmenu[] =
478 {
479 "Item by Type",
480 "Magic Item",
481 "Monster",
482 "Spell",
483 "Skill",
484 "Experience",
485 "Befriend Gods",
486 "Level Teleport",
487 "Create Room",
488 "Shape Shift",
489 0
490 };
491
492 static int topchoice = 0;
493 int select, aorb;
494
495 select = gfx_selectmenu(5, 3, topmenu, aorb, topchoice);
496 if (select < 0 || aorb)
497 {
498 for (int y = 3; y < 19; y++)
499 gfx_cleartextline(y);
500 return false;
501 }
502 topchoice = select;
503
504
505 switch (topchoice)
506 {
507 case 0: // Item by type...
508 {
509 const char *typelist[NUM_ITEMTYPES+1];
510 ITEMTYPE_NAMES itype;
511 static int lasttype = 0;
512
513 FOREACH_ITEMTYPE(itype)
514 {
515 typelist[itype] = glb_itemtypedefs[itype].name;
516 }
517 typelist[NUM_ITEMTYPES] = 0;
518 select = gfx_selectmenu(5, 3, typelist, aorb, lasttype);
519 if (select < 0 || aorb)
520 break;
521 itype = (ITEMTYPE_NAMES) select;
522 lasttype = select;
523
524 // Build our list of matching items.
525 const char *itemlist[NUM_ITEMS+1];
526 ITEM_NAMES itemname[NUM_ITEMS+1], item;
527 int i = 0;
528
529 FOREACH_ITEM(item)
530 {
531 if (itype == ITEMTYPE_ANY
532 || itype == ITEMTYPE_ARTIFACT
533 || glb_itemdefs[item].itemtype == itype)
534 {
535 itemlist[i] = glb_itemdefs[item].name;
536 itemname[i] = item;
537 i++;
538 }
539 }
540 itemlist[i] = 0;
541
542 // Empty list means no selection possible
543 if (!i)
544 break;
545
546 static int lastitemchoice = 0;
547 select = gfx_selectmenu(5, 3, itemlist, aorb, lastitemchoice);
548 if (select < 0 || aorb)
549 break;
550 lastitemchoice = select;
551
552 {
553 // Acquire the item...
554 ITEM *wish;
555 wish = ITEM::create((ITEM_NAMES) itemname[select]);
556 if (itype == ITEMTYPE_ARTIFACT)
557 {
558 wish->makeArtifact();
559 }
560 // Some items need built in charges
561 if (itemname[select] == ITEM_BONES ||
562 itemname[select] == ITEM_CORPSE)
563 {
564 wish->addCharges(20);
565 }
566 avatar->formatAndReport("%U <wish> for %IU.", wish);
567 if (!avatar->acquireItem(wish))
568 {
569 avatar->formatAndReport("%IU <I:fall> at %r feet.", wish);
570 glbCurLevel->acquireItem(wish, avatar->getX(), avatar->getY(), avatar);
571 }
572 }
573
574 didsomething = true;
575 break;
576 }
577
578 case 1: // Magic Item
579 {
580 const char *typelist[NUM_MAGICTYPES+1];
581 MAGICTYPE_NAMES mtype;
582 static int lasttype = 0;
583
584 FOREACH_MAGICTYPE(mtype)
585 {
586 typelist[mtype] = glb_magictypedefs[mtype].name;
587 }
588 typelist[NUM_MAGICTYPES] = 0;
589 select = gfx_selectmenu(5, 3, typelist, aorb, lasttype);
590 if (select < 0 || aorb)
591 break;
592 mtype = (MAGICTYPE_NAMES) select;
593 lasttype = select;
594
595 int numtype[NUM_MAGICTYPES] =
596 {
597 0,
598 NUM_POTIONS,
599 NUM_SCROLLS,
600 NUM_RINGS,
601 NUM_HELMS,
602 NUM_WANDS,
603 NUM_AMULETS,
604 NUM_SPELLBOOKS,
605 NUM_BOOTSS,
606 NUM_STAFFS
607 };
608
609 const char *itemlist[NUM_ITEMS+1];
610 int i = 0;
611 for (i = 0; i < numtype[mtype]; i++)
612 {
613 switch (mtype)
614 {
615 case MAGICTYPE_NONE:
616 case NUM_MAGICTYPES:
617 UT_ASSERT(!"Illegal magic type");
618 itemlist[i] = "illegal";
619 break;
620 case MAGICTYPE_POTION:
621 itemlist[i] = glb_potiondefs[i].name;
622 break;
623 case MAGICTYPE_SCROLL:
624 itemlist[i] = glb_scrolldefs[i].name;
625 break;
626 case MAGICTYPE_RING:
627 itemlist[i] = glb_ringdefs[i].name;
628 break;
629 case MAGICTYPE_HELM:
630 itemlist[i] = glb_helmdefs[i].name;
631 break;
632 case MAGICTYPE_WAND:
633 itemlist[i] = glb_wanddefs[i].name;
634 break;
635 case MAGICTYPE_AMULET:
636 itemlist[i] = glb_amuletdefs[i].name;
637 break;
638 case MAGICTYPE_SPELLBOOK:
639 itemlist[i] = glb_spellbookdefs[i].name;
640 break;
641 case MAGICTYPE_BOOTS:
642 itemlist[i] = glb_bootsdefs[i].name;
643 break;
644 case MAGICTYPE_STAFF:
645 itemlist[i] = glb_staffdefs[i].name;
646 break;
647 }
648 }
649 itemlist[i] = 0;
650
651 // Nothing in the list, nothing to do.
652 if (!i)
653 break;
654
655 static int lastitemchoice = 0;
656 select = gfx_selectmenu(5, 3, itemlist, aorb, lastitemchoice);
657 if (select < 0 || aorb)
658 break;
659 lastitemchoice = select;
660
661 {
662 // Acquire the item...
663 ITEM *wish;
664 wish = ITEM::createMagicItem(mtype, select);
665
666 avatar->formatAndReport("%U <wish> for %IU.", wish);
667 if (!avatar->acquireItem(wish))
668 {
669 avatar->formatAndReport("%IU <I:fall> at %r feet.", wish);
670 glbCurLevel->acquireItem(wish, avatar->getX(), avatar->getY(), avatar);
671 }
672 }
673
674 didsomething = true;
675 break;
676 }
677
678 case 2: // Monster...
679 {
680 const char *moblist[NUM_MOBS+1];
681 MOB_NAMES mobname;
682 static int lastmob = 0;
683 int tx, ty;
684
685 FOREACH_MOB(mobname)
686 {
687 moblist[mobname] = glb_mobdefs[mobname].name;
688 }
689 moblist[NUM_MOBS] = 0;
690
691 select = gfx_selectmenu(5, 3, moblist, aorb, lastmob);
692 if (select < 0 || aorb)
693 break;
694 mobname = (MOB_NAMES) select;
695 lastmob = select;
696
697 for (int y = 3; y < 19; y++)
698 gfx_cleartextline(y);
699
700 // Ask for creation location.
701 avatar->formatAndReport("Create where?");
702 tx = avatar->getX();
703 ty = avatar->getY();
704 if (!gfx_selecttile(tx, ty))
705 break;
706
707 MOB *mob = MOB::create(mobname);
708
709 mob->move(tx, ty);
710 glbCurLevel->registerMob(mob);
711
712 avatar->formatAndReport("%U <wish> for %MU.", mob);
713
714 didsomething = true;
715 break;
716 }
717
718 case 3: // Spell...
719 {
720 const char *spelllist[NUM_SPELLS+1];
721 SPELL_NAMES spellname;
722 static int lastspell = 0;
723
724 FOREACH_SPELL(spellname)
725 {
726 spelllist[spellname] = glb_spelldefs[spellname].name;
727 }
728 spelllist[NUM_SPELLS] = 0;
729
730 select = gfx_selectmenu(5, 3, spelllist, aorb, lastspell);
731 if (select < 0 || aorb)
732 break;
733 spellname = (SPELL_NAMES) select;
734 lastspell = select;
735
736 for (int y = 3; y < 19; y++)
737 gfx_cleartextline(y);
738
739 avatar->formatAndReport("%U <wish> for %B1.", spelllist[spellname]);
740
741 if (!avatar->canCastSpell(spellname))
742 {
743 // Make sure this is a "free" spell and we don't run
744 // out of spell points..
745 avatar->incrementMagicDie(1);
746 // Likewise, verify we have sufficient mana for it.
747 avatar->incrementMaxMP(glb_spelldefs[spellname].mpcost);
748 avatar->learnSpell(spellname);
749 didsomething = true;
750 }
751 else
752 {
753 avatar->formatAndReport("%U already know %B1.", spelllist[spellname]);
754 }
755 break;
756 }
757 case 4: // Skill...
758 {
759 const char *skilllist[NUM_SKILLS+1];
760 SKILL_NAMES skillname;
761 static int lastskill = 0;
762
763 FOREACH_SKILL(skillname)
764 {
765 skilllist[skillname] = glb_skilldefs[skillname].name;
766 }
767 skilllist[NUM_SKILLS] = 0;
768
769 select = gfx_selectmenu(5, 3, skilllist, aorb, lastskill);
770 if (select < 0 || aorb)
771 break;
772 skillname = (SKILL_NAMES) select;
773 lastskill = select;
774
775 for (int y = 3; y < 19; y++)
776 gfx_cleartextline(y);
777
778 avatar->formatAndReport("%U <wish> for %B1.", skilllist[skillname]);
779
780 if (!avatar->hasSkill(skillname))
781 {
782 avatar->incrementHitDie(1);
783 avatar->learnSkill(skillname);
784 didsomething = true;
785 }
786 else
787 {
788 avatar->formatAndReport("%U already know %B1.", skilllist[skillname]);
789 }
790 break;
791 }
792 case 5: // Experience
793 {
794 const char *xpmenu[] =
795 {
796 "250 XP",
797 "1 Level",
798 "10 Levels",
799 "Ascension Kit",
800 0
801 };
802 int xpval[] =
803 {
804 250,
805 1000,
806 10000,
807 -1
808 };
809
810 select = gfx_selectmenu(5, 3, xpmenu, aorb);
811 if (select < 0 || aorb)
812 break;
813
814 // GAining exp triggers level up warnings
815 for (int y = 3; y < 19; y++)
816 gfx_cleartextline(y);
817
818 avatar->formatAndReport("You gain experience...");
819
820 if (xpval[select] < 0)
821 avatar->buildAscensionKit();
822 else
823 avatar->receiveExp(xpval[select]);
824
825 didsomething = true;
826 break;
827 }
828
829 case 6: // Befriend gods
830 {
831 const char *godlist[NUM_GODS+1];
832 GOD_NAMES godname;
833
834 FOREACH_GOD(godname)
835 {
836 godlist[godname] = glb_goddefs[godname].name;
837 }
838 godlist[NUM_GODS] = 0;
839 select = gfx_selectmenu(5, 3, godlist, aorb);
840 if (select < 0 || aorb)
841 break;
842 godname = (GOD_NAMES) select;
843
844 avatar->formatAndReport("%U <win> the favour of %B1.", godlist[godname]);
845 avatar->pietyGrant(godname, 100);
846
847 didsomething = true;
848 break;
849 }
850
851 case 7: // Level teleport
852 {
853 char buf[50];
854 int lvl;
855
856 {
857 for (int y = 3; y < 19; y++)
858 gfx_cleartextline(y);
859 }
860 gfx_printtext(0, 4, "Level? ");
861 input_getline(buf, 3, 7, 4, 23, 8);
862 {
863 int y;
864 for (y = 0; y < 20; y++)
865 gfx_cleartextline(y);
866 }
867
868 if (!isdigit(buf[0]))
869 break;
870
871 // It is suspected that there exist platforms where
872 // atoi does not work. Sigh.
873 // What is more depressing that this has taken me
874 // three tries to get right.
875 lvl = 0;
876 int off = 0;
877 while (isdigit(buf[off]))
878 {
879 lvl *= 10;
880 lvl += buf[off] - '0';
881 off++;
882 }
883
884 // Our depth is a s8 so can't support greater than 127,
885 // we just cap at 100 (anyways, the game proper stops at 25
886 // so not much point for the higher options)
887 // This avoids infinite loops if someone asks for a really deep
888 // level.
889 if (lvl > 100)
890 lvl = 100;
891
892 // Move to level lvl!
893 MAP *newlevel;
894 glbCurLevel->unregisterMob(avatar);
895 newlevel = glbCurLevel;
896 while (newlevel && newlevel->getDepth() != lvl)
897 {
898 if (lvl < newlevel->getDepth())
899 newlevel = newlevel->getMapUp();
900 else
901 newlevel = newlevel->getMapDown();
902 }
903
904 if (newlevel)
905 {
906 MAP::changeCurrentLevel(newlevel);
907 }
908
909 int x, y;
910 if (!glbCurLevel->findTile(SQUARE_LADDERUP, x, y))
911 {
912 if (!glbCurLevel->findTile(SQUARE_LADDERDOWN, x, y))
913 {
914 if (!glbCurLevel->findTile(SQUARE_DIMDOOR, x, y))
915 {
916 // Default paranoid position.
917 x = y = 15;
918 }
919 }
920 }
921 // Find a tile where we don't sit on an existing mob.
922 // If walk/swim fail, just go on top of existing mob.
923 if (!glbCurLevel->findCloseTile(x, y, MOVE_WALK))
924 glbCurLevel->findCloseTile(x, y, MOVE_WALK);
925
926 avatar->move(x, y, true);
927 glbCurLevel->registerMob(avatar);
928 didsomething = true;
929
930 break;
931 }
932
933 case 8: // Create a room
934 {
935 const char *roomlist[NUM_ALLROOMDEFS+1];
936 int rnum;
937 int tx, ty;
938 static int lastroom = 0;
939
940 for (rnum = 0; rnum < NUM_ALLROOMDEFS; rnum++)
941 {
942 roomlist[rnum] = glb_allroomdefs[rnum]->name;
943 }
944 roomlist[rnum] = 0;
945
946 select = gfx_selectmenu(5, 3, roomlist, aorb, lastroom);
947 if (select < 0 || aorb)
948 break;
949
950 lastroom = select;
951 rnum = select;
952
953 for (int y = 3; y < 19; y++)
954 gfx_cleartextline(y);
955
956 avatar->formatAndReport("Create where?");
957 tx = avatar->getX();
958 ty = avatar->getY();
959 if (!gfx_selecttile(tx, ty))
960 break;
961
962 avatar->formatAndReport("%U <wish> for %B1.", roomlist[rnum]);
963
964 ROOM room;
965
966 glbCurLevel->buildRandomRoomFromDefinition(room, glb_allroomdefs[rnum]);
967 // Center on desired tx/ty
968 int dx, dy;
969 dx = tx - (room.l.x + room.h.x)/2;
970 if (room.l.x + dx < 1)
971 dx = 1 - room.l.x;
972 if (room.h.x + dx > MAP_WIDTH - 2)
973 dx = MAP_WIDTH-2 - room.h.x;
974 dy = ty - (room.l.y + room.h.y)/2;
975 if (room.l.y + dy < 1)
976 dy = 1 - room.l.y;
977 if (room.h.y + dy > MAP_HEIGHT - 2)
978 dy = MAP_HEIGHT-2 - room.h.y;
979
980 room.l.x += dx;
981 room.h.x += dx;
982 room.l.y += dy;
983 room.h.y += dy;
984
985 glbCurLevel->drawRoom(room, false, false, false);
986 glbCurLevel->populateRoom(room);
987 didsomething = true;
988 break;
989 }
990
991 case 9: // Shape Shift
992 {
993 const char *moblist[NUM_MOBS+1];
994 MOB_NAMES mobname;
995 static int lastmob = 0;
996 int tx, ty;
997
998 FOREACH_MOB(mobname)
999 {
1000 moblist[mobname] = glb_mobdefs[mobname].name;
1001 }
1002 moblist[NUM_MOBS] = 0;
1003
1004 select = gfx_selectmenu(5, 3, moblist, aorb, lastmob);
1005 if (select < 0 || aorb)
1006 break;
1007 mobname = (MOB_NAMES) select;
1008 lastmob = select;
1009
1010 for (int y = 3; y < 19; y++)
1011 gfx_cleartextline(y);
1012
1013 // Determine if we should reset our stats.
1014 if (gfx_yesnomenu("Match monster stats?"))
1015 avatar->changeDefinition(mobname, true);
1016 else
1017 avatar->changeDefinition(mobname, false);
1018
1019 didsomething = true;
1020 break;
1021 }
1022 }
1023
1024 for (int y = 3; y < 19; y++)
1025 gfx_cleartextline(y);
1026
1027 return didsomething;
1028 }
1029
1030 void
1031 writeYesNoBar()
1032 {
1033 #ifdef HAS_STYLUS
1034 if (!glbActionBar)
1035 return;
1036
1037 int x, sx, sy;
1038 for (x = 0; x < STRIPLENGTH; x++)
1039 {
1040 if (x == 18)
1041 {
1042 gfx_spritetile(x+1, SPRITE_YESWAY);
1043 }
1044 else if (x == 26)
1045 {
1046 gfx_spritetile(x+1, SPRITE_NOWAY);
1047 }
1048 else
1049 {
1050 hamfake_enablesprite(x+1, false);
1051 continue;
1052 }
1053 action_indexToOverlayPos(x, sx, sy);
1054 hamfake_movesprite(x+1, sx*TILEWIDTH, sy*TILEHEIGHT);
1055 hamfake_enablesprite(x+1, true);
1056 }
1057 #endif
1058 }
1059
1060 void
1061 writeGlobalActionBar(bool useemptyslot)
1062 {
1063 // Set the absolute overlay tiles we need?
1064 #ifdef HAS_STYLUS
1065 int x, sx, sy, endx;
1066 hamfake_setinventorymode(false);
1067
1068 #ifdef USE_VIRTUAL_SCREEN
1069 // Post the current grey values for all spells
1070 SPELL_NAMES spell;
1071 FOREACH_SPELL(spell)
1072 {
1073 int grey;
1074 action_spriteFromStripButton(action_packStripButton(spell), &grey);
1075
1076 glb_spellgreylist[spell] = grey;
1077 }
1078 #endif
1079
1080 endx = STRIPLENGTH;
1081 if (!glbActionBar)
1082 {
1083 // Only draw if in drag mode, then only top bar.
1084 #ifdef HAS_KEYBOARD
1085 if (useemptyslot)
1086 endx = 15;
1087 else
1088 endx = 0;
1089 #else
1090 // If we lack a keyboard, we don't want the F shortcuts put up
1091 // there to confuse people.
1092 endx = 0;
1093 #endif
1094 }
1095
1096 for (x = 0; x < endx; x++)
1097 {
1098 if (!glb_globalactionstrip[x])
1099 {
1100 if (!useemptyslot)
1101 {
1102 hamfake_enablesprite(x+1, false);
1103 continue;
1104 }
1105 else
1106 {
1107 gfx_spritetile(x+1, SPRITE_EMPTYSLOT);
1108 }
1109 }
1110 else
1111 {
1112 SPRITE_NAMES sprite, overlay = SPRITE_INVALID;
1113 int grey;
1114 sprite = action_spriteFromStripButton(glb_globalactionstrip[x], &grey);
1115 if (grey == 256)
1116 overlay = SPRITE_SPELLREADY;
1117 gfx_spritetile_grey(x+1, sprite, grey, overlay);
1118 }
1119 action_indexToOverlayPos(x, sx, sy);
1120 hamfake_movesprite(x+1, sx*TILEWIDTH, sy*TILEHEIGHT);
1121 hamfake_enablesprite(x+1, true);
1122 }
1123 // Disable any remaining sprites.
1124 for (; x < STRIPLENGTH; x++)
1125 {
1126 hamfake_enablesprite(x+1, false);
1127 }
1128 #endif
1129 }
1130
1131 COLOUR_NAMES
1132 getColourFromRatio(int cur, int max)
1133 {
1134 if (cur < 5 || (cur < max / 6))
1135 return COLOUR_LIGHTRED;
1136 if (cur < max / 2)
1137 return COLOUR_LIGHTYELLOW;
1138 return COLOUR_LIGHTGREEN;
1139 }
1140
1141 void
1142 writeStatusLine()
1143 {
1144 BUF text;
1145 MOB *avatar;
1146 HUNGER_NAMES hunger;
1147 u8 dsymbol;
1148
1149 if (glbCurLevel)
1150 dsymbol = glb_branchdefs[glbCurLevel->branchName()].symbol;
1151 else
1152 dsymbol = SYMBOL_DLEVEL;
1153
1154 avatar = MOB::getAvatar();
1155 if (avatar)
1156 {
1157 #ifdef HAS_KEYBOARD
1158 text.sprintf(
1159 "%c%2d%c%03d/%02d%c%03d/%02d%c%03d%c%02d Zinc ",
1160 SYMBOL_AC, avatar->getAC(),
1161 SYMBOL_HEART, avatar->getHP(),avatar->getHitDie(),
1162 SYMBOL_MAGIC, avatar->getMP(),avatar->getMagicDie(),
1163 SYMBOL_EXP, avatar->getExp(),
1164 dsymbol, glbCurLevel->getDepth());
1165 #else
1166 #ifdef HAS_XYBUTTON
1167 text.sprintf(
1168 "%c%2d%c%03d/%02d%c%03d/%02d%c%03d%c%02d%c%c%c%c%c%c",
1169 SYMBOL_AC, avatar->getAC(),
1170 SYMBOL_HEART, avatar->getHP(),avatar->getHitDie(),
1171 SYMBOL_MAGIC, avatar->getMP(),avatar->getMagicDie(),
1172 SYMBOL_EXP, avatar->getExp(),
1173 dsymbol, glbCurLevel->getDepth(),
1174 // As the A is on the RHS, we do this B then A.
1175 // Likewise for Y then X.
1176 getActionChar(glb_actionbuttons[BUTTON_L]),
1177 getActionChar(glb_actionbuttons[BUTTON_R]),
1178 getActionChar(glb_actionbuttons[BUTTON_Y]),
1179 getActionChar(glb_actionbuttons[BUTTON_X]),
1180 getActionChar(glb_actionbuttons[BUTTON_B]),
1181 getActionChar(glb_actionbuttons[BUTTON_A]));
1182 #else
1183 text.sprintf(
1184 "%c%2d%c%03d/%02d%c%03d/%02d%c%03d%c%02d %c%c %c%c",
1185 SYMBOL_AC, avatar->getAC(),
1186 SYMBOL_HEART, avatar->getHP(),avatar->getHitDie(),
1187 SYMBOL_MAGIC, avatar->getMP(),avatar->getMagicDie(),
1188 SYMBOL_EXP, avatar->getExp(),
1189 dsymbol, glbCurLevel->getDepth(),
1190 // As the A is on the RHS, we do this B then A.
1191 getActionChar(glb_actionbuttons[BUTTON_L]),
1192 getActionChar(glb_actionbuttons[BUTTON_R]),
1193 getActionChar(glb_actionbuttons[BUTTON_B]),
1194 getActionChar(glb_actionbuttons[BUTTON_A]));
1195 #endif
1196 #endif
1197 // text.sprintf( "Time: %d ", speed_gettime());
1198 gfx_printtext(0, 19, text);
1199
1200 COLOUR_NAMES colour;
1201 int x;
1202
1203 // Rewrite our health in coloured text...
1204 if (glbColouredFont && avatar->getHP() < avatar->getMaxHP())
1205 {
1206 colour = getColourFromRatio(avatar->getHP(), avatar->getMaxHP());
1207 for (x = 0; x < 3; x++)
1208 gfx_printcolourchar(4+x, 19, text.buffer()[4+x], colour);
1209 }
1210 // And the magic...
1211 if (glbColouredFont && avatar->getMP() < avatar->getMaxMP())
1212 {
1213 colour = getColourFromRatio(avatar->getMP(), avatar->getMaxMP());
1214 for (x = 0; x < 3; x++)
1215 gfx_printcolourchar(11+x, 19, text.buffer()[11+x], colour);
1216 }
1217
1218 // Build the status line... Check the stats we think are cool.
1219 text.clear();
1220
1221 hunger = avatar->getHungerLevel();
1222 if (glb_hungerdefs[hunger].name)
1223 {
1224 text.strcat(glb_hungerdefs[hunger].name);
1225 text.strcat(" ");
1226 }
1227
1228 if (avatar->hasIntrinsic(INTRINSIC_STONING))
1229 text.strcat("Hardening ");
1230 if (avatar->hasIntrinsic(INTRINSIC_AFLAME))
1231 text.strcat("Aflame ");
1232 if (avatar->hasIntrinsic(INTRINSIC_STRANGLE))
1233 text.strcat("Strangled ");
1234 if (avatar->hasIntrinsic(INTRINSIC_PARALYSED))
1235 text.strcat("Paralysed ");
1236 if (avatar->hasIntrinsic(INTRINSIC_ASLEEP))
1237 text.strcat("Asleep ");
1238 if (avatar->hasIntrinsic(INTRINSIC_CONFUSED))
1239 text.strcat("Confused ");
1240 if (avatar->hasIntrinsic(INTRINSIC_POISON_DEADLY))
1241 text.strcat("Deadly Poisoned ");
1242 if (avatar->hasIntrinsic(INTRINSIC_POISON_HARSH))
1243 text.strcat("Harshly Poisoned ");
1244 if (avatar->hasIntrinsic(INTRINSIC_POISON_STRONG))
1245 text.strcat("Strongly Poisoned ");
1246 if (avatar->hasIntrinsic(INTRINSIC_POISON_NORMAL))
1247 text.strcat("Poisoned ");
1248 if (avatar->hasIntrinsic(INTRINSIC_POISON_MILD))
1249 text.strcat("Mildly Poisoned ");
1250 if (avatar->hasIntrinsic(INTRINSIC_BLEED))
1251 text.strcat("Bleeding ");
1252 if (avatar->hasIntrinsic(INTRINSIC_BLIND))
1253 text.strcat("Blind ");
1254 if (avatar->hasIntrinsic(INTRINSIC_DEAF))
1255 text.strcat("Deaf ");
1256 if (avatar->hasIntrinsic(INTRINSIC_TIRED))
1257 text.strcat("Tired ");
1258 if (avatar->hasIntrinsic(INTRINSIC_SUBMERGED))
1259 {
1260 // Switch on tile type...
1261 int tile;
1262
1263 tile = glbCurLevel->getTile(avatar->getX(), avatar->getY());
1264 if (glb_squaredefs[tile].submergeattack == ATTACK_DROWN)
1265 text.strcat("Submerged ");
1266 else
1267 text.strcat("Buried ");
1268 }
1269 if (avatar->hasIntrinsic(INTRINSIC_INPIT))
1270 text.strcat("In Pit ");
1271 if (avatar->hasIntrinsic(INTRINSIC_INTREE))
1272 text.strcat("In Tree ");
1273 if (avatar->hasIntrinsic(INTRINSIC_SLOW))
1274 text.strcat("Slow ");
1275 if (avatar->hasIntrinsic(INTRINSIC_AMNESIA))
1276 text.strcat("Amnesic ");
1277 if (avatar->hasIntrinsic(INTRINSIC_OFFBALANCE))
1278 text.strcat("Off Balance ");
1279 if (glbHasJustSearched)
1280 {
1281 if (avatar->hasIntrinsic(INTRINSIC_SEARCH))
1282 text.strcat("Searching ");
1283 else if (glbSearchCount == 1)
1284 text.strcat("Searched ");
1285 else
1286 text.appendSprintf("Searched x%d ", glbSearchCount);
1287 }
1288
1289 gfx_cleartextline(18);
1290 gfx_printtext(0, 18, text);
1291 }
1292 else
1293 {
1294 gfx_printtext(0, 19, SYMBOLSTRING_STRENGTH "0"
1295 SYMBOLSTRING_SMARTS "0"
1296 SYMBOLSTRING_AC "00"
1297 SYMBOLSTRING_HEART "000/00"
1298 SYMBOLSTRING_MAGIC "000/00"
1299 SYMBOLSTRING_EXP "000"
1300 SYMBOLSTRING_DLEVEL "00"
1301 " ");
1302
1303 gfx_cleartextline(18);
1304 if (glbHasAlreadyWon)
1305 gfx_printtext(0, 18, "Celebrating");
1306 else
1307 gfx_printtext(0, 18, "Dead");
1308 }
1309
1310 // Write out our buttons...
1311 writeGlobalActionBar(false);
1312 }
1313
1314 // This menu should be freed with delete []
1315 const char **
1316 buildMenu(ACTION_NAMES *menu, u8 **stripvalue = 0)
1317 {
1318 int num;
1319 int i;
1320 const char **result;
1321
1322 // Count number of actions...
1323 for (i = 0; menu[i] != ACTION_NONE; i++);
1324 num = i;
1325
1326 result = new const char *[num+1];
1327 if (stripvalue)
1328 *stripvalue = new u8 [num+1];
1329
1330 for (i = 0; menu[i] != ACTION_NONE; i++)
1331 {
1332 result[i] = glb_actiondefs[menu[i]].name;
1333 if (stripvalue)
1334 (*stripvalue)[i] = action_packStripButton(menu[i]);
1335 }
1336 // Terminating null...
1337 result[i] = 0;
1338 if (stripvalue)
1339 (*stripvalue)[i] = action_packStripButton(ACTION_NONE);
1340
1341 return result;
1342 }
1343
1344 ACTION_NAMES
1345 selectMenu(ACTION_NAMES *menu)
1346 {
1347 const char **m;
1348 int select, aorb, def;
1349 static ACTION_NAMES lastaction = ACTION_NONE;
1350
1351 m = buildMenu(menu);
1352
1353 // Find our default...
1354 {
1355 ACTION_NAMES *iter;
1356
1357 def = 0;
1358 for (iter = menu; *iter != ACTION_NONE; iter++)
1359 {
1360 if (*iter == lastaction)
1361 def = iter - menu;
1362 }
1363 }
1364
1365 select = gfx_selectmenu(30-10, 3, m, aorb, def);
1366
1367 if (aorb != 0)
1368 select = -1;
1369
1370 delete [] m;
1371
1372 if (select == -1)
1373 return ACTION_NONE;
1374 else
1375 {
1376 lastaction = menu[select];
1377 return menu[select];
1378 }
1379 }
1380
1381 // aorb = 0, a, 1, then b
1382 ACTION_NAMES
1383 selectAssignment(ACTION_NAMES *menu, int &button)
1384 {
1385 const char **m;
1386 static int lastsel = 0;
1387 int select;
1388 u8 *stripvalue;
1389
1390 m = buildMenu(menu, &stripvalue);
1391 select = gfx_selectmenu(30-12, 3, m, button, lastsel, true, true,
1392 stripvalue,
1393 glb_globalactionstrip);
1394 if (select >= 0)
1395 lastsel = select;
1396 delete [] m;
1397 delete [] stripvalue;
1398
1399 if (select == -1)
1400 return ACTION_NONE;
1401 else
1402 return menu[select];
1403 }
1404
1405 //
1406 // Prebuilt action menus
1407 //
1408
1409 // These are generic actions that can be assigned to a or b.
1410 ACTION_NAMES menu_generic[] =
1411 {
1412 ACTION_CLIMB,
1413 ACTION_SEARCH,
1414 ACTION_ZAP,
1415 ACTION_FIRE,
1416 ACTION_JUMP,
1417 ACTION_LOOK,
1418 ACTION_WAIT,
1419 ACTION_PICKUP,
1420 ACTION_EAT,
1421 ACTION_OPEN,
1422 ACTION_CLOSE,
1423 // ACTION_LOCK, // Does nothing. Should be item activated anyways.
1424 ACTION_BREATHE, // Breathes in a selected direction.
1425 ACTION_WISH, // Wishes that you knew the wizard password.
1426 ACTION_RELEASE,
1427 ACTION_COMMAND,
1428 ACTION_NAME,
1429 ACTION_SWAP,
1430 ACTION_SLEEP,
1431 ACTION_FORGET,
1432 ACTION_MOVE,
1433 ACTION_SAFEWALK,
1434 ACTION_RUN,
1435 ACTION_MINIMAP, // Message queue + minimap. Only back queues on L.
1436 ACTION_HISTORY, // Brings up message history display.
1437 ACTION_PRAY,
1438 ACTION_USE,
1439 ACTION_VERBLIST, // Needed so you can bind to this list itself
1440 ACTION_INVENTORY, // Likewise required for binding targets.
1441 ACTION_OPTIONS,
1442 ACTION_NONE
1443 };
1444
1445 void
1446 buildActionMenu(ITEM *item, ACTION_NAMES *menu)
1447 {
1448 bool equipped = false;
1449
1450 if (!item)
1451 {
1452 // Trivially true...
1453 *menu++ = ACTION_SORT;
1454 *menu++ = ACTION_NONE;
1455 return;
1456 }
1457
1458 if (!item->getX())
1459 equipped = true;
1460
1461 // Standard actions..
1462 if (item->getMagicType() == MAGICTYPE_POTION ||
1463 item->getDefinition() == ITEM_WATER)
1464 *menu++ = ACTION_QUAFF;
1465 if (item->getMagicType() == MAGICTYPE_SCROLL ||
1466 item->getMagicType() == MAGICTYPE_SPELLBOOK)
1467 *menu++ = ACTION_READ;
1468 if (item->getMagicType() == MAGICTYPE_WAND)
1469 *menu++ = ACTION_ZAP;
1470 if (item->getDefinition() == ITEM_LIGHTNINGRAPIER)
1471 *menu++ = ACTION_ZAP;
1472
1473 if (equipped)
1474 {
1475 // A smaller set of actions is valid for equipped items.
1476 *menu++ = ACTION_DEQUIP;
1477 *menu++ = ACTION_DIP;
1478 *menu++ = ACTION_NAME;
1479 *menu++ = ACTION_FAVORITE;
1480 *menu++ = ACTION_EXAMINE;
1481 *menu++ = ACTION_SORT;
1482 }
1483 else
1484 {
1485 // Standard actions..
1486 *menu++ = ACTION_EQUIP;
1487 *menu++ = ACTION_EAT;
1488 *menu++ = ACTION_DIP;
1489 *menu++ = ACTION_DROP;
1490 *menu++ = ACTION_SORT;
1491
1492 // If there is more than one item in a stack, you can split
1493 // the stack.
1494 if (item->getStackCount() > 1)
1495 *menu++ = ACTION_SPLITSTACK;
1496
1497 *menu++ = ACTION_NAME;
1498 *menu++ = ACTION_FAVORITE;
1499 *menu++ = ACTION_EXAMINE;
1500 *menu++ = ACTION_QUIVER;
1501 *menu++ = ACTION_THROW;
1502 }
1503
1504 // End the sequence.
1505 *menu++ = ACTION_NONE;
1506 }
1507
1508 void
1509 writeItemActionBar(ITEM *item, bool onlyexamine = false)
1510 {
1511 ACTION_NAMES menu[NUM_ACTIONS];
1512
1513 hamfake_setinventorymode(true);
1514
1515 if (onlyexamine)
1516 {
1517 ACTION_NAMES *m = menu;
1518 *m++ = ACTION_INVENTORY;
1519 if (item)
1520 *m++ = ACTION_EXAMINE;
1521 *m++ = ACTION_NONE;
1522 }
1523 else
1524 buildActionMenu(item, menu);
1525
1526 // Set the absolute overlay tiles we need?
1527 #ifdef HAS_STYLUS
1528 ACTION_NAMES action;
1529 int x, sx, sy;
1530
1531 if (!glbActionBar)
1532 {
1533 for (x = 0; x < STRIPLENGTH; x++)
1534 {
1535 hamfake_enablesprite(x+1, false);
1536 }
1537 return;
1538 }
1539 for (x = 0; x < STRIPLENGTH; x++)
1540 {
1541 bool enable = true;
1542
1543 action = glb_inventoryactionstrip[x];
1544
1545 action_indexToOverlayPos(x, sx, sy);
1546 hamfake_movesprite(x+1, sx*TILEWIDTH, sy*TILEHEIGHT);
1547 // First parse always succeed/always fail.
1548 // Then look at our action menu for insight.
1549 if (action == ACTION_NONE)
1550 enable = false;
1551 else if (action == ACTION_INVENTORY)
1552 enable = true;
1553 else
1554 {
1555 int j;
1556 // See if we can find it in our menu.
1557 for (j = 0; menu[j] != ACTION_NONE; j++)
1558 {
1559 if (menu[j] == action)
1560 break;
1561 }
1562 if (menu[j] == ACTION_NONE)
1563 enable = false;
1564 else
1565 enable = true;
1566 }
1567 // Display if enabled.
1568 hamfake_enablesprite(x+1, enable);
1569 if (enable)
1570 {
1571 gfx_spritetile(x+1, (SPRITE_NAMES) glb_actiondefs[action].tile);
1572 }
1573 }
1574 #endif
1575 }
1576
1577 void
1578 introMessage()
1579 {
1580 // Display the intro message.
1581 #ifndef MAPTEST
1582 if (hamfake_isunlocked())
1583 encyc_viewentry("HELP", HELP_POWDER);
1584 else
1585 encyc_viewentry("HELP", HELP_POWDER_LOCKED);
1586 #endif
1587 }
1588
1589 // Paranoid much?
1590 /* Original: "By Jeff Lait " */
1591 static unsigned char glb_author[] = {
1592 'B'+34, 'y'+13, ' '+22, 'J'+94, 'e'+22, 'f'+13, 'f'+52, ' '+54,
1593 'L'+72, 'a'+45, 'i'+64, 't'+90, ' '+32, 0
1594 };
1595
1596 static const unsigned char glb_author_off[] = {
1597 34,13,22,94,22,13,52,54,72,45,64,90,32,
1598 };
1599
1600 static const char *
1601 get_glb_author() {
1602 int i;
1603 if (glb_author[0] != 66)
1604 {
1605 for (i=0; i<13; i++)
1606 glb_author[i] -= glb_author_off[i];
1607 }
1608 return (const char *)glb_author;
1609 }
1610
1611 // This makes me feel like it is 1988.
1612 void processOptions();
1613
1614 bool
1615 intro_screen()
1616 {
1617 MAINMENU_NAMES selection;
1618 static int def = 0;
1619
1620 {
1621 int y;
1622 for (y = 2; y < 19; y++)
1623 gfx_cleartextline(y);
1624 }
1625 glbAutoRunEnabled = false;
1626
1627 #ifdef ANDROID
1628 while (1)
1629 #else
1630 while (!hamfake_forceQuit())
1631 #endif
1632 {
1633 for (int y = 2; y < 19; y++)
1634 gfx_cleartextline(y);
1635
1636 gfx_printtext(10, 1, "POWDER 118");
1637
1638 gfx_printtext(30 - strlen(get_glb_author()), 19,
1639 get_glb_author());
1640
1641 #ifdef iPOWDER
1642 // So what *can* I badge this?
1643 //gfx_printtext(0, 18, "Built with *redacted*");
1644 if (hamfake_isunlocked())
1645 gfx_printtext(0, 18, "Full Version");
1646 #else
1647 #ifdef ANDROID
1648 gfx_printtext(0, 18, "Built with Android 2.2");
1649 #else
1650 #ifdef USING_SDL
1651 gfx_printtext(0, 18, "Built with SDL 1.2");
1652 #else
1653 #ifdef USING_DS
1654 gfx_printtext(0, 18, "Built with DevkitPro 1.4.4 r21");
1655 #else
1656 gfx_printtext(0, 18, "Built with HAM 2.8");
1657 #endif
1658 #endif
1659 #endif
1660 #endif
1661
1662 // Ask the user what they want to do.
1663 {
1664 int aorb;
1665 const char *textmenu[NUM_MAINMENUS];
1666 int i;
1667
1668 const MAINMENU_NAMES menu[] =
1669 {
1670 MAINMENU_NEWGAME,
1671 MAINMENU_TUTORIAL,
1672 MAINMENU_VIEWSCORES,
1673 MAINMENU_OPTIONS,
1674 #if defined(USING_SDL) && !defined(SYS_PSP) && !defined(ANDROID)
1675 #ifndef iPOWDER
1676 MAINMENU_QUIT,
1677 #endif
1678 #ifdef iPOWDER
1679 MAINMENU_STRESSTEST,
1680 MAINMENU_UNLOCK,
1681 #endif
1682 #endif
1683 MAINMENU_NONE
1684 };
1685
1686 const MAINMENU_NAMES menu_load[] =
1687 {
1688 MAINMENU_LOAD,
1689 MAINMENU_NEWGAME,
1690 MAINMENU_TUTORIAL,
1691 MAINMENU_VIEWSCORES,
1692 MAINMENU_OPTIONS,
1693 #if defined(USING_SDL) && !defined(SYS_PSP) && !defined(ANDROID)
1694 #ifndef iPOWDER
1695 MAINMENU_QUIT,
1696 #endif
1697 #ifdef iPOWDER
1698 MAINMENU_STRESSTEST,
1699 MAINMENU_UNLOCK,
1700 #endif
1701 #endif
1702 MAINMENU_NONE
1703 };
1704
1705 const MAINMENU_NAMES menu_scum[] =
1706 {
1707 MAINMENU_NEWGAME,
1708 MAINMENU_TUTORIAL,
1709 MAINMENU_SAVESCUM,
1710 MAINMENU_VIEWSCORES,
1711 MAINMENU_OPTIONS,
1712 #if defined(USING_SDL) && !defined(SYS_PSP) && !defined(ANDROID)
1713 #ifndef iPOWDER
1714 MAINMENU_QUIT,
1715 #endif
1716 #ifdef iPOWDER
1717 MAINMENU_STRESSTEST,
1718 MAINMENU_UNLOCK,
1719 #endif
1720 #endif
1721 MAINMENU_NONE
1722 };
1723
1724 const MAINMENU_NAMES *usemenu;
1725
1726 switch (hiscore_savecount())
1727 {
1728 case 0:
1729 usemenu = menu;
1730 break;
1731 case 1:
1732 usemenu = menu_load;
1733 break;
1734 default:
1735 usemenu = menu_scum;
1736 break;
1737 }
1738
1739 for (i = 0; usemenu[i] != MAINMENU_NONE; i++)
1740 {
1741 #ifdef iPOWDER
1742 // Disable the unlock if unlocked.
1743 if (usemenu[i] == MAINMENU_UNLOCK &&
1744 hamfake_isunlocked())
1745 break;
1746 #endif
1747 textmenu[i] = glb_mainmenudefs[usemenu[i]].name;
1748 }
1749 textmenu[i] = 0;
1750
1751 int rawchoice;
1752
1753 rawchoice = gfx_selectmenu(2, 6, textmenu, aorb, def);
1754
1755 // If we have reset, we reset our selection here as
1756 // we will have just greedily picked the first one.
1757 if (hamfake_forceQuit())
1758 {
1759 hamfake_softReset();
1760 continue;
1761 }
1762
1763 // Ignore bad selection.
1764 if (rawchoice < 0)
1765 continue;
1766
1767 def = rawchoice;
1768
1769 // Ignore b.
1770 if (aorb)
1771 continue;
1772
1773 selection = usemenu[rawchoice];
1774 }
1775
1776 // Selection is greater than 0.
1777
1778 // Special case: Quit
1779 if (selection == MAINMENU_QUIT)
1780 {
1781 hamfake_softReset();
1782 // No return from the valley of the dead!
1783 // Except, of course, the Android.
1784 continue;
1785 }
1786
1787 // Special case: View scores.
1788 if (selection == MAINMENU_VIEWSCORES)
1789 {
1790 hiscore_display(0, false);
1791
1792 {
1793 int y;
1794 for (y = 2; y < 19; y++)
1795 gfx_cleartextline(y);
1796 }
1797
1798 // Continue
1799 continue;
1800 }
1801
1802 // Special case: Options.
1803 if (selection == MAINMENU_OPTIONS)
1804 {
1805 int y;
1806 for (y = 2; y < 19; y++)
1807 gfx_cleartextline(y);
1808 processOptions();
1809 hamfake_clearKeyboardBuffer();
1810 for (y = 2; y < 19; y++)
1811 gfx_cleartextline(y);
1812
1813 // Do not start a new game :>
1814 continue;
1815 }
1816
1817 if (selection == MAINMENU_STRESSTEST)
1818 {
1819 #ifdef iPOWDER
1820 if (!hamfake_isunlocked())
1821 {
1822 // Let them know they have to unlock!
1823 for (int y = 2; y < 19; y++)
1824 gfx_cleartextline(y);
1825
1826 gfx_pager_addtext(
1827 "The stress test will automatically play short "
1828 "games of POWDER, picking random levels and "
1829 "random actions to perform. It is "
1830 "surprisingly soothing to watch the computer "
1831 "lose as often as you do.");
1832 gfx_pager_newline(); gfx_pager_newline();
1833 gfx_pager_addtext(
1834 "Access to the stress test requires your copy "
1835 "of POWDER to be unlocked, however."
1836 );
1837 gfx_pager_display();
1838 continue;
1839 }
1840 #endif
1841 }
1842
1843 if (selection == MAINMENU_UNLOCK)
1844 {
1845 attemptunlock();
1846
1847 // We can't test for success here as we return immediately
1848 // from posting even though the user is still mucking
1849 // about trying to decide if they want to pay the paltry
1850 // unlock fee or not.
1851 // (After XX years I can call it paltry compared to may
1852 // own contribution to the cause)
1853 def = 0;
1854 continue;
1855 }
1856
1857 if (selection == MAINMENU_NEWGAME)
1858 {
1859 // Double check if they have an active game.
1860 // It doesn't always overwrite, but likely will thanks
1861 // to Quit & Save being the default everywhere.
1862 if (hiscore_savecount() == 1)
1863 {
1864 int y;
1865 for (y = 2; y < 19; y++)
1866 gfx_cleartextline(y);
1867 if (!gfx_yesnomenu("Replace your active game?"))
1868 continue;
1869 }
1870 }
1871
1872 // Fall out.
1873 break;
1874 }
1875
1876 {
1877 int y;
1878 for (y = 2; y < 19; y++)
1879 gfx_cleartextline(y);
1880 }
1881
1882 glbTutorial = false;
1883
1884 if (selection == MAINMENU_NEWGAME)
1885 {
1886 // Request a deity to worship. Since this involves extra decisions
1887 // violates the principle of POWDER that one can just start
1888 // playing without thinking. However, many advanced players
1889 // would want to try a specific god so end up start-scumming
1890 // until they got equipment good for that god. I'm strongly
1891 // opposed to start scumming, so thus the decision to allow
1892 // advanced players to select a starting god, and thus
1893 // implied equipment. Grats to Derek S. Ray for writing
1894 // a patch for this and allowing me to wash hands of any
1895 // hypocrisy for going against my official policy.
1896
1897 if (hiscore_advancedmode())
1898 {
1899 GOD_NAMES god;
1900 const char *menu[NUM_GODS+1];
1901
1902 FOREACH_GOD(god)
1903 {
1904 menu[god] = glb_goddefs[god].name;
1905 }
1906 menu[NUM_GODS] = 0;
1907
1908 static int startgod = 0;
1909 int choice;
1910 while (1)
1911 {
1912 if (hamfake_forceQuit())
1913 hamfake_softReset();
1914
1915 int aorb, y;
1916 gfx_printtext(5,3, "Initial God?");
1917 choice = gfx_selectmenu(30 - 20, 4, menu, aorb, startgod);
1918 for (y = 3; y < 19; y++)
1919 gfx_cleartextline(y);
1920 if (choice >= 0)
1921 break;
1922 }
1923 startgod = choice;
1924 god = (GOD_NAMES) choice;
1925
1926 piety_setgod(god);
1927 }
1928 else
1929 {
1930 // Default to no one god, the classic value.
1931 piety_setgod(GOD_AGNOSTIC);
1932 }
1933
1934 // Request the gender. This is before name so those who pick
1935 // randomly can pick an appropriate name.
1936
1937 // Default to a random gender choice on boot up, after that
1938 // keep the users last preference.
1939 if (glbGender == -1)
1940 glbGender = rand_choice(2);
1941
1942 const char *gendermenu[] =
1943 {
1944 "Male",
1945 "Female",
1946 0
1947 };
1948 int aorb;
1949
1950 gfx_printtext(0, 6, "Gender? ");
1951 glbGender = gfx_selectmenu(5, 7, gendermenu, aorb, glbGender);
1952
1953 // Secret code: [Start] to pick random gender.
1954 if (glbGender == -1)
1955 glbGender = rand_choice(2);
1956
1957 {
1958 int y;
1959 for (y = 6; y < 10; y++)
1960 gfx_cleartextline(y);
1961 }
1962
1963 // Request the name.
1964 BUF welcome;
1965 welcome.sprintf("You are a %s %s.",
1966 glbGender ? "female" : "male",
1967 glb_goddefs[piety_chosengod()].classname);
1968 gfx_printtext(0, 3, welcome);
1969
1970 // Now get their name.
1971 gfx_printtext(0, 4, "Name? ");
1972
1973 input_getline(glbAvatarName, 23, 6, 4, 23, 8, glbAvatarName);
1974
1975 // See if it is time to invoke the stress test.
1976 #ifndef iPOWDER
1977 if (rand_hashstring(glbAvatarName) == 0x7a51ad9b)
1978 glbStressTest = true;
1979 else
1980 #endif
1981 glbStressTest = false;
1982
1983 // We don't want to save the game if the user just
1984 // hit home on the name screen!
1985 if (hamfake_forceQuit())
1986 hamfake_softReset();
1987 }
1988
1989 if (selection == MAINMENU_TUTORIAL)
1990 {
1991 // Tutorial has no name
1992 strcpy(glbAvatarName, "George");
1993 glbTutorial = true;
1994 }
1995
1996 if (selection == MAINMENU_STRESSTEST)
1997 {
1998 // Getting here implies we are unlocked.
1999 strcpy(glbAvatarName, "Defiant");
2000 glbStressTest = true;
2001 }
2002
2003 {
2004 int y;
2005 for (y = 0; y < 20; y++)
2006 gfx_cleartextline(y);
2007 }
2008
2009 if (hamfake_forceQuit())
2010 hamfake_softReset();
2011
2012 // Return whether one should load or not.
2013 return (selection == MAINMENU_LOAD || selection == MAINMENU_SAVESCUM);
2014 }
2015
2016 void
2017 loadGame()
2018 {
2019 // For testing, we can verify the new map...
2020 SRAMSTREAM is(false);
2021 int val;
2022
2023 glbAutoRunEnabled = false;
2024
2025 // Verify we can load...
2026 if (!hiscore_savecount())
2027 {
2028 msg_report("No saved game! Aborting load!");
2029 return;
2030 }
2031
2032 // Flag that this is now longer a new game.
2033 hiscore_flagnewgame(false);
2034
2035 // If we are a save scummer...
2036 if (hiscore_savecount() > 1)
2037 {
2038 BUF buf;
2039 buf.sprintf("You have save scummed %d times. ",
2040 hiscore_savecount() - 1);
2041 msg_report(buf);
2042 }
2043
2044 msg_report("Loading...");
2045
2046 ai_reset();
2047 if (glbCurLevel)
2048 glbCurLevel->deleteAllMaps();
2049
2050 artifact_init();
2051
2052 // Load hiscore details.
2053 hiscore_load(is);
2054
2055 // Load our name.
2056 is.readString(glbAvatarName, 30);
2057
2058 is.read(val, 8);
2059 glbWizard = val ? true : false;
2060
2061 is.read(val, 8);
2062 glbTutorial = val ? true : false;
2063
2064 is.read(val, 8);
2065 glbHasAlreadyWon = val ? true : false;
2066
2067 int totaltime;
2068 is.read(totaltime, 32);
2069 // Push our time back in time to account for already
2070 // used time.
2071 glbGameStartFrame = gfx_getframecount() - totaltime;
2072
2073 // Get the number of moves.
2074 speed_load(is);
2075
2076 // Load our name table.
2077 name_load(is);
2078
2079 ITEM::loadGlobal(is);
2080 MOB::loadGlobal(is);
2081 MAP::loadGlobal(is);
2082 piety_load(is);
2083
2084 MAP::changeCurrentLevel(new MAP(0, BRANCH_NONE));
2085 glbCurLevel->loadBranch(is);
2086
2087 // At the very end, we load the avatar number.
2088 // This way we ensure the avatar has been
2089 // loaded before we try and load its refid.
2090 MOB::loadAvatar(is);
2091
2092 // Rebuild FOV & Light tables.
2093 // They were wiped out by the save to
2094 // save space.
2095 gfx_forceLockTile(TILE_AVATAR, 2);
2096 if (MOB::getAvatar())
2097 {
2098 gfx_scrollcenter(MOB::getAvatar()->getX(), MOB::getAvatar()->getY());
2099 MOB::getAvatar()->rebuildAppearance();
2100 glbCurLevel->buildFOV(MOB::getAvatar()->getX(),
2101 MOB::getAvatar()->getY(),
2102 7, 5);
2103 }
2104 glbCurLevel->updateLights();
2105 glbCurLevel->refresh();
2106
2107 writeStatusLine();
2108 msg_report("Done!");
2109
2110 // Successfully loaded. Any reload is thus a save scum.
2111 hiscore_bumpsavecount();
2112
2113 // Save the new hiscore with the new save count!
2114 {
2115 SRAMSTREAM os(false);
2116 hiscore_save(os);
2117
2118 // Trigger non-GBA sessions to complete the save.
2119 hamfake_endWritingSession();
2120 }
2121
2122 // Report whim of xom
2123 if (MOB::getAvatar())
2124 MOB::getAvatar()->pietyAnnounceWhimOfXom();
2125
2126 #ifdef STRESS_TEST
2127 // Finished loading our game, verify our mob structure.
2128 bool valid;
2129
2130 valid = glbCurLevel->verifyMob();
2131 if (!valid)
2132 msg_report("MOBREF Corrupt. ");
2133 else
2134 msg_report("MOBREF Intact. ");
2135 #endif
2136 }
2137
2138 void
2139 rebuild_all()
2140 {
2141 // Ensure there is no avatar currently!
2142 MOB::setAvatar(0);
2143
2144 // Ensure we don't get any embarassing level changes.
2145 MAP::delayedLevelChange(0);
2146
2147 ai_reset();
2148 if (glbCurLevel)
2149 glbCurLevel->deleteAllMaps();
2150
2151 // Everything should be gone now!
2152 // printf("Map: %d, Mob: %d, Item: %d\n", glbMapCount, glbMobCount, glbItemCount);
2153
2154 glbWizard = false;
2155 glbHasAlreadyWon = false;
2156
2157 MOB::init();
2158 ITEM::init();
2159 piety_init();
2160 artifact_init();
2161
2162 MAP *map;
2163
2164 map = new MAP(1, BRANCH_MAIN);
2165
2166 map->build(false);
2167 map->populate();
2168
2169 MAP::changeCurrentLevel(map);
2170 map->setWindDirection(0, 0, 0);
2171
2172 // Dude's position.
2173 int x, y;
2174 MOB *avatar;
2175
2176 bool found = false;
2177
2178 // Find a valid x & y...
2179 // Ideally we want to always start on the up stair case.
2180 found = glbCurLevel->findTile(SQUARE_LADDERUP, x, y);
2181 if (found)
2182 found = glbCurLevel->findCloseTile(x, y, MOVE_WALK);
2183
2184 if (!found)
2185 {
2186 found = glbCurLevel->findTile(SQUARE_DIMDOOR, x, y);
2187 if (found)
2188 found = glbCurLevel->findCloseTile(x, y, MOVE_WALK);
2189
2190 if (!found)
2191 {
2192 found = glbCurLevel->findRandomLoc(x, y, MOVE_WALK, false,
2193 false, false, false, false, true);
2194 if (!found)
2195 {
2196 // This could be hell and nothing but lava.
2197 found = glbCurLevel->findRandomLoc(x, y, MOVE_STD_SWIM, false,
2198 false, false, false, false, true);
2199
2200 if (!found)
2201 {
2202 // Very bad!
2203 UT_ASSERT(!"No valid starting location!");
2204 x = y = 15;
2205 }
2206 }
2207 }
2208 }
2209
2210 avatar = MOB::create(MOB_AVATAR);
2211 avatar->move(x, y);
2212 glbCurLevel->registerMob(avatar);
2213
2214 glbCurLevel->buildFOV(x, y, 7, 5);
2215
2216 MOB::setAvatar(avatar);
2217
2218 // ID all items...
2219 ITEM *id;
2220
2221 for (y = 0; y < MOBINV_HEIGHT; y++)
2222 for (x = 0; x < MOBINV_WIDTH; x++)
2223 {
2224 id = avatar->getItem(x, y);
2225 #if 1
2226 if (id)
2227 {
2228 id->markIdentified(true);
2229 id->markMapped();
2230 }
2231 #endif
2232 }
2233
2234 #ifndef MAPTEST
2235 gfx_scrollcenter(avatar->getX(), avatar->getY());
2236 #endif
2237 glbCurLevel->doHeartbeat();
2238 glbCurLevel->updateLights();
2239 // Only mark mapped with full range if we are not blind...
2240 if (avatar->hasIntrinsic(INTRINSIC_BLIND))
2241 glbCurLevel->markMapped(avatar->getX(), avatar->getY(), 0, 0);
2242 else
2243 glbCurLevel->markMapped(avatar->getX(), avatar->getY(), 7, 5);
2244 glbCurLevel->refresh();
2245
2246 gfx_forceLockTile(TILE_AVATAR, 2);
2247 avatar->rebuildAppearance();
2248
2249 glbGameStartFrame = gfx_getframecount();
2250
2251 speed_reset();
2252
2253 msg_clear();
2254 writeStatusLine();
2255
2256 if (!glbStressTest)
2257 {
2258 hamfake_rebuildScreen();
2259
2260 introMessage();
2261
2262 avatar->reportMessage("Welcome to POWDER!");
2263
2264 // Describe where the avatar is standing, in case there
2265 // is some item.
2266 glbCurLevel->describeSquare(avatar->getX(), avatar->getY(),
2267 avatar->hasIntrinsic(INTRINSIC_BLIND),
2268 false);
2269
2270 avatar->pietyAnnounceWhimOfXom();
2271 if (glbCurLevel->isSquareInLockedRoom(avatar->getX(), avatar->getY()))
2272 {
2273 avatar->reportMessage("There is a secret door in the walls of this room. Keep Searching!");
2274 }
2275 }
2276
2277 // Second write is to restore the action bar.
2278 writeStatusLine();
2279 }
2280
2281 void
2282 processWorld()
2283 {
2284 // We check for avatar map consistency both at the start and the
2285 // end of this method.
2286 MAP::checkForAvatarMapChange();
2287
2288 // Update our smoke.
2289 // This is an annoying time to do this - you get insta-flood and
2290 // insta-smoke move. We want the newly created pit to last one
2291 // turn before it floods.
2292 // I have no idea how to do this. We sort of want the update
2293 // after the user chooses, but before the user acts?
2294 glbCurLevel->moveSmoke();
2295 glbCurLevel->moveFluids();
2296
2297 if (MOB::getAvatar())
2298 {
2299 gfx_scrollcenter(MOB::getAvatar()->getX(),
2300 MOB::getAvatar()->getY());
2301 glbCurLevel->buildFOV(MOB::getAvatar()->getX(),
2302 MOB::getAvatar()->getY(),
2303 7, 5);
2304
2305 }
2306 // This needs to be done so rays hit where you are, not where
2307 // you will be.
2308 // This is too expensive, however. Our solution is to update
2309 // the display whenever the status line scrolls. This way you'll
2310 // always get the partial effects.
2311 // glbCurLevel->refresh();
2312
2313 // Move the NPCs...
2314 glbCurLevel->moveNPCs();
2315
2316 // Give the gods a chance to meddle.
2317 if (speed_isheartbeatphase())
2318 if (MOB::getAvatar())
2319 MOB::getAvatar()->pietyRungods();
2320
2321 speed_ticktime();
2322
2323 writeStatusLine();
2324
2325 // Rebuild our map...
2326 glbCurLevel->doHeartbeat();
2327 glbCurLevel->updateLights();
2328 if (MOB::getAvatar())
2329 {
2330 if (MOB::getAvatar()->hasIntrinsic(INTRINSIC_BLIND))
2331 glbCurLevel->markMapped(MOB::getAvatar()->getX(), MOB::getAvatar()->getY(), 0, 0);
2332 else
2333 glbCurLevel->markMapped(MOB::getAvatar()->getX(), MOB::getAvatar()->getY(), 7, 5);
2334 }
2335 //if (ctrl_rawpressed(BUTTON_R))
2336
2337 MAP::checkForAvatarMapChange();
2338
2339 // If the avatar exists, we only refresh on valid move phases.
2340 // Note this is tested AFTER speed_ticktime! This actually means
2341 // that we refresh just before a turn in which the avatar will
2342 // be able to move. If we used the pretick value, we'd refresh on
2343 // processWorlds that occur after the avatar move, but not update
2344 // for any succeeding non-avatar moves.
2345 if (!MOB::getAvatar() || MOB::getAvatar()->validMovePhase(speed_getphase()))
2346 glbCurLevel->refresh();
2347 }
2348
2349 bool
2350 performAutoPickup()
2351 {
2352 MOB *avatar = MOB::getAvatar();
2353 ITEMSTACK stack;
2354 ITEM *item;
2355 bool submerged, didpickup;
2356 int i, n;
2357
2358 if (!avatar)
2359 return false;
2360
2361 if (avatar->hasIntrinsic(INTRINSIC_INTREE))
2362 return false;
2363
2364 submerged = avatar->hasIntrinsic(INTRINSIC_INPIT) ||
2365 avatar->hasIntrinsic(INTRINSIC_SUBMERGED);
2366 glbCurLevel->getItemStack(stack, avatar->getX(), avatar->getY(),
2367 submerged);
2368
2369 didpickup = false;
2370 n = stack.entries();
2371 for (i = 0; i < n; i++)
2372 {
2373 if (stack(i)->isQuivered())
2374 {
2375 item = stack(i);
2376 didpickup = avatar->actionPickUp(item);
2377 if (!didpickup)
2378 {
2379 // We failed to pickup! Better not try any more items.
2380 return false;
2381 }
2382 }
2383 }
2384
2385 // Return if we found anything to pickup.
2386 return didpickup;
2387 }
2388
2389 bool
2390 performRandomAction(MOB *avatar)
2391 {
2392 ITEM *item;
2393 int ix, iy;
2394 int dx, dy, dz;
2395 bool doitem;
2396
2397 if (!avatar)
2398 return false;
2399
2400 doitem = avatar->findItemSlot(ix, iy);
2401
2402 switch (rand_choice(1 + doitem * 4))
2403 {
2404 case 0: // spell
2405 {
2406 SPELL_NAMES spell;
2407
2408 // Cast a random spell
2409 spell = (SPELL_NAMES) rand_choice(NUM_SPELLS);
2410 if (spell == SPELL_NONE)
2411 break;
2412
2413 // Ensure we have sufficient xp,hp,mp
2414 avatar->incrementMaxMP(glb_spelldefs[spell].mpcost);
2415 avatar->incrementMaxHP(glb_spelldefs[spell].hpcost);
2416 if (avatar->getExp() < glb_spelldefs[spell].xpcost)
2417 {
2418 avatar->receiveExp(glb_spelldefs[spell].xpcost);
2419 }
2420 avatar->setIntrinsic((INTRINSIC_NAMES)glb_spelldefs[spell].intrinsic);
2421
2422 dx = dy = dz = 0;
2423 if (glb_spelldefs[spell].needsdir)
2424 {
2425 dx = rand_range(-1, 1);
2426 dy = rand_range(-1, 1);
2427 if (!dx && !dy)
2428 {
2429 dz = rand_range(-1, 1);
2430 }
2431 }
2432 if (glb_spelldefs[spell].needstarget)
2433 {
2434 // Really should try to target someone...
2435 dx = rand_range(-3, 3);
2436 dy = rand_range(-3, 3);
2437 }
2438
2439 return avatar->actionCast(spell, dx, dy, dz);
2440 }
2441
2442 case 1: // quaff
2443 item = ITEM::createRandomType(ITEMTYPE_POTION);
2444 avatar->acquireItem(item, ix, iy);
2445
2446 return avatar->actionEat(ix, iy);
2447
2448 case 2: // zap
2449 item = ITEM::createRandomType(ITEMTYPE_WAND);
2450 avatar->acquireItem(item, ix, iy);
2451
2452 dx = rand_range(-1, 1);
2453 dy = rand_range(-1, 1);
2454 dz = 0;
2455 if (!dx && !dy)
2456 {
2457 dz = rand_range(-1, 1);
2458 }
2459
2460 return avatar->actionZap(ix, iy, dx, dy, dz);
2461
2462 case 3: // read
2463 item = ITEM::createRandomType(ITEMTYPE_SCROLL);
2464 avatar->acquireItem(item, ix, iy);
2465
2466 return avatar->actionRead(ix, iy);
2467
2468 case 4: // dip
2469 {
2470 ITEM *dipper;
2471
2472 item = ITEM::createRandomType(ITEMTYPE_POTION);
2473 avatar->acquireItem(item, ix, iy);
2474
2475 dipper = avatar->randomItem();
2476 if (dipper)
2477 {
2478 return avatar->actionDip(ix, iy, dipper->getX(), dipper->getY());
2479 }
2480 break;
2481 }
2482
2483 default:
2484 UT_ASSERT(!"Invalid choice for random action");
2485 break;
2486 }
2487
2488 return false;
2489 }
2490
2491 void
2492 performAutoRead()
2493 {
2494 MOB *avatar = MOB::getAvatar();
2495 SIGNPOST_NAMES signpost;
2496
2497 if (!avatar)
2498 return;
2499
2500 // Check to see if we are on a signpost, in which case we want
2501 // to spam the proper message
2502 signpost = glbCurLevel->getSignPost(avatar->getX(), avatar->getY());
2503 if (signpost != SIGNPOST_NONE)
2504 {
2505 #if defined(iPOWDER) || defined(ANDROID)
2506 encyc_viewentry("SIGNPOST_iPOWDER", signpost);
2507 #else
2508 #ifdef HAS_KEYBOARD
2509 encyc_viewentry("SIGNPOST_SDL", signpost);
2510 #else
2511 encyc_viewentry("SIGNPOST_GBA", signpost);
2512 #endif
2513 #endif
2514 }
2515 }
2516
2517 void
2518 performAutoPrompt()
2519 {
2520 MOB *avatar = MOB::getAvatar();
2521 ITEMSTACK stack, eatstack;
2522 bool submerged;
2523 bool hasladder = false, didclimb = false;
2524 bool consumed;
2525 int i, j, n, aorb, select, laddernum, cancelnum;
2526 SQUARE_NAMES square;
2527
2528 if (!avatar)
2529 return;
2530
2531 if (avatar->hasIntrinsic(INTRINSIC_INTREE))
2532 return;
2533
2534 submerged = avatar->hasIntrinsic(INTRINSIC_INPIT) ||
2535 avatar->hasIntrinsic(INTRINSIC_SUBMERGED);
2536 glbCurLevel->getItemStack(stack, avatar->getX(), avatar->getY(),
2537 submerged);
2538
2539 // Determine if we are on a ladder that the user might want to climb.
2540 square = glbCurLevel->getTile(avatar->getX(), avatar->getY());
2541
2542 hasladder = glb_squaredefs[square].promptclimb;
2543
2544 // Whenever we climb we disable the next autoprompt to avoid
2545 // having the user get a double-prompt if they manually climb.
2546 if (glbSuppressAutoClimb)
2547 hasladder = false;
2548 glbSuppressAutoClimb = false;
2549
2550 // Repeatedly prompt the user until they either accept or run out
2551 // of things to do.
2552 while (stack.entries() || hasladder)
2553 {
2554 // Build our accept/reject string.
2555 char **list;
2556
2557 n = stack.entries();
2558 list = new char *[stack.entries()*2 + 2 + hasladder];
2559
2560 stack.reverse();
2561 i = 0;
2562 for (j = 0; j < n; j++)
2563 {
2564 list[i++] = stack(j)->getName(false).strdup();
2565 }
2566 // Add the option of eating any corpses here
2567 eatstack.clear();
2568 for (j = 0; j < n; j++)
2569 {
2570 if (avatar->canDigest(stack(j)))
2571 {
2572 BUF buf;
2573 buf.strcpy("eat ");
2574 buf.strcat(stack(j)->getName());
2575 list[i++] = strdup(buf.buffer());
2576 // Track our edible items
2577 eatstack.append(stack(j));
2578 }
2579 }
2580 laddernum = i;
2581 if (hasladder)
2582 {
2583 if (square == SQUARE_DIMDOOR)
2584 list[i++] = strdup("Enter Portal");
2585 else
2586 list[i++] = strdup("Climb Ladder");
2587 }
2588 cancelnum = i;
2589 list[i++] = strdup("cancel");
2590 list[i] = 0;
2591
2592 // Determine what the user wants.
2593 gfx_printtext(1, 3, "Action?");
2594 select = gfx_selectmenu(3, 4, (const char **) list, aorb);
2595 // Handle cancels.
2596 if (select == cancelnum)
2597 select = -1;
2598
2599 // Empty the screen:
2600 for (i = 3; i < 18; i++)
2601 {
2602 gfx_cleartextline(i);
2603 }
2604
2605 if (select < 0 || aorb)
2606 {
2607 // The user has cancelled, quit!
2608 break;
2609 }
2610
2611 // Check if the use climbed or picked up.
2612 if (hasladder && (select == laddernum))
2613 {
2614 // Climb request
2615 consumed = avatar->actionClimb();
2616 didclimb = true;
2617 }
2618 else if (select >= n)
2619 {
2620 // Eat request...
2621 j = select - n;
2622 if (j >= eatstack.entries())
2623 {
2624 // Cancel request.
2625 break;
2626 }
2627 // Perform the eat
2628 ITEM *newitem = 0;
2629
2630 consumed = avatar->actionPickUp(eatstack(j), &newitem);
2631 if (newitem && newitem->isInventory())
2632 {
2633 consumed |= avatar->actionEat(newitem->getX(), newitem->getY());
2634 didclimb = true;
2635 }
2636 }
2637 else
2638 {
2639 // Pick up request.
2640 consumed = avatar->actionPickUp(stack(select));
2641 }
2642
2643 // Destroy the list.
2644 for (i = 0; list[i]; i++)
2645 {
2646 free(list[i]);
2647 }
2648 delete [] list;
2649
2650 // If, and only if, we consumed, pass time.
2651 // We really need to abstract this!
2652 while (consumed)
2653 {
2654 if (MOB::getAvatar())
2655 MOB::getAvatar()->doHeartbeat();
2656 processWorld();
2657
2658 // The mob may be paralysed or what not. If a save
2659 // request occurs during this time slice, we want to
2660 // ensure we quit *this* turn!
2661 if (hamfake_forceQuit())
2662 {
2663 return;
2664 }
2665
2666 // Run the move prequel code. If this return false,
2667 // it means we should skip the move - ie: process heartbeat
2668 // and world and continue.
2669 // Thus, if it passes, we want to break out and
2670 // go to the normal control selection.
2671 if (!MOB::getAvatar() ||
2672 MOB::getAvatar()->doMovePrequel())
2673 {
2674 break;
2675 }
2676 }
2677
2678 // Recover if we have no avatar anymore.
2679 avatar = MOB::getAvatar();
2680 if (!avatar)
2681 break;
2682
2683 // Update the item stack
2684 stack.clear();
2685
2686 submerged = avatar->hasIntrinsic(INTRINSIC_INPIT) ||
2687 avatar->hasIntrinsic(INTRINSIC_SUBMERGED);
2688 glbCurLevel->getItemStack(stack, avatar->getX(), avatar->getY(),
2689 submerged);
2690
2691 // If we climbed, we don't want to continue.
2692 if (didclimb)
2693 break;
2694 }
2695 }
2696
2697 bool
2698 useInventory(MOB *avatar, bool quickselect)
2699 {
2700 // Something will happen, clear the message queue.
2701 msg_clear();
2702
2703 bool done = false;
2704 bool consumed = true;
2705 bool forceconsume = false;
2706 int aorb = 0;
2707 STYLUSLOCK styluslock(REGION_BOTTOMBUTTON);
2708
2709 // If we are doing quickselect, determine which key will cause
2710 // the quick select.
2711 if (quickselect)
2712 {
2713 if (ctrl_rawpressed(BUTTON_A))
2714 aorb = BUTTON_A;
2715 else if (ctrl_rawpressed(BUTTON_B))
2716 aorb = BUTTON_B;
2717 else if (ctrl_rawpressed(BUTTON_R))
2718 aorb = BUTTON_R;
2719 else if (ctrl_rawpressed(BUTTON_L))
2720 aorb = BUTTON_L;
2721 else if (ctrl_rawpressed(BUTTON_X))
2722 aorb = BUTTON_X;
2723 else if (ctrl_rawpressed(BUTTON_Y))
2724 aorb = BUTTON_Y;
2725 else
2726 {
2727 // They let go of quick select before we got here!
2728 return false;
2729 }
2730 }
2731
2732 gfx_showinventory(avatar);
2733 hamfake_enableexternalactions(true);
2734
2735 {
2736 int cx, cy;
2737 ITEM *item;
2738 gfx_getinvcursor(cx, cy);
2739 item = avatar->getItem(cx, cy);
2740 writeItemActionBar(item);
2741 }
2742 // Wait for a key...
2743 while (!done)
2744 {
2745 if (!gfx_isnewframe())
2746 continue;
2747
2748 if (hamfake_forceQuit())
2749 break;
2750
2751 ITEM *item;
2752 int dx = 0, dy = 0, cx, cy;
2753 bool apress, bpress, dirty;
2754 int sx, sy, ex, ey;
2755 ACTION_NAMES action = ACTION_NONE;
2756
2757 // Select cancels...
2758 if (ctrl_hit(BUTTON_SELECT))
2759 {
2760 consumed = forceconsume;
2761 break;
2762 }
2763
2764 if (styluslock.performInventoryDrag(sx, sy, ex, ey, avatar, dirty))
2765 {
2766 // No mater what, we
2767 // Update our cursor location.
2768 gfx_setinvcursor(ex, ey, false);
2769
2770 if (!sx)
2771 {
2772 // If they drag to another equip slot, no-op
2773 // In any case, our final slot location should be the
2774 // staring slot, not the ending slot, as the ending
2775 // slot isn't where the item ends up.
2776 // (We could try and figure that out, but it is very
2777 // difficult due to stack merges, etc)
2778 gfx_setinvcursor(sx, sy, false);
2779 if (ex)
2780 {
2781 // Item is in equip slots, dequip.
2782 // TODO: move it to the requested location?
2783 gfx_hideinventory();
2784 consumed = avatar->actionDequip((ITEMSLOT_NAMES) sy);
2785 done = true;
2786 break;
2787 }
2788 }
2789 else
2790 {
2791 // Dragging from inventory. If it goes onto an equipment
2792 // slot it is an equip request.
2793 if (!ex)
2794 {
2795 gfx_hideinventory();
2796 consumed = avatar->actionEquip(sx, sy,
2797 (ITEMSLOT_NAMES) ey);
2798 done = true;
2799 break;
2800 }
2801 else
2802 {
2803 // Swap of items, this is safe since we use
2804 // linked lists right now.
2805 ITEM *i1, *i2;
2806 i1 = avatar->getItem(sx, sy);
2807 i2 = avatar->getItem(ex, ey);
2808 if (i1)
2809 i1->setPos(ex, ey);
2810 if (i2)
2811 i2->setPos(sx, sy);
2812 }
2813 }
2814 }
2815
2816 if (dirty)
2817 {
2818 ITEM *item;
2819 gfx_getinvcursor(cx, cy);
2820 // Redisplay the inventory as we'll have erased the void tile.
2821 gfx_hideinventory();
2822 item = avatar->getItem(cx, cy);
2823 writeItemActionBar(item);
2824 gfx_showinventory(avatar);
2825 }
2826
2827 ctrl_getdir(dx, dy);
2828
2829 if (quickselect)
2830 {
2831 apress = false;
2832 if (!ctrl_rawpressed(aorb))
2833 {
2834 apress = true;
2835 }
2836 bpress = false;
2837 }
2838 else
2839 {
2840 apress = ctrl_hit(BUTTON_A);
2841 bpress = ctrl_hit(BUTTON_B);
2842 // Allow more generous cancelling so one can get out
2843 // the way one got in.
2844 bpress |= ctrl_hit(BUTTON_X);
2845 bpress |= ctrl_hit(BUTTON_Y);
2846 }
2847
2848 // Abort with b.
2849 if (bpress)
2850 {
2851 consumed = forceconsume;
2852 break;
2853 }
2854
2855 // Check to see if we are hovering an inventory item
2856 if (stylus_queryinventoryitem(sx, sy))
2857 {
2858 gfx_getinvcursor(cx, cy);
2859 dx = sx - cx;
2860 dy = sy - cy;
2861 }
2862
2863 if (dx || dy)
2864 {
2865 // A direction key is hit, update the cursor pos.
2866 gfx_getinvcursor(cx, cy);
2867 cx += dx;
2868 cy += dy;
2869 gfx_setinvcursor(cx, cy, false);
2870 // No more tests...
2871
2872 // Refetch the cursor to handle wrapping...
2873 gfx_getinvcursor(cx, cy);
2874 item = avatar->getItem(cx, cy);
2875 writeItemActionBar(item);
2876 continue;
2877 }
2878
2879 // Determine if we have key press, so thus a fake action.
2880 if (!apress)
2881 {
2882 int keypress;
2883
2884 keypress = hamfake_getKeyPress(false);
2885
2886 switch (keypress)
2887 {
2888 case 'E':
2889 case 'w':
2890 action = ACTION_EQUIP;
2891 apress = true;
2892 break;
2893
2894 case 'u':
2895 case 'T':
2896 action = ACTION_DEQUIP;
2897 apress = true;
2898 break;
2899
2900 case 'd':
2901 action = ACTION_DROP;
2902 apress = true;
2903 break;
2904
2905 case 'e':
2906 action = ACTION_EAT;
2907 apress = true;
2908 break;
2909
2910 case 'r':
2911 action = ACTION_READ;
2912 apress = true;
2913 break;
2914
2915 case '!':
2916 case 'D':
2917 action = ACTION_DIP;
2918 apress = true;
2919 break;
2920
2921 case 'z':
2922 action = ACTION_ZAP;
2923 apress = true;
2924 break;
2925
2926 case 't':
2927 action = ACTION_THROW;
2928 apress = true;
2929 break;
2930
2931 case 'f':
2932 action = ACTION_FAVORITE;
2933 apress = true;
2934 break;
2935
2936 case 'q':
2937 action = ACTION_QUIVER;
2938 apress = true;
2939 break;
2940
2941 case 'n':
2942 action = ACTION_NAME;
2943 apress = true;
2944 break;
2945
2946 case 's':
2947 action = ACTION_SORT;
2948 apress = true;
2949 break;
2950
2951 case 'S':
2952 action = ACTION_SPLITSTACK;
2953 apress = true;
2954 break;
2955
2956 case 'x':
2957 action = ACTION_EXAMINE;
2958 apress = true;
2959 break;
2960 }
2961 }
2962
2963 if (!apress)
2964 {
2965 int button;
2966
2967 // Check to see if the action menu was used.
2968 if (glbActionBar && styluslock.getbottombutton(button))
2969 {
2970 action = glb_inventoryactionstrip[button];
2971 if (action != ACTION_NONE)
2972 {
2973 apress = true;
2974 }
2975 }
2976 }
2977
2978 if (!apress)
2979 {
2980 int iact, ispell;
2981 hamfake_externalaction(iact, ispell);
2982 if (iact != ACTION_NONE)
2983 {
2984 action = (ACTION_NAMES) iact;
2985 apress = true;
2986 }
2987 }
2988
2989 if (apress)
2990 {
2991 // User has selected something, process the
2992 // request...
2993 gfx_getinvcursor(cx, cy);
2994 item = avatar->getItem(cx, cy);
2995
2996 if (!item)
2997 {
2998 if (quickselect)
2999 {
3000 consumed = false;
3001 break;
3002 }
3003 if (action != ACTION_INVENTORY &&
3004 action != ACTION_SORT)
3005 continue; // Ignore
3006 }
3007
3008 // Build the menu...
3009 ACTION_NAMES menu[NUM_ACTIONS];
3010
3011 buildActionMenu(item, menu);
3012
3013 // If the user selected quickly from keyboard, we don't
3014 // need an action here.
3015 if (action == ACTION_NONE)
3016 action = selectMenu(menu);
3017
3018 // Clear out the selection menu.
3019 gfx_showinventory(avatar);
3020 switch (action)
3021 {
3022 case ACTION_NONE:
3023 // Do nothing...
3024 break;
3025 case ACTION_QUAFF: // Drink is same as eat
3026 case ACTION_EAT:
3027 gfx_hideinventory();
3028 consumed = avatar->actionEat(cx, cy);
3029 done = true;
3030 break;
3031 case ACTION_QUIVER:
3032 gfx_hideinventory();
3033 consumed = avatar->actionQuiver(cx, cy);
3034 // Since quiver is free, we let people chain
3035 // quivers.
3036 done = false;
3037 gfx_showinventory(avatar);
3038 break;
3039 case ACTION_FAVORITE:
3040 gfx_hideinventory();
3041 item->markFavorite(!item->isFavorite());
3042 // Since <pedantic>favorite</pedantic> is free, we let
3043 // people chain <pedantic>favorites</pedantic>.
3044 done = false;
3045 gfx_showinventory(avatar);
3046 break;
3047 case ACTION_READ:
3048 gfx_hideinventory();
3049 consumed = avatar->actionRead(cx, cy);
3050 // If someone aborts a read, they should get
3051 // back to menu.
3052 done = consumed;
3053 // Clear out the description viewer.
3054 if (!done)
3055 gfx_showinventory(avatar);
3056 break;
3057 case ACTION_EXAMINE:
3058 gfx_hideinventory();
3059
3060 item->viewDescription();
3061
3062 consumed = false;
3063 // Do not return to the main screen as they likely
3064 // are examinig a chain of items.
3065 // This is the sort of impressive change I make while
3066 // waiting in the Signet Ana lounge for my first class
3067 // seat.
3068 // done = true;
3069
3070 // Clear out the description viewer.
3071 gfx_showinventory(avatar);
3072 break;
3073 case ACTION_NAME:
3074 {
3075 char buf[100];
3076 int nametype;
3077
3078 gfx_hideinventory();
3079
3080 msg_clear();
3081 gfx_printtext(0, 0, "Specific or type? ");
3082
3083 {
3084 const char *nametype_menu[] =
3085 {
3086 "Specific",
3087 "Type",
3088 0
3089 };
3090 static int namedef = 1;
3091 int aorb;
3092
3093 nametype = gfx_selectmenu(30-10, 3, nametype_menu, aorb, namedef);
3094 // B button cancels.
3095 if (aorb)
3096 nametype = -1;
3097 if (nametype >= 0)
3098 namedef = nametype;
3099 }
3100
3101 {
3102 int y;
3103 for (y = 0; y < 18; y++)
3104 gfx_cleartextline(y);
3105 }
3106 if (nametype < 0)
3107 {
3108 consumed = false;
3109 done = true;
3110 break;
3111 }
3112 // Prohibit renaming artifacts.
3113 if (item->isArtifact() && !nametype)
3114 {
3115 msg_report("You cannot rename an artifact! ");
3116 consumed = false;
3117 done = true;
3118 break;
3119 }
3120
3121 gfx_printtext(0, 0, "New Name? ");
3122 input_getline(buf, 19, 10, 0, 23, 8);
3123
3124 {
3125 int y;
3126 for (y = 0; y < 18; y++)
3127 gfx_cleartextline(y);
3128 }
3129
3130 if (nametype)
3131 item->renameType(buf);
3132 else
3133 item->rename(buf);
3134
3135 consumed = false;
3136 done = true;
3137 break;
3138 }
3139 case ACTION_THROW:
3140 {
3141 // Find the zap direction...
3142 BUF buf;
3143 int tx, ty;
3144 int dx, dy, dz;
3145
3146 gfx_hideinventory();
3147 done = true;
3148 avatar->formatAndReport("Throw %I1U where?", item);
3149
3150 tx = avatar->getX();
3151 ty = avatar->getY();
3152 if (gfx_selectdirection(tx, ty, dx, dy, dz))
3153 {
3154 msg_clear();
3155
3156 consumed = avatar->actionThrow(
3157 cx, cy, dx, dy, dz);
3158 }
3159 else
3160 {
3161 // Cancelled...
3162 msg_clear();
3163 consumed = false;
3164 }
3165 break;
3166 }
3167 case ACTION_DIP:
3168 {
3169 // Request a second item, dip this into
3170 // that item. Check if we are on a well
3171 // in which case we want to dip into
3172 // the well.
3173 int selectx, selecty;
3174
3175 selectx = cx;
3176 selecty = cy;
3177
3178 avatar->formatAndReport("Dip %IU in what?", item);
3179
3180 if (gfx_selectinventory(selectx, selecty))
3181 {
3182 // Attempt to dip the two
3183 // selected items together...
3184 gfx_getinvcursor(selectx, selecty);
3185 // Restore cursor to start item.
3186 gfx_setinvcursor(cx, cy, false);
3187 gfx_hideinventory();
3188
3189 consumed = avatar->actionDip(
3190 selectx, selecty,
3191 cx, cy);
3192
3193 done = true;
3194 }
3195 else
3196 {
3197 // Wipe our dip prompt.
3198 gfx_setinvcursor(cx, cy, false);
3199 msg_clear();
3200 }
3201
3202 break;
3203 }
3204 case ACTION_DROP:
3205 gfx_hideinventory();
3206 consumed = avatar->actionDrop(cx, cy);
3207
3208 // We allow an arbitrary number of drops at once.
3209 done = false;
3210 // We want to cost a turn.
3211 forceconsume = true;
3212 gfx_showinventory(avatar);
3213 break;
3214 case ACTION_EQUIP:
3215 {
3216 bool madechoice = false;
3217 int slotx, sloty;
3218 BUF buf;
3219
3220 slotx = 0;
3221 sloty = ITEMSLOT_RHAND;
3222
3223 avatar->formatAndReport("Equip %I1U where?", item);
3224
3225 // We want to make it easy for the user, so default
3226 // to the most logical slot.
3227 if (item->isHelmet())
3228 sloty = ITEMSLOT_HEAD;
3229 if (item->isBoots())
3230 sloty = ITEMSLOT_FEET;
3231 if (item->isShield())
3232 sloty = ITEMSLOT_LHAND;
3233 if (item->isJacket())
3234 sloty = ITEMSLOT_BODY;
3235 if (item->isAmulet())
3236 sloty = ITEMSLOT_AMULET;
3237 if (item->isRing())
3238 {
3239 sloty = ITEMSLOT_RRING;
3240 if (avatar->getEquippedItem(ITEMSLOT_RRING)
3241 && !avatar->getEquippedItem(ITEMSLOT_LRING))
3242 sloty = ITEMSLOT_LRING;
3243 }
3244 // If the item is a bow, default to the left hand.
3245 if (item->getDefinition() == ITEM_BOW)
3246 {
3247 sloty = ITEMSLOT_LHAND;
3248 }
3249
3250 // Item is in main inventory, equip.
3251 // Get the user to choose a inventory slot...
3252 gfx_setinvcursor(slotx, sloty, true);
3253
3254 STYLUSLOCK styluslock(REGION_SLOTS);
3255 while (1)
3256 {
3257 if (!gfx_isnewframe())
3258 continue;
3259 if (hamfake_forceQuit())
3260 break;
3261
3262 ctrl_getdir(dx, dy);
3263 apress = ctrl_hit(BUTTON_A);
3264 bpress = ctrl_hit(BUTTON_B);
3265
3266 if (bpress)
3267 {
3268 // cancel.
3269 madechoice = false;
3270 break;
3271 }
3272 if (apress)
3273 {
3274 // Made a choice.
3275 madechoice = true;
3276 break;
3277 }
3278
3279 int sslot;
3280 if (styluslock.selectinventoryslot(sslot))
3281 {
3282 if (sslot == -1)
3283 madechoice = false;
3284 else
3285 {
3286 madechoice = true;
3287 sloty = sslot;
3288 gfx_setinvcursor(slotx, sloty, true);
3289 }
3290 break;
3291 }
3292
3293 // Consume invalid keys.
3294 hamfake_getKeyPress(false);
3295
3296 // Move in dy direction...
3297 gfx_getinvcursor(slotx, sloty);
3298 sloty += dy;
3299 gfx_setinvcursor(slotx, sloty, true);
3300 }
3301
3302 // If aborted, reset cursor to cx/cy
3303 if (!madechoice)
3304 {
3305 gfx_setinvcursor(cx, cy, false);
3306 msg_clear();
3307 }
3308 else
3309 {
3310 // Try to do the action...
3311 gfx_getinvcursor(slotx, sloty);
3312 // Even if we accepted, leave the cursor
3313 // where it started as this seems to be
3314 // what I expect. (And I am all who
3315 // counts here, right???)
3316 gfx_setinvcursor(cx, cy, false);
3317 gfx_hideinventory();
3318
3319 msg_clear();
3320
3321 consumed = avatar->actionEquip(cx, cy,
3322 (ITEMSLOT_NAMES) sloty);
3323 done = true;
3324 }
3325 break;
3326 }
3327 case ACTION_DEQUIP:
3328 {
3329 if (cx)
3330 {
3331 avatar->formatAndReport("%U <be> not wearing %IU.", item);
3332 consumed = false;
3333 }
3334 else
3335 {
3336 // Item is in equip slots, dequip.
3337 gfx_hideinventory();
3338 consumed = avatar->actionDequip((ITEMSLOT_NAMES) cy);
3339 done = true;
3340 }
3341 break;
3342 }
3343 case ACTION_ZAP:
3344 {
3345 // Find the zap direction...
3346 BUF buf;
3347 int tx, ty;
3348 int dx, dy, dz;
3349
3350 gfx_hideinventory();
3351 done = true;
3352 avatar->formatAndReport("Zap %IU where?", item);
3353
3354 tx = avatar->getX();
3355 ty = avatar->getY();
3356 if (gfx_selectdirection(tx, ty, dx, dy, dz))
3357 {
3358 msg_clear();
3359
3360 consumed = avatar->actionZap(
3361 cx, cy, dx, dy, dz);
3362 }
3363 else
3364 {
3365 // Cancelled...
3366 msg_clear();
3367 consumed = false;
3368 }
3369 break;
3370 }
3371 case ACTION_SORT:
3372 {
3373 // Re arrange backpack
3374 gfx_hideinventory();
3375 consumed = avatar->actionSort();
3376 done = false;
3377 // Clear out the description viewer.
3378 gfx_showinventory(avatar);
3379 break;
3380 }
3381 case ACTION_SPLITSTACK:
3382 {
3383 // This doesn't take a turn or cost time.
3384 done = false;
3385 consumed = false;
3386
3387 // Take one item from this stack and add back
3388 // to our inventory.
3389 if (item->getStackCount() <= 1)
3390 {
3391 // Illegal split.
3392 avatar->formatAndReport("%U can't split %IU in two.", item);
3393 break;
3394 }
3395 ITEM *split;
3396 int sx, sy;
3397
3398 if (avatar->findItemSlot(sx, sy))
3399 {
3400 // Have to update the inventory so they
3401 // can see that something happened.
3402 gfx_hideinventory();
3403 split = item->splitStack();
3404 avatar->formatAndReport("%U <separate> %IU.", split);
3405 avatar->acquireItem(split, sx, sy);
3406 gfx_showinventory(avatar);
3407 }
3408 else
3409 {
3410 avatar->formatAndReport("%U <lack> space to split the stack.");
3411 break;
3412 }
3413 break;
3414 }
3415 case ACTION_INVENTORY:
3416 // We just want to quit back to the game.
3417 gfx_hideinventory();
3418
3419 // We don't want to waste a turn doing this!
3420 consumed = forceconsume;
3421 done = true;
3422 break;
3423 default:
3424 {
3425 UT_ASSERT(!"Unhandled ACTION!");
3426 break;
3427 }
3428 }
3429
3430 // If the avatar is not done yet, we want to rebuild the lost
3431 // action bar.
3432 if (!done)
3433 {
3434 gfx_getinvcursor(cx, cy);
3435 item = avatar->getItem(cx, cy);
3436 writeItemActionBar(item);
3437 }
3438
3439 if (quickselect)
3440 {
3441 // Even if the user didn't make a valid choice,
3442 // we still exit the inventory screen.
3443 consumed = false;
3444 break;
3445 }
3446 }
3447 }
3448
3449 if (!done)
3450 gfx_hideinventory();
3451
3452 return consumed;
3453 }
3454
3455 void
3456 processHelp()
3457 {
3458 while (1)
3459 {
3460 HELP_NAMES helplist[NUM_HELPS];
3461 const char *helptype[NUM_HELPS];
3462
3463 int helpidx = 0;
3464
3465 if (hamfake_isunlocked())
3466 helplist[helpidx++] = HELP_POWDER;
3467 else
3468 helplist[helpidx++] = HELP_POWDER_LOCKED;
3469
3470 #ifdef HAS_KEYBOARD
3471 helplist[helpidx++] = HELP_KEYBOARD;
3472 #endif
3473 #ifdef HAS_STYLUS
3474 #if defined(iPOWDER) || defined(ANDROID)
3475 helplist[helpidx++] = HELP_TOUCH;
3476 #else
3477 helplist[helpidx++] = HELP_STYLUS;
3478 #endif
3479 #endif
3480 #ifndef HAS_KEYBOARD
3481 #if !defined(iPOWDER) && !defined(ANDROID)
3482 helplist[helpidx++] = HELP_GAMEBOY;
3483 #endif
3484 #endif
3485 helplist[helpidx++] = HELP_STATUSLINE;
3486 helplist[helpidx++] = HELP_SPELL;
3487 helplist[helpidx++] = HELP_SKILL;
3488 helplist[helpidx++] = HELP_GOD;
3489
3490 for (int i = 0; i < helpidx; i++)
3491 {
3492 helptype[i] = glb_helpdefs[helplist[i]].menu;
3493 }
3494 helptype[helpidx] = 0;
3495
3496 int selection, aorb;
3497 static int helpdef = 0;
3498
3499 selection = gfx_selectmenu(30 - 15, 3, helptype, aorb, helpdef);
3500 if (aorb || selection < 0)
3501 {
3502 // TODO: Return to start menu rather than cancelling all!
3503 return;
3504 }
3505 helpdef = selection;
3506
3507 selection = helplist[selection];
3508
3509 switch (selection)
3510 {
3511 case HELP_GOD:
3512 {
3513 // Secondary menu.
3514 const char *menu[NUM_GODS+1];
3515 int god;
3516
3517 for (god = 0; god < NUM_GODS; god++)
3518 {
3519 menu[god] = glb_goddefs[god].name;
3520 }
3521 menu[god] = 0;
3522
3523 static int goddef = 0;
3524
3525 while (1)
3526 {
3527 god = gfx_selectmenu(30 - 15, 3, menu, aorb, goddef);
3528 if (aorb || god < 0)
3529 {
3530 break;
3531 }
3532 goddef = god;
3533
3534 encyc_viewentry("GOD", god);
3535 }
3536 break;
3537 }
3538 case HELP_SPELL:
3539 {
3540 // Secondary menu.
3541 const char *menu[NUM_SPELLS+1];
3542 int spell;
3543
3544 // Skip first as it is no-spell.
3545 for (spell = 1; spell < NUM_SPELLS; spell++)
3546 {
3547 menu[spell-1] = glb_spelldefs[spell].name;
3548 }
3549 menu[spell-1] = 0;
3550
3551 static int spelldef = 0;
3552
3553 while (1)
3554 {
3555 spell = gfx_selectmenu(30 - 15, 3, menu, aorb, spelldef);
3556 if (aorb || spell < 0)
3557 {
3558 break;
3559 }
3560 spelldef = spell;
3561
3562 // Increment by one for no-spell
3563 encyc_viewSpellDescription((SPELL_NAMES)(spell+1));
3564 }
3565 break;
3566 }
3567
3568 case HELP_SKILL:
3569 {
3570 // Secondary menu.
3571 const char *menu[NUM_SKILLS+1];
3572 int skill;
3573
3574 // Skip first as it is no-skill.
3575 for (skill = 1; skill < NUM_SKILLS; skill++)
3576 {
3577 menu[skill-1] = glb_skilldefs[skill].name;
3578 }
3579 menu[skill-1] = 0;
3580
3581 static int skilldef = 0;
3582
3583 while (1)
3584 {
3585 skill = gfx_selectmenu(30 - 15, 3, menu, aorb, skilldef);
3586 if (aorb || skill < 0)
3587 {
3588 break;
3589 }
3590 skilldef = skill;
3591
3592 // Increment by one for no-skill
3593 encyc_viewSkillDescription((SKILL_NAMES)(skill+1));
3594 }
3595 break;
3596 }
3597
3598 default:
3599 encyc_viewentry("HELP", selection);
3600 break;
3601 }
3602 }
3603 }
3604
3605 void
3606 killAvatar()
3607 {
3608 MOB *avatar;
3609 avatar = MOB::getAvatar();
3610 MOB::setAvatar(0);
3611 // Kill the avatar.
3612 if (avatar)
3613 {
3614 glbCurLevel->unregisterMob(avatar);
3615 delete avatar;
3616 }
3617 }
3618
3619 void
3620 saveGame(bool withnoquit)
3621 {
3622 SRAMSTREAM os(withnoquit);
3623
3624 if (!withnoquit)
3625 msg_report("Saving...");
3626
3627 // If the save count is 0,
3628 // - we are writing a new game for the first time.
3629 // If the save count is 1,
3630 // - we are overwriting a valid game.
3631 // If the save count is 2,
3632 // - We are resaving a game we loaded.
3633 // Else, we have cheated!
3634 // If we have flagged as a new game, we know we should reset.
3635 if (hiscore_isnewgame() || hiscore_savecount() <= 2)
3636 hiscore_setsavecount(1);
3637 else
3638 hiscore_setsavecount(hiscore_savecount()-1);
3639
3640 // In any case, it is no longer a new game.
3641 hiscore_flagnewgame(false);
3642
3643 hiscore_save(os);
3644
3645 // Save our name.
3646 os.writeString(glbAvatarName, 30);
3647
3648 os.write(glbWizard, 8);
3649 os.write(glbTutorial, 8);
3650 os.write(glbHasAlreadyWon, 8);
3651
3652 // Save our current total time so far.
3653 int totaltime;
3654
3655 totaltime = gfx_getframecount() - glbGameStartFrame;
3656
3657 os.write(totaltime, 32);
3658
3659 // Save total moves so far.
3660 speed_save(os);
3661
3662 // Save our speed table.
3663 name_save(os);
3664
3665 ITEM::saveGlobal(os);
3666 MOB::saveGlobal(os);
3667 MAP::saveGlobal(os);
3668 piety_save(os);
3669
3670 glbCurLevel->saveBranch(os);
3671
3672 // Now save our avatar...
3673 MOB::saveAvatar(os);
3674
3675 os.flush();
3676
3677 // Trigger non-GBA sessions to complete the save.
3678 hamfake_endWritingSession();
3679
3680 // The saving process may generate temp levels
3681 // that will refresh...
3682 glbCurLevel->refresh();
3683 if (!withnoquit)
3684 msg_report("Done!");
3685
3686 #ifdef STRESS_TEST
3687 // Verify the mobref structure is good.
3688 bool valid;
3689
3690 valid = glbCurLevel->verifyMob();
3691 if (!valid)
3692 msg_report("MOBREF Corrupt. ");
3693 else
3694 msg_report("MOBREF Intact. ");
3695 #endif
3696
3697 // Having saved, we now axe the avatar, forcing a reload.
3698 if (!withnoquit)
3699 killAvatar();
3700
3701 hamfake_clearKeyboardBuffer();
3702 }
3703
3704 void
3705 processOptions()
3706 {
3707 int selection;
3708 OPTION_NAMES option;
3709 int aorb;
3710 static int def = 0, defnoavatar = 0;
3711
3712 const char *menu[] =
3713 {
3714 "Help",
3715 "Save&Quit",
3716 #ifdef USE_VIRTUAL_SCREEN
3717 "Buttons",
3718 #endif
3719 "Tiles",
3720 "Opacity",
3721 "Fonts",
3722 "ColorText",
3723 "AutoPrompt",
3724 "SafeWalk",
3725 "ActionBar",
3726 "CharDump",
3727 "KnownItems",
3728 "DebugInfo",
3729 "Collapse",
3730 "Quit",
3731 "Defaults",
3732 0, // Full Screen.
3733 0
3734 };
3735
3736 OPTION_NAMES menuopt[] =
3737 {
3738 OPTION_HELP,
3739 OPTION_SAVE,
3740 #ifdef USE_VIRTUAL_SCREEN
3741 OPTION_BUTTONS,
3742 #endif
3743 OPTION_TILES,
3744 OPTION_OPACITY,
3745 OPTION_FONTS,
3746 OPTION_COLOUREDFONTS,
3747 OPTION_AUTOPROMPT,
3748 OPTION_SAFEWALK,
3749 OPTION_ACTIONBAR,
3750 OPTION_CHARDUMP,
3751 OPTION_KNOWNITEMS,
3752 OPTION_DEBUGINFO,
3753 OPTION_COLLAPSE,
3754 OPTION_QUIT,
3755 OPTION_DEFAULTS,
3756 OPTION_FULLSCREEN
3757 };
3758
3759 const char *menunoavatar[] =
3760 {
3761 "Help",
3762 "Tiles",
3763 "Opacity",
3764 "Fonts",
3765 "ColorText",
3766 "AutoPrompt",
3767 "SafeWalk",
3768 "ActionBar",
3769 "Defaults",
3770 #ifdef iPOWDER
3771 "Unlocking",
3772 #endif
3773 0, // Full Screen.
3774 0
3775 };
3776
3777 OPTION_NAMES menunoavataropt[] =
3778 {
3779 OPTION_HELP,
3780 OPTION_TILES,
3781 OPTION_OPACITY,
3782 OPTION_FONTS,
3783 OPTION_COLOUREDFONTS,
3784 OPTION_AUTOPROMPT,
3785 OPTION_SAFEWALK,
3786 OPTION_ACTIONBAR,
3787 OPTION_DEFAULTS,
3788 #ifdef iPOWDER
3789 OPTION_UNLOCKING,
3790 #endif
3791 OPTION_FULLSCREEN
3792 };
3793
3794 #if defined(USING_SDL) && !defined(iPOWDER) && !defined(ANDROID)
3795 int i;
3796
3797 // Toggle the Full Screen option depending on our actual full
3798 // screen settings.
3799 for (i = 0; menu[i]; i++);
3800 if (hamfake_isFullScreen())
3801 menu[i] = "Windowed";
3802 else
3803 menu[i] = "FullScreen";
3804 for (i = 0; menunoavatar[i]; i++);
3805 if (hamfake_isFullScreen())
3806 menunoavatar[i] = "Windowed";
3807 else
3808 menunoavatar[i] = "FullScreen";
3809 #endif
3810
3811 option = OPTION_NONE;
3812 if (MOB::getAvatar())
3813 {
3814 selection = gfx_selectmenu(30-11, 3, menu, aorb, def);
3815 if (selection >= 0 && !aorb)
3816 {
3817 def = selection;
3818 option = menuopt[selection];
3819 }
3820 }
3821 else
3822 {
3823 selection = gfx_selectmenu(30-11, 3, menunoavatar, aorb, defnoavatar);
3824 if (selection >= 0 && !aorb)
3825 {
3826 defnoavatar = selection;
3827 option = menunoavataropt[selection];
3828 }
3829 }
3830
3831 if (option == OPTION_NONE)
3832 return;
3833
3834 // Cancel on b press
3835 if (aorb)
3836 return;
3837
3838 if (option == OPTION_SAVE)
3839 {
3840 saveGame(false);
3841 glbFinishedASave = true;
3842 return;
3843 }
3844
3845 if (option == OPTION_QUIT)
3846 {
3847 if (gfx_yesnomenu("Really quit?", false))
3848 {
3849 // Suicide the avatar.
3850 MOB *avatar;
3851 avatar = MOB::getAvatar();
3852 MOB::setAvatar(0);
3853 // Kill the avatar.
3854 if (avatar)
3855 {
3856 glbCurLevel->unregisterMob(avatar);
3857 delete avatar;
3858 }
3859 glbFinishedASave = true;
3860 return;
3861 }
3862 }
3863
3864 if (option == OPTION_LOAD)
3865 {
3866 // Check the current time. If we are too late
3867 // we don't want to axe the current game!
3868 if (speed_gettime() > 100)
3869 {
3870 BUF buf;
3871
3872 buf.sprintf("%s cries: I still have a chance! "
3873 "Let me continue! ",
3874 glbAvatarName[0] ? glbAvatarName
3875 : "Lazy Player");
3876 msg_report(buf);
3877 }
3878 else
3879 {
3880 loadGame();
3881 }
3882
3883 return;
3884 }
3885
3886 if (option == OPTION_CHARDUMP)
3887 {
3888 if (MOB::getAvatar())
3889 MOB::getAvatar()->characterDump(false, false, true);
3890
3891 return;
3892 }
3893
3894 if (option == OPTION_KNOWNITEMS)
3895 {
3896 ITEM::viewDiscoveries();
3897
3898 return;
3899 }
3900
3901 if (option == OPTION_DEBUGINFO)
3902 {
3903 BUF buf;
3904 bool valid;
3905
3906 // Perform mob verification.
3907 if (glbCurLevel)
3908 {
3909 valid = glbCurLevel->verifyMob();
3910 if (valid)
3911 gfx_pager_addtext("MOBREF chain valid. ");
3912 else
3913 gfx_pager_addtext("MOBREF chain broken! ");
3914 gfx_pager_newline();
3915 }
3916
3917 buf.sprintf("%d mobs, %d tables, %d buffers",
3918 mobref_getnumrefs(),
3919 mobref_getnumtables(),
3920 buf_numbufs());
3921 gfx_pager_addtext(buf); gfx_pager_newline();
3922
3923 buf.sprintf("%d items, %d mobs, %d maps.",
3924 glbItemCount, glbMobCount, glbMapCount);
3925 gfx_pager_addtext(buf); gfx_pager_newline();
3926
3927 gfx_pager_addtext("Meaningless Stats:");
3928 gfx_pager_newline();
3929
3930 buf.sprintf("%d Mob Types", NUM_MOBS);
3931 gfx_pager_addtext(buf); gfx_pager_newline();
3932 buf.sprintf("%d Item Types", NUM_ITEMS);
3933 gfx_pager_addtext(buf); gfx_pager_newline();
3934 buf.sprintf("%d Wand Types", NUM_WANDS);
3935 gfx_pager_addtext(buf); gfx_pager_newline();
3936 buf.sprintf("%d Book Types", NUM_SPELLBOOKS);
3937 gfx_pager_addtext(buf); gfx_pager_newline();
3938 buf.sprintf("%d Amulet Types", NUM_AMULETS);
3939 gfx_pager_addtext(buf); gfx_pager_newline();
3940 buf.sprintf("%d Helms Types", NUM_HELMS);
3941 gfx_pager_addtext(buf); gfx_pager_newline();
3942 buf.sprintf("%d Ring Types", NUM_RINGS);
3943 gfx_pager_addtext(buf); gfx_pager_newline();
3944 buf.sprintf("%d Scroll Types", NUM_SCROLLS);
3945 gfx_pager_addtext(buf); gfx_pager_newline();
3946 buf.sprintf("%d Potion Types", NUM_POTIONS);
3947 gfx_pager_addtext(buf); gfx_pager_newline();
3948 buf.sprintf("%d Attack Types", NUM_ATTACKS);
3949 gfx_pager_addtext(buf); gfx_pager_newline();
3950 buf.sprintf("%d Element Types", NUM_ELEMENTS);
3951 gfx_pager_addtext(buf); gfx_pager_newline();
3952 buf.sprintf("%d Spells", NUM_SPELLS);
3953 gfx_pager_addtext(buf); gfx_pager_newline();
3954 buf.sprintf("%d Skills", NUM_SKILLS);
3955 gfx_pager_addtext(buf); gfx_pager_newline();
3956 buf.sprintf("%d Intrinsics", NUM_INTRINSICS);
3957 gfx_pager_addtext(buf); gfx_pager_newline();
3958 buf.sprintf("%d Actions", NUM_ACTIONS);
3959 gfx_pager_addtext(buf); gfx_pager_newline();
3960 buf.sprintf("%d Sprites", NUM_SPRITES);
3961 gfx_pager_addtext(buf); gfx_pager_newline();
3962 buf.sprintf("%d Tiles", NUM_TILES);
3963 gfx_pager_addtext(buf); gfx_pager_newline();
3964 buf.sprintf("%d Paperdoll Tiles", NUM_MINIS);
3965 gfx_pager_addtext(buf); gfx_pager_newline();
3966
3967 gfx_pager_display();
3968 return;
3969 }
3970
3971 if (option == OPTION_COLLAPSE)
3972 {
3973 const char *actionbar[] =
3974 {
3975 "Collapse the Dungeon",
3976 "Cancel",
3977 0
3978 };
3979
3980 // Always default to 1.
3981 selection = gfx_selectmenu(30 - 20, 3, actionbar, aorb, 1);
3982 if (aorb || selection < 0)
3983 {
3984 // TODO: Return to start menu rather than cancelling all!
3985 return;
3986 }
3987
3988 if (!selection)
3989 {
3990 MOB::getAvatar()->formatAndReport("%U <hear> a distant rumble. The world feels more empty.");
3991 glbCurLevel->collapseDungeon();
3992 }
3993 return;
3994 }
3995
3996 if (option == OPTION_FULLSCREEN)
3997 {
3998 hamfake_setFullScreen(!hamfake_isFullScreen());
3999 return;
4000 }
4001
4002 if (option == OPTION_DEFAULTS)
4003 {
4004 const char *actionbar[] =
4005 {
4006 "Revert to Defaults",
4007 "Cancel",
4008 0
4009 };
4010
4011 selection = gfx_selectmenu(30 - 20, 3, actionbar, aorb);
4012 if (aorb || selection < 0)
4013 {
4014 // TODO: Return to start menu rather than cancelling all!
4015 return;
4016 }
4017
4018 if (!selection)
4019 {
4020 resetDefaultActionStrip();
4021 writeGlobalActionBar(false);
4022 hamfake_buttonreq(4);
4023 }
4024 return;
4025 }
4026
4027 if (option == OPTION_BUTTONS)
4028 {
4029 const char *buttons[] =
4030 {
4031 "Move Buttons",
4032 "Create Action",
4033 "Create Spell",
4034 "Delete Button",
4035 "Screen Tap",
4036 "Cancel",
4037 0
4038 };
4039 MOB *avatar;
4040 avatar = MOB::getAvatar();
4041
4042 static int def = 0;
4043
4044 selection = gfx_selectmenu(30 - 20, 3, buttons, aorb, def);
4045 if (aorb || selection < 0 || selection == 5)
4046 {
4047 // TODO: Return to start menu rather than cancelling all!
4048 return;
4049 }
4050 // Remember last choice.
4051 // Damn newb mistake, but just made it.
4052 def = selection;
4053
4054 if (selection == 0) // move
4055 {
4056 hamfake_buttonreq(0);
4057 }
4058 else if (selection == 1) // action
4059 {
4060 const char *m[NUM_ACTIONS + 3];
4061 ACTION_NAMES a[NUM_ACTIONS+3];
4062 int i, j, choice, aorb;
4063 static int lastchoice = 0;
4064
4065 for (i = 0; menu_generic[i] != ACTION_NONE; i++)
4066 {
4067 m[i] = glb_actiondefs[menu_generic[i]].name;
4068 a[i] = menu_generic[i];
4069 }
4070 a[i] = ACTION_NONE;
4071 m[i] = "cancel";
4072 m[i+1] = 0;
4073
4074 for (int y = 3; y <= 19; y++)
4075 gfx_cleartextline(y);
4076 gfx_printtext(0, 3, "Which action?");
4077 choice = gfx_selectmenu(5, 4, m, aorb, lastchoice);
4078 if (aorb || (choice != -1 && a[choice] == ACTION_NONE))
4079 choice = -1;
4080
4081 if (choice != -1)
4082 {
4083 // Create the button
4084 hamfake_buttonreq(1, a[choice]);
4085 }
4086 }
4087 else if (selection == 2) // spell
4088 {
4089 const char *m[NUM_SPELLS + 3];
4090 SPELL_NAMES s[NUM_SPELLS + 3];
4091 SPELL_NAMES spell;
4092 int i, j, choice, aorb;
4093 static int lastchoice = 0;
4094
4095 // Select a magic spell
4096 j = 0;
4097 FOREACH_SPELL(spell)
4098 {
4099 if (avatar->hasSpell(spell))
4100 {
4101 s[j] = spell;
4102 m[j] = glb_spelldefs[spell].name;
4103 j++;
4104 }
4105 }
4106
4107 // Include cancel option to cast nothing.
4108 s[j] = SPELL_NONE;
4109 m[j] = "cancel";
4110 j++;
4111 m[j] = 0;
4112
4113 if (j <= 1)
4114 {
4115 avatar->reportMessage("You know no spells!");
4116 return;
4117 }
4118 for (int y = 3; y <= 19; y++)
4119 gfx_cleartextline(y);
4120 gfx_printtext(0, 3, "Which spell?");
4121 choice = gfx_selectmenu(5, 4, m, aorb, lastchoice);
4122 if (aorb || (choice != -1 && s[choice] == SPELL_NONE))
4123 choice = -1;
4124
4125 if (choice != -1)
4126 {
4127 // Create the button
4128 hamfake_buttonreq(2, s[choice]);
4129 }
4130 }
4131 else if (selection == 3) // delete
4132 {
4133 hamfake_buttonreq(3);
4134 }
4135 else if (selection == 4) // Toggle screen tapping
4136 {
4137 const char *screentap[] =
4138 {
4139 "Disable Screen Tap",
4140 "Enable Screen Tap",
4141 0
4142 };
4143
4144 for (int y = 3; y <= 19; y++)
4145 gfx_cleartextline(y);
4146 int screentapdef;
4147
4148 screentapdef = glbScreenTap;
4149 selection = gfx_selectmenu(30 - 20, 3, screentap, aorb, screentapdef);
4150 if (aorb || selection < 0)
4151 {
4152 // TODO: Return to start menu rather than cancelling all!
4153 return;
4154 }
4155
4156 glbScreenTap = selection ? true : false;
4157 return;
4158 }
4159 return;
4160 }
4161
4162 #ifdef iPOWDER
4163 if (option == OPTION_UNLOCKING)
4164 {
4165 const char *actionbar[] =
4166 {
4167 "Restore Unlock",
4168 "Erase Unlock",
4169 "Cancel",
4170 0
4171 };
4172
4173 selection = gfx_selectmenu(30 - 20, 3, actionbar, aorb);
4174 if (aorb || selection < 0)
4175 {
4176 // TODO: Return to start menu rather than cancelling all!
4177 return;
4178 }
4179
4180 if (selection == 0)
4181 {
4182 if (hamfake_isunlocked())
4183 {
4184 gfx_pager_addtext(
4185 "Your copy of POWDER is already unlocked, so there is nothing "
4186 "to restore."
4187 );
4188 gfx_pager_newline();
4189 gfx_pager_display();
4190 }
4191 else
4192 {
4193 hamfake_buttonreq(6, 1);
4194
4195 gfx_pager_addtext(
4196 "Accessing the store..."
4197 );
4198 gfx_pager_newline(); gfx_pager_newline();
4199 gfx_pager_addtext(
4200 "This may take a while. System pop-ups will take you through "
4201 "the rest of the process. In the meantime, we return you to "
4202 "your regularly scheduled game."
4203 );
4204 gfx_pager_newline();
4205 gfx_pager_display();
4206 }
4207 }
4208 else if (selection == 1)
4209 {
4210 if (!hamfake_isunlocked())
4211 {
4212 gfx_pager_addtext(
4213 "Your copy of POWDER isn't unlocked yet, so there "
4214 "is no status to revoke."
4215 );
4216 gfx_pager_newline();
4217 gfx_pager_display();
4218 }
4219 else
4220 {
4221 if (gfx_yesnomenu("Lose your unlock status?"))
4222 {
4223 hamfake_setunlocked(false);
4224 FILE *fp;
4225
4226 fp = hamfake_fopen("powder.unlockcode", "w");
4227 if (fp)
4228 {
4229 fputs("This is not the unlock code.", fp);
4230 fclose(fp);
4231 }
4232 }
4233 else
4234 {
4235 gfx_pager_addtext(
4236 "A wise decision."
4237 );
4238 gfx_pager_newline();
4239 gfx_pager_display();
4240 }
4241 }
4242 }
4243 return;
4244 }
4245 #endif
4246 if (option == OPTION_ACTIONBAR)
4247 {
4248 const char *actionbar[] =
4249 {
4250 "Disable Action Bar",
4251 "Enable Action Bar",
4252 0
4253 };
4254
4255 int actionbardef;
4256
4257 actionbardef = glbActionBar;
4258 selection = gfx_selectmenu(30 - 20, 3, actionbar, aorb, actionbardef);
4259 if (aorb || selection < 0)
4260 {
4261 // TODO: Return to start menu rather than cancelling all!
4262 return;
4263 }
4264
4265 glbActionBar = selection ? true : false;
4266 return;
4267 }
4268
4269 if (option == OPTION_AUTOPROMPT)
4270 {
4271 const char *autoprompt[] =
4272 {
4273 "Disable Prompt",
4274 "Enable Prompt",
4275 0
4276 };
4277
4278 int autopromptdef;
4279
4280 autopromptdef = glbAutoPrompt;
4281 selection = gfx_selectmenu(30 - 15, 3, autoprompt, aorb, autopromptdef);
4282 if (aorb || selection < 0)
4283 {
4284 // TODO: Return to start menu rather than cancelling all!
4285 return;
4286 }
4287
4288 glbAutoPrompt = selection ? true : false;
4289 glbSuppressAutoClimb = false;
4290 return;
4291 }
4292
4293 if (option == OPTION_SAFEWALK)
4294 {
4295 const char *autoprompt[] =
4296 {
4297 "Attack by Default",
4298 "Walk by Default",
4299 0
4300 };
4301
4302 int autopromptdef;
4303
4304 autopromptdef = glbSafeWalk;
4305 selection = gfx_selectmenu(30 - 20, 3, autoprompt, aorb, autopromptdef);
4306 if (aorb || selection < 0)
4307 {
4308 // TODO: Return to start menu rather than cancelling all!
4309 return;
4310 }
4311
4312 glbSafeWalk = selection ? true : false;
4313 return;
4314 }
4315
4316 if (option == OPTION_COLOUREDFONTS)
4317 {
4318 const char *autoprompt[] =
4319 {
4320 "No Colored Text",
4321 "Colored Text",
4322 0
4323 };
4324
4325 int autopromptdef;
4326
4327 autopromptdef = glbColouredFont;
4328 selection = gfx_selectmenu(30 - 15, 3, autoprompt, aorb, autopromptdef);
4329 if (aorb || selection < 0)
4330 {
4331 // TODO: Return to start menu rather than cancelling all!
4332 return;
4333 }
4334
4335 glbColouredFont = selection ? true : false;
4336 return;
4337 }
4338
4339 if (option == OPTION_OPACITY)
4340 {
4341 const char *autoprompt[] =
4342 {
4343 "Transparent",
4344 "Opaque",
4345 0
4346 };
4347
4348 int opaquepromptdef;
4349
4350 opaquepromptdef = glbOpaqueTiles;
4351 selection = gfx_selectmenu(30 - 15, 3, autoprompt, aorb, opaquepromptdef);
4352 if (aorb || selection < 0)
4353 {
4354 // TODO: Return to start menu rather than cancelling all!
4355 return;
4356 }
4357
4358 glbOpaqueTiles = selection ? true : false;
4359 return;
4360 }
4361
4362 if (option == OPTION_TILES)
4363 {
4364 const char *tiles[NUM_TILESETS + 2];
4365 int i;
4366
4367 for (i = 0; i < NUM_TILESETS; i++)
4368 {
4369 tiles[i] = glb_tilesets[i].name;
4370 }
4371 tiles[NUM_TILESETS] = 0;
4372
4373 // Clear out from disk if requested.
4374 if (!hamfake_extratileset())
4375 tiles[NUM_TILESETS-1] = 0;
4376
4377 int tiledef = gfx_gettileset();
4378
4379 selection = gfx_selectmenu(30 - 15, 3,
4380 tiles,
4381 aorb, tiledef);
4382
4383 if (aorb || selection < 0)
4384 {
4385 // TODO: Return to start menu rather than cancelling all!
4386 return;
4387 }
4388
4389 // New tile default.
4390 tiledef = selection;
4391
4392 // Switch to the new tiles.
4393 gfx_switchtilesets(selection);
4394 return;
4395 }
4396
4397 if (option == OPTION_FONTS)
4398 {
4399 const char *fonts[] =
4400 {
4401 "Classic",
4402 "Brass",
4403 "Shadow",
4404 "Heavy",
4405 "Light",
4406 0
4407 };
4408
4409 int fontdef = gfx_getfont();
4410
4411 selection = gfx_selectmenu(30 - 15, 3,
4412 fonts,
4413 aorb, fontdef);
4414 if (aorb || selection < 0)
4415 {
4416 // TODO: Return to start menu rather than cancelling all!
4417 return;
4418 }
4419
4420 // New tile default.
4421 fontdef = selection;
4422
4423 // Switch to the new tiles.
4424 gfx_switchfonts(selection);
4425 return;
4426 }
4427
4428 if (option == OPTION_HELP)
4429 {
4430 processHelp();
4431 return;
4432 }
4433 }
4434
4435 ACTION_NAMES
4436 processVerbList()
4437 {
4438 // Nothing will happen as we will merely build an
4439 // assignment here...
4440 int button;
4441 int y;
4442 ACTION_NAMES action;
4443
4444 action = selectAssignment(menu_generic, button);
4445
4446 // Wait for the action to go high so we don't get
4447 // another trigger for USE...
4448 while (ctrl_rawpressed(button));
4449
4450 switch (button)
4451 {
4452 case BUTTON_SELECT:
4453 {
4454 bool found = false;
4455 int test = 0;
4456 BUF buf;
4457
4458 if (action == ACTION_NONE)
4459 break;
4460
4461 // Perform a remap of A or B.
4462 for (y = 3; y <= 19; y++)
4463 gfx_cleartextline(y);
4464 writeStatusLine();
4465 buf.sprintf("Bind %s to? ", glb_actiondefs[action].name);
4466 msg_report(buf);
4467
4468 while (!found)
4469 {
4470 for (test = BUTTON_A; test <= BUTTON_Y; test++)
4471 {
4472 if (ctrl_hit(test))
4473 {
4474 found = true;
4475 break;
4476 }
4477 }
4478 }
4479 // We bind it to test.
4480 if (action != ACTION_NONE)
4481 glb_actionbuttons[test] = action;
4482
4483 // Report appropriate message.
4484 if (test == BUTTON_START ||
4485 test == BUTTON_SELECT)
4486 {
4487 // Cancel.
4488 msg_report("Aborted. ");
4489 }
4490 else
4491 {
4492 msg_report("Bound. ");
4493 }
4494
4495 // But don't process...
4496 action = ACTION_NONE;
4497
4498 break;
4499 }
4500
4501 case BUTTON_START:
4502 case BUTTON_B:
4503 // Quick bind of X & Y works poorly with the default of one
4504 // of them being the verb list - you want to hit it again to
4505 // make the verb list go bye-bye but that then rebinds!
4506 case BUTTON_X:
4507 case BUTTON_Y:
4508 // Abort operation.
4509 action = ACTION_NONE;
4510 break;
4511
4512 case BUTTON_A:
4513 // Force the action
4514 break;
4515
4516 case BUTTON_L:
4517 case BUTTON_R:
4518 // Immediate remap.
4519 if (action != ACTION_NONE)
4520 glb_actionbuttons[button] = action;
4521 action = ACTION_NONE;
4522 break;
4523 }
4524
4525 // Now, rebuild our scene & status line...
4526 for (y = 3; y <= 19; y++)
4527 gfx_cleartextline(y);
4528 writeStatusLine();
4529
4530 return action;
4531 }
4532
4533 bool
4534 processAction(ACTION_NAMES action, SPELL_NAMES forcespell, MOB *avatar)
4535 {
4536 int dx, dy;
4537
4538 bool done = false, success;
4539
4540 switch (action)
4541 {
4542 case ACTION_NONE:
4543 break;
4544
4545 case ACTION_EQUIP:
4546 UT_ASSERT(0);
4547 break;
4548
4549 case ACTION_DEQUIP:
4550 UT_ASSERT(0);
4551 break;
4552
4553 case ACTION_DROP:
4554 UT_ASSERT(0);
4555 break;
4556
4557 case ACTION_INVENTORY:
4558 done = useInventory(avatar, false);
4559 break;
4560
4561 case ACTION_VERBLIST:
4562 action = processVerbList();
4563 if (action != ACTION_NONE && action != ACTION_VERBLIST)
4564 done = processAction(action, SPELL_NONE, avatar);
4565 break;
4566
4567 // Special case: ACTION_OPTIONS...
4568 case ACTION_OPTIONS:
4569 case ACTION_HELP:
4570 {
4571 if (action == ACTION_OPTIONS)
4572 processOptions();
4573 else
4574 processHelp();
4575
4576 // Now, rebuild our scene & status line...
4577 int y;
4578
4579 for (y = 3; y <= 19; y++)
4580 gfx_cleartextline(y);
4581 writeStatusLine();
4582 done = false;
4583 break;
4584 }
4585
4586 case ACTION_OPEN:
4587 success = msg_askdir("Open in what direction?", dx, dy, avatar->canMoveDiabolically());
4588
4589 if (success)
4590 {
4591 done = avatar->actionOpen(dx, dy);
4592 }
4593 break;
4594
4595 case ACTION_CLOSE:
4596 // Prompt direction, close that door.
4597 success = msg_askdir("Close in what direction?", dx, dy, avatar->canMoveDiabolically());
4598
4599 if (success)
4600 {
4601 done = avatar->actionClose(dx, dy);
4602 }
4603 break;
4604
4605 case ACTION_WISH:
4606 #ifdef iPOWDER
4607 if (!glbWizard)
4608 {
4609 if (hamfake_isunlocked())
4610 {
4611 if (gfx_yesnomenu("Enable Wishing?"))
4612 {
4613 glbWizard = true;
4614 }
4615 }
4616 else
4617 {
4618 if (gfx_yesnomenu("Unlock Wishing?"))
4619 {
4620 attemptunlock();
4621 // We can't set wizard here as we don't know
4622 // when/if they succeed in the unlock.
4623 }
4624 }
4625 }
4626 #endif
4627
4628 if (glbStressTest || !glbWizard)
4629 {
4630 // A couple of friendly rumours to justify this
4631 // option to those who do not know the super sekrit
4632 // password.
4633 const char *wizlist[] =
4634 {
4635 "%U <wish> %p knew the wizard password.",
4636 "If wishes were slugs, the slime pits would be full.",
4637 "%U <wish> %p had some more pickled slugs. Yum!",
4638 "%U <wish> very, very hard. Nothing happens.",
4639 "If wishes were kiwis, we'd all be dead by now.",
4640 "If wishes were granted, this game would be easier.",
4641 "%U <wish> %p knew the purpose of the Y-rune.",
4642 "A flock of bats, a quarry of cockatrices. %U <wish> %p were at home.",
4643 "%U <wish> for advice. %U <hear> a fighter's voice: \"It's the mushroom fume making us weary. We need lighter armour to keep in shape.\"",
4644 "%U <wish> for advice. %U <hear> a priest's voice: \"It's no use going down there. The blue dragons are everywhere. I haven't seen them, but I *know* it.\"",
4645 "%U <wish> for advice. %U <hear> a wizard's voice: \"The shallowest part of the dungeon has some wicked magical energies running in it. I swear I didn't fumble and cast it myself!\"",
4646 "%U <wish> for advice. %U <hear> a thief's voice: \"They come in packs of three and sparkle so loud you can hear them through four meters of thick gravel!\"",
4647 "%U <wish> for advice. %U <hear> a thief's voice: \"Invisible == Invincible? I wonder...\"",
4648 "%U <wish> for advice. %U <hear> an adventurer's voice: \"If the water of the lake freezes: Run!\"",
4649 "%U <wish> for advice. %U <hear> a necromancer's voice: \"Be content with the zombies, disturb not the middle room!\"",
4650 "%U <wish> for advice. %U <hear> a thief's voice: \"The kiwi's are hiding something!\"",
4651
4652 0
4653 };
4654 avatar->formatAndReport(rand_string(wizlist));
4655
4656 // Yes, this uses a turn. I am cruel that way.
4657 done = true;
4658 }
4659 else
4660 {
4661 done = makeAWish(avatar);
4662 }
4663 break;
4664
4665 case ACTION_FIRE:
4666 {
4667 // Find the fire direction...
4668 int tx, ty;
4669 int dx, dy, dz;
4670
4671 done = true;
4672 avatar->reportMessage("Fire where?");
4673
4674 tx = avatar->getX();
4675 ty = avatar->getY();
4676 if (gfx_selectdirection(tx, ty, dx, dy, dz))
4677 {
4678 msg_clear();
4679
4680 done = avatar->actionFire(dx, dy, dz);
4681 }
4682 else
4683 {
4684 // Cancelled...
4685 msg_clear();
4686 done = false;
4687 }
4688 break;
4689 }
4690
4691 case ACTION_MOVE:
4692 {
4693 // Find the movement direction...
4694 int dx, dy, dz, tx, ty;
4695
4696 done = true;
4697 avatar->reportMessage("Move where?");
4698
4699 tx = avatar->getX();
4700 ty = avatar->getY();
4701 if (gfx_selectdirection(tx, ty, dx, dy, dz))
4702 {
4703 msg_clear();
4704
4705 if (dz)
4706 {
4707 if (dz > 0)
4708 done = avatar->actionClimbUp();
4709 else
4710 done = avatar->actionClimbDown();
4711 }
4712 else
4713 {
4714 if (dx && dy && !avatar->canMoveDiabolically())
4715 {
4716 msg_clear();
4717 msg_report("You cannot move in that fashion.");
4718 }
4719 else
4720 done = avatar->actionBump(dx, dy);
4721 }
4722 }
4723 else
4724 {
4725 // Cancelled...
4726 msg_clear();
4727 done = false;
4728 }
4729 break;
4730 }
4731
4732 case ACTION_BREATHE:
4733 {
4734 // Find the breath direction...
4735 int tx, ty;
4736 int dx, dy, dz;
4737
4738 done = true;
4739 avatar->reportMessage("Breathe where?");
4740
4741 tx = avatar->getX();
4742 ty = avatar->getY();
4743 if (gfx_selectdirection(tx, ty, dx, dy, dz))
4744 {
4745 msg_clear();
4746
4747 done = avatar->actionBreathe(dx, dy);
4748 }
4749 else
4750 {
4751 // Cancelled...
4752 msg_clear();
4753 done = false;
4754 }
4755 break;
4756 }
4757
4758 case ACTION_LOCK:
4759 // Prompt dir, lock.
4760 break;
4761
4762 case ACTION_CLIMB:
4763 // Try to climb the square we are on.
4764 done = avatar->actionClimb();
4765 break;
4766
4767 case ACTION_PICKUP:
4768 {
4769 ITEMSTACK stack;
4770 bool submerged;
4771 ITEM *item;
4772
4773 submerged = avatar->hasIntrinsic(INTRINSIC_INPIT) ||
4774 avatar->hasIntrinsic(INTRINSIC_SUBMERGED);
4775
4776 // Pick up from our square.
4777 glbCurLevel->getItemStack(stack, avatar->getX(), avatar->getY(),
4778 submerged);
4779
4780 stack.reverse();
4781
4782 if (stack.entries() == 0)
4783 item = 0;
4784 else if (stack.entries() == 1)
4785 item = stack(0);
4786 else
4787 {
4788 // Ask the user what to pick up.
4789 avatar->reportMessage("Pick up what?");
4790
4791 char **list;
4792 int i, n, aorb, select;
4793
4794 n = stack.entries();
4795 list = new char *[n+1];
4796 for (i = 0; i < n; i++)
4797 {
4798 list[i] = stack(i)->getName(false).strdup();
4799 }
4800 list[i] = 0;
4801
4802 // Select the item to pickup
4803 select = gfx_selectmenu(3, 3, (const char **) list, aorb);
4804 if (select < 0 || aorb)
4805 item = 0;
4806 else
4807 item = stack(select);
4808
4809 // Free the list.
4810 for (i = 0; i < n; i++)
4811 free(list[i]);
4812 delete [] list;
4813
4814 // And clear it...
4815 for (i = 3; i < 18; i++)
4816 {
4817 gfx_cleartextline(i);
4818 }
4819
4820 if (!item)
4821 {
4822 avatar->reportMessage("You decide against it.");
4823 break;
4824 }
4825 }
4826
4827 done = avatar->actionPickUp(item);
4828 break;
4829 }
4830
4831 case ACTION_QUAFF:
4832 case ACTION_EAT:
4833 {
4834 ITEMSTACK stack;
4835 bool submerged;
4836 ITEM *item, *newitem;
4837
4838 submerged = avatar->hasIntrinsic(INTRINSIC_INPIT) ||
4839 avatar->hasIntrinsic(INTRINSIC_SUBMERGED);
4840
4841 // Eat from our square.
4842 glbCurLevel->getItemStack(stack, avatar->getX(), avatar->getY(),
4843 submerged);
4844
4845 stack.reverse();
4846
4847 if (stack.entries() == 0)
4848 item = 0;
4849 else if (stack.entries() == 1)
4850 item = stack(0);
4851 else
4852 {
4853 // Ask the user what to pick up.
4854 avatar->reportMessage("Eat what?");
4855
4856 char **list;
4857 int i, n, aorb, select;
4858
4859 n = stack.entries();
4860 list = new char *[n+1];
4861 for (i = 0; i < n; i++)
4862 {
4863 list[i] = stack(i)->getName(false).strdup();
4864 }
4865 list[i] = 0;
4866
4867 // Select the item to pickup
4868 select = gfx_selectmenu(3, 3, (const char **) list, aorb);
4869 if (select < 0 || aorb)
4870 item = 0;
4871 else
4872 item = stack(select);
4873
4874 // Free the list.
4875 for (i = 0; i < n; i++)
4876 free(list[i]);
4877 delete [] list;
4878
4879 // And clear it...
4880 for (i = 3; i < 18; i++)
4881 {
4882 gfx_cleartextline(i);
4883 }
4884
4885 if (!item)
4886 {
4887 avatar->reportMessage("You decide against it.");
4888 break;
4889 }
4890 }
4891
4892 if (!item)
4893 {
4894 avatar->reportMessage("There is nothing here to eat!");
4895 break;
4896 }
4897
4898 // First, pick the item up...
4899 newitem = 0;
4900 done = avatar->actionPickUp(item, &newitem);
4901
4902 // Determine if we picked it up.
4903 if (newitem && newitem->isInventory())
4904 {
4905 // Attempt to eat...
4906 done |= avatar->actionEat(newitem->getX(), newitem->getY());
4907 }
4908 break;
4909 }
4910
4911 case ACTION_SWAP:
4912 success = msg_askdir("Swap places in what direction?", dx, dy, avatar->canMoveDiabolically());
4913
4914 if (success)
4915 {
4916 done = avatar->actionSwap(dx, dy);
4917 }
4918 break;
4919
4920 case ACTION_RELEASE:
4921 done = avatar->actionReleasePossession();
4922 break;
4923
4924 case ACTION_SLEEP:
4925 done = avatar->actionSleep();
4926 break;
4927
4928 case ACTION_FORGET:
4929 {
4930 SKILL_NAMES *skill_list;
4931 SPELL_NAMES *spell_list;
4932 const char **options;
4933 int i, n, choice, aorb;
4934 SPELL_NAMES spell = SPELL_NONE;
4935 SKILL_NAMES skill = SKILL_NONE;
4936
4937 n = 0;
4938 for (i = 0; i < NUM_SKILLS; i++)
4939 {
4940 if (avatar->hasSkill((SKILL_NAMES)i))
4941 n++;
4942 }
4943
4944 for (i = 0; i < NUM_SPELLS; i++)
4945 {
4946 if (avatar->hasSpell((SPELL_NAMES)i))
4947 n++;
4948 }
4949
4950 if (!n)
4951 {
4952 avatar->reportMessage("You have nothing to forget.");
4953 break;
4954 }
4955
4956 avatar->reportMessage("Forget what?");
4957
4958 skill_list = new SKILL_NAMES[n];
4959 spell_list = new SPELL_NAMES[n];
4960 options = new const char *[n+1];
4961
4962 n = 0;
4963 for (i = 0; i < NUM_SKILLS; i++)
4964 {
4965 if (avatar->hasSkill((SKILL_NAMES)i))
4966 {
4967 skill_list[n] = (SKILL_NAMES) i;
4968 spell_list[n] = SPELL_NONE;
4969 options[n] = glb_skilldefs[i].name;
4970 n++;
4971 }
4972 }
4973
4974 for (i = 0; i < NUM_SPELLS; i++)
4975 {
4976 if (avatar->hasSpell((SPELL_NAMES)i))
4977 {
4978 spell_list[n] = (SPELL_NAMES) i;
4979 skill_list[n] = SKILL_NONE;
4980 options[n] = glb_spelldefs[i].name;
4981 n++;
4982 }
4983 }
4984 options[n] = 0;
4985
4986 // Get the forget choice.
4987 static int lastforget = 0;
4988
4989 choice = gfx_selectmenu(12, 4, options, aorb, lastforget);
4990
4991 if (aorb)
4992 choice = -1;
4993
4994 msg_clear();
4995
4996 {
4997 int y;
4998 for (y = 3; y < 19; y++)
4999 gfx_cleartextline(y);
5000 }
5001 if (choice >= 0)
5002 {
5003 lastforget = choice;
5004 skill = skill_list[choice];
5005 spell = spell_list[choice];
5006
5007 // Verify the player wants to forget this
5008 BUF forgetprompt;
5009 if (skill != SKILL_NONE)
5010 forgetprompt.sprintf("Forget %s?", glb_skilldefs[skill].name);
5011 else
5012 forgetprompt.sprintf("Forget %s?", glb_spelldefs[spell].name);
5013
5014 if (!gfx_yesnomenu(forgetprompt))
5015 choice = -1;
5016 }
5017
5018 if (choice >= 0)
5019 {
5020 done = avatar->actionForget(spell, skill);
5021 }
5022
5023 delete [] skill_list;
5024 delete [] spell_list;
5025 delete [] options;
5026
5027 break;
5028 }
5029
5030 case ACTION_SEARCH:
5031 // Look all around us...
5032 done = avatar->actionSearch();
5033 break;
5034
5035 case ACTION_JUMP:
5036 success = msg_askdir("Jump where?", dx, dy, avatar->canMoveDiabolically());
5037
5038 if (success)
5039 {
5040 done = avatar->actionJump(dx, dy);
5041 }
5042 break;
5043
5044 case ACTION_WAIT:
5045 // Do nothing for a turn...
5046 done = true;
5047 break;
5048
5049 case ACTION_RUN:
5050 if (avatar->hasIntrinsic(INTRINSIC_BLIND))
5051 {
5052 avatar->formatAndReport("%U <decide> against running around blindly.");
5053 break;
5054 }
5055 // Note we prohibit diagonal running as our run code
5056 // doesn't like it. Er, that sounds suspiciously like
5057 // laziness! I mean, because moving diabolically requires
5058 // utmost concentration so cannot be done in a haphazard
5059 // manner!
5060 success = msg_askdir("Run where?", dx, dy, false);
5061
5062 if (success && (dx || dy))
5063 {
5064 glbAutoRunDX = dx;
5065 glbAutoRunDY = dy;
5066 glbAutoRunEnabled = true;
5067 glbAutoRunOpenSpace = true;
5068 // Note we do not consume a turn here, but instead
5069 // let the main loop pick up those global variables.
5070 // We do wait for the direct to go high as we
5071 // don't want our direction key to interrupt the run.
5072 #ifndef HAS_KEYBOARD
5073 while (ctrl_anyrawpressed())
5074 hamfake_awaitEvent();
5075 #endif
5076
5077 }
5078 break;
5079
5080 case ACTION_LOOK:
5081 {
5082 int tx, ty;
5083 int oldoverlay;
5084 int aorb = 0;
5085
5086 // This needs to be cleaned up, as it can be of sync
5087 // with how map determines the invisible avatar.
5088 // Too bad I'm relaxing in the ANA Signet Lounge drinking free
5089 // beer to care.
5090 // The code that used to be here has been elided as I have
5091 // since realized it is wiser just to read the overlay
5092 // from the screen itself.
5093
5094 avatar->reportMessage("Where do you want to look?");
5095
5096 tx = avatar->getX();
5097 ty = avatar->getY();
5098
5099 // Rather than trying to guess our overlay tile,
5100 // just read it directly from the screen.
5101 oldoverlay = gfx_getoverlay(tx, ty);
5102 while (1)
5103 {
5104 bool stylus, select;
5105 int ox, oy;
5106
5107 ox = tx;
5108 oy = ty;
5109
5110 select = gfx_selecttile(tx, ty, true, &oldoverlay, &aorb, &stylus);
5111 // Complete selection on a stylus press.
5112 if (select && !stylus)
5113 break;
5114 // Finish stylus selction if they select themselves
5115 if (select && stylus && (tx == ox) && (ty == oy))
5116 break;
5117
5118 msg_clear();
5119 // Describe that square...
5120 if (!glbCurLevel->describeSquare(tx, ty,
5121 avatar->hasIntrinsic(INTRINSIC_BLIND),
5122 true))
5123 {
5124 // Nothing to report, so say so..
5125 avatar->reportMessage("There is nothing of interest there.");
5126 }
5127 }
5128 msg_clear();
5129
5130 // Give extended description of mobs.
5131 MOB *mob;
5132
5133 mob = glbCurLevel->getMob(tx, ty);
5134 // We only do full describe if [A] is hit.
5135 if (!aorb && mob)
5136 {
5137 if (mob->isAvatar() || avatar->getSenseType(mob) == SENSE_SIGHT)
5138 {
5139 // We can see the avatar, we can see what they are wearing.
5140 // Build the long description text.
5141 mob->viewDescription();
5142 }
5143 }
5144
5145 break;
5146 }
5147
5148 case ACTION_NAME:
5149 {
5150 int tx, ty;
5151
5152 avatar->reportMessage("Who do you want to name?");
5153
5154 tx = avatar->getX();
5155 ty = avatar->getY();
5156 if (gfx_selecttile(tx, ty))
5157 {
5158 MOB *mob;
5159
5160 mob = glbCurLevel->getMob(tx, ty);
5161
5162 // You can't name what isn't there. You shouldn't
5163 // find out about invisible mobs by naming.
5164 if (!mob || !avatar->canSense(mob))
5165 {
5166 avatar->reportMessage("There is noone there to name.");
5167 }
5168 else if (mob->hasIntrinsic(INTRINSIC_UNIQUE))
5169 {
5170 avatar->reportMessage("You cannot rename uniques.");
5171 }
5172 else
5173 {
5174 char buf[50];
5175
5176 msg_clear();
5177
5178 gfx_printtext(0, 0, "New Name? ");
5179 input_getline(buf, 19, 10, 0, 23, 8);
5180
5181 {
5182 int y;
5183 for (y = 0; y < 18; y++)
5184 gfx_cleartextline(y);
5185 }
5186
5187 // Special case: renaming the mob will rename
5188 // the glbAvatarName.
5189 if (mob->isAvatar())
5190 {
5191 strcpy(glbAvatarName, buf);
5192 }
5193 else
5194 mob->christen(buf);
5195 }
5196 }
5197 else
5198 {
5199 avatar->reportMessage("You decide against it.");
5200 }
5201 break;
5202 }
5203
5204 case ACTION_COMMAND:
5205 {
5206 MOBSTACK pets;
5207
5208 glbCurLevel->getPets(pets, avatar, true);
5209
5210 if (!pets.entries())
5211 {
5212 // Failed as no pets.
5213 avatar->reportMessage("You have no visible pets to command.");
5214 break;
5215 }
5216
5217 char **menu;
5218 static int lastchoice = 0;
5219 int i, choice, aorb;
5220 MOB *target = 0;
5221 BUF buf;
5222
5223 menu = new char *[pets.entries() + 2];
5224
5225 for (i = 0; i < pets.entries(); i++)
5226 {
5227 menu[i] = pets(i)->getName(false, false, true, true).strdup();
5228 }
5229
5230 // Add in all.
5231 menu[i++] = "All Pets";
5232 menu[i++] = 0;
5233
5234 // Let the user pick.
5235 gfx_printtext(0, 3, "Command Which Pet?");
5236 choice = gfx_selectmenu(5, 4, (const char **) menu,
5237 aorb, lastchoice);
5238
5239 if (choice != -1)
5240 lastchoice = choice;
5241
5242 // Erase...
5243 for (i = 3; i < 19; i++)
5244 gfx_cleartextline(i);
5245
5246 // Free the menu
5247 for (i = 0; i < pets.entries(); i++)
5248 {
5249 free(menu[i]);
5250 }
5251 delete [] menu;
5252
5253 if (aorb || choice == -1)
5254 {
5255 // B is hit, so cancel...
5256 break;
5257 }
5258
5259 const char *commandlist[] =
5260 {
5261 "Kill...",
5262 "Guard Me",
5263 "Stay",
5264 0
5265 };
5266 static int lastcommand = 0;
5267 int command;
5268 gfx_printtext(0, 3, "Command?");
5269 command = gfx_selectmenu(5, 4, commandlist, aorb, lastcommand);
5270
5271 if (command != -1)
5272 lastcommand = command;
5273
5274 // Erase...
5275 for (i = 3; i < 19; i++)
5276 gfx_cleartextline(i);
5277
5278 // Setup the target.
5279 switch (command)
5280 {
5281 case 0: // Kill...
5282 {
5283 int tx, ty;
5284
5285 avatar->reportMessage("Kill who?");
5286
5287 tx = avatar->getX();
5288 ty = avatar->getY();
5289 if (!gfx_selecttile(tx, ty))
5290 {
5291 msg_clear();
5292 break;
5293 }
5294
5295 target = glbCurLevel->getMob(tx, ty);
5296
5297 // Can only target people we know!
5298 if (!avatar->canSense(target))
5299 target = 0;
5300
5301 msg_clear();
5302 break;
5303 }
5304 case 1: // Guard Me
5305 case 2: // Stay
5306 break;
5307 }
5308
5309 // If we are to kill and have no target, abort.
5310 if (command == 0 && !target)
5311 {
5312 avatar->reportMessage("You decide against violence.");
5313 break;
5314 }
5315
5316 // Apply the command to all critters.
5317 for (i = 0; i < pets.entries(); i++)
5318 {
5319 // Skip unless it is the right creature, or
5320 // the user picked all. God this is disgusting code.
5321 if (choice != pets.entries() &&
5322 choice != i)
5323 continue;
5324
5325 BUF petname = pets(i)->getName(false, false, true, true);
5326 switch (command)
5327 {
5328 case 0: // Kill!
5329 buf.sprintf("%s kill!", petname.buffer());
5330 avatar->reportMessage(buf);
5331 pets(i)->setAIFSM(AI_FSM_ATTACK);
5332 pets(i)->setAITarget(target);
5333 break;
5334
5335 case 1: // Guard me!
5336 buf.sprintf("%s guard me!", petname.buffer());
5337 avatar->reportMessage(buf);
5338 pets(i)->setAIFSM(AI_FSM_GUARD);
5339 pets(i)->clearAITarget();
5340 break;
5341
5342 case 2: // Stay!
5343 buf.sprintf("%s stay!", petname.buffer());
5344 avatar->reportMessage(buf);
5345 pets(i)->setAIFSM(AI_FSM_STAY);
5346 pets(i)->clearAITarget();
5347 break;
5348 }
5349 }
5350 done = true;
5351
5352 break;
5353 }
5354
5355 case ACTION_ZAP:
5356 {
5357 const char *m[NUM_SPELLS + 3];
5358 u8 stripbutton[NUM_SPELLS+3];
5359 SPELL_NAMES s[NUM_SPELLS + 3];
5360 SPELL_NAMES spell;
5361 int i, j, choice, aorb;
5362 int dx, dy, dz;
5363 static int lastchoice = 0;
5364 BUF buf;
5365
5366 // Cast a magic spell
5367 j = 0;
5368 FOREACH_SPELL(spell)
5369 {
5370 if (avatar->hasSpell(spell))
5371 {
5372 s[j] = spell;
5373 m[j] = glb_spelldefs[spell].name;
5374 if (!avatar->canCastSpell(spell))
5375 {
5376 char *mi = new char[strlen(m[j])+10];
5377 sprintf(mi, "%s (!)", m[j]);
5378 m[j] = mi;
5379 }
5380 stripbutton[j] = action_packStripButton(spell);
5381 j++;
5382 }
5383 }
5384
5385 // Include cancel option to cast nothing.
5386 s[j] = SPELL_NONE;
5387 m[j] = "cancel";
5388 stripbutton[j] = action_packStripButton(ACTION_NONE);
5389 j++;
5390
5391 m[j] = 0;
5392
5393 if (j <= 1)
5394 {
5395 avatar->reportMessage("You know no spells!");
5396 break;
5397 }
5398
5399 spell = forcespell;
5400
5401 choice = -1;
5402 while (spell == SPELL_NONE)
5403 {
5404 gfx_printtext(0, 3, "Which spell?");
5405
5406 choice = gfx_selectmenu(5, 4, m, aorb, lastchoice, true,
5407 false, stripbutton, glb_globalactionstrip);
5408
5409 // Add cancel option.
5410 if (choice != -1 && s[choice] == SPELL_NONE)
5411 choice = -1;
5412
5413 if (choice != -1)
5414 {
5415 lastchoice = choice;
5416 }
5417
5418 if (aorb == BUTTON_SELECT && choice != -1)
5419 {
5420 // Give long description..
5421 encyc_viewSpellDescription((SPELL_NAMES) s[choice]);
5422 }
5423
5424 if (aorb == BUTTON_B || choice == -1)
5425 {
5426 // B is hit, so cancel...
5427 choice = -1;
5428 break;
5429 }
5430 // Check for selection
5431 if (aorb == BUTTON_A)
5432 break;
5433 }
5434 // Erase...
5435 for (i = 3; i < 19; i++)
5436 gfx_cleartextline(i);
5437
5438 for (i = 0; i < j; i++)
5439 {
5440 if (s[i] != SPELL_NONE &&
5441 m[i] != glb_spelldefs[s[i]].name)
5442 {
5443 delete [] m[i];
5444 }
5445 }
5446
5447 if (spell == SPELL_NONE && choice == -1)
5448 break;
5449
5450 if (spell == SPELL_NONE)
5451 spell = s[choice];
5452
5453 // This may seem silly, but you may be using
5454 // quick keys
5455 if (!avatar->hasSpell(spell))
5456 {
5457 avatar->formatAndReport("%U <do> not know that spell.");
5458 break;
5459 }
5460
5461 // Now, cast the spell... First, get a direction...
5462 dx = dy = dz = 0;
5463 if (glb_spelldefs[spell].needsdir)
5464 {
5465 int tx, ty;
5466
5467 avatar->formatAndReport("Cast %B1 in direction?",
5468 glb_spelldefs[spell].name);
5469
5470 tx = avatar->getX();
5471 ty = avatar->getY();
5472 if (!gfx_selectdirection(tx, ty, dx, dy, dz))
5473 {
5474 msg_clear();
5475 break;
5476 }
5477
5478 // Chose direction, clear message...
5479 msg_clear();
5480 }
5481 else if (glb_spelldefs[spell].needstarget)
5482 {
5483 int tx, ty;
5484
5485 avatar->formatAndReport("Cast %B1 at?",
5486 glb_spelldefs[spell].name);
5487
5488 tx = avatar->getX();
5489 ty = avatar->getY();
5490 if (!gfx_selecttile(tx, ty))
5491 {
5492 msg_clear();
5493 break;
5494 }
5495
5496 // Choose direction, clear message...
5497 dx = tx - avatar->getX();
5498 dy = ty - avatar->getY();
5499 msg_clear();
5500 }
5501
5502 // A hit, so cast the spell. Check if we have the MP.
5503 if (avatar->getMP() < glb_spelldefs[spell].mpcost)
5504 {
5505 buf.sprintf("You need %d magic points!",
5506 glb_spelldefs[spell].mpcost);
5507 avatar->reportMessage(buf);
5508 break;
5509 }
5510
5511 // Check for HP...
5512 if (avatar->getHP() < glb_spelldefs[spell].hpcost)
5513 {
5514 buf.sprintf("You need %d health points!",
5515 glb_spelldefs[spell].hpcost);
5516 avatar->reportMessage(buf);
5517 break;
5518 }
5519
5520 // Check for XP...
5521 if (avatar->getExp() < glb_spelldefs[spell].xpcost)
5522 {
5523 buf.sprintf("You need %d free experience points!",
5524 glb_spelldefs[spell].xpcost);
5525 avatar->reportMessage(buf);
5526 break;
5527 }
5528
5529 // NOw, cast the spell...
5530 done = avatar->actionCast(spell, dx, dy, dz);
5531
5532 break;
5533 }
5534
5535 case ACTION_USE:
5536 {
5537 done = useInventory(avatar, true);
5538 break;
5539 }
5540
5541 case ACTION_PRAY:
5542 {
5543 done = avatar->actionPray();
5544 break;
5545 }
5546
5547 case ACTION_HISTORY:
5548 {
5549 // History doesn't consume time (duh)
5550 done = false;
5551
5552 msg_displayhistory();
5553 break;
5554 }
5555
5556 case ACTION_SAFEWALK:
5557 {
5558 // Selecting this will toggle the current mode
5559 // permamently (as opposed to binding which lets
5560 // you just enter and leave)
5561 done = false;
5562 glbSafeWalk = !glbSafeWalk;
5563 if (glbSafeWalk)
5564 avatar->formatAndReport("%U <tread> carefully through the corridors.");
5565 else
5566 avatar->formatAndReport("%U <prepare> for battle at a moments notice.");
5567 break;
5568 }
5569
5570 case ACTION_MINIMAP:
5571 {
5572 // Minimap doesn't consume time.
5573 done = false;
5574
5575 // Display, wait for keypress, return.
5576 gfx_displayminimap(glbCurLevel);
5577
5578 msg_awaitaccept();
5579
5580 gfx_hideminimap();
5581
5582 break;
5583 }
5584
5585 default:
5586 UT_ASSERT(0);
5587 break;
5588 }
5589
5590 return done;
5591 }
5592
5593 bool
5594 processDirectionStylus(int &dx, int &dy, bool &walkonly, bool &xmajor, bool allowdiag)
5595 {
5596 int x, y;
5597
5598 walkonly = true;
5599 xmajor = false;
5600
5601 // If the user has turned off screen tapping, we don't move
5602 // when the screen is hit.
5603 if (!glbScreenTap)
5604 return false;
5605
5606 hamfake_getstyluspos(x, y);
5607 x -= 15*TILEWIDTH;
5608 y -= 10*TILEHEIGHT;
5609 // We want a very large active area!
5610 // Indeed, argueably there should be no limit!
5611 // The 8 pixel border ensures the DS has a 16 pixel border
5612 // which can be used for the side buttons.
5613 // But, if we have the action bar disabled, this dead zone is of
5614 // no use to us!
5615 if (glbActionBar)
5616 if (ABS(x) > 14*TILEWIDTH || ABS(y) > 9*TILEHEIGHT)
5617 return false;
5618
5619 dx = dy = 0;
5620
5621 // This means search.
5622 if (ABS(x) < TILEWIDTH && ABS(y) < TILEHEIGHT)
5623 return true;
5624
5625 // Check if we click on one of the 8 squares around us. This means
5626 // bump.
5627 if (ABS(x) <= 3*TILEWIDTH && ABS(y) < TILEHEIGHT)
5628 {
5629 walkonly = false;
5630 }
5631 else if (ABS(x) < TILEWIDTH && ABS(y) <= 3*TILEHEIGHT)
5632 {
5633 walkonly = false;
5634 }
5635 else if (allowdiag && ABS(x) <= 3*TILEWIDTH && ABS(y) < 3*TILEHEIGHT)
5636 {
5637 walkonly = false;
5638 }
5639
5640 if (ABS(x) > ABS(y))
5641 {
5642 xmajor = true;
5643 dx = SIGN(x);
5644 // We want values less than 8 to be zero
5645 y /= TILEHEIGHT;
5646 dy = SIGN(y);
5647 }
5648 else
5649 {
5650 xmajor = false;
5651 x /= TILEWIDTH;
5652 dx = SIGN(x);
5653 dy = SIGN(y);
5654 }
5655
5656 return true;
5657 }
5658
5659 void
5660 checkMiniMapButtons()
5661 {
5662 int i;
5663
5664 // Check for new map request...
5665 for (i = 0; glb_mapbuttons[i] != NUM_BUTTONS; i++)
5666 {
5667 if (glb_actionbuttons[glb_mapbuttons[i]] == ACTION_MINIMAP &&
5668 ctrl_rawpressed(glb_mapbuttons[i]))
5669 {
5670 bool minimap = false;
5671
5672 // L button goes back, all other ones go forward.
5673 if (!msg_restore(glb_mapbuttons[i] == BUTTON_L))
5674 {
5675 minimap = true;
5676 gfx_displayminimap(glbCurLevel);
5677 }
5678
5679 // Wait for it to be released...
5680 while (ctrl_rawpressed(glb_mapbuttons[i]));
5681
5682 if (minimap)
5683 gfx_hideminimap();
5684 }
5685 }
5686 }
5687
5688
5689 void
5690 checkModifierButtons()
5691 {
5692 // Update our safewalk status.
5693 glbToggleSafeWalk = false;
5694 if (hamfake_getKeyModifiers() & GFX_KEYMODCTRL)
5695 {
5696 glbToggleSafeWalk = true;
5697 }
5698
5699 glbToggleSwap = false;
5700 if (hamfake_getKeyModifiers() & GFX_KEYMODSHIFT)
5701 {
5702 glbToggleSwap = true;
5703 }
5704
5705 glbToggleJump = false;
5706 if (hamfake_getKeyModifiers() & GFX_KEYMODALT)
5707 {
5708 glbToggleJump = true;
5709 }
5710
5711 int i;
5712
5713 // Check for new map request...
5714 for (i = 0; glb_mapbuttons[i] != NUM_BUTTONS; i++)
5715 {
5716 if (glb_actionbuttons[glb_mapbuttons[i]] == ACTION_SAFEWALK &&
5717 ctrl_rawpressed(glb_mapbuttons[i]))
5718 {
5719 glbToggleSafeWalk = true;
5720 }
5721 }
5722 }
5723
5724 void
5725 processAllInput(ACTION_NAMES &action, SPELL_NAMES &spell, int &dx, int &dy)
5726 {
5727 STYLUSLOCK styluslock(REGION_BOTTOMBUTTON | REGION_SIDEBUTTON);
5728
5729 // Check for forced actions.
5730 if (glbAutoRunEnabled)
5731 {
5732 if (!MOB::getAvatar())
5733 glbAutoRunEnabled = false;
5734
5735 if (hamfake_peekKeyPress())
5736 glbAutoRunEnabled = false;
5737
5738 #ifndef HAS_KEYBOARD
5739 if (ctrl_anyrawpressed())
5740 glbAutoRunEnabled = false;
5741 #endif
5742
5743 if (!glbAutoRunEnabled)
5744 msg_report("Running interrupted.");
5745 }
5746
5747 if (glbAutoRunEnabled)
5748 {
5749 MOB *avatar = MOB::getAvatar(), *mob;
5750 int dx, dy, ax, ay, cx, cy;
5751
5752 ax = avatar->getX();
5753 ay = avatar->getY();
5754
5755 // Verify there are no non-friendly creatures around us.
5756 FORALL_4DIR(dx, dy)
5757 {
5758 mob = glbCurLevel->getMob(ax+dx, ay+dy);
5759
5760 if (mob && avatar->getAttitude(mob) != ATTITUDE_FRIENDLY)
5761 {
5762 if (glbAutoRunEnabled)
5763 avatar->formatAndReport("Seeing a foe, %U <stop> running.");
5764 glbAutoRunEnabled = false;
5765 }
5766 }
5767
5768 dx = glbAutoRunDX;
5769 dy = glbAutoRunDY;
5770 cx = !dx;
5771 cy = !dy;
5772
5773 // I am being *extraordinarily* kind here and not letting
5774 // people run into acid pools. I doubt players will recognize
5775 // it as an act of kindness, though.
5776 if (avatar->canMoveDelta(dx, dy, true, false, true))
5777 {
5778 // We can move forward. It is required that we cannot
5779 // move to either side or we have a branch and must abort.
5780 if (avatar->canMoveDelta(cx, cy, true, false, true) ||
5781 avatar->canMoveDelta(-cx, -cy, true, false, true))
5782 {
5783 if (!glbAutoRunOpenSpace)
5784 {
5785 if (glbAutoRunEnabled)
5786 avatar->formatAndReport("%U <stop> at an open area.");
5787 glbAutoRunEnabled = false;
5788 }
5789 }
5790 else
5791 {
5792 // We have entered a corridor, so next time we are in
5793 // open space we'll abort.
5794 glbAutoRunOpenSpace = false;
5795 }
5796 }
5797 else
5798 {
5799 // We can't move forward. If we can move in only one
5800 // of the cross directions, yay, that is our new direction
5801 // Otherwise we abort confused.
5802 // If we have not yet left open space we do not turn!
5803 // Otherwise in a closed room we'd happily run in circles,
5804 // which is cute, but not what we really want.
5805 if (glbAutoRunOpenSpace)
5806 {
5807 if (glbAutoRunEnabled)
5808 avatar->formatAndReport("%U <stop> to take %R bearings.");
5809 glbAutoRunEnabled = false;
5810 }
5811
5812 if (avatar->canMoveDelta(cx, cy, true, false, true))
5813 {
5814 if (!avatar->canMoveDelta(-cx, -cy, true, false, true))
5815 {
5816 glbAutoRunDX = cx;
5817 glbAutoRunDY = cy;
5818 }
5819 else
5820 {
5821 if (glbAutoRunEnabled)
5822 avatar->formatAndReport("%U <stop> at a crossroads.");
5823 glbAutoRunEnabled = false;
5824 }
5825 }
5826 else if (avatar->canMoveDelta(-cx, -cy, true, false, true))
5827 {
5828 glbAutoRunDX = -cx;
5829 glbAutoRunDY = -cy;
5830 }
5831 else
5832 {
5833 if (glbAutoRunEnabled)
5834 avatar->formatAndReport("%U <stop> at a dead end.");
5835 glbAutoRunEnabled = false;
5836 }
5837 }
5838 }
5839
5840 // If we still have auto run, force us to bump in the current direction.
5841 if (glbAutoRunEnabled)
5842 {
5843 action = ACTION_NONE;
5844 spell = SPELL_NONE;
5845 dx = glbAutoRunDX;
5846 dy = glbAutoRunDY;
5847 return;
5848 }
5849
5850 hamfake_enableexternalactions(true);
5851 while (1)
5852 {
5853 // Wait for a new frame...
5854 while (!gfx_isnewframe());
5855
5856 if (hamfake_forceQuit())
5857 break;
5858
5859 bool forceaction = false;
5860 bool walkonly = false;
5861 bool xmajor = false;
5862 MOB *avatar = MOB::getAvatar();
5863
5864 action = ACTION_NONE;
5865 dx = dy = 0;
5866
5867 // We only work when the avatar lives.
5868 if (!avatar)
5869 {
5870 return;
5871 }
5872
5873 // Check for an inventory request.
5874 if (ctrl_hit(BUTTON_SELECT))
5875 {
5876 action = ACTION_INVENTORY;
5877 return;
5878 }
5879 if (ctrl_hit(BUTTON_START))
5880 {
5881 action = ACTION_VERBLIST;
5882 return;
5883 }
5884
5885 checkMiniMapButtons();
5886
5887 int actionbutton = NUM_BUTTONS;
5888
5889 // Move our dude if the button is down and it is possible.
5890 ctrl_getdir(dx, dy, 0, avatar->canMoveDiabolically());
5891 // We do not want to clear our keyboard buffer here
5892 // because we want direction keys to repeat!!
5893 // hamfake_clearKeyboardBuffer();
5894
5895 checkModifierButtons();
5896 if (dx || dy)
5897 {
5898 // Trigger a walk mode..
5899 if (glbToggleSafeWalk != glbSafeWalk)
5900 {
5901 // Walking has no real sense of a major direction
5902 // so we just ensure we are sane for ortho cases
5903 if (dx)
5904 xmajor = true;
5905 else
5906 xmajor = false;
5907 forceaction = true;
5908 action = ACTION_WALK;
5909 }
5910 if (glbToggleSwap != glbSwap)
5911 {
5912 if (dx)
5913 xmajor = true;
5914 else
5915 xmajor = false;
5916 forceaction = true;
5917 action = ACTION_SWAP;
5918 }
5919 if (glbToggleJump != glbJump)
5920 {
5921 if (dx)
5922 xmajor = true;
5923 else
5924 xmajor = false;
5925 forceaction = true;
5926 action = ACTION_JUMP;
5927 }
5928 }
5929
5930 #ifdef HAS_KEYBOARD
5931 // Get the keypress here. Note that we will always consume
5932 // the keypress! Our hope is that the previous tests, which
5933 // occur after the awaitEvent, will help us avoid losing
5934 // good keypresses here.
5935 int keypress;
5936
5937 keypress = hamfake_getKeyPress(false);
5938 if (forceaction)
5939 keypress = 0;
5940 else
5941 action = ACTION_NONE;
5942
5943 switch (keypress)
5944 {
5945 case GFX_KEYF1:
5946 case GFX_KEYF2:
5947 case GFX_KEYF3:
5948 case GFX_KEYF4:
5949 case GFX_KEYF5:
5950 case GFX_KEYF6:
5951 case GFX_KEYF7:
5952 case GFX_KEYF8:
5953 case GFX_KEYF9:
5954 case GFX_KEYF10:
5955 case GFX_KEYF11:
5956 case GFX_KEYF12:
5957 case GFX_KEYF13:
5958 case GFX_KEYF14:
5959 case GFX_KEYF15:
5960 // Use the mapped key.
5961 action_unpackStripButton(
5962 glb_globalactionstrip[keypress - GFX_KEYF1],
5963 action, spell);
5964 break;
5965 case 'o':
5966 action = ACTION_OPEN;
5967 break;
5968
5969 case '&':
5970 action = ACTION_WISH;
5971 break;
5972
5973 case 'c':
5974 action = ACTION_CLOSE;
5975 break;
5976
5977 case 'e':
5978 action = ACTION_EAT;
5979 break;
5980
5981 case 'B':
5982 action = ACTION_BREATHE;
5983 break;
5984
5985 case '<':
5986 case '>':
5987 action = ACTION_CLIMB;
5988 break;
5989
5990 case 'S':
5991 action = ACTION_SWAP;
5992 break;
5993
5994 case 'R':
5995 action = ACTION_SLEEP;
5996 break;
5997
5998 case 'q':
5999 action = ACTION_RELEASE;
6000 break;
6001
6002 case 'F':
6003 action = ACTION_FORGET;
6004 break;
6005
6006 case 'f':
6007 action = ACTION_FIRE;
6008 break;
6009
6010 case 'W':
6011 action = ACTION_MOVE;
6012 break;
6013
6014 case 's':
6015 action = ACTION_SEARCH;
6016 break;
6017
6018 case '.':
6019 case '5': // Num pad option.
6020 case 'w':
6021 action = ACTION_WAIT;
6022 break;
6023
6024 case 'r':
6025 action = ACTION_RUN;
6026 break;
6027
6028 case 'x':
6029 action = ACTION_LOOK;
6030 break;
6031
6032 case 'z':
6033 action = ACTION_ZAP;
6034 break;
6035
6036 case 'J':
6037 action = ACTION_JUMP;
6038 break;
6039
6040 case 'm':
6041 case 'X':
6042 action = ACTION_MINIMAP;
6043 break;
6044
6045 case 'C':
6046 action = ACTION_COMMAND;
6047 break;
6048
6049 case 'N':
6050 action = ACTION_NAME;
6051 break;
6052
6053 case '_':
6054 action = ACTION_PRAY;
6055 break;
6056
6057 case '?':
6058 action = ACTION_HELP;
6059 break;
6060 case 'O':
6061 action = ACTION_OPTIONS;
6062 break;
6063
6064 case 'v':
6065 action = ACTION_HISTORY;
6066 break;
6067
6068 case 'V':
6069 action = ACTION_VERBLIST;
6070 break;
6071
6072 case ',':
6073 case 'g':
6074 action = ACTION_PICKUP;
6075 break;
6076
6077 case 'p':
6078 case '\x10': // ^p
6079 msg_restore(true);
6080 break;
6081
6082 case GFX_KEYLMB:
6083 if (processDirectionStylus(dx, dy, walkonly, xmajor,
6084 avatar->canMoveDiabolically()))
6085 {
6086 if (!dx && !dy)
6087 action = ACTION_SEARCH;
6088 else if (walkonly)
6089 action = ACTION_WALK;
6090 }
6091 break;
6092 }
6093 #else
6094 if (!forceaction)
6095 {
6096 for (int i = 0; glb_mapbuttons[i] != NUM_BUTTONS; i++)
6097 {
6098 if (ctrl_hit(glb_mapbuttons[i]))
6099 actionbutton = glb_mapbuttons[i];
6100 }
6101 if (actionbutton != NUM_BUTTONS)
6102 action = glb_actionbuttons[actionbutton];
6103 else
6104 action = ACTION_NONE;
6105
6106 // We do not accept minimap actions at this juncture - they
6107 // are handled outside this loop so we can use the press-and-hold
6108 // paradigm like cool people.
6109 if (action == ACTION_MINIMAP)
6110 action = ACTION_NONE;
6111 // Likewise, safewalk is a toggle
6112 if (action == ACTION_SAFEWALK)
6113 action = ACTION_NONE;
6114
6115 if (ctrl_hit(BUTTON_TOUCH))
6116 {
6117 if (processDirectionStylus(dx, dy, walkonly, xmajor,
6118 avatar->canMoveDiabolically()))
6119 {
6120 if (!dx && !dy)
6121 action = ACTION_SEARCH;
6122 else if (walkonly)
6123 action = ACTION_WALK;
6124 }
6125 }
6126 }
6127 #endif
6128
6129 // If we are set to ACTION_WALK we want to clear out the dx/dy
6130 // appropriately if there is something for us to bump into.
6131 // Monsters do *not* trigger a collision as if they are hostile
6132 // we do want to come to a stop so the user can attack and if
6133 // they are friendly we will swap.
6134 if (action == ACTION_WALK)
6135 {
6136 bool blockdir = true;
6137 if (dx && dy && avatar->canMoveDiabolically())
6138 {
6139 // If we are able to move on diagonal, keep it!
6140 if (avatar->canMoveDelta(dx, dy,
6141 false, false, false))
6142 {
6143 blockdir = false;
6144 }
6145 }
6146 if (!blockdir)
6147 {
6148 }
6149 else if (xmajor)
6150 {
6151 // See if blocked in x. IF so, zero x so we bump in
6152 // dy. If not, zero dy.
6153 if (avatar->canMove(avatar->getX() + dx, avatar->getY(),
6154 false, false, false))
6155 {
6156 dy = 0;
6157 }
6158 else
6159 {
6160 dx = 0;
6161 }
6162 }
6163 else
6164 {
6165 // See if blocked in y. IF so, zero y so we bump in
6166 // dx. If not, zero dx.
6167 if (avatar->canMove(avatar->getX(), avatar->getY() + dy,
6168 false, false, false))
6169 {
6170 dx = 0;
6171 }
6172 else
6173 {
6174 dy = 0;
6175 }
6176 }
6177
6178 // If our direction is zeroed, clear the action
6179 if (!dx && !dy)
6180 action = ACTION_NONE;
6181 }
6182
6183 int buttonsel;
6184 if (action == ACTION_NONE && glbActionBar && styluslock.getbottombutton(buttonsel))
6185 {
6186 action_unpackStripButton(glb_globalactionstrip[buttonsel],
6187 action, spell);
6188 }
6189 if (action == ACTION_NONE && spell == SPELL_NONE)
6190 {
6191 int iact, ispell;
6192 hamfake_externalaction(iact, ispell);
6193 action = (ACTION_NAMES) iact;
6194 spell = (SPELL_NAMES) ispell;
6195 }
6196
6197 // If we now have a valid action or dx/dy, return it!
6198 if (action != ACTION_NONE || dx || dy || spell != SPELL_NONE)
6199 {
6200 return;
6201 }
6202 }
6203 }
6204
6205 #ifdef USE_VIRTUAL_SCREEN
6206
6207 int fake_main();
6208
6209 void *
6210 fakemainCB(void *)
6211 {
6212 fake_main();
6213
6214 return 0;
6215 }
6216
6217 void
6218 ipowder_main(const char *path)
6219 {
6220 static THREAD *powderthread;
6221
6222 // Already started, don't restart!
6223 if (powderthread)
6224 return;
6225
6226 hamfake_setdatapath(path);
6227
6228 powderthread = THREAD::alloc();
6229
6230 powderthread->start(fakemainCB, 0);
6231 }
6232
6233 bool
6234 android_main(const char *path)
6235 {
6236 static THREAD *powderthread;
6237
6238 hamfake_setdatapath(path);
6239
6240 // Already started, continue from where we left off.
6241 if (powderthread)
6242 {
6243 hamfake_clearForceQuit();
6244 hamfake_postResurrect();
6245 return true;
6246 }
6247
6248 powderthread = THREAD::alloc();
6249
6250 powderthread->start(fakemainCB, 0);
6251 hamfake_setdatapath(path);
6252
6253 return false;
6254 }
6255
6256 #define main fake_main
6257
6258 #endif
6259
6260 #ifdef MAPSTATS
6261 void
6262 buildMapStats()
6263 {
6264 glbStressTest = true;
6265 glbMapStats = true;
6266 int maxiter = 10000;
6267 int level = 3;
6268 MAP *map;
6269 FILE *fp;
6270
6271 fp = fopen("mapstats.csv", "wt");
6272
6273 MAP::printStatsHeader(fp);
6274
6275 int i;
6276 for (i = 0; i < maxiter; i++)
6277 {
6278 rebuild_all();
6279 MAP *newlevel;
6280 newlevel = glbCurLevel;
6281 while (newlevel && newlevel->getDepth() != level)
6282 {
6283 if (level < newlevel->getDepth())
6284 newlevel = newlevel->getMapUp();
6285 else
6286 newlevel = newlevel->getMapDown();
6287 }
6288 if (newlevel->printStats(fp))
6289 break;
6290 }
6291
6292 fclose(fp);
6293
6294 if (i == maxiter)
6295 exit(0);
6296 }
6297 #endif
6298
6299 int
6300 main(void)
6301 {
6302 int lastturn = -1;
6303 int howlongdead = 0;
6304
6305 // Initialize button maps:
6306 #ifdef USE_VIRTUAL_SCREEN
6307 glb_actionbuttons[BUTTON_A] = ACTION_NONE;
6308 glb_actionbuttons[BUTTON_B] = ACTION_NONE;
6309 #else
6310 glb_actionbuttons[BUTTON_A] = ACTION_FIRE;
6311 glb_actionbuttons[BUTTON_B] = ACTION_ZAP;
6312 #endif
6313 glb_actionbuttons[BUTTON_R] = ACTION_MINIMAP;
6314 glb_actionbuttons[BUTTON_L] = ACTION_MINIMAP;
6315 glb_actionbuttons[BUTTON_X] = ACTION_VERBLIST;
6316 glb_actionbuttons[BUTTON_Y] = ACTION_INVENTORY;
6317
6318 resetDefaultActionStrip();
6319
6320 rand_init();
6321 gfx_init();
6322 ctrl_init();
6323 msg_init();
6324 item_init();
6325 mob_init();
6326 mobref_init();
6327 speed_init();
6328 ai_init();
6329 name_init();
6330 artifact_init();
6331
6332 // Ensure we are happy with our original tileset
6333 // We do this before hiscore_init as it will invoke loadOptions
6334 // and thus set the users desired tileset.
6335 #ifdef USE_VIRTUAL_SCREEN
6336 #ifdef ANDROID
6337 gfx_settilesetmode(0, 2); // 8 pixel
6338 gfx_settilesetmode(1, 4); // 10 pixel
6339 #else
6340 gfx_settilesetmode(0, 4); // 10 pixel
6341 gfx_settilesetmode(1, 3); // 12 pixel
6342 #endif
6343 #else
6344 gfx_settilesetmode(0, 4); // Akoi Meexx
6345 #endif
6346
6347 hiscore_init();
6348
6349 // Even if we have a valid RNG from hiscore load, or even if we
6350 // don't, we still want to muss up the seed a bit!
6351 // Mix in any other entropy we might have.
6352 #if defined(USING_SDL) || defined(USING_DS)
6353 glbRNGSeed ^= time(0);
6354 #elif defined(_WIN32_WCE)
6355 glbRNGSeed ^= GetTickCount();
6356 #endif
6357 rand_setseed(glbRNGSeed);
6358
6359 #ifdef MAPSTATS
6360 gfx_setmode(0);
6361 buildMapStats();
6362 glbMapStats = false;
6363 glbStressTest = false;
6364 glbWizard = true;
6365 #else
6366
6367 // Run the intro screen.
6368 bool doload = false;
6369
6370 // Start in graphics mode:
6371 gfx_setmode(3);
6372
6373 #ifdef FORCESTRESSTEST
6374 glbStressTest = true;
6375 #else
6376 doload = intro_screen();
6377 #endif
6378
6379 // Switch to tiled mode.
6380 gfx_setmode(0);
6381
6382 {
6383 int y;
6384 for (y = 0; y < 20; y++)
6385 gfx_cleartextline(y);
6386 }
6387
6388 if (doload)
6389 loadGame();
6390 else
6391 {
6392 hiscore_flagnewgame(true);
6393 {
6394 SRAMSTREAM os(false);
6395 hiscore_save(os);
6396 }
6397
6398 rebuild_all();
6399 }
6400 #endif
6401
6402
6403 while(1)
6404 {
6405 if (gfx_isnewframe())
6406 {
6407 ACTION_NAMES action = ACTION_NONE;
6408 SPELL_NAMES spell = SPELL_NONE; // Quick cast a spell!
6409 int dx, dy;
6410
6411 if (glbStressTest)
6412 {
6413 static int phase = 100;
6414 static int totalmoves = 0;
6415 totalmoves++;
6416
6417 if (hamfake_forceQuit())
6418 {
6419 // quit request, run away!
6420 #ifdef ANDROID
6421 // We do want to abandon the stress test.
6422 glbStressTest = false;
6423 // Ensure avatar is dead.
6424 // We don't want players picking up from this point.
6425 killAvatar();
6426 continue;
6427 #else
6428 break;
6429 #endif
6430 }
6431
6432
6433 if (phase <= 0)
6434 {
6435 // printf("Total moves: %d\n", totalmoves);
6436 rebuild_all();
6437
6438 //MOB::getAvatar()->receiveDamage(ATTACK_SPELL_LIGHTNING, 0, 0);
6439 // Allow a little bit longer for the avatar to explore
6440 phase = rand_range(100,1000);
6441 continue;
6442 }
6443 phase--;
6444
6445 // A chance of the avatar doing a zany action
6446 if (MOB::getAvatar() && rand_chance(1))
6447 {
6448 UT_ASSERT(!MOB::getAvatar()->hasIntrinsic(INTRINSIC_DEAD));
6449 performRandomAction(MOB::getAvatar());
6450 }
6451
6452 // Always run the processWorld without avatar movement.
6453 if (MOB::getAvatar())
6454 {
6455 UT_ASSERT(!MOB::getAvatar()->hasIntrinsic(INTRINSIC_DEAD));
6456 if (MOB::getAvatar()->doHeartbeat())
6457 {
6458 // If you lose possesssion during heartbeat
6459 // the avatar flag may be set to null.
6460 if (MOB::getAvatar())
6461 {
6462 UT_ASSERT(!MOB::getAvatar()->hasIntrinsic(INTRINSIC_DEAD));
6463 }
6464 if (MOB::getAvatar() && MOB::getAvatar()->doMovePrequel())
6465 {
6466 UT_ASSERT(!MOB::getAvatar()->hasIntrinsic(INTRINSIC_DEAD));
6467 MOB::getAvatar()->doAI();
6468 }
6469 }
6470 processWorld();
6471 continue;
6472 }
6473 else
6474 {
6475 // Don't waste more than 50 moves after the avatar
6476 // has died
6477 if (phase > 50)
6478 phase = 50;
6479 processWorld();
6480 continue;
6481 }
6482 }
6483
6484 if (hamfake_forceQuit())
6485 {
6486 #ifdef ANDROID
6487 // Only save when we are not in tutorial!
6488 if (MOB::getAvatar() && !glbTutorial)
6489 saveGame(true);
6490 hamfake_softReset();
6491
6492 // We loaded the game, albeit virtually, so
6493 // we must restore the save scum flag.
6494 hiscore_bumpsavecount();
6495 glbCurLevel->refresh();
6496 writeStatusLine();
6497 continue;
6498 #else
6499 // Only save when we are not in tutorial!
6500 if (MOB::getAvatar() && !glbTutorial)
6501 saveGame(false);
6502 break;
6503 #endif
6504 }
6505
6506 // Run the move prequel code. If this return false,
6507 // it means we should skip the move - ie: process heartbeat
6508 // and world and continue.
6509 // We only want to run the move prequel on net new turns,
6510 // otherwise probabilistic failures will be tied to world
6511 // clock not game clock.
6512 if (MOB::getAvatar() &&
6513 lastturn != speed_gettime() &&
6514 !MOB::getAvatar()->doMovePrequel())
6515 {
6516 lastturn = speed_gettime();
6517 MOB::getAvatar()->doHeartbeat();
6518 processWorld();
6519 continue;
6520 }
6521 lastturn = speed_gettime();
6522
6523 // For each button which is assigned to the minimap action,
6524 // check to see if it is pressed.
6525
6526 checkMiniMapButtons();
6527
6528 // Handle free auto-pickup.
6529 if (MOB::isAvatarOnFreshSquare())
6530 {
6531 bool anychange = false;
6532 anychange |= performAutoPickup();
6533 // Rebuild our map. There may have been light changes
6534 // if we picked up torches, for example.
6535 if (anychange)
6536 {
6537 glbCurLevel->updateLights();
6538 glbCurLevel->refresh();
6539 }
6540
6541 // Prompt the user for any auto pickup.
6542 performAutoRead();
6543 if (glbAutoPrompt)
6544 {
6545 performAutoPrompt();
6546 }
6547 MOB::setAvatarOnFreshSquare(false);
6548 }
6549
6550 // Check to see if the avatar is dead, and if the start
6551 // is hit. Then restart.
6552 if (!MOB::getAvatar())
6553 {
6554 bool restart = glbFinishedASave;
6555 #ifdef HAS_KEYBOARD
6556 int key;
6557 // Blocking wait. At least the mouse properly triggers
6558 // keypresses.
6559 key = hamfake_getKeyPress(false);
6560 if (!key)
6561 continue;
6562
6563 if (key == '\n' || key == '\r')
6564 restart = true;
6565 if (key == GFX_KEYLMB)
6566 {
6567 // We want to support pure stylus devices so these
6568 // count as hitting top of screen.
6569 // Literal minded people can HIT the Enter text :>
6570 int sx, sy;
6571 hamfake_getstyluspos(sx, sy);
6572 if (sy < 2*TILEHEIGHT)
6573 restart = true;
6574 }
6575 char *msg = "Hit [Enter] to play again. ";
6576 #else
6577 bool hit = false;
6578 int i;
6579 for (i = BUTTON_UP; i < NUM_BUTTONS; i++)
6580 {
6581 if (ctrl_hit((BUTTONS) i))
6582 {
6583 hit = true;
6584 if (i == BUTTON_START)
6585 restart = true;
6586 if (i == BUTTON_TOUCH)
6587 {
6588 int sx, sy;
6589 hamfake_getstyluspos(sx, sy);
6590 if (sy < 2*TILEHEIGHT)
6591 restart = true;
6592 }
6593 }
6594 }
6595 // Do nothing if no button hit.
6596 if (!hit)
6597 continue;
6598 #ifdef USE_VIRTUAL_SCREEN
6599 const char *msg = "Tap [Here] to play again. ";
6600 #else
6601 const char *msg = "Hit [Start] to play again. ";
6602 #endif
6603 #endif
6604 // Wait for a key if start wasn't hit.
6605 if (!restart)
6606 {
6607 if (!(howlongdead & 63))
6608 {
6609 msg_clear();
6610 msg_report(msg);
6611 }
6612 howlongdead++;
6613 processWorld();
6614 continue;
6615 }
6616
6617 howlongdead = 0;
6618
6619 // Getting here means we have a restart request!
6620 bool doload;
6621 glbFinishedASave = false;
6622
6623 {
6624 int y;
6625 for (y = 0; y < 20; y++)
6626 gfx_cleartextline(y);
6627 }
6628 hamfake_clearKeyboardBuffer();
6629 #ifndef HAS_KEYBOARD
6630 while (ctrl_anyrawpressed() && !hamfake_forceQuit())
6631 {
6632 if (gfx_isnewframe())
6633 continue;
6634 }
6635 #endif
6636
6637 // Reset our inventory position so we start the
6638 // new game with a reasonable location
6639 gfx_setinvcursor(1, 0, false);
6640
6641 doload = intro_screen();
6642
6643 {
6644 int y;
6645 for (y = 0; y < 20; y++)
6646 gfx_cleartextline(y);
6647 }
6648
6649 glbCurLevel->deleteAllMaps();
6650
6651 if (doload)
6652 loadGame();
6653 else
6654 {
6655 hiscore_flagnewgame(true);
6656 {
6657 SRAMSTREAM os(false);
6658 hiscore_save(os);
6659 }
6660
6661 rebuild_all();
6662 }
6663 hamfake_clearKeyboardBuffer();
6664 #ifndef HAS_KEYBOARD
6665 while (ctrl_anyrawpressed())
6666 {
6667 if (gfx_isnewframe() && !hamfake_forceQuit())
6668 continue;
6669 }
6670 #endif
6671 }
6672
6673 if (glbHasJustSearched)
6674 glbSearchCount++;
6675 else
6676 glbSearchCount = 1;
6677
6678 glbHasJustSearched = false;
6679
6680 // All interesting things can be so easily summarized.
6681 processAllInput(action, spell, dx, dy);
6682
6683 // No dir key? No go...
6684 if (!dx && !dy && action == ACTION_NONE && spell == SPELL_NONE)
6685 continue;
6686
6687 // Something will happen, clear the message queue.
6688 msg_clear();
6689
6690 // Check if the avatar is still alive...
6691 if (MOB::getAvatar())
6692 {
6693 bool done = false;
6694
6695 MOB::getAvatar()->flagLastMoveAsOld();
6696 if (action == ACTION_WALK)
6697 {
6698 done = MOB::getAvatar()->actionWalk(dx, dy);
6699 }
6700 else if ((action == ACTION_SWAP) && (dx || dy))
6701 {
6702 done = MOB::getAvatar()->actionSwap(dx, dy);
6703 }
6704 else if ((action == ACTION_JUMP) && (dx || dy))
6705 {
6706 done = MOB::getAvatar()->actionJump(dx, dy);
6707 }
6708 else if (action == ACTION_NONE)
6709 {
6710 // If it is a cast request?
6711 if (spell != SPELL_NONE)
6712 {
6713 done = processAction(ACTION_ZAP, spell, MOB::getAvatar());
6714 }
6715 else
6716 {
6717 // Default to bump.
6718 done = MOB::getAvatar()->actionBump(dx, dy);
6719 }
6720 }
6721 else
6722 {
6723 done = processAction(action,
6724 SPELL_NONE,
6725 MOB::getAvatar());
6726 }
6727 if (!done)
6728 {
6729 writeStatusLine();
6730 continue; // Aborted the action, nothing done.
6731 }
6732 if (MOB::getAvatar())
6733 {
6734 // Actually did a move so we complete the reset
6735 // of the last move direction. This will only
6736 // clear the dx/dy if it is flagged as old.
6737 MOB::getAvatar()->resetLastMoveDirection();
6738 MOB::getAvatar()->doHeartbeat();
6739 }
6740
6741 #if 0
6742 // Give the Avatar some extra experience...
6743 if (MOB::getAvatar())
6744 MOB::getAvatar()->receiveExp(100);
6745 #endif
6746
6747 processWorld();
6748
6749 }
6750 else
6751 {
6752 msg_report("Being dead, you do nothing. ");
6753
6754 processWorld();
6755 }
6756 }
6757 }
6758 hamfake_postShutdown();
6759
6760 return 0;
6761 }
6762
6763