X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fworldmap.cpp;h=85eb56ec8f38e2798a9de1aadd730d18e1c9fadc;hb=b56d18cd12b1892a7588b1fe620b1df26f7e7d4f;hp=d4a024c28552d3e5b9984551efc22cda069f472c;hpb=537b2ae5ee0a47eb361b5e651b048e917c7f8d5a;p=supertux.git diff --git a/src/worldmap.cpp b/src/worldmap.cpp index d4a024c28..85eb56ec8 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -1,7 +1,7 @@ // $Id$ // -// Pingus - A free Lemmings clone -// Copyright (C) 2002 Ingo Ruhnke +// SuperTux - A Jump'n Run +// Copyright (C) 2004 Ingo Ruhnke // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -17,25 +17,81 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#include +#include #include #include +#include "globals.h" #include "texture.h" #include "screen.h" #include "lispreader.h" +#include "gameloop.h" +#include "setup.h" #include "worldmap.h" namespace WorldMapNS { +Direction reverse_dir(Direction direction) +{ + switch(direction) + { + case WEST: + return EAST; + case EAST: + return WEST; + case NORTH: + return SOUTH; + case SOUTH: + return NORTH; + case NONE: + return NONE; + } + return NONE; +} + +std::string +direction_to_string(Direction direction) +{ + switch(direction) + { + case WEST: + return "west"; + case EAST: + return "east"; + case NORTH: + return "north"; + case SOUTH: + return "south"; + default: + return "none"; + } +} + +Direction +string_to_direction(const std::string& directory) +{ + if (directory == "west") + return WEST; + else if (directory == "east") + return EAST; + else if (directory == "north") + return NORTH; + else if (directory == "south") + return SOUTH; + else + return NONE; +} + TileManager* TileManager::instance_ = 0; TileManager::TileManager() { - lisp_stream_t stream; - FILE* in = fopen(DATA_PREFIX "tiles/antarctica.scm", "r"); - assert(in); - lisp_stream_init_file (&stream, in); - lisp_object_t* root_obj = lisp_read (&stream); - + std::string stwt_filename = datadir + "images/worldmap/antarctica.stwt"; + lisp_object_t* root_obj = lisp_read_from_file(stwt_filename); + + if (!root_obj) + st_abort("Couldn't load file", stwt_filename); + if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0) { lisp_object_t* cur = lisp_cdr(root_obj); @@ -44,7 +100,6 @@ TileManager::TileManager() { lisp_object_t* element = lisp_car(cur); - lisp_dump (element, stdout); if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0) { int id = 0; @@ -66,11 +121,11 @@ TileManager::TileManager() reader.read_bool("stop", &tile->stop); reader.read_string("image", &filename); - texture_load(&tile->sprite, - const_cast((std::string(DATA_PREFIX "/images/worldmap/") + filename).c_str()), + tile->sprite = new Surface( + datadir + "/images/worldmap/" + filename, USE_ALPHA); - if (id >= tiles.size()) + if (id >= int(tiles.size())) tiles.resize(id+1); tiles[id] = tile; @@ -92,180 +147,401 @@ TileManager::TileManager() Tile* TileManager::get(int i) { - assert(i >=0 && i < tiles.size()); + assert(i >=0 && i < int(tiles.size())); return tiles[i]; } -WorldMap::WorldMap() +Tux::Tux(WorldMap* worldmap_) + : worldmap(worldmap_) { - quit = false; - width = 20; - height = 15; - tux_moving = false; - tilemap.resize(width * height); - - tilemap[0] = 5; - tilemap[1] = 1; - tilemap[2] = 6; - tilemap[3] = 1; - tilemap[4] = 3; - tilemap[4+20] = 2; - tilemap[4+40] = 7; - tilemap[4+60] = 2; - tilemap[4+80] = 4; + sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA); + offset = 0; + moving = false; + tile_pos.x = 5; + tile_pos.y = 5; + direction = NONE; + input_direction = NONE; +} - texture_load(&tux_sprite, DATA_PREFIX "/images/worldmap/tux.png", USE_ALPHA); +void +Tux::draw(const Point& offset) +{ + Point pos = get_pos(); + sprite->draw(pos.x + offset.x, + pos.y + offset.y); +} - tux_offset = 0; - tux_tile_pos.x = 0; - tux_tile_pos.y = 0; +Point +Tux::get_pos() +{ + float x = tile_pos.x * 32; + float y = tile_pos.y * 32; - input_direction = NONE; - tux_direction = NONE; - enter_level = false; + switch(direction) + { + case WEST: + x -= offset - 32; + break; + case EAST: + x += offset - 32; + break; + case NORTH: + y -= offset - 32; + break; + case SOUTH: + y += offset - 32; + break; + case NONE: + break; + } + + return Point((int)x, (int)y); } -WorldMap::~WorldMap() +void +Tux::stop() { + offset = 0; + direction = NONE; + moving = false; } void -WorldMap::get_input() +Tux::update(float delta) { - SDL_Event event; + if (!moving) + { + if (input_direction != NONE) + { + WorldMap::Level* level = worldmap->at_level(); + + // We got a new direction, so lets start walking when possible + Point next_tile; + if ((!level || level->solved) + && worldmap->path_ok(input_direction, tile_pos, &next_tile)) + { + tile_pos = next_tile; + moving = true; + direction = input_direction; + back_direction = reverse_dir(direction); + } + else if (input_direction == back_direction) + { + std::cout << "Back triggered" << std::endl; + moving = true; + direction = input_direction; + tile_pos = worldmap->get_next_tile(tile_pos, direction); + back_direction = reverse_dir(direction); + } + } + } + else + { + // Let tux walk a few pixels (20 pixel/sec) + offset += 20.0f * delta; - enter_level = false; + if (offset > 32) + { // We reached the next tile, so we check what to do now + offset -= 32; - while (SDL_PollEvent(&event)) - { - switch(event.type) - { - case SDL_QUIT: - quit = true; - break; - - case SDL_KEYDOWN: - switch(event.key.keysym.sym) + if (worldmap->at(tile_pos)->stop || worldmap->at_level()) { - case SDLK_ESCAPE: - quit = true; - break; - case SDLK_LCTRL: - case SDLK_RETURN: - if (!tux_moving) - enter_level = true; - break; + stop(); + } + else + { + // Walk automatically to the next tile + Point next_tile; + if (worldmap->path_ok(direction, tile_pos, &next_tile)) + { + tile_pos = next_tile; + } + else + { + puts("Tilemap data is buggy"); + stop(); + } } - break; } } +} + +WorldMap::WorldMap() +{ + tux = new Tux(this); + + width = 20; + height = 15; + + level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA); + leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA); + leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA); - Uint8 *keystate = SDL_GetKeyState(NULL); - input_direction = NONE; - - if (keystate[SDLK_LEFT]) - input_direction = WEST; - else if (keystate[SDLK_RIGHT]) - input_direction = EAST; - else if (keystate[SDLK_UP]) - input_direction = NORTH; - else if (keystate[SDLK_DOWN]) - input_direction = SOUTH; + enter_level = false; + + name = ""; + music = "SALCON.MOD"; + song = 0; + + load_map(); } -void -WorldMap::update() +WorldMap::~WorldMap() { - float speed = 4.5; + delete tux; +} - if (enter_level) - { - puts("Enter the current level"); - } - else +void +WorldMap::load_map() +{ + std::string filename = datadir + "levels/default/worldmap.stwm"; + + lisp_object_t* root_obj = lisp_read_from_file(filename); + if (!root_obj) + st_abort("Couldn't load file", filename); + + if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0) { - if (!tux_moving) + lisp_object_t* cur = lisp_cdr(root_obj); + + while(!lisp_nil_p(cur)) { - // FIXME: Cleanup, seperate tux - switch(input_direction) + lisp_object_t* element = lisp_car(cur); + + if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0) { - case WEST: - if (at(tux_tile_pos)->west) + LispReader reader(lisp_cdr(element)); + reader.read_int("width", &width); + reader.read_int("height", &height); + reader.read_int_vector("data", &tilemap); + } + else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0) + { + LispReader reader(lisp_cdr(element)); + reader.read_string("name", &name); + reader.read_string("music", &music); + } + else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0) + { + lisp_object_t* cur = lisp_cdr(element); + + while(!lisp_nil_p(cur)) { - tux_tile_pos.x -= 1; - tux_moving = true; - tux_direction = input_direction; + lisp_object_t* element = lisp_car(cur); + + if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0) + { + Level level; + LispReader reader(lisp_cdr(element)); + level.solved = false; + + level.north = true; + level.east = true; + level.south = true; + level.west = true; + + reader.read_string("name", &level.name); + reader.read_int("x", &level.x); + reader.read_int("y", &level.y); + levels.push_back(level); + } + + cur = lisp_cdr(cur); } + } + else + { + + } + + cur = lisp_cdr(cur); + } + } +} + +void +WorldMap::get_input() +{ + enter_level = false; + input_direction = NONE; + + SDL_Event event; + while (SDL_PollEvent(&event)) + { + if(!show_menu && event.key.keysym.sym == SDLK_ESCAPE) + Menu::set_current(worldmap_menu); + + current_menu->event(event); + if(!show_menu) + { + switch(event.type) + { + case SDL_QUIT: + st_abort("Received window close", ""); break; - case EAST: - if (at(tux_tile_pos)->east) + + case SDL_KEYDOWN: + switch(event.key.keysym.sym) { - tux_tile_pos.x += 1; - tux_moving = true; - tux_direction = input_direction; + case SDLK_LCTRL: + case SDLK_RETURN: + enter_level = true; + break; + default: + break; } break; - case NORTH: - if (at(tux_tile_pos)->north) + + case SDL_JOYAXISMOTION: + switch(event.jaxis.axis) { - tux_tile_pos.y -= 1; - tux_moving = true; - tux_direction = input_direction; + case JOY_X: + if (event.jaxis.value < -JOYSTICK_DEAD_ZONE) + input_direction = WEST; + else if (event.jaxis.value > JOYSTICK_DEAD_ZONE) + input_direction = EAST; + break; + case JOY_Y: + if (event.jaxis.value > JOYSTICK_DEAD_ZONE) + input_direction = SOUTH; + else if (event.jaxis.value < -JOYSTICK_DEAD_ZONE) + input_direction = NORTH; + break; } break; - case SOUTH: - if (at(tux_tile_pos)->south) - { - tux_tile_pos.y += 1; - tux_moving = true; - tux_direction = input_direction; - } + + case SDL_JOYBUTTONDOWN: + if (event.jbutton.button == JOY_B) + enter_level = true; break; - case NONE: - tux_moving = false; - tux_offset = 0; - tux_direction = input_direction; + + default: break; } } - else + } + + if (!show_menu) + { + Uint8 *keystate = SDL_GetKeyState(NULL); + + if (keystate[SDLK_LEFT]) + input_direction = WEST; + else if (keystate[SDLK_RIGHT]) + input_direction = EAST; + else if (keystate[SDLK_UP]) + input_direction = NORTH; + else if (keystate[SDLK_DOWN]) + input_direction = SOUTH; + } +} + +Point +WorldMap::get_next_tile(Point pos, Direction direction) +{ + switch(direction) + { + case WEST: + pos.x -= 1; + break; + case EAST: + pos.x += 1; + break; + case NORTH: + pos.y -= 1; + break; + case SOUTH: + pos.y += 1; + break; + case NONE: + break; + } + return pos; +} + +bool +WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos) +{ + *new_pos = get_next_tile(old_pos, direction); + + if (!(new_pos->x >= 0 && new_pos->x < width + && new_pos->y >= 0 && new_pos->y < height)) + { // New position is outsite the tilemap + return false; + } + else + { // Check if we the tile allows us to go to new_pos + switch(direction) { - tux_offset += speed; + case WEST: + return (at(old_pos)->west && at(*new_pos)->east); - if (tux_offset > 32) - { - tux_offset -= 32; + case EAST: + return (at(old_pos)->east && at(*new_pos)->west); - if (at(tux_tile_pos)->stop) - { - tux_direction = NONE; - tux_moving = false; - } - else - { - // FIXME: Cleanup, seperate tux - switch(tux_direction) - { - case WEST: - if (at(tux_tile_pos)->west) - tux_tile_pos.x -= 1; - break; - case EAST: - if (at(tux_tile_pos)->east) - tux_tile_pos.x += 1; - break; - case NORTH: - if (at(tux_tile_pos)->north) - tux_tile_pos.y -= 1; - break; - case SOUTH: - if (at(tux_tile_pos)->south) - tux_tile_pos.y += 1; - break; - } - } + case NORTH: + return (at(old_pos)->north && at(*new_pos)->south); + + case SOUTH: + return (at(old_pos)->south && at(*new_pos)->north); + + case NONE: + assert(!"path_ok() can't work if direction is NONE"); + } + return false; + } +} + +void +WorldMap::update() +{ + if (enter_level && !tux->is_moving()) + { + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->x == tux->get_tile_pos().x && + i->y == tux->get_tile_pos().y) + { + std::cout << "Enter the current level: " << i->name << std::endl;; + halt_music(); + GameSession session(datadir + "levels/" + i->name, + 1, ST_GL_LOAD_LEVEL_FILE); + session.run(); + + if (1) // FIXME: insert exit status checker here + i->solved = true; + + play_music(song, 1); + show_menu = 0; + menu_reset(); + if (!savegame_file.empty()) + savegame(savegame_file); + return; + } + } + std::cout << "Nothing to enter at: " + << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl; + } + else + { + tux->set_direction(input_direction); + tux->update(0.33f); + } + + if(show_menu) + { + if(current_menu == worldmap_menu) + { + switch (worldmap_menu->check()) + { + case 2: // Return to game + menu_reset(); + break; + case 5: // Quit Worldmap + quit = true; + break; } } } @@ -278,63 +554,232 @@ WorldMap::at(Point p) && p.x < width && p.y >= 0 && p.y < height); + return TileManager::instance()->get(tilemap[width * p.y + p.x]); } +WorldMap::Level* +WorldMap::at_level() +{ + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->x == tux->get_tile_pos().x && + i->y == tux->get_tile_pos().y) + return &*i; + } + + return 0; +} + + void -WorldMap::draw() +WorldMap::draw(const Point& offset) { for(int y = 0; y < height; ++y) for(int x = 0; x < width; ++x) { Tile* tile = at(Point(x, y)); - texture_draw(&tile->sprite, x*32, y*32, NO_UPDATE); + tile->sprite->draw(x*32 + offset.x, + y*32 + offset.y); } - - float x = tux_tile_pos.x * 32; - float y = tux_tile_pos.y * 32; + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->solved) + leveldot_green->draw(i->x*32 + offset.x, + i->y*32 + offset.y); + else + leveldot_red->draw(i->x*32 + offset.x, + i->y*32 + offset.y); + } + + tux->draw(offset); + draw_status(); +} + +void +WorldMap::draw_status() +{ + char str[80]; + sprintf(str, "%d", player_status.score); + white_text->draw("SCORE", 0, 0); + gold_text->draw(str, 96, 0); + + sprintf(str, "%d", player_status.distros); + white_text->draw_align("COINS", 320-64, 0, A_LEFT, A_TOP); + gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP); - switch(tux_direction) + white_text->draw("LIVES", 480, 0); + if (player_status.lives >= 5) { - case WEST: - x -= tux_offset - 32; - break; - case EAST: - x += tux_offset - 32; - break; - case NORTH: - y -= tux_offset - 32; - break; - case SOUTH: - y += tux_offset - 32; - break; + sprintf(str, "%dx", player_status.lives); + gold_text->draw(str, 585, 0); + tux_life->draw(565+(18*3), 0); + } + else + { + for(int i= 0; i < player_status.lives; ++i) + tux_life->draw(565+(18*i),0); } - texture_draw(&tux_sprite, (int)x, (int)y, NO_UPDATE); - flipscreen(); + if (!tux->is_moving()) + { + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->x == tux->get_tile_pos().x && + i->y == tux->get_tile_pos().y) + { + white_text->draw_align(i->name.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM); + break; + } + } + } } void WorldMap::display() { + show_menu = false; + quit = false; + song = load_song(datadir + "/music/" + music); + play_music(song, 1); + while(!quit) { - draw(); + Point tux_pos = tux->get_pos(); + if (1) + { + offset.x = -tux_pos.x + screen->w/2; + offset.y = -tux_pos.y + screen->h/2; + + if (offset.x > 0) offset.x = 0; + if (offset.y > 0) offset.y = 0; + + if (offset.x < screen->w - width*32) offset.x = screen->w - width*32; + if (offset.y < screen->h - height*32) offset.y = screen->h - height*32; + } + + draw(offset); get_input(); update(); + + if(show_menu) + { + menu_process_current(); + mouse_cursor->draw(); + } + flipscreen(); + SDL_Delay(20); } + + free_music(song); } -} // namespace WorldMapNS +void +WorldMap::savegame(const std::string& filename) +{ + std::cout << "savegame: " << filename << std::endl; + std::ofstream out(filename.c_str()); + + int nb_solved_levels = 0; + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->solved) + ++nb_solved_levels; + } + + out << "(supertux-savegame\n" + << " (version 1)\n" + << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n" + << " (lives " << player_status.lives << ")\n" + << " (score " << player_status.score << ")\n" + << " (distros " << player_status.distros << ")\n" + << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")" + << " (back \"" << direction_to_string(tux->back_direction) << "\"))\n" + << " (levels\n"; + + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->solved) + { + out << " (level (name \"" << i->name << "\")\n" + << " (solved #t))\n"; + } + } -void worldmap_run() + out << " )\n" + << " )\n\n;; EOF ;;" << std::endl; +} + +void +WorldMap::loadgame(const std::string& filename) { - WorldMapNS::WorldMap worldmap; + std::cout << "loadgame: " << filename << std::endl; + savegame_file = filename; + + if (access(filename.c_str(), F_OK) == 0) + { + lisp_object_t* cur = lisp_read_from_file(filename); + + if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0) + return; + + cur = lisp_cdr(cur); + LispReader reader(cur); - worldmap.display(); + reader.read_int("lives", &player_status.lives); + reader.read_int("score", &player_status.score); + reader.read_int("distros", &player_status.distros); + + lisp_object_t* tux_cur = 0; + if (reader.read_lisp("tux", &tux_cur)) + { + Point p; + LispReader tux_reader(tux_cur); + tux_reader.read_int("x", &p.x); + tux_reader.read_int("y", &p.y); + + tux->set_tile_pos(p); + } + + lisp_object_t* level_cur = 0; + if (reader.read_lisp("levels", &level_cur)) + { + while(level_cur) + { + lisp_object_t* sym = lisp_car(lisp_car(level_cur)); + lisp_object_t* data = lisp_cdr(lisp_car(level_cur)); + + if (strcmp(lisp_symbol(sym), "level") == 0) + { + std::string name; + bool solved = false; + std::string back_str =""; + Direction back = NONE; + + LispReader level_reader(data); + level_reader.read_string("name", &name); + level_reader.read_bool("solved", &solved); + if (level_reader.read_string("back", &back_str)) + back = string_to_direction(back_str); + + std::cout << "Name: " << name << " " << solved << std::endl; + + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (name == i->name) + i->solved = solved; + } + } + + level_cur = lisp_cdr(level_cur); + } + } + } } +} // namespace WorldMapNS + /* EOF */