2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
21 #include "Devtools/ConsoleCmds.hpp"
24 #include "Level/Dialog.hpp"
25 #include "Level/Hotspot.hpp"
26 #include "Tutorial.hpp"
27 #include "Utils/Folders.hpp"
29 const char* cmd_names[cmd_count] = {
30 #define DECLARE_COMMAND(cmd) #cmd,
31 #include "ConsoleCmds.def"
32 #undef DECLARE_COMMAND
35 console_handler cmd_handlers[cmd_count] = {
36 #define DECLARE_COMMAND(cmd) ch_##cmd,
37 #include "ConsoleCmds.def"
38 #undef DECLARE_COMMAND
46 extern bool cellophane;
47 extern int editoractive;
48 extern int editorpathtype;
49 extern int environment;
50 extern float fadestart;
51 extern float slomospeed;
52 extern float slomofreq;
56 extern float slomodelay;
57 extern bool skyboxtexture;
61 extern float skyboxlightr;
62 extern float skyboxlightg;
63 extern float skyboxlightb;
64 extern Terrain terrain;
65 extern float viewdistance;
67 /* defined in GameTick.cpp */
69 extern int whichlevel;
71 float tintr = 1, tintg = 1, tintb = 1;
73 /* Helpers used in console commands */
75 /* Return true if PFX is a prefix of STR (case-insensitive). */
76 static bool stripfx(const char* str, const char* pfx)
78 return !strncasecmp(str, pfx, strlen(pfx));
81 static void set_proportion(int pnum, const char* args)
83 float headprop, bodyprop, armprop, legprop;
85 sscanf(args, "%f%f%f%f", &headprop, &bodyprop, &armprop, &legprop);
87 if (Person::players[pnum]->creature == wolftype) {
88 Person::players[pnum]->proportionhead = 1.1 * headprop;
89 Person::players[pnum]->proportionbody = 1.1 * bodyprop;
90 Person::players[pnum]->proportionarms = 1.1 * armprop;
91 Person::players[pnum]->proportionlegs = 1.1 * legprop;
92 } else if (Person::players[pnum]->creature == rabbittype) {
93 Person::players[pnum]->proportionhead = 1.2 * headprop;
94 Person::players[pnum]->proportionbody = 1.05 * bodyprop;
95 Person::players[pnum]->proportionarms = 1.00 * armprop;
96 Person::players[pnum]->proportionlegs = 1.1 * legprop;
97 Person::players[pnum]->proportionlegs.y = 1.05 * legprop;
101 static void set_protection(int pnum, const char* args)
103 float head, high, low;
104 sscanf(args, "%f%f%f", &head, &high, &low);
106 Person::players[pnum]->protectionhead = head;
107 Person::players[pnum]->protectionhigh = high;
108 Person::players[pnum]->protectionlow = low;
111 static void set_armor(int pnum, const char* args)
113 float head, high, low;
114 sscanf(args, "%f%f%f", &head, &high, &low);
116 Person::players[pnum]->armorhead = head;
117 Person::players[pnum]->armorhigh = high;
118 Person::players[pnum]->armorlow = low;
121 static void set_metal(int pnum, const char* args)
123 float head, high, low;
124 sscanf(args, "%f%f%f", &head, &high, &low);
126 Person::players[pnum]->metalhead = head;
127 Person::players[pnum]->metalhigh = high;
128 Person::players[pnum]->metallow = low;
131 static void set_noclothes(int pnum, const char*)
133 Person::players[pnum]->numclothes = 0;
134 Person::players[pnum]->skeleton.drawmodel.textureptr.load(
135 creatureskin[Person::players[pnum]->creature][Person::players[pnum]->whichskin], 1,
136 &Person::players[pnum]->skeleton.skinText[0], &Person::players[pnum]->skeleton.skinsize);
139 static void set_clothes(int pnum, const char* args)
142 snprintf(buf, 63, "Textures/%s.png", args);
144 const std::string file_path = Folders::getResourcePath(buf);
146 tfile = fopen(file_path.c_str(), "rb");
148 perror((std::string("Couldn't find file ") + file_path + " to assign as clothes").c_str());
150 // FIXME: Reduce code duplication with GameTick (should come from a Console class)
151 for (int k = 14; k >= 1; k--) {
152 consoletext[k] = consoletext[k - 1];
154 consoletext[0] = std::string("Could not load the requested texture '") + args + "', aborting.";
160 int id = Person::players[pnum]->numclothes;
161 strncpy(Person::players[pnum]->clothes[id], buf, 64);
162 Person::players[pnum]->clothestintr[id] = tintr;
163 Person::players[pnum]->clothestintg[id] = tintg;
164 Person::players[pnum]->clothestintb[id] = tintb;
165 Person::players[pnum]->numclothes++;
167 if (!Person::players[pnum]->addClothes(id)) {
171 Person::players[pnum]->DoMipmaps();
174 /* Console commands themselves */
176 void ch_quit(const char*)
181 void ch_map(const char* args)
183 if (!LoadLevel(args)) {
184 // FIXME: Reduce code duplication with GameTick (should come from a Console class)
185 for (int k = 14; k >= 1; k--) {
186 consoletext[k] = consoletext[k - 1];
188 consoletext[0] = std::string("Could not load the requested level '") + args + "', aborting.";
195 void ch_save(const char* args)
197 std::string map_path = Folders::getUserDataPath() + "/Maps";
198 Folders::makeDirectory(map_path);
199 map_path = map_path + "/" + args;
204 tfile = fopen(map_path.c_str(), "wb");
206 perror((std::string("Couldn't open file ") + map_path + " for saving").c_str());
209 fpackf(tfile, "Bi", mapvers);
210 fpackf(tfile, "Bi", maptype);
211 fpackf(tfile, "Bi", hostile);
212 fpackf(tfile, "Bf Bf", viewdistance, fadestart);
213 fpackf(tfile, "Bb Bf Bf Bf", skyboxtexture, skyboxr, skyboxg, skyboxb);
214 fpackf(tfile, "Bf Bf Bf", skyboxlightr, skyboxlightg, skyboxlightb);
215 fpackf(tfile, "Bf Bf Bf Bf Bf Bi", Person::players[0]->coords.x, Person::players[0]->coords.y, Person::players[0]->coords.z,
216 Person::players[0]->yaw, Person::players[0]->targetyaw, Person::players[0]->num_weapons);
217 if (Person::players[0]->num_weapons > 0 && Person::players[0]->num_weapons < 5) {
218 for (int j = 0; j < Person::players[0]->num_weapons; j++) {
219 fpackf(tfile, "Bi", weapons[Person::players[0]->weaponids[j]].getType());
223 fpackf(tfile, "Bf Bf Bf", Person::players[0]->armorhead, Person::players[0]->armorhigh, Person::players[0]->armorlow);
224 fpackf(tfile, "Bf Bf Bf", Person::players[0]->protectionhead, Person::players[0]->protectionhigh, Person::players[0]->protectionlow);
225 fpackf(tfile, "Bf Bf Bf", Person::players[0]->metalhead, Person::players[0]->metalhigh, Person::players[0]->metallow);
226 fpackf(tfile, "Bf Bf", Person::players[0]->power, Person::players[0]->speedmult);
228 fpackf(tfile, "Bi", Person::players[0]->numclothes);
230 fpackf(tfile, "Bi Bi", Person::players[0]->whichskin, Person::players[0]->creature);
232 Dialog::saveDialogs(tfile);
234 for (int k = 0; k < Person::players[0]->numclothes; k++) {
235 int templength = strlen(Person::players[0]->clothes[k]);
236 fpackf(tfile, "Bi", templength);
237 for (int l = 0; l < templength; l++) {
238 fpackf(tfile, "Bb", Person::players[0]->clothes[k][l]);
240 fpackf(tfile, "Bf Bf Bf", Person::players[0]->clothestintr[k], Person::players[0]->clothestintg[k], Person::players[0]->clothestintb[k]);
243 fpackf(tfile, "Bi", environment);
245 fpackf(tfile, "Bi", Object::objects.size());
247 for (unsigned int k = 0; k < Object::objects.size(); k++) {
248 fpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", Object::objects[k]->type, Object::objects[k]->yaw, Object::objects[k]->pitch,
249 Object::objects[k]->position.x, Object::objects[k]->position.y, Object::objects[k]->position.z, Object::objects[k]->scale);
252 fpackf(tfile, "Bi", Hotspot::hotspots.size());
253 for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
254 fpackf(tfile, "Bi Bf Bf Bf Bf", Hotspot::hotspots[i].type, Hotspot::hotspots[i].size, Hotspot::hotspots[i].position.x, Hotspot::hotspots[i].position.y, Hotspot::hotspots[i].position.z);
255 int templength = strlen(Hotspot::hotspots[i].text);
256 fpackf(tfile, "Bi", templength);
257 for (int l = 0; l < templength; l++) {
258 fpackf(tfile, "Bb", Hotspot::hotspots[i].text[l]);
262 fpackf(tfile, "Bi", Person::players.size());
263 if (Person::players.size() > maxplayers) {
264 cout << "Warning: this level contains more players than allowed" << endl;
266 for (unsigned j = 1; j < Person::players.size(); j++) {
267 fpackf(tfile, "Bi Bi Bf Bf Bf Bi Bi Bf Bb Bf", Person::players[j]->whichskin, Person::players[j]->creature,
268 Person::players[j]->coords.x, Person::players[j]->coords.y, Person::players[j]->coords.z,
269 Person::players[j]->num_weapons, Person::players[j]->howactive, Person::players[j]->scale, Person::players[j]->immobile, Person::players[j]->yaw);
270 if (Person::players[j]->num_weapons < 5) {
271 for (int k = 0; k < Person::players[j]->num_weapons; k++) {
272 fpackf(tfile, "Bi", weapons[Person::players[j]->weaponids[k]].getType());
275 if (Person::players[j]->numwaypoints < 30) {
276 fpackf(tfile, "Bi", Person::players[j]->numwaypoints);
277 for (int k = 0; k < Person::players[j]->numwaypoints; k++) {
278 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].x);
279 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].y);
280 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].z);
281 fpackf(tfile, "Bi", Person::players[j]->waypointtype[k]);
283 fpackf(tfile, "Bi", Person::players[j]->waypoint);
285 Person::players[j]->numwaypoints = 0;
286 Person::players[j]->waypoint = 0;
287 fpackf(tfile, "Bi Bi Bi", Person::players[j]->numwaypoints, Person::players[j]->waypoint, Person::players[j]->waypoint);
290 fpackf(tfile, "Bf Bf Bf", Person::players[j]->armorhead, Person::players[j]->armorhigh, Person::players[j]->armorlow);
291 fpackf(tfile, "Bf Bf Bf", Person::players[j]->protectionhead, Person::players[j]->protectionhigh, Person::players[j]->protectionlow);
292 fpackf(tfile, "Bf Bf Bf", Person::players[j]->metalhead, Person::players[j]->metalhigh, Person::players[j]->metallow);
293 fpackf(tfile, "Bf Bf", Person::players[j]->power, Person::players[j]->speedmult);
295 float headprop, bodyprop, armprop, legprop;
296 if (Person::players[j]->creature == wolftype) {
297 headprop = Person::players[j]->proportionhead.x / 1.1;
298 bodyprop = Person::players[j]->proportionbody.x / 1.1;
299 armprop = Person::players[j]->proportionarms.x / 1.1;
300 legprop = Person::players[j]->proportionlegs.x / 1.1;
303 headprop = Person::players[j]->proportionhead.x / 1.2;
304 bodyprop = Person::players[j]->proportionbody.x / 1.05;
305 armprop = Person::players[j]->proportionarms.x / 1.00;
306 legprop = Person::players[j]->proportionlegs.x / 1.1;
309 fpackf(tfile, "Bf Bf Bf Bf", headprop, bodyprop, armprop, legprop);
311 fpackf(tfile, "Bi", Person::players[j]->numclothes);
312 if (Person::players[j]->numclothes) {
313 for (int k = 0; k < Person::players[j]->numclothes; k++) {
315 templength = strlen(Person::players[j]->clothes[k]);
316 fpackf(tfile, "Bi", templength);
317 for (int l = 0; l < templength; l++) {
318 fpackf(tfile, "Bb", Person::players[j]->clothes[k][l]);
320 fpackf(tfile, "Bf Bf Bf", Person::players[j]->clothestintr[k], Person::players[j]->clothestintg[k], Person::players[j]->clothestintb[k]);
325 fpackf(tfile, "Bi", numpathpoints);
326 for (int j = 0; j < numpathpoints; j++) {
327 fpackf(tfile, "Bf Bf Bf Bi", pathpoint[j].x, pathpoint[j].y, pathpoint[j].z, numpathpointconnect[j]);
328 for (int k = 0; k < numpathpointconnect[j]; k++) {
329 fpackf(tfile, "Bi", pathpointconnect[j][k]);
333 fpackf(tfile, "Bf Bf Bf Bf", mapcenter.x, mapcenter.y, mapcenter.z, mapradius);
338 void ch_cellar(const char*)
340 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Furdarko.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
343 void ch_tint(const char* args)
345 sscanf(args, "%f%f%f", &tintr, &tintg, &tintb);
348 void ch_tintr(const char* args)
353 void ch_tintg(const char* args)
358 void ch_tintb(const char* args)
363 void ch_speed(const char* args)
365 Person::players[0]->speedmult = atof(args);
368 void ch_strength(const char* args)
370 Person::players[0]->power = atof(args);
373 void ch_power(const char* args)
375 Person::players[0]->power = atof(args);
378 void ch_size(const char* args)
380 Person::players[0]->scale = atof(args) * .2;
383 void ch_sizenear(const char* args)
385 int closest = findClosestPlayer();
387 Person::players[closest]->scale = atof(args) * .2;
391 void ch_proportion(const char* args)
393 set_proportion(0, args);
396 void ch_proportionnear(const char* args)
398 int closest = findClosestPlayer();
400 set_proportion(closest, args);
404 void ch_protection(const char* args)
406 set_protection(0, args);
409 void ch_protectionnear(const char* args)
411 int closest = findClosestPlayer();
413 set_protection(closest, args);
417 void ch_armor(const char* args)
422 void ch_armornear(const char* args)
424 int closest = findClosestPlayer();
426 set_armor(closest, args);
430 void ch_protectionreset(const char*)
432 set_protection(0, "1 1 1");
433 set_armor(0, "1 1 1");
436 void ch_metal(const char* args)
441 void ch_noclothes(const char* args)
443 set_noclothes(0, args);
446 void ch_noclothesnear(const char* args)
448 int closest = findClosestPlayer();
450 set_noclothes(closest, args);
454 void ch_clothes(const char* args)
456 set_clothes(0, args);
459 void ch_clothesnear(const char* args)
461 int closest = findClosestPlayer();
463 set_clothes(closest, args);
467 void ch_belt(const char*)
469 Person::players[0]->skeleton.clothes = !Person::players[0]->skeleton.clothes;
472 void ch_cellophane(const char*)
474 cellophane = !cellophane;
475 float mul = (cellophane ? 0 : 1);
477 for (auto player : Person::players) {
478 player->proportionhead.z = player->proportionhead.x * mul;
479 player->proportionbody.z = player->proportionbody.x * mul;
480 player->proportionarms.z = player->proportionarms.x * mul;
481 player->proportionlegs.z = player->proportionlegs.x * mul;
485 void ch_funnybunny(const char*)
487 Person::players[0]->creature = rabbittype;
488 Person::players[0]->skeletonLoad(true);
489 Person::players[0]->scale = .2;
490 Person::players[0]->headless = 0;
491 Person::players[0]->damagetolerance = 200;
492 set_proportion(0, "1 1 1 1");
495 void ch_wolfie(const char*)
497 Person::players[0]->creature = wolftype;
498 Person::players[0]->skeletonLoad();
499 Person::players[0]->damagetolerance = 300;
500 set_proportion(0, "1 1 1 1");
503 void ch_wolfieisgod(const char* args)
508 void ch_wolf(const char*)
510 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Wolf.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
513 void ch_snowwolf(const char*)
515 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/SnowWolf.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
518 void ch_darkwolf(const char*)
520 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/DarkWolf.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
523 void ch_lizardwolf(const char*)
525 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/LizardWolf.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
528 void ch_white(const char*)
530 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Fur.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
533 void ch_brown(const char*)
535 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Fur3.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
538 void ch_black(const char*)
540 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Fur2.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
543 void ch_sizemin(const char*)
545 for (unsigned i = 1; i < Person::players.size(); i++) {
546 if (Person::players[i]->scale < 0.8 * 0.2) {
547 Person::players[i]->scale = 0.8 * 0.2;
552 void ch_tutorial(const char* args)
554 Tutorial::active = atoi(args);
557 void ch_hostile(const char* args)
559 hostile = atoi(args);
562 void ch_type(const char* args)
564 int n = sizeof(editortypenames) / sizeof(editortypenames[0]);
565 for (int i = 0; i < n; i++) {
566 if (stripfx(args, editortypenames[i])) {
573 void ch_path(const char* args)
575 unsigned int n = sizeof(pathtypenames) / sizeof(pathtypenames[0]);
576 for (unsigned int i = 0; i < n; i++) {
577 if (stripfx(args, pathtypenames[i])) {
584 void ch_hs(const char* args)
588 sscanf(args, "%f%d %n", &size, &type, &shift);
590 Hotspot::hotspots.emplace_back(Person::players[0]->coords, type, size);
592 strcpy(Hotspot::hotspots.back().text, args + shift);
593 strcat(Hotspot::hotspots.back().text, "\n");
596 void ch_dialogue(const char* args)
601 sscanf(args, "%d %31s", &type, buf1);
602 std::string filename = std::string("Dialogues/") + buf1 + ".txt";
604 Dialog::dialogs.push_back(Dialog(type, filename));
606 Dialog::directing = true;
607 Dialog::indialogue = 0;
608 Dialog::whichdialogue = Dialog::dialogs.size();
611 void ch_fixdialogue(const char* args)
616 sscanf(args, "%d %31s", &whichdi, buf1);
617 std::string filename = std::string("Dialogues/") + buf1 + ".txt";
619 Dialog::dialogs[whichdi] = Dialog(Dialog::dialogs[whichdi].type, filename);
622 void ch_fixtype(const char* args)
625 sscanf(args, "%d", &dlg);
626 Dialog::dialogs[0].type = dlg;
629 void ch_fixrotation(const char*)
631 int playerId = Dialog::currentScene().participantfocus;
632 Dialog::currentDialog().participantyaw[playerId] = Person::players[playerId]->yaw;
635 void ch_ddialogue(const char*)
637 if (!Dialog::dialogs.empty()) {
638 Dialog::dialogs.pop_back();
642 void ch_dhs(const char*)
644 if (!Hotspot::hotspots.empty()) {
645 Hotspot::hotspots.pop_back();
649 void ch_immobile(const char*)
651 Person::players[0]->immobile = 1;
654 void ch_allimmobile(const char*)
656 for (unsigned i = 1; i < Person::players.size(); i++) {
657 Person::players[i]->immobile = 1;
661 void ch_mobile(const char*)
663 Person::players[0]->immobile = 0;
666 void ch_default(const char*)
668 Person::players[0]->armorhead = 1;
669 Person::players[0]->armorhigh = 1;
670 Person::players[0]->armorlow = 1;
671 Person::players[0]->protectionhead = 1;
672 Person::players[0]->protectionhigh = 1;
673 Person::players[0]->protectionlow = 1;
674 Person::players[0]->metalhead = 1;
675 Person::players[0]->metalhigh = 1;
676 Person::players[0]->metallow = 1;
677 Person::players[0]->power = 1;
678 Person::players[0]->speedmult = 1;
679 Person::players[0]->scale = 1;
681 if (Person::players[0]->creature == wolftype) {
682 Person::players[0]->proportionhead = 1.1;
683 Person::players[0]->proportionbody = 1.1;
684 Person::players[0]->proportionarms = 1.1;
685 Person::players[0]->proportionlegs = 1.1;
686 } else if (Person::players[0]->creature == rabbittype) {
687 Person::players[0]->proportionhead = 1.2;
688 Person::players[0]->proportionbody = 1.05;
689 Person::players[0]->proportionarms = 1.00;
690 Person::players[0]->proportionlegs = 1.1;
691 Person::players[0]->proportionlegs.y = 1.05;
694 Person::players[0]->numclothes = 0;
695 Person::players[0]->skeleton.drawmodel.textureptr.load(
696 creatureskin[Person::players[0]->creature][Person::players[0]->whichskin], 1,
697 &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
699 editoractive = typeactive;
700 Person::players[0]->immobile = 0;
703 void ch_play(const char* args)
706 sscanf(args, "%d", &dlg);
707 Dialog::whichdialogue = dlg;
709 if (Dialog::whichdialogue >= int(Dialog::dialogs.size())) {
713 Dialog::currentDialog().play();
716 void ch_mapkilleveryone(const char*)
718 maptype = mapkilleveryone;
721 void ch_mapkillmost(const char*)
723 maptype = mapkillmost;
726 void ch_mapkillsomeone(const char*)
728 maptype = mapkillsomeone;
731 void ch_mapgosomewhere(const char*)
733 maptype = mapgosomewhere;
736 void ch_viewdistance(const char* args)
738 viewdistance = atof(args) * 100;
741 void ch_fadestart(const char* args)
743 fadestart = atof(args);
746 void ch_slomo(const char* args)
748 slomospeed = atof(args);
753 void ch_slofreq(const char* args)
755 slomofreq = atof(args);
758 void ch_skytint(const char* args)
760 sscanf(args, "%f%f%f", &skyboxr, &skyboxg, &skyboxb);
762 skyboxlightr = skyboxr;
763 skyboxlightg = skyboxg;
764 skyboxlightb = skyboxb;
772 void ch_skylight(const char* args)
774 sscanf(args, "%f%f%f", &skyboxlightr, &skyboxlightg, &skyboxlightb);
782 void ch_skybox(const char*)
784 skyboxtexture = !skyboxtexture;