2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - 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 int id = Person::players[pnum]->numclothes;
145 strncpy(Person::players[pnum]->clothes[id], buf, 64);
146 Person::players[pnum]->clothestintr[id] = tintr;
147 Person::players[pnum]->clothestintg[id] = tintg;
148 Person::players[pnum]->clothestintb[id] = tintb;
149 Person::players[pnum]->numclothes++;
151 if (!Person::players[pnum]->addClothes(id)) {
155 Person::players[pnum]->DoMipmaps();
158 /* Console commands themselves */
160 void ch_quit(const char*)
165 void ch_map(const char* args)
167 if (!LoadLevel(args)) {
168 // FIXME: Reduce code duplication with GameTick (should come from a Console class)
169 for (int k = 14; k >= 1; k--) {
170 consoletext[k] = consoletext[k - 1];
172 consoletext[0] = std::string("Could not load the requested level '") + args + "', aborting.";
179 void ch_save(const char* args)
181 std::string map_path = Folders::getUserDataPath() + "/Maps";
182 Folders::makeDirectory(map_path);
183 map_path = map_path + "/" + args;
188 tfile = fopen(map_path.c_str(), "wb");
190 perror((std::string("Couldn't open file ") + map_path + " for saving").c_str());
193 fpackf(tfile, "Bi", mapvers);
194 fpackf(tfile, "Bi", maptype);
195 fpackf(tfile, "Bi", hostile);
196 fpackf(tfile, "Bf Bf", viewdistance, fadestart);
197 fpackf(tfile, "Bb Bf Bf Bf", skyboxtexture, skyboxr, skyboxg, skyboxb);
198 fpackf(tfile, "Bf Bf Bf", skyboxlightr, skyboxlightg, skyboxlightb);
199 fpackf(tfile, "Bf Bf Bf Bf Bf Bi", Person::players[0]->coords.x, Person::players[0]->coords.y, Person::players[0]->coords.z,
200 Person::players[0]->yaw, Person::players[0]->targetyaw, Person::players[0]->num_weapons);
201 if (Person::players[0]->num_weapons > 0 && Person::players[0]->num_weapons < 5) {
202 for (int j = 0; j < Person::players[0]->num_weapons; j++) {
203 fpackf(tfile, "Bi", weapons[Person::players[0]->weaponids[j]].getType());
207 fpackf(tfile, "Bf Bf Bf", Person::players[0]->armorhead, Person::players[0]->armorhigh, Person::players[0]->armorlow);
208 fpackf(tfile, "Bf Bf Bf", Person::players[0]->protectionhead, Person::players[0]->protectionhigh, Person::players[0]->protectionlow);
209 fpackf(tfile, "Bf Bf Bf", Person::players[0]->metalhead, Person::players[0]->metalhigh, Person::players[0]->metallow);
210 fpackf(tfile, "Bf Bf", Person::players[0]->power, Person::players[0]->speedmult);
212 fpackf(tfile, "Bi", Person::players[0]->numclothes);
214 fpackf(tfile, "Bi Bi", Person::players[0]->whichskin, Person::players[0]->creature);
216 Dialog::saveDialogs(tfile);
218 for (int k = 0; k < Person::players[0]->numclothes; k++) {
219 int templength = strlen(Person::players[0]->clothes[k]);
220 fpackf(tfile, "Bi", templength);
221 for (int l = 0; l < templength; l++) {
222 fpackf(tfile, "Bb", Person::players[0]->clothes[k][l]);
224 fpackf(tfile, "Bf Bf Bf", Person::players[0]->clothestintr[k], Person::players[0]->clothestintg[k], Person::players[0]->clothestintb[k]);
227 fpackf(tfile, "Bi", environment);
229 fpackf(tfile, "Bi", Object::objects.size());
231 for (unsigned int k = 0; k < Object::objects.size(); k++) {
232 fpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", Object::objects[k]->type, Object::objects[k]->yaw, Object::objects[k]->pitch,
233 Object::objects[k]->position.x, Object::objects[k]->position.y, Object::objects[k]->position.z, Object::objects[k]->scale);
236 fpackf(tfile, "Bi", Hotspot::hotspots.size());
237 for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
238 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);
239 int templength = strlen(Hotspot::hotspots[i].text);
240 fpackf(tfile, "Bi", templength);
241 for (int l = 0; l < templength; l++) {
242 fpackf(tfile, "Bb", Hotspot::hotspots[i].text[l]);
246 fpackf(tfile, "Bi", Person::players.size());
247 if (Person::players.size() > maxplayers) {
248 cout << "Warning: this level contains more players than allowed" << endl;
250 for (unsigned j = 1; j < Person::players.size(); j++) {
251 fpackf(tfile, "Bi Bi Bf Bf Bf Bi Bi Bf Bb Bf", Person::players[j]->whichskin, Person::players[j]->creature,
252 Person::players[j]->coords.x, Person::players[j]->coords.y, Person::players[j]->coords.z,
253 Person::players[j]->num_weapons, Person::players[j]->howactive, Person::players[j]->scale, Person::players[j]->immobile, Person::players[j]->yaw);
254 if (Person::players[j]->num_weapons < 5) {
255 for (int k = 0; k < Person::players[j]->num_weapons; k++) {
256 fpackf(tfile, "Bi", weapons[Person::players[j]->weaponids[k]].getType());
259 if (Person::players[j]->numwaypoints < 30) {
260 fpackf(tfile, "Bi", Person::players[j]->numwaypoints);
261 for (int k = 0; k < Person::players[j]->numwaypoints; k++) {
262 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].x);
263 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].y);
264 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].z);
265 fpackf(tfile, "Bi", Person::players[j]->waypointtype[k]);
267 fpackf(tfile, "Bi", Person::players[j]->waypoint);
269 Person::players[j]->numwaypoints = 0;
270 Person::players[j]->waypoint = 0;
271 fpackf(tfile, "Bi Bi Bi", Person::players[j]->numwaypoints, Person::players[j]->waypoint, Person::players[j]->waypoint);
274 fpackf(tfile, "Bf Bf Bf", Person::players[j]->armorhead, Person::players[j]->armorhigh, Person::players[j]->armorlow);
275 fpackf(tfile, "Bf Bf Bf", Person::players[j]->protectionhead, Person::players[j]->protectionhigh, Person::players[j]->protectionlow);
276 fpackf(tfile, "Bf Bf Bf", Person::players[j]->metalhead, Person::players[j]->metalhigh, Person::players[j]->metallow);
277 fpackf(tfile, "Bf Bf", Person::players[j]->power, Person::players[j]->speedmult);
279 float headprop, bodyprop, armprop, legprop;
280 if (Person::players[j]->creature == wolftype) {
281 headprop = Person::players[j]->proportionhead.x / 1.1;
282 bodyprop = Person::players[j]->proportionbody.x / 1.1;
283 armprop = Person::players[j]->proportionarms.x / 1.1;
284 legprop = Person::players[j]->proportionlegs.x / 1.1;
287 headprop = Person::players[j]->proportionhead.x / 1.2;
288 bodyprop = Person::players[j]->proportionbody.x / 1.05;
289 armprop = Person::players[j]->proportionarms.x / 1.00;
290 legprop = Person::players[j]->proportionlegs.x / 1.1;
293 fpackf(tfile, "Bf Bf Bf Bf", headprop, bodyprop, armprop, legprop);
295 fpackf(tfile, "Bi", Person::players[j]->numclothes);
296 if (Person::players[j]->numclothes) {
297 for (int k = 0; k < Person::players[j]->numclothes; k++) {
299 templength = strlen(Person::players[j]->clothes[k]);
300 fpackf(tfile, "Bi", templength);
301 for (int l = 0; l < templength; l++) {
302 fpackf(tfile, "Bb", Person::players[j]->clothes[k][l]);
304 fpackf(tfile, "Bf Bf Bf", Person::players[j]->clothestintr[k], Person::players[j]->clothestintg[k], Person::players[j]->clothestintb[k]);
309 fpackf(tfile, "Bi", numpathpoints);
310 for (int j = 0; j < numpathpoints; j++) {
311 fpackf(tfile, "Bf Bf Bf Bi", pathpoint[j].x, pathpoint[j].y, pathpoint[j].z, numpathpointconnect[j]);
312 for (int k = 0; k < numpathpointconnect[j]; k++) {
313 fpackf(tfile, "Bi", pathpointconnect[j][k]);
317 fpackf(tfile, "Bf Bf Bf Bf", mapcenter.x, mapcenter.y, mapcenter.z, mapradius);
322 void ch_cellar(const char*)
324 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Furdarko.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
327 void ch_tint(const char* args)
329 sscanf(args, "%f%f%f", &tintr, &tintg, &tintb);
332 void ch_tintr(const char* args)
337 void ch_tintg(const char* args)
342 void ch_tintb(const char* args)
347 void ch_speed(const char* args)
349 Person::players[0]->speedmult = atof(args);
352 void ch_strength(const char* args)
354 Person::players[0]->power = atof(args);
357 void ch_power(const char* args)
359 Person::players[0]->power = atof(args);
362 void ch_size(const char* args)
364 Person::players[0]->scale = atof(args) * .2;
367 void ch_sizenear(const char* args)
369 int closest = findClosestPlayer();
371 Person::players[closest]->scale = atof(args) * .2;
375 void ch_proportion(const char* args)
377 set_proportion(0, args);
380 void ch_proportionnear(const char* args)
382 int closest = findClosestPlayer();
384 set_proportion(closest, args);
388 void ch_protection(const char* args)
390 set_protection(0, args);
393 void ch_protectionnear(const char* args)
395 int closest = findClosestPlayer();
397 set_protection(closest, args);
401 void ch_armor(const char* args)
406 void ch_armornear(const char* args)
408 int closest = findClosestPlayer();
410 set_armor(closest, args);
414 void ch_protectionreset(const char*)
416 set_protection(0, "1 1 1");
417 set_armor(0, "1 1 1");
420 void ch_metal(const char* args)
425 void ch_noclothes(const char* args)
427 set_noclothes(0, args);
430 void ch_noclothesnear(const char* args)
432 int closest = findClosestPlayer();
434 set_noclothes(closest, args);
438 void ch_clothes(const char* args)
440 set_clothes(0, args);
443 void ch_clothesnear(const char* args)
445 int closest = findClosestPlayer();
447 set_clothes(closest, args);
451 void ch_belt(const char*)
453 Person::players[0]->skeleton.clothes = !Person::players[0]->skeleton.clothes;
456 void ch_cellophane(const char*)
458 cellophane = !cellophane;
459 float mul = (cellophane ? 0 : 1);
461 for (auto player : Person::players) {
462 player->proportionhead.z = player->proportionhead.x * mul;
463 player->proportionbody.z = player->proportionbody.x * mul;
464 player->proportionarms.z = player->proportionarms.x * mul;
465 player->proportionlegs.z = player->proportionlegs.x * mul;
469 void ch_funnybunny(const char*)
471 Person::players[0]->creature = rabbittype;
472 Person::players[0]->skeletonLoad(true);
473 Person::players[0]->scale = .2;
474 Person::players[0]->headless = 0;
475 Person::players[0]->damagetolerance = 200;
476 set_proportion(0, "1 1 1 1");
479 void ch_wolfie(const char*)
481 Person::players[0]->creature = wolftype;
482 Person::players[0]->skeletonLoad();
483 Person::players[0]->damagetolerance = 300;
484 set_proportion(0, "1 1 1 1");
487 void ch_wolfieisgod(const char* args)
492 void ch_wolf(const char*)
494 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Wolf.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
497 void ch_snowwolf(const char*)
499 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/SnowWolf.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
502 void ch_darkwolf(const char*)
504 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/DarkWolf.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
507 void ch_lizardwolf(const char*)
509 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/LizardWolf.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
512 void ch_white(const char*)
514 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Fur.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
517 void ch_brown(const char*)
519 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Fur3.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
522 void ch_black(const char*)
524 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/Fur2.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
527 void ch_sizemin(const char*)
529 for (unsigned i = 1; i < Person::players.size(); i++) {
530 if (Person::players[i]->scale < 0.8 * 0.2) {
531 Person::players[i]->scale = 0.8 * 0.2;
536 void ch_tutorial(const char* args)
538 Tutorial::active = atoi(args);
541 void ch_hostile(const char* args)
543 hostile = atoi(args);
546 void ch_type(const char* args)
548 int n = sizeof(editortypenames) / sizeof(editortypenames[0]);
549 for (int i = 0; i < n; i++) {
550 if (stripfx(args, editortypenames[i])) {
557 void ch_path(const char* args)
559 unsigned int n = sizeof(pathtypenames) / sizeof(pathtypenames[0]);
560 for (unsigned int i = 0; i < n; i++) {
561 if (stripfx(args, pathtypenames[i])) {
568 void ch_hs(const char* args)
572 sscanf(args, "%f%d %n", &size, &type, &shift);
574 Hotspot::hotspots.emplace_back(Person::players[0]->coords, type, size);
576 strcpy(Hotspot::hotspots.back().text, args + shift);
577 strcat(Hotspot::hotspots.back().text, "\n");
580 void ch_dialogue(const char* args)
585 sscanf(args, "%d %31s", &type, buf1);
586 std::string filename = std::string("Dialogues/") + buf1 + ".txt";
588 Dialog::dialogs.push_back(Dialog(type, filename));
590 Dialog::directing = true;
591 Dialog::indialogue = 0;
592 Dialog::whichdialogue = Dialog::dialogs.size();
595 void ch_fixdialogue(const char* args)
600 sscanf(args, "%d %31s", &whichdi, buf1);
601 std::string filename = std::string("Dialogues/") + buf1 + ".txt";
603 Dialog::dialogs[whichdi] = Dialog(Dialog::dialogs[whichdi].type, filename);
606 void ch_fixtype(const char* args)
609 sscanf(args, "%d", &dlg);
610 Dialog::dialogs[0].type = dlg;
613 void ch_fixrotation(const char*)
615 int playerId = Dialog::currentScene().participantfocus;
616 Dialog::currentDialog().participantyaw[playerId] = Person::players[playerId]->yaw;
619 void ch_ddialogue(const char*)
621 if (!Dialog::dialogs.empty()) {
622 Dialog::dialogs.pop_back();
626 void ch_dhs(const char*)
628 if (!Hotspot::hotspots.empty()) {
629 Hotspot::hotspots.pop_back();
633 void ch_immobile(const char*)
635 Person::players[0]->immobile = 1;
638 void ch_allimmobile(const char*)
640 for (unsigned i = 1; i < Person::players.size(); i++) {
641 Person::players[i]->immobile = 1;
645 void ch_mobile(const char*)
647 Person::players[0]->immobile = 0;
650 void ch_default(const char*)
652 Person::players[0]->armorhead = 1;
653 Person::players[0]->armorhigh = 1;
654 Person::players[0]->armorlow = 1;
655 Person::players[0]->protectionhead = 1;
656 Person::players[0]->protectionhigh = 1;
657 Person::players[0]->protectionlow = 1;
658 Person::players[0]->metalhead = 1;
659 Person::players[0]->metalhigh = 1;
660 Person::players[0]->metallow = 1;
661 Person::players[0]->power = 1;
662 Person::players[0]->speedmult = 1;
663 Person::players[0]->scale = 1;
665 if (Person::players[0]->creature == wolftype) {
666 Person::players[0]->proportionhead = 1.1;
667 Person::players[0]->proportionbody = 1.1;
668 Person::players[0]->proportionarms = 1.1;
669 Person::players[0]->proportionlegs = 1.1;
670 } else if (Person::players[0]->creature == rabbittype) {
671 Person::players[0]->proportionhead = 1.2;
672 Person::players[0]->proportionbody = 1.05;
673 Person::players[0]->proportionarms = 1.00;
674 Person::players[0]->proportionlegs = 1.1;
675 Person::players[0]->proportionlegs.y = 1.05;
678 Person::players[0]->numclothes = 0;
679 Person::players[0]->skeleton.drawmodel.textureptr.load(
680 creatureskin[Person::players[0]->creature][Person::players[0]->whichskin], 1,
681 &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
683 editoractive = typeactive;
684 Person::players[0]->immobile = 0;
687 void ch_play(const char* args)
690 sscanf(args, "%d", &dlg);
691 Dialog::whichdialogue = dlg;
693 if (Dialog::whichdialogue >= int(Dialog::dialogs.size())) {
697 Dialog::currentDialog().play();
700 void ch_mapkilleveryone(const char*)
702 maptype = mapkilleveryone;
705 void ch_mapkillmost(const char*)
707 maptype = mapkillmost;
710 void ch_mapkillsomeone(const char*)
712 maptype = mapkillsomeone;
715 void ch_mapgosomewhere(const char*)
717 maptype = mapgosomewhere;
720 void ch_viewdistance(const char* args)
722 viewdistance = atof(args) * 100;
725 void ch_fadestart(const char* args)
727 fadestart = atof(args);
730 void ch_slomo(const char* args)
732 slomospeed = atof(args);
737 void ch_slofreq(const char* args)
739 slomofreq = atof(args);
742 void ch_skytint(const char* args)
744 sscanf(args, "%f%f%f", &skyboxr, &skyboxg, &skyboxb);
746 skyboxlightr = skyboxr;
747 skyboxlightg = skyboxg;
748 skyboxlightb = skyboxb;
756 void ch_skylight(const char* args)
758 sscanf(args, "%f%f%f", &skyboxlightr, &skyboxlightg, &skyboxlightb);
766 void ch_skybox(const char*)
768 skyboxtexture = !skyboxtexture;