lua_sticky_candidate_action_wrapper( rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx) : lua_candidate_action_wrapper(context, cfg, lua_ai_ctx) , bound_unit_() { map_location loc(cfg["unit_x"], cfg["unit_y"], wml_loc()); // lua and c++ coords differ by one bound_unit_ = unit_ptr(new unit(*resources::units->find(loc))); }
temporary_unit_mover::temporary_unit_mover(game_board& b, const map_location& src, const map_location& dst) : m_(b.units_), src_(src), dst_(dst), old_moves_(-1), temp_(src == dst ? unit_ptr() : m_.extract(dst)) { m_.move(src_, dst_); }
/** * Constructor * This version does not change (nor restore) the unit's movement. */ temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src, const map_location& dst) : m_(m), src_(src), dst_(dst), old_moves_(-1), temp_(src == dst ? unit_ptr() : m_.extract(dst)) { m_.move(src_, dst_); }
unit_ptr unit_map::extract(const map_location &loc) { self_check(); t_lmap::iterator i = lmap_.find(loc); if (i == lmap_.end()) { return unit_ptr(); } t_umap::iterator uit(i->second); unit_ptr u = uit->second.unit; size_t uid( u->underlying_id() ); DBG_NG << "Extract unit " << uid << " - " << u->id() << " from location: (" << loc << ")\n"; assert(uit->first == uit->second.unit->underlying_id()); if(uit->second.ref_count == 0){ umap_.erase(uit); } else { //Soft extraction keeps the old lit item if any iterators reference it uit->second.unit.reset(); } lmap_.erase(i); self_check(); return u; }
std::pair<unit_map::unit_iterator, bool> unit_map::add(const map_location &l, const unit &u) { self_check(); unit_ptr p = unit_ptr (new unit(u)); //TODO: should this instead take a shared pointer to a unit, rather than make a copy? p->set_location(l); std::pair<unit_map::unit_iterator, bool> res( insert(p) ); if(res.second == false) { p.reset(); } return res; }
recall::recall(size_t team_index, bool hidden, const unit& unit, const map_location& recall_hex): action(team_index,hidden), temp_unit_(new class unit(unit)), recall_hex_(recall_hex), fake_unit_(unit_ptr( new class unit(unit) ) ) { this->init(); }
unit_ptr move::get_unit() const { unit_map::iterator itor = resources::units->find(unit_underlying_id_); if (itor.valid()) return itor.get_shared_ptr(); else return unit_ptr(); }
move::move(config const& cfg, bool hidden) : action(cfg,hidden) , unit_underlying_id_(0) , unit_id_() , route_(new pathfind::marked_route()) , movement_cost_(0) , turn_number_(0) , arrow_(new arrow(hidden)) , fake_unit_() , arrow_brightness_() , arrow_texture_() , mover_() , fake_unit_hidden_(false) { // Construct and validate unit_ unit_map::iterator unit_itor = resources::units->find(cfg["unit_"]); if(unit_itor == resources::units->end()) throw action::ctor_err("move: Invalid underlying_id"); unit_underlying_id_ = unit_itor->underlying_id(); // Construct and validate route_ config const& route_cfg = cfg.child("route_"); if(!route_cfg) throw action::ctor_err("move: Invalid route_"); route_->move_cost = route_cfg["move_cost"]; BOOST_FOREACH(config const& loc_cfg, route_cfg.child_range("step")) { route_->steps.push_back(map_location(loc_cfg["x"],loc_cfg["y"])); } BOOST_FOREACH(config const& mark_cfg, route_cfg.child_range("mark")) { route_->marks[map_location(mark_cfg["x"],mark_cfg["y"])] = pathfind::marked_route::mark(mark_cfg["turns"], mark_cfg["zoc"].to_bool(), mark_cfg["capture"].to_bool(), mark_cfg["invisible"].to_bool()); } // Validate route_ some more std::vector<map_location> const& steps = route_->steps; if(steps.empty()) throw action::ctor_err("move: Invalid route_"); // Construct arrow_ arrow_->set_color(team::get_side_color_index(side_number())); arrow_->set_style(arrow::STYLE_STANDARD); arrow_->set_path(route_->steps); // Construct fake_unit_ fake_unit_ = fake_unit_ptr( unit_ptr(new unit(*get_unit())) , resources::fake_units ); if(hidden) fake_unit_->set_hidden(true); fake_unit_->anim_comp().set_ghosted(true); unit_display::move_unit(route_->steps, fake_unit_.get_unit_ptr(), false); //get facing right fake_unit_->set_location(route_->steps.back()); this->init(); }
unit_ptr recall_list_manager::find_if_matches_underlying_id(size_t uid) { std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(), boost::bind(&find_if_matches_uid_helper, _1, uid)); if (it != recall_list_.end()) { return *it; } else { return unit_ptr(); } }
/** * Used to find units in vectors by their ID. */ unit_const_ptr recall_list_manager::find_if_matches_id(const std::string &unit_id) const { std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(), boost::bind(&find_if_matches_helper, _1, unit_id)); if (it != recall_list_.end()) { return *it; } else { return unit_ptr(); } }
recruit::recruit(size_t team_index, bool hidden, const std::string& unit_name, const map_location& recruit_hex): action(team_index,hidden), unit_name_(unit_name), recruit_hex_(recruit_hex), temp_unit_(create_corresponding_unit()), //auto-ptr ownership transfer fake_unit_(unit_ptr(new unit(*temp_unit_))), //temp_unit_ *copied* into new fake unit cost_(0) { this->init(); }
unit_const_ptr recall_list_manager::find_if_matches_underlying_id(size_t uid) const { std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(), [uid](const unit_ptr & ptr) { return ptr->underlying_id() == uid; }); if (it != recall_list_.end()) { return *it; } else { return unit_ptr(); } }
/** * Used to find units in vectors by their ID. */ unit_const_ptr recall_list_manager::find_if_matches_id(const std::string &unit_id) const { std::vector<unit_ptr >::const_iterator it = std::find_if(recall_list_.begin(), recall_list_.end(), [&unit_id](const unit_ptr & ptr) { return ptr->matches_id(unit_id); }); if (it != recall_list_.end()) { return *it; } else { return unit_ptr(); } }
unit_ptr recall_list_manager::extract_if_matches_id(const std::string &unit_id) { std::vector<unit_ptr >::iterator it = std::find_if(recall_list_.begin(), recall_list_.end(), boost::bind(&find_if_matches_helper, _1, unit_id)); if (it != recall_list_.end()) { unit_ptr ret = *it; recall_list_.erase(it); return ret; } else { return unit_ptr(); } }
temporary_unit_mover::temporary_unit_mover(game_board& b, const map_location& src, const map_location& dst, int new_moves) : m_(b.units_), src_(src), dst_(dst), old_moves_(-1), temp_(src == dst ? unit_ptr() : m_.extract(dst)) { std::pair<unit_map::iterator, bool> move_result = m_.move(src_, dst_); // Set the movement. if ( move_result.second ) { old_moves_ = move_result.first->movement_left(true); move_result.first->set_movement(new_moves); } }
recruit::recruit(const config& cfg, bool hidden) : action(cfg,hidden) , unit_name_(cfg["unit_name_"]) , recruit_hex_(cfg.child("recruit_hex_")["x"],cfg.child("recruit_hex_")["y"], wml_loc()) , temp_unit_() , fake_unit_() , cost_(0) { // Validate unit_name_ if(!unit_types.find(unit_name_)) throw action::ctor_err("recruit: Invalid recruit unit type"); // Construct temp_unit_ and fake_unit_ temp_unit_ = create_corresponding_unit(); //auto-ptr ownership transfer fake_unit_.reset(unit_ptr (new unit(*temp_unit_))), //temp_unit_ copied into new fake_unit this->init(); }
/* Note: Hide the unit in its current location; do not actually remove it. * Otherwise the status displays will be wrong during the movement. */ void unit_mover::replace_temporary(unit_ptr u) { if ( disp_ == nullptr ) // No point in creating a temp unit with no way to display it. return; // Save the hidden state of the unit. was_hidden_ = u->get_hidden(); // Make our temporary unit mostly match u... temp_unit_ptr_ = fake_unit_ptr(unit_ptr(new unit(*u)), resources::fake_units); // ... but keep the temporary unhidden and hide the original. temp_unit_ptr_->set_hidden(false); u->set_hidden(true); // Update cached data. is_enemy_ = (*resources::teams)[u->side()-1].is_enemy(disp_->viewing_side()); }
recall::recall(config const& cfg, bool hidden) : action(cfg,hidden) , temp_unit_() , recall_hex_(cfg.child("recall_hex_")["x"],cfg.child("recall_hex_")["y"], wml_loc()) , fake_unit_() { // Construct and validate temp_unit_ size_t underlying_id = cfg["temp_unit_"]; for(const unit_const_ptr & recall_unit : resources::gameboard->teams().at(team_index()).recall_list()) { if(recall_unit->underlying_id()==underlying_id) { temp_unit_.reset(new class unit(*recall_unit)); //TODO: is it necessary to make a copy? break; } } if(!temp_unit_.get()) { throw action::ctor_err("recall: Invalid underlying_id"); } fake_unit_.reset(unit_ptr(new class unit(*temp_unit_))); //makes copy of temp_unit_ this->init(); }
void clear_ref() { uid = 0; ptr = unit_ptr(); side = 0; c_ptr = NULL; }