void item_list_gen2_keyitemimpl::_to_native(
        int index
    )
    {
        boost::lock_guard<item_list_gen2_keyitemimpl> lock(*this);

        if(index >= 0)
        {
            _pksav_list.item_indices[index] = uint8_t(pkmn::database::item_entry(
                                                          _item_slots[index].item,
                                                          get_game()
                                                      ).get_item_index());
        }
        else
        {
            for(int item_index = 0; item_index < _capacity; ++item_index)
            {
                _pksav_list.item_indices[item_index] = uint8_t(pkmn::database::item_entry(
                                                                   _item_slots[item_index].item,
                                                                   get_game()
                                                               ).get_item_index());
            }
        }

        _pksav_list.count = uint8_t(_num_items);
    }
/**
 * \brief Returns the current map, creating it if necessary.
 */
Map& TestEnvironment::get_map() {

    if (!get_game().has_current_map()) {
        step();  // Advance one tick to start the map.
        Debug::check_assertion(get_game().has_current_map(), "Missing map");
    }

    return get_game().get_current_map();
}
Example #3
0
/**
 * \brief Notifies this detector that the player is interacting with it by
 * pressing the action command.
 *
 * This function is called when the player presses the action command
 * while the hero is facing this detector, and the action command effect lets
 * him do this.
 */
void Destructible::notify_action_command_pressed() {

  KeysEffect::ActionKeyEffect effect = get_keys_effect().get_action_key_effect();

  if ((effect == KeysEffect::ACTION_KEY_LIFT || effect == KeysEffect::ACTION_KEY_LOOK)
      && features[subtype].can_be_lifted
      && !is_being_cut
      && !is_disabled()
      && !is_regenerating) {

    int weight = features[subtype].weight;

    if (get_equipment().has_ability("lift", weight)) {

      uint32_t explosion_date = can_explode() ? System::now() + 6000 : 0;
      get_hero().start_lifting(new CarriedItem(
          get_hero(),
          *this,
          get_animation_set_id(),
          get_destruction_sound_id(),
          get_damage_on_enemies(),
          explosion_date)
      );

      // play the sound
      Sound::play("lift");

      // create the pickable item
      create_pickable();

      // remove the item from the map
      if (!features[subtype].can_regenerate) {
        destruction_callback();
        remove_from_map();
      }
      else {
        // the item can actually regenerate
        play_destroy_animation();
      }
    }
    else {
      if (features[subtype].can_be_cut
          && !features[subtype].can_explode
          && !get_equipment().has_ability("sword", 1)) {
        get_game().start_dialog("_cannot_lift_should_cut", LUA_REFNIL);
      }
      else if (!get_equipment().has_ability("lift", 1)) {
        get_game().start_dialog("_cannot_lift_too_heavy", LUA_REFNIL);
      }
      else {
        get_game().start_dialog("_cannot_lift_still_too_heavy", LUA_REFNIL);
      }
    }
  }
}
Example #4
0
/**
 * @brief Updates the entity.
 */
void ShopItem::update() {

  if (is_looking_item && !get_game().is_dialog_enabled()) {

    // the description message has just finished
    const std::string question_dialog_id = "_shop.question";
    get_dialog_box().start_dialog(question_dialog_id);
    get_dialog_box().set_variable(question_dialog_id, price);
    is_asking_question = true;
    is_looking_item = false;
  }
  else if (is_asking_question && !get_game().is_dialog_enabled()) {

    // the question has just finished
    is_asking_question = false;
    int answer = get_dialog_box().get_last_answer();

    if (answer == 0) {

      // the player wants to buy the item
      Equipment& equipment = get_equipment();
      EquipmentItem& item = treasure.get_item();

      if (equipment.get_money() < price) {
        // not enough rupees
        Sound::play("wrong");
        get_dialog_box().start_dialog("_shop.not_enough_money");
      }
      else if (item.has_amount() && item.get_amount() >= item.get_max_amount()) {
        // the player already has the maximum amount of this item
        Sound::play("wrong");
        get_dialog_box().start_dialog("_shop.amount_full");
      }
      else {

        bool can_buy = get_lua_context().shop_item_on_buying(*this);
        if (can_buy) {

          // give the treasure
          equipment.remove_money(price);

          get_hero().start_treasure(treasure, LUA_REFNIL);
          if (treasure.is_saved()) {
            remove_from_map();
            get_savegame().set_boolean(treasure.get_savegame_variable(), true);
          }
          get_lua_context().shop_item_on_bought(*this);
        }
      }
    }
  }
}
Example #5
0
/**
 * \brief Gives the item to the player.
 */
