'g'o should not miss items in corners anymore
[k8-i-v-a-n.git] / src / game / command.cpp
blobb8d16a4e31edb89e344e2c96dd5156ff8da6e221
1 /* *
2 * Iter Vehemens ad Necem (IVAN)
3 * Copyright (C) Timo Kiviluoto
4 * Released under the GNU General
5 * Public License
7 * See LICENSING which should be included
8 * along with this file for more details
11 #include <string.h>
12 #include <stdio.h>
14 #include "fesave.h"
15 #include "feparse.h"
16 #include "command.h"
17 #include "char.h"
18 #include "message.h"
19 #include "game.h"
20 #include "stack.h"
21 #include "room.h"
22 #include "god.h"
23 #include "felist.h"
24 #include "iconf.h"
25 #include "bitmap.h"
26 #include "actions.h"
27 #include "itemset.h"
28 #include "worldmap.h"
29 #include "wsquare.h"
30 #include "materia.h"
31 #include "database.h"
32 #include "team.h"
34 #ifdef WIZARD
35 #include "proto.h"
36 #endif
39 #define COMMAND(name) static truth name (character *Char)
42 ////////////////////////////////////////////////////////////////////////////////
43 /* k8 */
44 /* SLOOOOWWW! */
45 static void cstrTrim (char *buf) {
46 char *p = buf, *t;
47 while (*p && *((unsigned char *)(p)) <= ' ') p++;
48 if (!p[0]) { buf[0] = '\0'; return; } /* empty line */
49 if (p > buf) { t = buf; while (*p) *t++ = *p++; }
50 p = buf+strlen(buf)-1;
51 while (p >= buf && *((unsigned char *)(p)) <= ' ') p--;
52 if (p < buf) { buf[0] = '\0'; return; } /* empty line */
53 *(++p) = '\0';
57 ////////////////////////////////////////////////////////////////////////////////
58 command::command(cchar *name, truth (*LinkedFunction)(character *), cchar *Description, char Key1, truth UsableInWilderness, truth WizardModeFunction) :
59 mName(name),
60 LinkedFunction(LinkedFunction),
61 Description(Description),
62 Key1(Key1),
63 UsableInWilderness(UsableInWilderness),
64 WizardModeFunction(WizardModeFunction)
69 ////////////////////////////////////////////////////////////////////////////////
71 static const char *adirNames[9] = {
72 "dir-left-up",
73 "dir-up",
74 "dir-right-up",
75 "dir-left",
76 "dir-right",
77 "dir-left-down",
78 "dir-down",
79 "dir-right-down",
80 "dir-none"
85 /* k8 */
86 void commandsystem::ConfigureKeys () {
87 command *cmd;
88 char buf[512];
89 festring fname;
90 #ifdef LOCAL_SAVES
91 fname << ivanconfig::GetMyDir();
92 #else
93 fname << getenv("HOME");
94 #endif
95 fname << "/.ivan.keys.rc";
96 FILE *fl = fopen(fname.CStr(), "r");
97 if (!fl) return;
98 while (fgets(buf, 510, fl)) {
99 cstrTrim(buf);
100 char k1, i1;
101 if (buf[0] == '=' && buf[1] != '=') {
102 i1 = 1;
103 k1 = 0;
104 } else if (buf[1] != '=') {
105 continue;
106 } else {
107 i1 = 2;
108 k1 = buf[0];
110 for (int f = 0; (cmd = commandsystem::GetCommand(f)) != 0; ++f) {
111 if (!strcmp(buf+i1, cmd->GetName().CStr())) cmd->SetKey(k1);
113 //for (int f = 0; f <= 8; f++) if (!strcmp(buf+i1, adirNames[f])) game::SetNormalMoveKey(f, k1);
115 fclose(fl);
119 void commandsystem::SaveKeys (truth forced) {
120 command *cmd;
121 festring fname;
122 int isWizard = 0;
123 #ifdef LOCAL_SAVES
124 fname << ivanconfig::GetMyDir();
125 #else
126 fname << getenv("HOME");
127 #endif
128 fname << "/.ivan.keys.rc";
129 if (!forced) {
130 if (inputfile::fileExists(fname)) return;
132 FILE *fl = fopen(fname.CStr(), "w");
133 if (!fl) return;
134 for (; isWizard < 2; isWizard++) {
135 fputs(isWizard ? "\n# wizard actions\n" : "# actions\n", fl);
136 for (int f = 0; (cmd = commandsystem::GetCommand(f)) != 0; ++f) {
137 if (cmd->IsWizardModeFunction()) {
138 if (!isWizard) continue;
139 } else {
140 if (isWizard) continue;
142 cchar *dsc = cmd->GetName().CStr();
143 char k1 = cmd->GetKey();
144 if (!dsc || !dsc[0]) continue;
145 fprintf(fl, "%c", (k1 ? k1 : '\x7f'));
146 fprintf(fl, "=%s\n", dsc);
150 fputs("\n# alternate movement\n", fl);
151 for (int f = 0; f <= 8; f++) {
152 char ch = game::GetAbnormalMoveKey(f);
153 if (ch) fputc(ch, fl);
154 fprintf(fl, "=%s\n", adirNames[f]);
157 fclose(fl);
159 /* k8 */
162 ////////////////////////////////////////////////////////////////////////////////
163 std::vector<command *> commandsystem::mCommands;
166 void commandsystem::RegisterCommand (command *cmd) {
167 for (unsigned int f = 0; f < mCommands.size(); ++f) {
168 if (mCommands[f]->GetName() == cmd->GetName()) {
169 // replace command
170 delete mCommands[f];
171 mCommands[f] = cmd;
172 return;
175 mCommands.push_back(cmd);
179 ////////////////////////////////////////////////////////////////////////////////
180 static truth Consume (character* Char, cchar *ConsumeVerb, sorter Sorter) {
181 lsquare *Square = Char->GetLSquareUnder();
182 stack *Inventory = Char->GetStack();
183 stack *StackUnder = Square->GetStack();
184 if ((game::IsInWilderness() || !StackUnder->SortedItems(Char, Sorter)) && !Inventory->SortedItems(Char, Sorter)) {
185 ADD_MESSAGE("You have nothing to %s!", ConsumeVerb);
186 return false;
188 itemvector Item;
189 festring Question = CONST_S("What do you wish to ")+ConsumeVerb+'?';
190 if (!game::IsInWilderness() && StackUnder->SortedItems(Char, Sorter))
191 Inventory->DrawContents(Item, StackUnder, Char, Question, CONST_S("Items in your inventory"), CONST_S("Items on the ground"), CONST_S(""), 0, NO_MULTI_SELECT|SELECT_ZEROPICK_FIRST, Sorter);
192 else
193 Inventory->DrawContents(Item, Char, Question, NO_MULTI_SELECT, Sorter);
194 return !Item.empty() ? Char->ConsumeItem(Item[0], ConsumeVerb+CONST_S("ing")) : false;
198 ////////////////////////////////////////////////////////////////////////////////
199 #include "commands/0list.cpp"
202 commandsystem::commandsystem () {
203 RegisterCommand(new command("Apply", &Apply, "apply", 'a'));
204 RegisterCommand(new command("Talk", &Talk, "chat", 'C'));
205 RegisterCommand(new command("Close", &Close, "close", 'c'));
206 RegisterCommand(new command("Dip", &Dip, "dip", '!'));
207 RegisterCommand(new command("Dump", &Dump, "dump potion/can contents", 'U'));
208 RegisterCommand(new command("Drink", &Drink, "drink", 'D', true));
209 RegisterCommand(new command("Drop", &Drop, "drop", 'd', true));
210 RegisterCommand(new command("Eat", &Eat, "eat", 'e', true));
211 RegisterCommand(new command("WhatToEngrave", &WhatToEngrave, "engrave", 'G'));
212 RegisterCommand(new command("EquipmentScreen", &EquipmentScreen, "equipment menu", 'E', true));
213 RegisterCommand(new command("Go", &Go, "go", 'g'));
214 RegisterCommand(new command("GoDown", &GoDown, "go down/enter area", '>', true));
215 RegisterCommand(new command("GoUp", &GoUp, "go up", '<', true));
216 RegisterCommand(new command("IssueCommand", &IssueCommand, "issue command(s) to team member(s)", 'I'));
217 RegisterCommand(new command("Kick", &Kick, "kick", 'k'));
218 RegisterCommand(new command("Look", &Look, "look", 'l', true));
219 RegisterCommand(new command("AssignName", &AssignName, "name", 'n'));
220 RegisterCommand(new command("Offer", &Offer, "offer", 'O'));
221 RegisterCommand(new command("Open", &Open, "open", 'o'));
222 RegisterCommand(new command("PickUp", &PickUp, "pick up", ','));
223 RegisterCommand(new command("Pray", &Pray, "pray", 'p'));
224 RegisterCommand(new command("Quit", &Quit, "quit", 'Q', true));
225 RegisterCommand(new command("Read", &Read, "read", 'r'));
226 RegisterCommand(new command("Rest", &Rest, "rest/heal", 'h', true));
227 RegisterCommand(new command("Save", &Save, "save game", 'S', true));
228 RegisterCommand(new command("ScrollMessagesDown", &ScrollMessagesDown, "scroll messages down", '=', true));
229 RegisterCommand(new command("ScrollMessagesUp", &ScrollMessagesUp, "scroll messages up", '-', true));
230 RegisterCommand(new command("ShowConfigScreen", &ShowConfigScreen, "show config screen", '\\', true));
231 RegisterCommand(new command("ShowInventory", &ShowInventory, "show inventory", 'i', true));
232 RegisterCommand(new command("ShowKeyLayout", &ShowKeyLayout, "show key layout", '?', true));
233 RegisterCommand(new command("DrawMessageHistory", &DrawMessageHistory, "show message history", 'M', true));
234 RegisterCommand(new command("ShowWeaponSkills", &ShowWeaponSkills, "show weapon skills", '@', true));
235 RegisterCommand(new command("Search", &Search, "search", 's'));
236 RegisterCommand(new command("Sit", &Sit, "sit", '_'));
237 RegisterCommand(new command("Throw", &Throw, "throw", 't'));
238 RegisterCommand(new command("ToggleRunning", &ToggleRunning, "toggle running", 'u', true));
239 RegisterCommand(new command("ForceVomit", &ForceVomit, "vomit", 'V'));
240 RegisterCommand(new command("NOP", &NOP, "wait", '.', true));
241 RegisterCommand(new command("WieldInRightArm", &WieldInRightArm, "wield in right arm", 'w', true));
242 RegisterCommand(new command("WieldInLeftArm", &WieldInLeftArm, "wield in left arm", 'W', true));
243 RegisterCommand(new command("Burn", &Burn, "burn", 'B'));
244 #ifdef WIZARD
245 RegisterCommand(new command("WizardMode", &WizardMode, "wizard mode activation", 'X', true));
246 #endif
247 RegisterCommand(new command("Zap", &Zap, "zap", 'z'));
248 #ifdef WIZARD
249 /* Sort according to key */
250 RegisterCommand(new command("RaiseStats", &RaiseStats, "raise stats", '1', true, true));
251 RegisterCommand(new command("LowerStats", &LowerStats, "lower stats", '2', true, true));
252 RegisterCommand(new command("SeeWholeMap", &SeeWholeMap, "see whole map", '3', true, true));
253 RegisterCommand(new command("WalkThroughWalls", &WalkThroughWalls, "toggle walk through walls mode", '4', true, true));
254 RegisterCommand(new command("RaiseGodRelations", &RaiseGodRelations, "raise your relations to the gods", '5', true, true));
255 RegisterCommand(new command("LowerGodRelations", &LowerGodRelations, "lower your relations to the gods", '6', true, true));
256 RegisterCommand(new command("WizardWish", &WizardWish, "wish something", '`', true, true));
257 RegisterCommand(new command("GainDivineKnowledge", &GainDivineKnowledge, "gain knowledge of all gods", '\"', true, true));
258 RegisterCommand(new command("GainAllItems", &GainAllItems, "gain all items", '$', true, true));
259 RegisterCommand(new command("SecretKnowledge", &SecretKnowledge, "reveal secret knowledge", '*', true, true));
260 RegisterCommand(new command("DetachBodyPart", &DetachBodyPart, "detach a limb", '0', true, true));
261 RegisterCommand(new command("SummonMonster", &SummonMonster, "summon monster", '&', false, true));
262 RegisterCommand(new command("LevelTeleport", &LevelTeleport, "level teleport", '|', false, true));
263 RegisterCommand(new command("Possess", &Possess, "possess creature", '{', false, true));
264 RegisterCommand(new command("Polymorph", &Polymorph, "polymorph", '[', true, true));
265 RegisterCommand(new command("GetScroll", &GetScroll, "get scroll", 'R', true, true));
266 RegisterCommand(new command("OpenMondedr", &OpenMondedr, "open Mondedr", 'N', true, true));
267 RegisterCommand(new command("OpenXinTomb", &OpenXinTomb, "open Xinroch Tomb", 'T', true, true));
268 RegisterCommand(new command("ShowCoords", &ShowCoords, "show current coordinates", '(', true, true));
269 RegisterCommand(new command("WizardHeal", &WizardHeal, "wizard healing", 'H', true, true));
270 RegisterCommand(new command("WizardBlow", &WizardBlow, "wizard blowing", '%', false, true));
271 RegisterCommand(new command("WizardTeam", &WizardTeam, "wizard teaming", '/', false, true));
272 #endif
276 static commandsystem cmdsystem;