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 Person::players[pnum]->setProportions(headprop, bodyprop, armprop, legprop);
90 static void set_protection(int pnum, const char* args)
92 float head, high, low;
93 sscanf(args, "%f%f%f", &head, &high, &low);
95 Person::players[pnum]->protectionhead = head;
96 Person::players[pnum]->protectionhigh = high;
97 Person::players[pnum]->protectionlow = low;
100 static void set_armor(int pnum, const char* args)
102 float head, high, low;
103 sscanf(args, "%f%f%f", &head, &high, &low);
105 Person::players[pnum]->armorhead = head;
106 Person::players[pnum]->armorhigh = high;
107 Person::players[pnum]->armorlow = low;
110 static void set_metal(int pnum, const char* args)
112 float head, high, low;
113 sscanf(args, "%f%f%f", &head, &high, &low);
115 Person::players[pnum]->metalhead = head;
116 Person::players[pnum]->metalhigh = high;
117 Person::players[pnum]->metallow = low;
120 static void set_noclothes(int pnum, const char*)
122 Person::players[pnum]->numclothes = 0;
123 Person::players[pnum]->skeleton.drawmodel.textureptr.load(
124 PersonType::types[Person::players[pnum]->creature].skins[Person::players[pnum]->whichskin], 1,
125 &Person::players[pnum]->skeleton.skinText[0], &Person::players[pnum]->skeleton.skinsize);
128 static void set_clothes(int pnum, const char* args)
131 snprintf(buf, 63, "Textures/%s.png", args);
133 const std::string file_path = Folders::getResourcePath(buf);
135 tfile = fopen(file_path.c_str(), "rb");
137 perror((std::string("Couldn't find file ") + file_path + " to assign as clothes").c_str());
139 // FIXME: Reduce code duplication with GameTick (should come from a Console class)
140 for (int k = 14; k >= 1; k--) {
141 consoletext[k] = consoletext[k - 1];
143 consoletext[0] = std::string("Could not load the requested texture '") + args + "', aborting.";
149 int id = Person::players[pnum]->numclothes;
150 strncpy(Person::players[pnum]->clothes[id], buf, 64);
151 Person::players[pnum]->clothestintr[id] = tintr;
152 Person::players[pnum]->clothestintg[id] = tintg;
153 Person::players[pnum]->clothestintb[id] = tintb;
154 Person::players[pnum]->numclothes++;
156 if (!Person::players[pnum]->addClothes(id)) {
160 Person::players[pnum]->DoMipmaps();
163 static void list_clothes(int pnum)
165 printf("Clothes from player %d:\n", pnum);
166 for (int i = 0; i < Person::players[pnum]->numclothes; i++) {
167 printf("%s (%f %f %f)\n",
168 Person::players[pnum]->clothes[i],
169 Person::players[pnum]->clothestintr[i],
170 Person::players[pnum]->clothestintg[i],
171 Person::players[pnum]->clothestintb[i]);
175 /* Console commands themselves */
177 void ch_quit(const char*)
182 void ch_map(const char* args)
184 if (!LoadLevel(args)) {
185 // FIXME: Reduce code duplication with GameTick (should come from a Console class)
186 for (int k = 14; k >= 1; k--) {
187 consoletext[k] = consoletext[k - 1];
189 consoletext[0] = std::string("Could not load the requested level '") + args + "', aborting.";
196 void ch_save(const char* args)
198 std::string map_path = Folders::getUserDataPath() + "/Maps";
199 Folders::makeDirectory(map_path);
200 map_path = map_path + "/" + args;
205 tfile = fopen(map_path.c_str(), "wb");
207 perror((std::string("Couldn't open file ") + map_path + " for saving").c_str());
210 fpackf(tfile, "Bi", mapvers);
211 fpackf(tfile, "Bi", maptype);
212 fpackf(tfile, "Bi", hostile);
213 fpackf(tfile, "Bf Bf", viewdistance, fadestart);
214 fpackf(tfile, "Bb Bf Bf Bf", skyboxtexture, skyboxr, skyboxg, skyboxb);
215 fpackf(tfile, "Bf Bf Bf", skyboxlightr, skyboxlightg, skyboxlightb);
216 fpackf(tfile, "Bf Bf Bf Bf Bf Bi", Person::players[0]->coords.x, Person::players[0]->coords.y, Person::players[0]->coords.z,
217 Person::players[0]->yaw, Person::players[0]->targetyaw, Person::players[0]->num_weapons);
218 if (Person::players[0]->num_weapons > 0 && Person::players[0]->num_weapons < 5) {
219 for (int j = 0; j < Person::players[0]->num_weapons; j++) {
220 fpackf(tfile, "Bi", weapons[Person::players[0]->weaponids[j]].getType());
224 fpackf(tfile, "Bf Bf Bf", Person::players[0]->armorhead, Person::players[0]->armorhigh, Person::players[0]->armorlow);
225 fpackf(tfile, "Bf Bf Bf", Person::players[0]->protectionhead, Person::players[0]->protectionhigh, Person::players[0]->protectionlow);
226 fpackf(tfile, "Bf Bf Bf", Person::players[0]->metalhead, Person::players[0]->metalhigh, Person::players[0]->metallow);
227 fpackf(tfile, "Bf Bf", Person::players[0]->power, Person::players[0]->speedmult);
229 fpackf(tfile, "Bi", Person::players[0]->numclothes);
231 fpackf(tfile, "Bi Bi", Person::players[0]->whichskin, Person::players[0]->creature);
233 Dialog::saveDialogs(tfile);
235 for (int k = 0; k < Person::players[0]->numclothes; k++) {
236 int templength = strlen(Person::players[0]->clothes[k]);
237 fpackf(tfile, "Bi", templength);
238 for (int l = 0; l < templength; l++) {
239 fpackf(tfile, "Bb", Person::players[0]->clothes[k][l]);
241 fpackf(tfile, "Bf Bf Bf", Person::players[0]->clothestintr[k], Person::players[0]->clothestintg[k], Person::players[0]->clothestintb[k]);
244 fpackf(tfile, "Bi", environment);
246 fpackf(tfile, "Bi", Object::objects.size());
248 for (unsigned int k = 0; k < Object::objects.size(); k++) {
249 fpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", Object::objects[k]->type, Object::objects[k]->yaw, Object::objects[k]->pitch,
250 Object::objects[k]->position.x, Object::objects[k]->position.y, Object::objects[k]->position.z, Object::objects[k]->scale);
253 fpackf(tfile, "Bi", Hotspot::hotspots.size());
254 for (unsigned i = 0; i < Hotspot::hotspots.size(); i++) {
255 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);
256 int templength = strlen(Hotspot::hotspots[i].text);
257 fpackf(tfile, "Bi", templength);
258 for (int l = 0; l < templength; l++) {
259 fpackf(tfile, "Bb", Hotspot::hotspots[i].text[l]);
263 fpackf(tfile, "Bi", Person::players.size());
264 if (Person::players.size() > maxplayers) {
265 cout << "Warning: this level contains more players than allowed" << endl;
267 for (unsigned j = 1; j < Person::players.size(); j++) {
268 fpackf(tfile, "Bi Bi Bf Bf Bf Bi Bi Bf Bb Bf", Person::players[j]->whichskin, Person::players[j]->creature,
269 Person::players[j]->coords.x, Person::players[j]->coords.y, Person::players[j]->coords.z,
270 Person::players[j]->num_weapons, Person::players[j]->howactive, Person::players[j]->scale, Person::players[j]->immobile, Person::players[j]->yaw);
271 if (Person::players[j]->num_weapons < 5) {
272 for (int k = 0; k < Person::players[j]->num_weapons; k++) {
273 fpackf(tfile, "Bi", weapons[Person::players[j]->weaponids[k]].getType());
276 if (Person::players[j]->numwaypoints < 30) {
277 fpackf(tfile, "Bi", Person::players[j]->numwaypoints);
278 for (int k = 0; k < Person::players[j]->numwaypoints; k++) {
279 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].x);
280 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].y);
281 fpackf(tfile, "Bf", Person::players[j]->waypoints[k].z);
282 fpackf(tfile, "Bi", Person::players[j]->waypointtype[k]);
284 fpackf(tfile, "Bi", Person::players[j]->waypoint);
286 Person::players[j]->numwaypoints = 0;
287 Person::players[j]->waypoint = 0;
288 fpackf(tfile, "Bi Bi Bi", Person::players[j]->numwaypoints, Person::players[j]->waypoint, Person::players[j]->waypoint);
291 fpackf(tfile, "Bf Bf Bf", Person::players[j]->armorhead, Person::players[j]->armorhigh, Person::players[j]->armorlow);
292 fpackf(tfile, "Bf Bf Bf", Person::players[j]->protectionhead, Person::players[j]->protectionhigh, Person::players[j]->protectionlow);
293 fpackf(tfile, "Bf Bf Bf", Person::players[j]->metalhead, Person::players[j]->metalhigh, Person::players[j]->metallow);
294 fpackf(tfile, "Bf Bf", Person::players[j]->power, Person::players[j]->speedmult);
295 fpackf(tfile, "Bf Bf Bf Bf", Person::players[j]->getProportion(0), Person::players[j]->getProportion(1), Person::players[j]->getProportion(2), Person::players[j]->getProportion(3));
297 fpackf(tfile, "Bi", Person::players[j]->numclothes);
298 if (Person::players[j]->numclothes) {
299 for (int k = 0; k < Person::players[j]->numclothes; k++) {
301 templength = strlen(Person::players[j]->clothes[k]);
302 fpackf(tfile, "Bi", templength);
303 for (int l = 0; l < templength; l++) {
304 fpackf(tfile, "Bb", Person::players[j]->clothes[k][l]);
306 fpackf(tfile, "Bf Bf Bf", Person::players[j]->clothestintr[k], Person::players[j]->clothestintg[k], Person::players[j]->clothestintb[k]);
311 fpackf(tfile, "Bi", numpathpoints);
312 for (int j = 0; j < numpathpoints; j++) {
313 fpackf(tfile, "Bf Bf Bf Bi", pathpoint[j].x, pathpoint[j].y, pathpoint[j].z, numpathpointconnect[j]);
314 for (int k = 0; k < numpathpointconnect[j]; k++) {
315 fpackf(tfile, "Bi", pathpointconnect[j][k]);
319 fpackf(tfile, "Bf Bf Bf Bf", mapcenter.x, mapcenter.y, mapcenter.z, mapradius);
324 void ch_tint(const char* args)
326 sscanf(args, "%f%f%f", &tintr, &tintg, &tintb);
329 void ch_tintr(const char* args)
334 void ch_tintg(const char* args)
339 void ch_tintb(const char* args)
344 void ch_speed(const char* args)
346 Person::players[0]->speedmult = atof(args);
349 void ch_strength(const char* args)
351 Person::players[0]->power = atof(args);
354 void ch_power(const char* args)
356 Person::players[0]->power = atof(args);
359 void ch_size(const char* args)
361 Person::players[0]->scale = atof(args) * .2;
364 void ch_sizenear(const char* args)
366 int closest = findClosestPlayer();
368 Person::players[closest]->scale = atof(args) * .2;
372 void ch_proportion(const char* args)
374 set_proportion(0, args);
377 void ch_proportionnear(const char* args)
379 int closest = findClosestPlayer();
381 set_proportion(closest, args);
385 void ch_protection(const char* args)
387 set_protection(0, args);
390 void ch_protectionnear(const char* args)
392 int closest = findClosestPlayer();
394 set_protection(closest, args);
398 void ch_armor(const char* args)
403 void ch_armornear(const char* args)
405 int closest = findClosestPlayer();
407 set_armor(closest, args);
411 void ch_protectionreset(const char*)
413 set_protection(0, "1 1 1");
414 set_armor(0, "1 1 1");
417 void ch_metal(const char* args)
422 void ch_noclothes(const char* args)
424 set_noclothes(0, args);
427 void ch_noclothesnear(const char* args)
429 int closest = findClosestPlayer();
431 set_noclothes(closest, args);
435 void ch_clothes(const char* args)
437 set_clothes(0, args);
440 void ch_clothesnear(const char* args)
442 int closest = findClosestPlayer();
444 set_clothes(closest, args);
448 void ch_clotheslist(const char*)
453 void ch_clotheslistnear(const char*)
455 int closest = findClosestPlayer();
457 list_clothes(closest);
461 void ch_belt(const char*)
463 Person::players[0]->skeleton.clothes = !Person::players[0]->skeleton.clothes;
466 void ch_cellophane(const char*)
468 cellophane = !cellophane;
471 void ch_funnybunny(const char*)
473 Person::players[0]->creature = rabbittype;
474 Person::players[0]->skeletonLoad(true);
475 Person::players[0]->scale = .2;
476 Person::players[0]->headless = 0;
477 Person::players[0]->damagetolerance = 200;
478 set_proportion(0, "1 1 1 1");
481 void ch_wolfie(const char*)
483 Person::players[0]->creature = wolftype;
484 Person::players[0]->skeletonLoad();
485 Person::players[0]->scale = .23;
486 Person::players[0]->damagetolerance = 300;
487 set_proportion(0, "1 1 1 1");
490 void ch_lizardwolf(const char*)
492 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/FurWolfLizard.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
495 void ch_darko(const char*)
497 Person::players[0]->skeleton.drawmodel.textureptr.load("Textures/FurDarko.jpg", 1, &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
500 void ch_sizemin(const char*)
502 for (unsigned i = 1; i < Person::players.size(); i++) {
503 if (Person::players[i]->scale < 0.8 * 0.2) {
504 Person::players[i]->scale = 0.8 * 0.2;
509 void ch_tutorial(const char* args)
511 Tutorial::active = atoi(args);
514 void ch_hostile(const char* args)
516 hostile = atoi(args);
519 void ch_type(const char* args)
521 int n = sizeof(editortypenames) / sizeof(editortypenames[0]);
522 for (int i = 0; i < n; i++) {
523 if (stripfx(args, editortypenames[i])) {
530 void ch_path(const char* args)
532 unsigned int n = sizeof(pathtypenames) / sizeof(pathtypenames[0]);
533 for (unsigned int i = 0; i < n; i++) {
534 if (stripfx(args, pathtypenames[i])) {
541 void ch_hs(const char* args)
545 sscanf(args, "%f%d %n", &size, &type, &shift);
547 Hotspot::hotspots.emplace_back(Person::players[0]->coords, type, size);
549 strcpy(Hotspot::hotspots.back().text, args + shift);
550 strcat(Hotspot::hotspots.back().text, "\n");
553 void ch_dialog(const char* args)
558 sscanf(args, "%d %31s", &type, buf1);
559 std::string filename = std::string("Dialogues/") + buf1 + ".txt";
561 Dialog::dialogs.push_back(Dialog(type, filename));
563 Dialog::directing = true;
564 Dialog::indialogue = 0;
565 Dialog::whichdialogue = Dialog::dialogs.size();
568 void ch_fixdialog(const char* args)
573 sscanf(args, "%d %31s", &whichdlg, buf1);
574 std::string filename = std::string("Dialogues/") + buf1 + ".txt";
576 Dialog::dialogs[whichdlg] = Dialog(Dialog::dialogs[whichdlg].type, filename);
579 void ch_fixtype(const char* args)
583 sscanf(args, "%d %d", &whichdlg, &type);
584 Dialog::dialogs[whichdlg].type = type;
587 void ch_fixrotation(const char*)
589 int playerId = Dialog::currentScene().participantfocus;
590 Dialog::currentDialog().participantyaw[playerId] = Person::players[playerId]->yaw;
593 void ch_ddialog(const char* args)
595 if (Dialog::dialogs.empty() || Dialog::inDialog()) {
600 sscanf(args, "%d", &dlg);
603 Dialog::dialogs.pop_back();
607 if (dlg >= int(Dialog::dialogs.size())) {
608 // Invalid index, abort
612 // Erase given index, higher indexes will be decreased by 1
613 Dialog::dialogs.erase(Dialog::dialogs.begin() + dlg);
616 void ch_dhs(const char*)
618 if (!Hotspot::hotspots.empty()) {
619 Hotspot::hotspots.pop_back();
623 void ch_immobile(const char*)
625 Person::players[0]->immobile = 1;
628 void ch_allimmobile(const char*)
630 for (unsigned i = 1; i < Person::players.size(); i++) {
631 Person::players[i]->immobile = 1;
635 void ch_mobile(const char*)
637 Person::players[0]->immobile = 0;
640 void ch_default(const char*)
642 Person::players[0]->armorhead = 1;
643 Person::players[0]->armorhigh = 1;
644 Person::players[0]->armorlow = 1;
645 Person::players[0]->protectionhead = 1;
646 Person::players[0]->protectionhigh = 1;
647 Person::players[0]->protectionlow = 1;
648 Person::players[0]->metalhead = 1;
649 Person::players[0]->metalhigh = 1;
650 Person::players[0]->metallow = 1;
651 Person::players[0]->power = 1;
652 Person::players[0]->speedmult = 1;
653 if (Person::players[0]->creature == wolftype) {
654 Person::players[0]->scale = .23;
655 } else if (Person::players[0]->creature == rabbittype) {
656 Person::players[0]->scale = .2;
659 Person::players[0]->setProportions(1, 1, 1, 1);
661 Person::players[0]->numclothes = 0;
662 Person::players[0]->skeleton.drawmodel.textureptr.load(
663 PersonType::types[Person::players[0]->creature].skins[Person::players[0]->whichskin], 1,
664 &Person::players[0]->skeleton.skinText[0], &Person::players[0]->skeleton.skinsize);
666 editoractive = typeactive;
667 Person::players[0]->immobile = 0;
670 void ch_play(const char* args)
673 sscanf(args, "%d", &dlg);
674 Dialog::whichdialogue = dlg;
676 if (Dialog::whichdialogue >= int(Dialog::dialogs.size())) {
680 Dialog::currentDialog().play();
683 void ch_mapkilleveryone(const char*)
685 maptype = mapkilleveryone;
688 void ch_mapkillmost(const char*)
690 maptype = mapkillmost;
693 void ch_mapkillsomeone(const char*)
695 maptype = mapkillsomeone;
698 void ch_mapgosomewhere(const char*)
700 maptype = mapgosomewhere;
703 void ch_viewdistance(const char* args)
705 viewdistance = atof(args) * 100;
708 void ch_fadestart(const char* args)
710 fadestart = atof(args);
713 void ch_slomo(const char* args)
715 slomospeed = atof(args);
720 void ch_slofreq(const char* args)
722 slomofreq = atof(args);
725 void ch_skytint(const char* args)
727 sscanf(args, "%f%f%f", &skyboxr, &skyboxg, &skyboxb);
729 skyboxlightr = skyboxr;
730 skyboxlightg = skyboxg;
731 skyboxlightb = skyboxb;
739 void ch_skylight(const char* args)
741 sscanf(args, "%f%f%f", &skyboxlightr, &skyboxlightg, &skyboxlightb);
749 void ch_skybox(const char*)
751 skyboxtexture = !skyboxtexture;