void Pickable::try_give_item_to_player() {

  EquipmentItem& item = treasure.get_item();

  if (!can_be_picked
      || given_to_player
      || get_game().is_dialog_enabled()
      || !get_hero().can_pick_treasure(item)) {
    return;
  }

  given_to_player = true;

  remove_from_map();

  // play the sound
  const std::string& sound_id = item.get_sound_when_picked();
  if (!sound_id.empty()) {
    Sound::play(sound_id);
  }

  // give the item
  if (item.get_brandish_when_picked()) {
    // The treasure is brandished.
    // on_obtained() will be called after the dialog.
    get_hero().start_treasure(treasure, ScopedLuaRef());
  }
  else {
    treasure.give_to_player();

    // Call on_obtained() immediately since the treasure is not brandished.
    get_lua_context().item_on_obtained(item, treasure);
    get_lua_context().map_on_obtained_treasure(get_map(), treasure);
  }
}
Example #6
0
/**
 * \copydoc Detector::notify_action_command_pressed
 */
bool Chest::notify_action_command_pressed() {

  if (is_enabled() &&
      get_hero().is_free() &&
      get_keys_effect().get_action_key_effect() != KeysEffect::ACTION_KEY_NONE
  ) {

    if (can_open()) {
      Sound::play("chest_open");
      set_open(true);
      treasure_date = System::now() + 300;

      get_keys_effect().set_action_key_effect(KeysEffect::ACTION_KEY_NONE);
      get_hero().start_freezed();
    }
    else if (!get_cannot_open_dialog_id().empty()) {
      Sound::play("wrong");
      get_game().start_dialog(get_cannot_open_dialog_id(), ScopedLuaRef(), ScopedLuaRef());
    }

    return true;
  }

  return false;
}
Example #7
0
/**
 * \brief Notifies this detector that the player is interacting with it by
 * pressing the action command.
 *
 * This function is called when the player presses the action command
 * while the hero is facing this detector, and the action command effect lets
 * him do this.
 */
void Crystal::notify_action_command_pressed() {

  if (get_hero().is_free()) {
    get_keys_effect().set_action_key_effect(KeysEffect::ACTION_KEY_NONE);

    // start a dialog
    get_game().start_dialog("_crystal", LUA_REFNIL);
  }
}
Example #8
0
/**
 * \brief Makes the teletransporter move the hero to the destination.
 * \param hero the hero
 */
