moved almost all hardcoded constants to "define.dat"
[k8-i-v-a-n.git] / src / game / command.cpp
blob3fe991b7b83a3be308ac383ccbe92bf84eb3d41c
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 "miscitem.h"
28 #include "worldmap.h"
29 #include "wsquare.h"
30 #include "wterras.h"
31 #include "materia.h"
32 #include "database.h"
33 #include "team.h"
35 #ifdef WIZARD
36 #include "proto.h"
37 #endif
40 #define COMMAND(name) static truth name (character *Char)
43 ////////////////////////////////////////////////////////////////////////////////
44 /* k8 */
45 /* SLOOOOWWW! */
46 static void cstrTrim (char *buf) {
47 char *p = buf, *t;
48 while (*p && *((unsigned char *)(p)) <= ' ') p++;
49 if (!p[0]) { buf[0] = '\0'; return; } /* empty line */
50 if (p > buf) { t = buf; while (*p) *t++ = *p++; }
51 p = buf+strlen(buf)-1;
52 while (p >= buf && *((unsigned char *)(p)) <= ' ') p--;
53 if (p < buf) { buf[0] = '\0'; return; } /* empty line */
54 *(++p) = '\0';
58 ////////////////////////////////////////////////////////////////////////////////
59 command::command(cchar *name, truth (*LinkedFunction)(character *), cchar *Description, char Key1, char Key2, truth UsableInWilderness, truth WizardModeFunction) :
60 mName(name),
61 LinkedFunction(LinkedFunction),
62 Description(Description),
63 Key1(Key1), Key2(Key2),
64 UsableInWilderness(UsableInWilderness),
65 WizardModeFunction(WizardModeFunction)
70 char command::GetKey () const { return !ivanconfig::GetUseAlternativeKeys() ? Key1 : Key2; }
73 ////////////////////////////////////////////////////////////////////////////////
74 static const char *adirNames[9] = {
75 "dir-left-up",
76 "dir-up",
77 "dir-right-up",
78 "dir-left",
79 "dir-right",
80 "dir-left-down",
81 "dir-down",
82 "dir-right-down",
83 "dir-none"
87 /* k8 */
88 void commandsystem::ConfigureKeys () {
89 command *cmd;
90 char buf[512];
91 festring fname;
92 #ifdef LOCAL_SAVES
93 fname << ivanconfig::GetMyDir();
94 #else
95 fname << getenv("HOME");
96 #endif
97 fname << "/.ivan.keys.rc";
98 FILE *fl = fopen(fname.CStr(), "r");
99 if (!fl) return;
100 while (fgets(buf, 510, fl)) {
101 cstrTrim(buf);
102 char k1 = 0, k2 = 0, i1;
103 if (buf[0] == '=') {
104 i1 = 1;
105 } else if (buf[2] != '=') {
106 if (buf[1] != '=') continue;
107 i1 = 2;
108 k1 = buf[0];
109 } else {
110 i1 = 3;
111 k1 = buf[0];
112 k2 = buf[1];
114 for (int f = 0; (cmd = commandsystem::GetCommand(f)) != 0; ++f) {
115 if (!strcmp(buf+i1, cmd->GetName().CStr())) cmd->SetKeys(k1, k2);
117 for (int f = 0; f <= 8; f++) if (!strcmp(buf+i1, adirNames[f])) game::SetAbnormalMoveKey(f, k1);
119 fclose(fl);
123 void commandsystem::SaveKeys (truth forced) {
124 command *cmd;
125 festring fname;
126 int isWizard = 0;
127 #ifdef LOCAL_SAVES
128 fname << ivanconfig::GetMyDir();
129 #else
130 fname << getenv("HOME");
131 #endif
132 fname << "/.ivan.keys.rc";
133 if (!forced) {
134 if (inputfile::fileExists(fname)) return;
136 FILE *fl = fopen(fname.CStr(), "w");
137 if (!fl) return;
138 for (; isWizard < 2; isWizard++) {
139 fputs(isWizard ? "\n# wizard actions\n" : "# actions\n", fl);
140 for (int f = 0; (cmd = commandsystem::GetCommand(f)) != 0; ++f) {
141 if (cmd->IsWizardModeFunction()) {
142 if (!isWizard) continue;
143 } else {
144 if (isWizard) continue;
146 cchar *dsc = cmd->GetName().CStr();
147 char k1 = cmd->GetKey1(), k2 = cmd->GetKey2();
148 if (!dsc || !dsc[0]) continue;
149 if (k1 && k2 && k1 != k2) fprintf(fl, "%c%c", k1, k2);
150 else if (k1) fprintf(fl, "%c", k1);
151 else if (k2) fprintf(fl, "%c", k2);
152 fprintf(fl, "=%s\n", dsc);
155 fputs("\n# alternate movement\n", fl);
156 for (int f = 0; f <= 8; f++) {
157 char ch = game::GetAbnormalMoveKey(f);
158 if (ch) fputc(ch, fl);
159 fprintf(fl, "=%s\n", adirNames[f]);
161 fclose(fl);
163 /* k8 */
166 ////////////////////////////////////////////////////////////////////////////////
167 std::vector<command *> commandsystem::mCommands;
170 void commandsystem::RegisterCommand (command *cmd) {
171 for (unsigned int f = 0; f < mCommands.size(); ++f) {
172 if (mCommands[f]->GetName() == cmd->GetName()) {
173 // replace command
174 delete mCommands[f];
175 mCommands[f] = cmd;
176 return;
179 mCommands.push_back(cmd);
183 ////////////////////////////////////////////////////////////////////////////////
184 static truth Consume (character* Char, cchar *ConsumeVerb, sorter Sorter) {
185 lsquare *Square = Char->GetLSquareUnder();
186 stack *Inventory = Char->GetStack();
187 stack *StackUnder = Square->GetStack();
188 if ((game::IsInWilderness() || !StackUnder->SortedItems(Char, Sorter)) && !Inventory->SortedItems(Char, Sorter)) {
189 ADD_MESSAGE("You have nothing to %s!", ConsumeVerb);
190 return false;
192 itemvector Item;
193 festring Question = CONST_S("What do you wish to ")+ConsumeVerb+'?';
194 if (!game::IsInWilderness() && StackUnder->SortedItems(Char, Sorter))
195 Inventory->DrawContents(Item, StackUnder, Char, Question, CONST_S("Items in your inventory"), CONST_S("Items on the ground"), CONST_S(""), 0, NO_MULTI_SELECT, Sorter);
196 else
197 Inventory->DrawContents(Item, Char, Question, NO_MULTI_SELECT, Sorter);
198 return !Item.empty() ? Char->ConsumeItem(Item[0], ConsumeVerb+CONST_S("ing")) : false;
202 ////////////////////////////////////////////////////////////////////////////////
203 #include "commands/0list.cpp"
206 commandsystem::commandsystem () {
207 RegisterCommand(new command("Apply", &Apply, "apply", 'a', 'a', false));
208 RegisterCommand(new command("Talk", &Talk, "chat", 'C', 'C', false));
209 RegisterCommand(new command("Close", &Close, "close", 'c', 'c', false));
210 RegisterCommand(new command("Dip", &Dip, "dip", '!', '!', false));
211 RegisterCommand(new command("Dump", &Dump, "dump potion/can contents", 'U', 'U', false));
212 RegisterCommand(new command("Drink", &Drink, "drink", 'D', 'D', true));
213 RegisterCommand(new command("Drop", &Drop, "drop", 'd', 'd', true));
214 RegisterCommand(new command("Eat", &Eat, "eat", 'e', 'e', true));
215 RegisterCommand(new command("WhatToEngrave", &WhatToEngrave, "engrave", 'G', 'G', false));
216 RegisterCommand(new command("EquipmentScreen", &EquipmentScreen, "equipment menu", 'E', 'E', true));
217 RegisterCommand(new command("Go", &Go, "go", 'g', 'g', false));
218 RegisterCommand(new command("GoDown", &GoDown, "go down/enter area", '>', '>', true));
219 RegisterCommand(new command("GoUp", &GoUp, "go up", '<', '<', true));
220 RegisterCommand(new command("IssueCommand", &IssueCommand, "issue command(s) to team member(s)", 'I', 'I', false));
221 RegisterCommand(new command("Kick", &Kick, "kick", 'k', 'K', false));
222 RegisterCommand(new command("Look", &Look, "look", 'l', 'L', true));
223 RegisterCommand(new command("AssignName", &AssignName, "name", 'n', 'n', false));
224 RegisterCommand(new command("Offer", &Offer, "offer", 'O', 'f', false));
225 RegisterCommand(new command("Open", &Open, "open", 'o', 'O', false));
226 RegisterCommand(new command("PickUp", &PickUp, "pick up", ',', ',', false));
227 RegisterCommand(new command("Pray", &Pray, "pray", 'p', 'p', false));
228 RegisterCommand(new command("Quit", &Quit, "quit", 'Q', 'Q', true));
229 RegisterCommand(new command("Read", &Read, "read", 'r', 'r', false));
230 RegisterCommand(new command("Rest", &Rest, "rest/heal", 'h', 'h', true));
231 RegisterCommand(new command("Save", &Save, "save game", 'S', 'S', true));
232 RegisterCommand(new command("ScrollMessagesDown", &ScrollMessagesDown, "scroll messages down", '+', '+', true));
233 RegisterCommand(new command("ScrollMessagesUp", &ScrollMessagesUp, "scroll messages up", '-', '-', true));
234 RegisterCommand(new command("ShowConfigScreen", &ShowConfigScreen, "show config screen", '\\', '\\', true));
235 RegisterCommand(new command("ShowInventory", &ShowInventory, "show inventory", 'i', 'i', true));
236 RegisterCommand(new command("ShowKeyLayout", &ShowKeyLayout, "show key layout", '?', '?', true));
237 RegisterCommand(new command("DrawMessageHistory", &DrawMessageHistory, "show message history", 'M', 'M', true));
238 RegisterCommand(new command("ShowWeaponSkills", &ShowWeaponSkills, "show weapon skills", '@', '@', true));
239 RegisterCommand(new command("Search", &Search, "search", 's', 's', false));
240 RegisterCommand(new command("Sit", &Sit, "sit", '_', '_', false));
241 RegisterCommand(new command("Throw", &Throw, "throw", 't', 't', false));
242 RegisterCommand(new command("ToggleRunning", &ToggleRunning, "toggle running", 'u', 'U', true));
243 RegisterCommand(new command("ForceVomit", &ForceVomit, "vomit", 'V', 'V', false));
244 RegisterCommand(new command("NOP", &NOP, "wait", '.', '.', true));
245 RegisterCommand(new command("WieldInRightArm", &WieldInRightArm, "wield in right arm", 'w', 'w', true));
246 RegisterCommand(new command("WieldInLeftArm", &WieldInLeftArm, "wield in left arm", 'W', 'W', true));
247 RegisterCommand(new command("Burn", &Burn, "burn", 'B', 'B', false));
248 #ifdef WIZARD
249 RegisterCommand(new command("WizardMode", &WizardMode, "wizard mode activation", 'X', 'X', true));
250 #endif
251 RegisterCommand(new command("Zap", &Zap, "zap", 'z', 'z', false));
252 #ifdef WIZARD
253 /* Sort according to key */
254 RegisterCommand(new command("RaiseStats", &RaiseStats, "raise stats", '1', '1', true, true));
255 RegisterCommand(new command("LowerStats", &LowerStats, "lower stats", '2', '2', true, true));
256 RegisterCommand(new command("SeeWholeMap", &SeeWholeMap, "see whole map", '3', '3', true, true));
257 RegisterCommand(new command("WalkThroughWalls", &WalkThroughWalls, "toggle walk through walls mode", '4', '4', true, true));
258 RegisterCommand(new command("RaiseGodRelations", &RaiseGodRelations, "raise your relations to the gods", '5', '5', true, true));
259 RegisterCommand(new command("LowerGodRelations", &LowerGodRelations, "lower your relations to the gods", '6', '6', true, true));
260 RegisterCommand(new command("WizardWish", &WizardWish, "wish something", '=', '=', true, true));
261 RegisterCommand(new command("GainDivineKnowledge", &GainDivineKnowledge, "gain knowledge of all gods", '\"', '\"', true, true));
262 RegisterCommand(new command("GainAllItems", &GainAllItems, "gain all items", '$', '$', true, true));
263 RegisterCommand(new command("SecretKnowledge", &SecretKnowledge, "reveal secret knowledge", '*', '*', true, true));
264 RegisterCommand(new command("DetachBodyPart", &DetachBodyPart, "detach a limb", '0', '0', true, true));
265 RegisterCommand(new command("SummonMonster", &SummonMonster, "summon monster", '&', '&', false, true));
266 RegisterCommand(new command("LevelTeleport", &LevelTeleport, "level teleport", '|', '|', false, true));
267 RegisterCommand(new command("Possess", &Possess, "possess creature", '{', '{', false, true));
268 RegisterCommand(new command("Polymorph", &Polymorph, "polymorph", '[', '[', true, true));
269 RegisterCommand(new command("GetScroll", &GetScroll, "get scroll", 'R', 'R', true, true));
270 RegisterCommand(new command("OpenMondedr", &OpenMondedr, "open Mondedr", 'M', 'M', true, true));
271 RegisterCommand(new command("ShowCoords", &ShowCoords, "show current coordinates", '(', '(', true, true));
272 RegisterCommand(new command("WizardHeal", &WizardHeal, "wizard healing", 'H', 'H', true, true));
273 RegisterCommand(new command("WizardBlow", &WizardBlow, "wizard blowing", '%', '%', false, true));
274 RegisterCommand(new command("WizardTeam", &WizardTeam, "wizard teaming", '/', '/', false, true));
275 #endif
279 static commandsystem cmdsystem;