/** * \brief Implementation of game:get_starting_location(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::game_api_get_starting_location(lua_State* l) { Savegame& savegame = check_game(l, 1); const std::string& starting_map = savegame.get_string(Savegame::KEY_STARTING_MAP); const std::string& starting_point = savegame.get_string(Savegame::KEY_STARTING_POINT); if (starting_map.empty()) { lua_pushnil(l); } else { push_string(l, savegame.get_string(Savegame::KEY_STARTING_MAP)); } if (starting_point.empty()) { lua_pushnil(l); } else { push_string(l, savegame.get_string(Savegame::KEY_STARTING_POINT)); } return 2; }
/** * Reads the file according to required format and * saves each fo the paremeters in the game struct. * * @param file_num Number of file to load game from * @return Game struct with state as read from file */ Game* load_game(int file_num) { int i, j, valid = 1; char path_to_read[MAX_STR_LEN]; FILE *read_file; char game_name[MAX_STR_LEN]; Game* loaded_game; if (check_validity(sprintf(path_to_read, FILE_PATH, file_num))) { read_file = fopen(path_to_read, "r"); if (read_file == NULL ) { printf("ERROR: Could not open saved game %s\n", path_to_read); return NULL; } if (check_validity(fscanf(read_file, "%s\n", game_name))) { // Create game according to name loaded_game = check_game(game_name); // Reading which player's turn if (check_validity(fscanf(read_file, "%d\n", &(loaded_game->current_player)))) { if ((loaded_game->current_player == FIRST_PL_TURN) || (loaded_game->current_player == SECOND_PL_TURN)) { // Reading board cells, according to size determined by game creation for (i = 0; i < loaded_game->board->n && valid; i++) { for (j = 0; j < (loaded_game->board->m - 1) && valid; j++) { valid = check_validity(fscanf(read_file, "%d ", &(loaded_game->board->cells[i][j]))); } // Last one will have line break valid = check_validity(fscanf(read_file, "%d\n", &(loaded_game->board->cells[i][j]))); } if (valid) { fclose(read_file); loaded_game->won_game(loaded_game); return loaded_game; } } } } fclose(read_file); } return NULL; }
/** * \brief Implementation of game:stop_dialog(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::game_api_stop_dialog(lua_State* l) { Savegame& savegame = check_game(l, 1); Game* game = savegame.get_game(); if (game == NULL) { error(l, "Cannot stop dialog: this game is not running."); } if (!game->is_dialog_enabled()) { error(l, "Cannot stop dialog: no dialog is active."); } // Optional parameter: status. int status_ref = LUA_REFNIL; if (lua_gettop(l) >= 2) { lua_settop(l, 2); status_ref = luaL_ref(l, LUA_REGISTRYINDEX); } game->stop_dialog(status_ref); return 0; }
static SCM game_init (SCM game_smob, SCM s_width, SCM s_height, SCM s_fullscreen) { Game *game = check_game(game_smob); int width = scm_to_int (s_width); int height = scm_to_int (s_height); bool fullscreen = scm_to_bool (s_fullscreen); /* Initialize Allegro things */ /* TODO: Handle these errors in a proper way */ if(!al_init ()) { fprintf (stderr, "failed to initialize allegro!\n"); exit (-1); } if (!al_init_image_addon ()) { fprintf (stderr, "failed to initialize image addon!\n"); exit (-1); } al_init_font_addon (); if(!al_init_ttf_addon ()) { fprintf (stderr, "failed to initialize ttf addon!\n"); exit (-1); } if(!al_install_audio ()) { fprintf (stderr, "failed to initialize audio addon!\n"); exit (-1); } if(!al_init_acodec_addon ()) { fprintf (stderr, "failed to initialize audio codecs addon!\n"); exit (-1); } if (!al_reserve_samples (16)) { fprintf (stderr, "failed to reserve samples!\n"); exit (-1); } if(!al_install_keyboard ()) { fprintf (stderr, "failed to initialize keyboard!\n"); exit (-1); } if (fullscreen) { al_set_new_display_flags (ALLEGRO_FULLSCREEN); } game->display = al_create_display (width, height); if (!game->display) { fprintf (stderr, "failed to create display!\n"); } game->timer = al_create_timer (game->timestep); game->event_queue = al_create_event_queue (); al_register_event_source (game->event_queue, al_get_display_event_source (game->display)); al_register_event_source (game->event_queue, al_get_timer_event_source (game->timer)); al_register_event_source (game->event_queue, al_get_keyboard_event_source ()); return SCM_UNSPECIFIED; }
/** * \brief Implementation of game:get_command_effect(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::game_api_get_command_effect(lua_State* l) { Savegame& savegame = check_game(l, 1); GameCommands::Command command = check_enum<GameCommands::Command>( l, 2, GameCommands::command_names); Game* game = savegame.get_game(); if (game == NULL) { lua_pushnil(l); } else { std::string effect_name; switch (command) { case GameCommands::ACTION: { KeysEffect::ActionKeyEffect effect = game->get_keys_effect().get_action_key_effect(); effect_name = KeysEffect::get_action_key_effect_name(effect); break; } case GameCommands::ATTACK: { KeysEffect::SwordKeyEffect effect = game->get_keys_effect().get_sword_key_effect(); effect_name = KeysEffect::get_sword_key_effect_name(effect); break; } case GameCommands::ITEM_1: { effect_name = game->is_suspended() ? "" : "use_item_1"; break; } case GameCommands::ITEM_2: { effect_name = game->is_suspended() ? "" : "use_item_2"; break; } case GameCommands::PAUSE: { KeysEffect::PauseKeyEffect effect = game->get_keys_effect().get_pause_key_effect(); effect_name = KeysEffect::get_pause_key_effect_name(effect); break; } case GameCommands::RIGHT: { effect_name = game->is_suspended() ? "" : "move_right"; break; } case GameCommands::UP: { effect_name = game->is_suspended() ? "" : "move_up"; break; } case GameCommands::LEFT: { effect_name = game->is_suspended() ? "" : "move_left"; break; } case GameCommands::DOWN: { effect_name = game->is_suspended() ? "" : "move_down"; break; } default: Debug::die(StringConcat() << "Invalid game command: " << command); } if (effect_name.empty()) { lua_pushnil(l); } else { push_string(l, effect_name); } } return 1; }