void Teletransporter::transport_hero(Hero& hero) {

  if (transporting_hero) {
    // already done
    return;
  }

  std::string name = destination_name;
  int hero_x = hero.get_x();
  int hero_y = hero.get_y();

  if (is_on_map_side()) {

    // special destination point: side of the map
    // we determine the appropriate side based on the teletransporter's position;
    // we also place the hero on the old map so that its position corresponds to the new map

    switch (destination_side) {

    case 0:
      name += '0'; // scroll to the west
      hero_x = 0;
      break;

    case 1:
      name += '1'; // scroll to the south
      hero_y = get_map().get_height() + 5;
      break;

    case 2:
      name += '2'; // scroll to the east
      hero_x = get_map().get_width();
      break;

    case 3:
      name += '3'; // scroll to the north
      hero_y = 5;
      break;

    default:
      Debug::die(std::string("Bad destination side for teletransporter '")
          + get_name() + "'");
    }
  }

  transporting_hero = true;

  get_lua_context()->teletransporter_on_activated(*this);

  if (!sound_id.empty()) {
    Sound::play(sound_id);
  }

  get_game().set_current_map(destination_map_id, name, transition_style);
  hero.set_xy(hero_x, hero_y);
}
Example #9
0
GameObjPtr GameObjFactory::construct_obj( const Name& classname, const SpawnParams& params )
{

    
    auto it = fe_classes.find(classname);
    if ( it != fe_classes.end() )
    {
        GameObjPtr obj((GameObjBase*)it->second->constructor(params));
        if ( get_game() && get_game()->is_editor() )
        {
            obj->set_editor_proxy(new EditorProxy());
            obj->get_editor_proxy()->set_object(obj);
        }
        obj->set_class_info(it->second);
        return obj;
    }
    logger("GameObjFactory") << "constructor of class:<" << classname <<"> not found." << endl;
    return GameObjPtr(nullptr);
}
Example #10
0
/**
 * @brief Returns whether a timer just created should be initially suspended.
 * @return true to initially suspend a new timer
 */
bool Script::is_new_timer_suspended(void) {

  if (apis_enabled && GAME_API) {
    // start the timer even if the game is suspended (e.g. a timer started during a camera movement)
    // except when it is suspended because of a dialog box
    return get_game().is_showing_dialog();
  }

  return false;
}
Example #11
0
/**
 * \brief Notifies this entity that its map has just become active.
 */
void Crystal::notify_map_started() {

  Detector::notify_map_started();

  bool state = get_game().get_crystal_state();
  if (state != this->state) {

    this->state = state;
    get_sprite().set_current_animation(state ? "blue_lowered" : "orange_lowered");
  }
}
Example #12
0
/**
 * \copydoc Entity::notify_action_command_pressed
 */
bool Npc::notify_action_command_pressed() {

  Hero& hero = get_hero();
  if (hero.is_free() &&
      get_commands_effects().get_action_key_effect() != CommandsEffects::ACTION_KEY_NONE
  ) {

    CommandsEffects::ActionKeyEffect effect = get_commands_effects().get_action_key_effect();
    get_commands_effects().set_action_key_effect(CommandsEffects::ACTION_KEY_NONE);

    SpritePtr sprite = get_sprite();

    // if this is a usual NPC, look towards the hero
    if (subtype == USUAL_NPC) {
      int direction = (get_hero().get_animation_direction() + 2) % 4;
      if (sprite != nullptr) {
        sprite->set_current_direction(direction);
      }
    }

    if (effect != CommandsEffects::ACTION_KEY_LIFT) {
      // start the normal behavior
      if (behavior == BEHAVIOR_DIALOG) {
        get_game().start_dialog(dialog_to_show, ScopedLuaRef(), ScopedLuaRef());
      }
      else {
        call_script_hero_interaction();
      }
      return true;
    }
    else {
      // lift the entity
      if (get_equipment().has_ability(Ability::LIFT)) {

        std::string animation_set_id = "stopped";
        if (sprite != nullptr) {
          animation_set_id = sprite->get_animation_set_id();
        }
        hero.start_lifting(std::make_shared<CarriedObject>(
            hero,
            *this,
            animation_set_id,
            "stone",
            2,
            0)
        );
        Sound::play("lift");
        remove_from_map();
        return true;
      }
    }
  }
  return false;
}
Example #13
0
/**
 * \brief Adds to the map the pickable treasure (if any) hidden under this destructible item.
 */
