add non-functional c1 Hatchery
[openc2e.git] / Engine.cpp
blob811c788a9c7c64153a8a9f3fdec89e73a7823301
1 /*
2 * Engine.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Tue Nov 28 2006.
6 * Copyright (c) 2006-2008 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "Room.h"
21 #include "Engine.h"
22 #include "World.h"
23 #include "MetaRoom.h"
24 #include "caosVM.h" // for setupCommandPointers()
25 #include "caosScript.h" // for executeNetwork()
26 #include "PointerAgent.h"
27 #include "dialect.h" // registerDelegates
28 #include "NullBackend.h"
29 #include "NullAudioBackend.h"
30 #include "SFCFile.h"
32 #include <boost/filesystem/path.hpp>
33 #include <boost/filesystem/operations.hpp>
34 #include <boost/filesystem/convenience.hpp>
35 #include <boost/program_options.hpp>
36 #include <boost/format.hpp>
37 namespace fs = boost::filesystem;
38 namespace po = boost::program_options;
40 #ifndef _WIN32
41 #include <sys/types.h> // passwd*
42 #include <pwd.h> // getpwuid
43 #endif
45 #ifdef _WIN32
46 #include <shlobj.h>
47 #include <windows.h>
48 #endif
50 Engine engine;
52 Engine::Engine() {
53 done = false;
54 dorendering = true;
55 fastticks = false;
56 refreshdisplay = false;
58 bmprenderer = false;
60 tickdata = 0;
61 for (unsigned int i = 0; i < 10; i++) ticktimes[i] = 0;
62 ticktimeptr = 0;
63 version = 0; // TODO: something something
65 srand(time(NULL)); // good a place as any :)
67 cmdline_enable_sound = true;
68 cmdline_norun = false;
70 palette = 0;
72 addPossibleBackend("null", shared_ptr<Backend>(new NullBackend()));
73 addPossibleAudioBackend("null", shared_ptr<AudioBackend>(new NullAudioBackend()));
76 Engine::~Engine() {
79 void Engine::addPossibleBackend(std::string s, boost::shared_ptr<Backend> b) {
80 assert(!backend);
81 assert(b);
82 preferred_backend = s;
83 possible_backends[s] = b;
86 void Engine::addPossibleAudioBackend(std::string s, boost::shared_ptr<AudioBackend> b) {
87 assert(!audio);
88 assert(b);
89 preferred_audiobackend = s;
90 possible_audiobackends[s] = b;
93 void Engine::setBackend(shared_ptr<Backend> b) {
94 backend = b;
95 lasttimestamp = backend->ticks();
97 // load palette for C1
98 if (world.gametype == "c1") {
99 // TODO: case-sensitivity for the lose
100 fs::path palpath(world.data_directories[0] / "/Palettes/palette.dta");
101 if (fs::exists(palpath) && !fs::is_directory(palpath)) {
102 palette = new unsigned char[768];
104 std::ifstream f(palpath.native_directory_string().c_str(), std::ios::binary);
105 f >> std::noskipws;
106 f.read((char *)palette, 768);
108 for (unsigned int i = 0; i < 768; i++) {
109 palette[i] = palette[i] * 4;
112 backend->setPalette((uint8 *)palette);
113 } else
114 throw creaturesException("Couldn't find C1 palette data!");
118 std::string Engine::executeNetwork(std::string in) {
119 // now parse and execute the CAOS we obtained
120 caosVM vm(0); // needs to be outside 'try' so we can reset outputstream on exception
121 try {
122 std::istringstream s(in);
123 caosScript script(world.gametype, "<network>"); // XXX
124 script.parse(s);
125 script.installScripts();
126 std::ostringstream o;
127 vm.setOutputStream(o);
128 vm.runEntirely(script.installer);
129 vm.outputstream = 0; // otherwise would point to dead stack
130 return o.str();
131 } catch (std::exception &e) {
132 vm.outputstream = 0; // otherwise would point to dead stack
133 return std::string("### EXCEPTION: ") + e.what();
137 bool Engine::needsUpdate() {
138 return (!world.paused) && (fastticks || (backend->ticks() > (tickdata + world.ticktime)));
141 unsigned int Engine::msUntilTick() {
142 if (fastticks) return 0;
143 if (world.paused) return world.ticktime; // TODO: correct?
145 int ival = (tickdata + world.ticktime) - backend->ticks();
146 return (ival < 0) ? 0 : ival;
149 void Engine::update() {
150 tickdata = backend->ticks();
152 // tick the world
153 world.tick();
155 // draw the world
156 if (dorendering || refreshdisplay) {
157 refreshdisplay = false;
159 if (backend->selfRender()) {
160 // TODO: this makes race/pace hilariously inaccurate, since render time isn't included
161 backend->requestRender();
162 } else {
163 world.drawWorld();
167 // play C1 music
168 // TODO: this doesn't seem to actually be every 7 seconds, but actually somewhat random
169 // TODO: this should be linked to 'real' time, so it doesn't go crazy when game speed is modified
170 // TODO: is this the right place for this?
171 if (version == 1 && (world.tickcount % 70) == 0) {
172 int piece = 1 + (rand() % 28);
173 std::string filename = boost::str(boost::format("MU%02d") % piece);
174 boost::shared_ptr<AudioSource> s = world.playAudio(filename, AgentRef(), false, false, true);
175 if (s) s->setVolume(0.4f);
178 // update our data for things like pace, race, ticktime, etc
179 ticktimes[ticktimeptr] = backend->ticks() - tickdata;
180 ticktimeptr++;
181 if (ticktimeptr == 10) ticktimeptr = 0;
182 float avgtime = 0;
183 for (unsigned int i = 0; i < 10; i++) avgtime += ((float)ticktimes[i] / world.ticktime);
184 world.pace = avgtime / 10;
186 world.race = backend->ticks() - lasttimestamp;
187 lasttimestamp = backend->ticks();
190 bool Engine::tick() {
191 assert(backend);
192 backend->handleEvents();
194 // tick+draw the world, if necessary
195 bool needupdate = needsUpdate();
196 if (needupdate)
197 update();
199 processEvents();
200 if (needupdate)
201 handleKeyboardScrolling();
203 return needupdate;
206 void Engine::handleKeyboardScrolling() {
207 // keyboard-based scrolling
208 static float accelspeed = 8, decelspeed = .5, maxspeed = 64;
209 static float velx = 0;
210 static float vely = 0;
212 bool wasdMode = false;
213 caosVar v = world.variables["engine_wasd"];
214 if (v.hasInt()) {
215 switch (v.getInt()) {
216 case 1: // enable if CTRL is held
217 wasdMode = backend->keyDown(17); // CTRL
218 break;
219 case 2: // enable unconditionally
220 // (this needs agent support to suppress chat bubbles etc)
221 wasdMode = true;
222 break;
223 case 0: // disable
224 wasdMode = false;
225 break;
226 default: // disable
227 std::cout << "Warning: engine_wasd_scrolling is set to unknown value " << v.getInt() << std::endl;
228 world.variables["engine_wasd_scrolling"] = caosVar(0);
229 wasdMode = false;
230 break;
234 // check keys
235 bool leftdown = backend->keyDown(37)
236 || (wasdMode && a_down);
237 bool rightdown = backend->keyDown(39)
238 || (wasdMode && d_down);
239 bool updown = backend->keyDown(38)
240 || (wasdMode && w_down);
241 bool downdown = backend->keyDown(40)
242 || (wasdMode && s_down);
244 if (leftdown)
245 velx -= accelspeed;
246 if (rightdown)
247 velx += accelspeed;
248 if (!leftdown && !rightdown) {
249 velx *= decelspeed;
250 if (fabs(velx) < 0.1) velx = 0;
252 if (updown)
253 vely -= accelspeed;
254 if (downdown)
255 vely += accelspeed;
256 if (!updown && !downdown) {
257 vely *= decelspeed;
258 if (fabs(vely) < 0.1) vely = 0;
261 // enforced maximum speed
262 if (velx >= maxspeed) velx = maxspeed;
263 else if (velx <= -maxspeed) velx = -maxspeed;
264 if (vely >= maxspeed) vely = maxspeed;
265 else if (vely <= -maxspeed) vely = -maxspeed;
267 // do the actual movement
268 if (velx || vely) {
269 int adjustx = world.camera.getX(), adjusty = world.camera.getY();
270 int adjustbyx = (int)velx, adjustbyy = (int) vely;
272 world.camera.moveTo(adjustx + adjustbyx, adjusty + adjustbyy, jump);
276 void Engine::processEvents() {
277 SomeEvent event;
278 while (backend->pollEvent(event)) {
279 switch (event.type) {
280 case eventresizewindow:
281 handleResizedWindow(event);
282 break;
284 case eventmousemove:
285 handleMouseMove(event);
286 break;
288 case eventmousebuttonup:
289 case eventmousebuttondown:
290 handleMouseButton(event);
291 break;
293 case eventkeydown:
294 handleKeyDown(event);
295 break;
297 case eventspecialkeydown:
298 handleSpecialKeyDown(event);
299 break;
301 case eventspecialkeyup:
302 handleSpecialKeyUp(event);
303 break;
305 case eventquit:
306 done = true;
307 break;
309 default:
310 break;
315 void Engine::handleResizedWindow(SomeEvent &event) {
316 // notify agents
317 for (std::list<boost::shared_ptr<Agent> >::iterator i = world.agents.begin(); i != world.agents.end(); i++) {
318 if (!*i) continue;
319 (*i)->queueScript(123, 0); // window resized script
323 void Engine::handleMouseMove(SomeEvent &event) {
324 // move the cursor
325 world.hand()->handleEvent(event);
327 // notify agents
328 for (std::list<boost::shared_ptr<Agent> >::iterator i = world.agents.begin(); i != world.agents.end(); i++) {
329 if (!*i) continue;
330 if ((*i)->imsk_mouse_move) {
331 caosVar x; x.setFloat(world.hand()->pointerX());
332 caosVar y; y.setFloat(world.hand()->pointerY());
333 (*i)->queueScript(75, 0, x, y); // Raw Mouse Move
338 void Engine::handleMouseButton(SomeEvent &event) {
339 // notify agents
340 for (std::list<boost::shared_ptr<Agent> >::iterator i = world.agents.begin(); i != world.agents.end(); i++) {
341 if (!*i) continue;
342 if ((event.type == eventmousebuttonup && (*i)->imsk_mouse_up) ||
343 (event.type == eventmousebuttondown && (*i)->imsk_mouse_down)) {
344 // set the button value as necessary
345 caosVar button;
346 switch (event.button) { // Backend guarantees that only one button will be set on a mousebuttondown event.
347 // the values here make fuzzie suspicious that c2e combines these events
348 // nornagon seems to think c2e doesn't
349 case buttonleft: button.setInt(1); break;
350 case buttonright: button.setInt(2); break;
351 case buttonmiddle: button.setInt(4); break;
352 default: break;
355 // if it was a mouse button we're interested in, then fire the relevant raw event
356 if (button.getInt() != 0) {
357 if (event.type == eventmousebuttonup)
358 (*i)->queueScript(77, 0, button); // Raw Mouse Up
359 else
360 (*i)->queueScript(76, 0, button); // Raw Mouse Down
363 if ((event.type == eventmousebuttondown &&
364 (event.button == buttonwheelup || event.button == buttonwheeldown) &&
365 (*i)->imsk_mouse_wheel)) {
366 // fire the mouse wheel event with the relevant delta value
367 caosVar delta;
368 if (event.button == buttonwheeldown)
369 delta.setInt(-120);
370 else
371 delta.setInt(120);
372 (*i)->queueScript(78, 0, delta); // Raw Mouse Wheel
376 world.hand()->handleEvent(event);
379 void Engine::handleKeyDown(SomeEvent &event) {
380 switch (event.key) {
381 case 'w': w_down = true; break;
382 case 'a': a_down = true; break;
383 case 's': s_down = true; break;
384 case 'd': d_down = true; break;
387 // tell the agent with keyboard focus
388 if (world.focusagent) {
389 TextEntryPart *t = (TextEntryPart *)((CompoundAgent *)world.focusagent.get())->part(world.focuspart);
390 if (t)
391 t->handleKey(event.key);
394 // notify agents
395 caosVar k;
396 k.setInt(event.key);
397 for (std::list<boost::shared_ptr<Agent> >::iterator i = world.agents.begin(); i != world.agents.end(); i++) {
398 if (!*i) continue;
399 if ((*i)->imsk_translated_char)
400 (*i)->queueScript(79, 0, k); // translated char script
404 void Engine::handleSpecialKeyUp(SomeEvent &event) {
405 switch (event.key) {
406 case 0x57:
407 w_down = false;
408 break;
409 case 0x41:
410 a_down = false;
411 break;
412 case 0x53:
413 s_down = false;
414 break;
415 case 0x44:
416 d_down = false;
417 break;
421 void Engine::handleSpecialKeyDown(SomeEvent &event) {
422 switch (event.key) {
423 case 0x57:
424 w_down = true;
425 break;
426 case 0x41:
427 a_down = true;
428 break;
429 case 0x53:
430 s_down = true;
431 break;
432 case 0x44:
433 d_down = true;
434 break;
438 // handle debug keys, if they're enabled
439 caosVar v = world.variables["engine_debug_keys"];
440 if (v.hasInt() && v.getInt() == 1) {
441 if (backend->keyDown(16)) { // shift down
442 MetaRoom *n; // for pageup/pagedown
444 switch (event.key) {
445 case 45: // insert
446 world.showrooms = !world.showrooms;
447 break;
449 case 19: // pause
450 // TODO: debug pause game
451 break;
453 case 32: // space
454 // TODO: force tick
455 break;
457 case 33: // pageup
458 // TODO: previous metaroom
459 if ((world.map.getMetaRoomCount() - 1) == world.camera.getMetaRoom()->id)
460 break;
461 n = world.map.getMetaRoom(world.camera.getMetaRoom()->id + 1);
462 if (n)
463 world.camera.goToMetaRoom(n->id);
464 break;
466 case 34: // pagedown
467 // TODO: next metaroom
468 if (world.camera.getMetaRoom()->id == 0)
469 break;
470 n = world.map.getMetaRoom(world.camera.getMetaRoom()->id - 1);
471 if (n)
472 world.camera.goToMetaRoom(n->id);
473 break;
475 default: break; // to shut up warnings
480 // tell the agent with keyboard focus
481 if (world.focusagent) {
482 TextEntryPart *t = (TextEntryPart *)((CompoundAgent *)world.focusagent.get())->part(world.focuspart);
483 if (t)
484 t->handleSpecialKey(event.key);
487 // notify agents
488 caosVar k;
489 k.setInt(event.key);
490 for (std::list<boost::shared_ptr<Agent> >::iterator i = world.agents.begin(); i != world.agents.end(); i++) {
491 if (!*i) continue;
492 if ((*i)->imsk_key_down)
493 (*i)->queueScript(73, 0, k); // key down script
497 static const char data_default[] = "./data";
499 static void opt_version() {
500 // We already showed the primary version bit, just throw in some random legalese
501 std::cout <<
502 "This is free software; see the source for copying conditions. There is NO" << std::endl <<
503 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." << std::endl << std::endl <<
504 "...please don't sue us." << std::endl;
507 bool Engine::parseCommandLine(int argc, char *argv[]) {
508 // variables for command-line flags
509 int optret;
510 std::vector<std::string> data_vec;
512 // generate help for backend options
513 std::string available_backends;
514 for (std::map<std::string, boost::shared_ptr<Backend> >::iterator i = possible_backends.begin(); i != possible_backends.end(); i++) {
515 if (available_backends.empty()) available_backends = i->first;
516 else available_backends += ", " + i->first;
518 available_backends = "Select the backend (options: " + available_backends + "), default is " + preferred_backend;
520 std::string available_audiobackends;
521 for (std::map<std::string, boost::shared_ptr<AudioBackend> >::iterator i = possible_audiobackends.begin(); i != possible_audiobackends.end(); i++) {
522 if (available_audiobackends.empty()) available_audiobackends = i->first;
523 else available_audiobackends += ", " + i->first;
525 available_audiobackends = "Select the audio backend (options: " + available_audiobackends + "), default is " + preferred_audiobackend;
527 // parse the command-line flags
528 po::options_description desc;
529 desc.add_options()
530 ("help,h", "Display help on command-line options")
531 ("version,V", "Display openc2e version")
532 ("silent,s", "Disable all sounds")
533 ("backend,k", po::value<std::string>(&preferred_backend)->composing(), available_backends.c_str())
534 ("audiobackend,o", po::value<std::string>(&preferred_audiobackend)->composing(), available_audiobackends.c_str())
535 ("data-path,d", po::value< std::vector<std::string> >(&data_vec)->composing(),
536 "Sets or adds a path to a data directory")
537 ("bootstrap,b", po::value< std::vector<std::string> >(&cmdline_bootstrap)->composing(),
538 "Sets or adds a path or COS file to bootstrap from")
539 ("gametype,g", po::value< std::string >(&world.gametype), "Set the game type (c1, c2, cv or c3)")
540 ("gamename,m", po::value< std::string >(&gamename), "Set the game name")
541 ("norun,n", "Don't run the game, just execute scripts")
542 ("autokill,a", "Enable autokill")
543 ("autostop", "Enable autostop (or disable it, for CV)")
545 po::variables_map vm;
546 po::store(po::parse_command_line(argc, argv, desc), vm);
547 po::notify(vm);
549 cmdline_enable_sound = !vm.count("silent");
550 cmdline_norun = vm.count("norun");
552 if (vm.count("help")) {
553 std::cout << desc << std::endl;
554 return false;
557 if (vm.count("version")) {
558 opt_version();
559 return false;
562 if (vm.count("autokill")) {
563 world.autokill = true;
566 if (vm.count("autostop")) {
567 world.autostop = true;
570 if (vm.count("data-path") == 0) {
571 std::cout << "Warning: No data path specified, trying default of '" << data_default << "', see --help if you need to specify one." << std::endl;
572 data_vec.push_back(data_default);
575 // add all the data directories to the list
576 for (std::vector<std::string>::iterator i = data_vec.begin(); i != data_vec.end(); i++) {
577 fs::path datadir(*i, fs::native);
578 if (!fs::exists(datadir)) {
579 throw creaturesException("data path '" + *i + "' doesn't exist");
581 world.data_directories.push_back(datadir);
584 // make a vague attempt at blacklisting some characters inside the gamename
585 // (it's used in directory names, registry keys, etc)
586 std::string invalidchars = "\\/:*?\"<>|";
587 for (unsigned int i = 0; i < invalidchars.size(); i++) {
588 if (gamename.find(invalidchars[i]) != gamename.npos)
589 throw creaturesException(std::string("The character ") + invalidchars[i] + " is not valid in a gamename.");
592 return true;
595 bool Engine::initialSetup() {
596 assert(world.data_directories.size() > 0);
598 // autodetect gametype if necessary
599 if (world.gametype.empty()) {
600 std::cout << "Warning: No gametype specified, ";
601 // TODO: is this sane? especially unsure about about.exe
602 if (!world.findFile("Creatures.exe").empty()) {
603 std::cout << "found Creatures.exe, assuming C1 (c1)";
604 world.gametype = "c1";
605 } else if (!world.findFile("Creatures2.exe").empty()) {
606 std::cout << "found Creatures2.exe, assuming C2 (c2)";
607 world.gametype = "c2";
608 } else if (!world.findFile("Sea-Monkeys.ico").empty()) {
609 std::cout << "found Sea-Monkeys.ico, assuming Sea-Monkeys (sm)";
610 world.gametype = "sm";
611 } else if (!world.findFile("about.exe").empty()) {
612 std::cout << "found about.exe, assuming CA, CP or CV (cv)";
613 world.gametype = "cv";
614 } else {
615 std::cout << "assuming C3/DS (c3)";
616 world.gametype = "c3";
618 std::cout << ", see --help if you need to specify one." << std::endl;
621 // set engine version
622 // TODO: set gamename
623 if (world.gametype == "c1") {
624 if (gamename.empty()) gamename = "Creatures 1";
625 version = 1;
626 } else if (world.gametype == "c2") {
627 if (gamename.empty()) gamename = "Creatures 2";
628 version = 2;
629 } else if (world.gametype == "c3") {
630 if (gamename.empty()) gamename = "Creatures 3";
631 version = 3;
632 } else if (world.gametype == "cv") {
633 if (gamename.empty()) gamename = "Creatures Village";
634 version = 3;
635 world.autostop = !world.autostop;
636 } else if (world.gametype == "sm") {
637 if (gamename.empty()) gamename = "Sea-Monkeys";
638 version = 3;
639 bmprenderer = true;
640 } else
641 throw creaturesException(boost::str(boost::format("unknown gametype '%s'!") % world.gametype));
643 // finally, add our cache directory to the end
644 world.data_directories.push_back(storageDirectory());
646 // initial setup
647 registerDelegates();
648 std::cout << "* Reading catalogue files..." << std::endl;
649 world.initCatalogue();
650 std::cout << "* Initial setup..." << std::endl;
651 world.init(); // just reads mouse cursor (we want this after the catalogue reading so we don't play "guess the filename")
652 if (engine.version > 2) {
653 std::cout << "* Reading PRAY files..." << std::endl;
654 world.praymanager.update();
657 #ifdef _WIN32
658 // Here we need to set the working directory since apparently windows != clever
659 char exepath[MAX_PATH] = "";
660 GetModuleFileName(0, exepath, sizeof(exepath) - 1);
661 char *exedir = strrchr(exepath, '\\');
662 if(exedir) {
663 // null terminate the string
664 *exedir = 0;
665 // Set working directory
666 SetCurrentDirectory(exepath);
668 else // err, oops
669 std::cerr << "Warning: Setting working directory to " << exepath << " failed.";
670 #endif
672 if (cmdline_norun) preferred_backend = "null";
673 if (preferred_backend != "null") std::cout << "* Initialising backend " << preferred_backend << "..." << std::endl;
674 shared_ptr<Backend> b = possible_backends[preferred_backend];
675 if (!b) throw creaturesException("No such backend " + preferred_backend);
676 b->init(); setBackend(b);
677 possible_backends.clear();
679 if (cmdline_norun || !cmdline_enable_sound) preferred_audiobackend = "null";
680 if (preferred_audiobackend != "null") std::cout << "* Initialising audio backend " << preferred_audiobackend << "..." << std::endl;
681 shared_ptr<AudioBackend> a = possible_audiobackends[preferred_audiobackend];
682 if (!a) throw creaturesException("No such audio backend " + preferred_audiobackend);
683 try{
684 a->init(); audio = a;
685 } catch (creaturesException &e) {
686 std::cerr << "* Couldn't initialize backend " << preferred_audiobackend << ": " << e.what() << std::endl << "* Continuing without sound." << std::endl;
687 audio = shared_ptr<AudioBackend>(new NullAudioBackend());
688 audio->init();
690 possible_audiobackends.clear();
692 world.camera.setBackend(backend); // TODO: hrr
694 int listenport = backend->networkInit();
695 if (listenport != -1) {
696 // inform the user of the port used, and store it in the relevant file
697 std::cout << "Listening for connections on port " << listenport << "." << std::endl;
698 #ifndef _WIN32
699 fs::path p = fs::path(homeDirectory().native_directory_string() + "/.creaturesengine", fs::native);
700 if (!fs::exists(p))
701 fs::create_directory(p);
702 if (fs::is_directory(p)) {
703 std::ofstream f((p.native_directory_string() + "/port").c_str(), std::ios::trunc);
704 f << boost::str(boost::format("%d") % listenport);
706 #endif
709 if (world.data_directories.size() < 3) {
710 // TODO: This is a hack for DS, basically. Not sure if it works properly. - fuzzie
711 caosVar name; name.setString("engine_no_auxiliary_bootstrap_1");
712 caosVar contents; contents.setInt(1);
713 eame_variables[name] = contents;
716 // execute the initial scripts!
717 std::cout << "* Executing initial scripts..." << std::endl;
718 if (cmdline_bootstrap.size() == 0) {
719 world.executeBootstrap(false);
720 } else {
721 std::vector<std::string> scripts;
723 if (engine.version < 3 && cmdline_bootstrap.size() != 1)
724 throw creaturesException("multiple bootstrap files provided in C1/C2 mode");
726 for (std::vector< std::string >::iterator bsi = cmdline_bootstrap.begin(); bsi != cmdline_bootstrap.end(); bsi++) {
727 fs::path scriptdir(*bsi, fs::native);
728 if (engine.version > 2 || fs::extension(scriptdir) == ".cos") {
729 // pass it to the world to execute (it handles both files and directories)
731 if (!fs::exists(scriptdir)) {
732 std::cerr << "Warning: Couldn't find a specified script directory (trying " << *bsi << ")!\n";
733 continue;
736 world.executeBootstrap(scriptdir);
737 } else {
738 // in c1/c2 mode, if not a cos file, assume it's an SFC file
739 if (!fs::exists(scriptdir) || fs::is_directory(scriptdir))
740 throw creaturesException("non-existant bootstrap file provided in C1/C2 mode");
741 // TODO: the default SFCFile loading code is in World, maybe this should be too..
742 SFCFile sfc;
743 std::ifstream f(scriptdir.native_directory_string().c_str(), std::ios::binary);
744 f >> std::noskipws;
745 sfc.read(&f);
746 sfc.copyToWorld();
751 // if there aren't any metarooms, we can't run a useful game, the user probably
752 // wanted to execute a CAOS script or something went badly wrong.
753 if (!cmdline_norun && world.map.getMetaRoomCount() == 0) {
754 shutdown();
755 throw creaturesException("No metarooms found in given bootstrap directories or files");
758 std::cout << "* Done startup." << std::endl;
760 if (cmdline_norun) {
761 // TODO: see comment above about avoiding backend when norun is set
762 std::cout << "Told not to run the world, so stopping now." << std::endl;
763 shutdown();
764 return false;
767 return true;
770 void Engine::shutdown() {
771 world.shutdown();
772 backend->shutdown();
773 audio->shutdown();
774 freeDelegates(); // does nothing if there are none (ie, no call to initialSetup)
777 fs::path Engine::homeDirectory() {
778 fs::path p;
780 #ifndef _WIN32
781 char *envhome = getenv("HOME");
782 if (envhome)
783 p = fs::path(envhome, fs::native);
784 if ((!envhome) || (!fs::is_directory(p)))
785 p = fs::path(getpwuid(getuid())->pw_dir, fs::native);
786 if (!fs::is_directory(p)) {
787 std::cerr << "Can't work out what your home directory is, giving up and using /tmp for now." << std::endl;
788 p = fs::path("/tmp", fs::native); // sigh
790 #else
791 TCHAR szPath[_MAX_PATH];
792 SHGetSpecialFolderPath(NULL, szPath, CSIDL_PERSONAL, TRUE);
794 p = fs::path(szPath, fs::native);
795 if (!fs::exists(p) || !fs::is_directory(p))
796 throw creaturesException("Windows reported that your My Documents folder is at '" + std::string(szPath) + "' but there's no directory there!");
797 #endif
799 return p;
802 fs::path Engine::storageDirectory() {
803 #ifndef _WIN32
804 std::string dirname = "/.openc2e";
805 #else
806 std::string dirname = "/My Games";
807 #endif
809 // main storage dir
810 fs::path p = fs::path(homeDirectory().native_directory_string() + dirname, fs::native);
811 if (!fs::exists(p))
812 fs::create_directory(p);
813 else if (!fs::is_directory(p))
814 throw creaturesException("Your openc2e data directory " + p.native_directory_string() + " is a file, not a directory. That's bad.");
816 // game-specific storage dir
817 p = fs::path(p.native_directory_string() + std::string("/" + gamename), fs::native);
818 if (!fs::exists(p))
819 fs::create_directory(p);
820 else if (!fs::is_directory(p))
821 throw creaturesException("Your openc2e game data directory " + p.native_directory_string() + " is a file, not a directory. That's bad.");
823 return p;
826 /* vim: set noet: */