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