void Destructible::create_treasure() {

  get_entities().add_entity(Pickable::create(
      get_game(),
      "",
      get_layer(),
      get_xy(),
      treasure,
      FALLING_MEDIUM,
      false
  ));
}
Example #14
0
    void pokemon_box_gcnimpl::set_pokemon(
        int index,
        const pkmn::pokemon::sptr& new_pokemon
    )
    {
        int max_index = get_capacity();
        pkmn::enforce_bounds("Box index", index, 0, max_index);

        if(_pokemon_list.at(index)->get_native_pc_data() == new_pokemon->get_native_pc_data())
        {
            throw std::invalid_argument("Cannot set a Pokémon to itself.");
        }

        boost::lock_guard<pokemon_box_gcnimpl> lock(*this);

        // If the given Pokémon isn't from this box's game, convert it if we can.
        pkmn::pokemon::sptr actual_new_pokemon;
        if(_game_id == new_pokemon->get_database_entry().get_game_id())
        {
            actual_new_pokemon = new_pokemon;
        }
        else
        {
            actual_new_pokemon = new_pokemon->to_game(get_game());
        }

        // Make sure no one else is using the new Pokémon variable.
        pokemon_gcnimpl* p_new_pokemon = dynamic_cast<pokemon_gcnimpl*>(
                                             actual_new_pokemon.get()
                                         );
        BOOST_ASSERT(p_new_pokemon != nullptr);
        boost::lock_guard<pokemon_gcnimpl> new_pokemon_lock(*p_new_pokemon);

        // Copy the underlying memory to the party. At the end of this process,
        // all existing variables will correspond to the same Pokémon, even if
        // their underlying memory has changed.
        //
        // Note: as we control the implementation, we know the PC data points
        // to the whole Pokémon data structure.
        delete _libpkmgc_box_uptr->pkm[index];

        _libpkmgc_box_uptr->pkm[index] =
            static_cast<LibPkmGC::GC::Pokemon*>(
                actual_new_pokemon->get_native_pc_data()
            )->clone();
        _pokemon_list[index] = std::make_shared<pokemon_gcnimpl>(
                                   dynamic_cast<LibPkmGC::GC::Pokemon*>(
                                       _libpkmgc_box_uptr->pkm[index]
                                   ),
                                   _game_id
                               );
    }
Example #15
0
/**
 * \brief Sets the level of the specified ability.
 * \param ability_name the ability to set
 * \param level the level of this ability
 */
void Equipment::set_ability(const std::string& ability_name, int level) {

  savegame.set_integer(get_ability_savegame_variable(ability_name), level);

  Game* game = get_game();
  if (game != NULL) {
    if (ability_name == "tunic" ||
        ability_name == "sword" ||
        ability_name == "shield") {
      // The hero's sprites depend on these abilities.
      game->get_hero().rebuild_equipment();
    }
  }
}
Example #16
0
/**
 * \copydoc Detector::notify_action_command_pressed
 */
bool Crystal::notify_action_command_pressed() {

    if (get_hero().is_free() &&
            get_keys_effect().get_action_key_effect() == KeysEffect::ACTION_KEY_LOOK
       ) {
        get_keys_effect().set_action_key_effect(KeysEffect::ACTION_KEY_NONE);

        // start a dialog
        get_game().start_dialog("_crystal", ScopedLuaRef(), ScopedLuaRef());
        return true;
    }

    return false;
}
Example #17
0
/**
 * @brief This function is called by the engine when an entity overlaps the shop item.
 *
 * If the entity is the hero, we allow him to buy the item.
 *
 * @param entity_overlapping the entity overlapping the detector
 * @param collision_mode the collision mode that detected the collision
 */
void ShopItem::notify_collision(MapEntity &entity_overlapping, CollisionMode collision_mode) {

  if (entity_overlapping.is_hero() && !get_game().is_suspended()) {

    Hero &hero = (Hero&) entity_overlapping;

    if (get_keys_effect().get_action_key_effect() == KeysEffect::ACTION_KEY_NONE
        && hero.is_free()) {

      // we show the 'look' icon
      get_keys_effect().set_action_key_effect(KeysEffect::ACTION_KEY_LOOK);
    }
  }
}
Example #18
0
/**
 * \brief Sets the level of the specified ability.
 * \param ability The ability to set.
 * \param level The level of this ability.
 */
