3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "lispreader.h"
32 namespace WorldMapNS {
34 TileManager* TileManager::instance_ = 0;
36 TileManager::TileManager()
38 std::string stwt_filename = datadir + "images/worldmap/antarctica.stwt";
39 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
42 st_abort("Couldn't load file", stwt_filename);
44 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
46 lisp_object_t* cur = lisp_cdr(root_obj);
48 while(!lisp_nil_p(cur))
50 lisp_object_t* element = lisp_car(cur);
52 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
55 std::string filename = "<invalid>";
57 Tile* tile = new Tile;
64 LispReader reader(lisp_cdr(element));
65 reader.read_int("id", &id);
66 reader.read_bool("north", &tile->north);
67 reader.read_bool("south", &tile->south);
68 reader.read_bool("west", &tile->west);
69 reader.read_bool("east", &tile->east);
70 reader.read_bool("stop", &tile->stop);
71 reader.read_string("image", &filename);
73 tile->sprite = new Surface(
74 datadir + "/images/worldmap/" + filename,
77 if (id >= int(tiles.size()))
84 puts("Unhandled symbol");
97 TileManager::get(int i)
99 assert(i >=0 && i < int(tiles.size()));
103 Tux::Tux(WorldMap* worldmap_)
104 : worldmap(worldmap_)
106 sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
112 input_direction = NONE;
116 Tux::draw(const Point& offset)
118 Point pos = get_pos();
119 sprite->draw(pos.x + offset.x,
127 float x = tile_pos.x * 32;
128 float y = tile_pos.y * 32;
148 return Point((int)x, (int)y);
160 Tux::update(float delta)
164 if (input_direction != NONE)
165 { // We got a new direction, so lets start walking when possible
167 if (worldmap->path_ok(input_direction, tile_pos, &next_tile))
169 tile_pos = next_tile;
171 direction = input_direction;
177 // Let tux walk a few pixels (20 pixel/sec)
178 offset += 20.0f * delta;
181 { // We reached the next tile, so we check what to do now
184 if (worldmap->at(tile_pos)->stop || worldmap->at_level())
191 if (worldmap->path_ok(direction, tile_pos, &next_tile))
193 tile_pos = next_tile;
197 puts("Tilemap data is buggy");
212 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
213 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA);
214 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA);
216 input_direction = NONE;
220 music = "SALCON.MOD";
226 WorldMap::~WorldMap()
234 std::string filename = datadir + "levels/default/worldmap.stwm";
236 lisp_object_t* root_obj = lisp_read_from_file(filename);
238 st_abort("Couldn't load file", filename);
240 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
242 lisp_object_t* cur = lisp_cdr(root_obj);
244 while(!lisp_nil_p(cur))
246 lisp_object_t* element = lisp_car(cur);
248 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
250 LispReader reader(lisp_cdr(element));
251 reader.read_int("width", &width);
252 reader.read_int("height", &height);
253 reader.read_int_vector("data", &tilemap);
255 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
257 LispReader reader(lisp_cdr(element));
258 reader.read_string("name", &name);
259 reader.read_string("music", &music);
261 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
263 lisp_object_t* cur = lisp_cdr(element);
265 while(!lisp_nil_p(cur))
267 lisp_object_t* element = lisp_car(cur);
269 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
272 LispReader reader(lisp_cdr(element));
273 level.solved = false;
274 reader.read_string("name", &level.name);
275 reader.read_int("x", &level.x);
276 reader.read_int("y", &level.y);
277 levels.push_back(level);
294 WorldMap::get_input()
297 input_direction = NONE;
300 while (SDL_PollEvent(&event))
304 current_menu->event(event);
311 st_abort("Received window close", "");
315 switch(event.key.keysym.sym)
318 Menu::set_current(worldmap_menu);
319 show_menu = !show_menu;
330 case SDL_JOYAXISMOTION:
331 switch(event.jaxis.axis)
334 if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
335 input_direction = WEST;
336 else if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
337 input_direction = EAST;
340 if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
341 input_direction = SOUTH;
342 else if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
343 input_direction = NORTH;
348 case SDL_JOYBUTTONDOWN:
349 if (event.jbutton.button == JOY_B)
361 Uint8 *keystate = SDL_GetKeyState(NULL);
363 if (keystate[SDLK_LEFT])
364 input_direction = WEST;
365 else if (keystate[SDLK_RIGHT])
366 input_direction = EAST;
367 else if (keystate[SDLK_UP])
368 input_direction = NORTH;
369 else if (keystate[SDLK_DOWN])
370 input_direction = SOUTH;
375 WorldMap::get_next_tile(Point pos, Direction direction)
398 WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos)
400 *new_pos = get_next_tile(old_pos, direction);
402 if (!(new_pos->x >= 0 && new_pos->x < width
403 && new_pos->y >= 0 && new_pos->y < height))
404 { // New position is outsite the tilemap
408 { // Check if we the tile allows us to go to new_pos
412 return (at(old_pos)->west && at(*new_pos)->east);
415 return (at(old_pos)->east && at(*new_pos)->west);
418 return (at(old_pos)->north && at(*new_pos)->south);
421 return (at(old_pos)->south && at(*new_pos)->north);
424 assert(!"path_ok() can't work if direction is NONE");
433 if (enter_level && !tux->is_moving())
435 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
437 if (i->x == tux->get_tile_pos().x &&
438 i->y == tux->get_tile_pos().y)
440 std::cout << "Enter the current level: " << i->name << std::endl;;
442 GameSession session(datadir + "levels/" + i->name,
443 1, ST_GL_LOAD_LEVEL_FILE);
446 if (1) // FIXME: insert exit status checker here
452 if (!savegame_file.empty())
453 savegame(savegame_file);
457 std::cout << "Nothing to enter at: "
458 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
462 tux->set_direction(input_direction);
468 if(current_menu == worldmap_menu)
470 switch (worldmap_menu->check())
472 case 2: // Return to game
475 case 5: // Quit Worldmap
484 WorldMap::at(Point p)
490 return TileManager::instance()->get(tilemap[width * p.y + p.x]);
496 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
498 if (i->x == tux->get_tile_pos().x &&
499 i->y == tux->get_tile_pos().y)
508 WorldMap::draw(const Point& offset)
510 for(int y = 0; y < height; ++y)
511 for(int x = 0; x < width; ++x)
513 Tile* tile = at(Point(x, y));
514 tile->sprite->draw(x*32 + offset.x,
518 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
521 leveldot_green->draw(i->x*32 + offset.x,
524 leveldot_red->draw(i->x*32 + offset.x,
533 WorldMap::draw_status()
536 sprintf(str, "%d", player_status.score);
537 white_text->draw("SCORE", 0, 0);
538 gold_text->draw(str, 96, 0);
540 sprintf(str, "%d", player_status.distros);
541 white_text->draw_align("COINS", 320-64, 0, A_LEFT, A_TOP);
542 gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP);
544 white_text->draw("LIVES", 480, 0);
545 if (player_status.lives >= 5)
547 sprintf(str, "%dx", player_status.lives);
548 gold_text->draw(str, 585, 0);
549 tux_life->draw(565+(18*3), 0);
553 for(int i= 0; i < player_status.lives; ++i)
554 tux_life->draw(565+(18*i),0);
557 if (!tux->is_moving())
559 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
561 if (i->x == tux->get_tile_pos().x &&
562 i->y == tux->get_tile_pos().y)
564 white_text->draw_align(i->name.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM);
579 song = load_song(datadir + "/music/" + music);
583 Point tux_pos = tux->get_pos();
586 offset.x = -tux_pos.x + screen->w/2;
587 offset.y = -tux_pos.y + screen->h/2;
589 if (offset.x > 0) offset.x = 0;
590 if (offset.y > 0) offset.y = 0;
592 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
593 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
600 menu_process_current();
610 WorldMap::savegame(const std::string& filename)
612 std::cout << "savegame: " << filename << std::endl;
613 std::ofstream out(filename.c_str());
615 int nb_solved_levels = 0;
616 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
622 out << "(supertux-savegame\n"
624 << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
625 << " (lives " << player_status.lives << ")\n"
626 << " (score " << player_status.score << ")\n"
627 << " (distros " << player_status.distros << ")\n"
628 << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << "))\n"
631 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
635 out << " (level (name \"" << i->name << "\")\n"
636 << " (solved #t))\n";
641 << " )\n\n;; EOF ;;" << std::endl;
645 WorldMap::loadgame(const std::string& filename)
647 std::cout << "loadgame: " << filename << std::endl;
648 savegame_file = filename;
650 if (access(filename.c_str(), F_OK) == 0)
652 lisp_object_t* cur = lisp_read_from_file(filename);
654 if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
658 LispReader reader(cur);
660 reader.read_int("lives", &player_status.lives);
661 reader.read_int("score", &player_status.score);
662 reader.read_int("distros", &player_status.distros);
664 lisp_object_t* tux_cur = 0;
665 if (reader.read_lisp("tux", &tux_cur))
668 LispReader tux_reader(tux_cur);
669 tux_reader.read_int("x", &p.x);
670 tux_reader.read_int("y", &p.y);
672 tux->set_tile_pos(p);
675 lisp_object_t* level_cur = 0;
676 if (reader.read_lisp("levels", &level_cur))
680 lisp_object_t* sym = lisp_car(lisp_car(level_cur));
681 lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
683 if (strcmp(lisp_symbol(sym), "level") == 0)
688 LispReader level_reader(data);
689 level_reader.read_string("name", &name);
690 level_reader.read_bool("solved", &solved);
692 std::cout << "Name: " << name << " " << solved << std::endl;
694 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
701 level_cur = lisp_cdr(level_cur);
707 } // namespace WorldMapNS