void Equipment::set_ability(Ability ability, int level) {

  savegame.set_integer(get_ability_savegame_variable(ability), level);

  Game* game = get_game();
  if (game != nullptr) {
    if (ability == Ability::TUNIC ||
        ability == Ability::SWORD ||
        ability == Ability::SHIELD) {
      // The hero's sprites may depend on these abilities.
      game->get_hero()->rebuild_equipment();
    }
  }
}
Example #19
0
/**
 * \brief This function is called by the engine when an entity overlaps the shop item.
 *
 * If the entity is the hero, we allow him to buy the item.
 *
 * \param entity_overlapping the entity overlapping the detector
 * \param collision_mode the collision mode that detected the collision
 */
void ShopTreasure::notify_collision(
    MapEntity& entity_overlapping, CollisionMode /* collision_mode */) {

  if (entity_overlapping.is_hero() && !get_game().is_suspended()) {

    Hero& hero = static_cast<Hero&>(entity_overlapping);

    if (get_keys_effect().get_action_key_effect() == KeysEffect::ACTION_KEY_NONE
        && hero.is_free()) {

      // we show the 'look' icon
      get_keys_effect().set_action_key_effect(KeysEffect::ACTION_KEY_LOOK);
    }
  }
}
Example #20
0
/**
 * \brief Activates the crystal if the delay since the last activation allows it.
 * \param entity_activating the entity that activates this crystal
 */
void Crystal::activate(MapEntity& entity_activating) {

  bool recently_activated = false;
  std::list<MapEntity*>::iterator it;
  for (it = entities_activating.begin(); it != entities_activating.end() && !recently_activated; it++) {
    recently_activated  = (*it == &entity_activating);
  }

  uint32_t now = System::now();
  if (!recently_activated || now >= next_possible_hit_date) {
    Sound::play("switch");
    get_game().change_crystal_state();
    next_possible_hit_date = now + 1000;
    entities_activating.push_back(&entity_activating);
  }
}
Example #21
0
static void			key_callback(
	GLFWwindow *window, int key, int scancode, int action, int mods)
{
	t_game		*game;
	t_keys		*keys;
	t_pad		*pad;

	(void)scancode;
	(void)mods;
	game = get_game();
	keys = game->keys;
	pad = game->levels[game->cur_level_index]->pad;
	handle_arrow_keys(keys, key, action);
	if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
		glfwSetWindowShouldClose(window, GL_TRUE);
	if (key == GLFW_KEY_2 && action == GLFW_PRESS)
		load_level(game, 2);
}
Example #22
0
/**
 * \brief Activates the crystal if the delay since the last activation allows it.
 * \param entity_activating the entity that activates this crystal
 */
void Crystal::activate(Entity& entity_activating) {

    bool recently_activated = false;
    for (Entity* entity: entities_activating) {
        if (entity == &entity_activating) {
            recently_activated = true;
            break;
        }
    }

    uint32_t now = System::now();
    if (!recently_activated || now >= next_possible_hit_date) {
        Sound::play("switch");
        get_game().change_crystal_state();
        next_possible_hit_date = now + 1000;
        entities_activating.push_back(&entity_activating);
    }
}
Example #23
0
/**
 * \brief Updates the entity.
 */
void CrystalBlock::update() {

  // see if the state has to be changed
  bool orange_raised = get_game().get_crystal_state();
  if (orange_raised != this->orange_raised) {

    this->orange_raised = orange_raised;

    if (subtype == ORANGE) {
      get_sprite().set_current_animation(orange_raised ? "orange_raised" : "orange_lowered");
    }
    else {
      get_sprite().set_current_animation(orange_raised ? "blue_lowered" : "blue_raised");
    }
  }
  get_sprite().update();

  MapEntity::update();
}
Example #24
0
/**
 * \brief Activates this sensor.
 *
 * This function is called when the hero overlaps the sensor.
 *
 * \param hero the hero
 */
void Sensor::activate(Hero& hero) {

  if (!activated_by_hero) {

    activated_by_hero = true;

    // Notify Lua.
    notifying_script = true;
    get_lua_context().sensor_on_activated(*this);
    notifying_script = false;
  }
  else {
    if (!notifying_script && !get_game().is_suspended()) {
      notifying_script = true;
      get_lua_context().sensor_on_activated_repeat(*this);
      notifying_script = false;
    }
  }
}
std::shared_ptr<CustomEntity> TestEnvironment::make_entity<CustomEntity>(
    const Point& xy,
    int layer
) {

    std::shared_ptr<CustomEntity> entity = std::make_shared<CustomEntity>(
            get_game(),
            "",
            0,
            layer,
            xy,
            Size(16, 16),
            "",
            ""
                                           );
    get_entities().add_entity(entity);

    return entity;
}
    void item_list_gen2_keyitemimpl::add(
        pkmn::e_item item,
        int amount
    )
    {
        if(amount != 1)
        {
            throw std::out_of_range("Amount: valid value 1");
        }

        pkmn::database::item_entry entry(item, get_game());
        if(entry.get_pocket() != get_name())
        {
            throw std::invalid_argument("This item is not valid for this list.");
        }

        boost::lock_guard<item_list_gen2_keyitemimpl> lock(*this);

        /*
         * Check if this item is already in the list. If so, it cannot
         * be added. If not, see if there's room to add another
         * item.
         */
        for(int item_index = 0; item_index < _num_items; ++item_index)
        {
            if(_item_slots[item_index].item == item)
            {
                throw std::invalid_argument("This item is already in this pocket.");
            }
        }

        if(_num_items < _capacity)
        {
            _item_slots[_num_items].item = item;
            _item_slots[_num_items].amount = amount;
            _to_native(_num_items++);
        }
        else
        {
            throw std::runtime_error("Cannot add any more items.");
        }
    }
Example #27
0
// Entry point into the application
int WINAPI WinMain (HINSTANCE hInstance, 
					HINSTANCE hPrevInstance,
                    PSTR szCmdLine, 
					int nCmdShow)	
{
	g_app_instance = hInstance;

	//use the factory to create our objects.
	IGame * game = get_game();
	IWindow * window = get_game_window( game );
	g_input = get_game_input();

	window->Run();			// Run the application

	// Cleanup our objects.
	release_game_input(&g_input);
	release_game_window( &window );
	release_game( &game );

	return 0;
}
Example #28
0
/**
 * @brief Updates the entity.
 */
void CrystalSwitch::update() {

  bool state = get_game().get_crystal_switch_state();
  if (state != this->state) {

    this->state = state;
    get_sprite().set_current_animation(state ? "blue_lowered" : "orange_lowered");
  }

  star_sprite->update();
  if (star_sprite->is_animation_finished()) {
    twinkle();
  }

  uint32_t now = System::now();
  if (now >= next_possible_hit_date) {
    entities_activating.clear();
  }
 
  MapEntity::update();
}
Example #29
0
File: ball.c Project: akabab/atari
void				update_ball(t_ball *ball, t_list_node *bricks)
{
	int				i;
	t_game			*game;

	game = get_game();
	i = 0;
	while (i < 10)
	{
		ball->x += (ball->speedx) / 10.0f;
		ball->y += (ball->speedy) / 10.0f;
		handle_bounds(ball);
		if (!INVINCIBLE_MODE && is_ball_outside(ball))
		{
			game->cur_level->lives--;
			reset_ball(ball);
		}
		check_collisions(ball, bricks);
		i++;
	}
}
Example #30
0
int		wait_game_start()
{
  int		cont;
  t_lemipc	*game;

  cont = 1;
  printf("Waiting for another team\n");
  while (cont)
    {
      if ((game = get_game()) == NULL)
	return (my_puterr("Can't retrieve shared game\n"));
      if (sem_lock())
	return (my_puterr("Can't lock semaphore\n"));
      cont = should_continue_wait(game);
      if (sem_unlock())
	return (my_puterr("Can't unlock semaphore\n"));
      release_game(game);
    }
  printf("Lets do this !!\n");
  return (0);
}