Commit 66c071a2 authored by louiz’'s avatar louiz’

Remove all the stupid pattern get_next_something to loop around lists

fixes #2392
parent d4dc209a
......@@ -15,16 +15,7 @@ void Replay::insert_action(Action* action)
this->actions.push_back(action);
}
void Replay::reset_action_iterator()
std::vector<Action*>& Replay::get_actions()
{
this->actions_iterator = this->actions.begin();
}
Action* Replay::get_next_action()
{
if (this->actions_iterator == this->actions.end())
return nullptr;
Action* action = *this->actions_iterator;
++this->actions_iterator;
return action;
return this->actions;
}
......@@ -23,18 +23,13 @@ public:
* Insert an action at the end of the action list.
*/
void insert_action(Action*);
/**
* Put the iterator at the begining of the action list.
*/
void reset_action_iterator();
Action* get_next_action();
std::vector<Action*>& get_actions();
private:
Replay(const Replay&);
Replay& operator=(const Replay&);
std::vector<Action*> actions;
std::vector<Action*>::iterator actions_iterator;
};
#endif // __REPLAY_HPP__
......
......@@ -48,23 +48,6 @@ bool Turn::is_validated() const
return false;
}
Action* Turn::get_next_action()
{
if (this->actions_iterator == this->actions.end())
{
this->actions_iterator = this->actions.begin();
return 0;
}
Action* action = *this->actions_iterator;
++this->actions_iterator;
return action;
}
void Turn::reset_action_iterator()
{
this->actions_iterator = this->actions.begin();
}
bool Turn::validate(const unsigned long int by,
const unsigned int confirmations_needed)
{
......@@ -86,10 +69,9 @@ void Turn::validate_completely()
std::ostream& operator<<(std::ostream& os, Turn& turn)
{
turn.reset_action_iterator();
const Action* action;
os << "[" << turn.is_validated() << "]";
while ((action = turn.get_next_action()) != 0)
for (const auto& action: turn.get_actions())
os << *action;
return os;
}
......@@ -98,3 +80,8 @@ unsigned int Turn::get_number_of_validations() const
{
return this->ready_clients.size();
}
std::vector<Action*>& Turn::get_actions()
{
return this->actions;
}
......@@ -12,8 +12,6 @@ public:
void execute(bool delete_actions = true);
void insert(Action*);
bool is_validated() const;
Action* get_next_action();
void reset_action_iterator();
/**
* Validate the turn for the given client id.
* If the number of ready clients becomes equal to the given number needed,
......@@ -29,10 +27,10 @@ public:
* Just set the validated bool to true.
*/
void validate_completely();
std::vector<Action*>& get_actions();
private:
std::vector<Action*> actions;
std::vector<Action*>::const_iterator actions_iterator;
std::vector<unsigned long int> ready_clients;
bool validated;
......
......@@ -127,23 +127,17 @@ Turn* TurnHandler::get_turn(const unsigned int number)
bool TurnHandler::validate_action(const unsigned int id, const unsigned long int by)
{
std::deque<Turn>::iterator it;
Action* action;
bool res;
for (it = this->turns.begin(); it != this->turns.end(); ++it)
{
(*it).reset_action_iterator();
while ((action = (*it).get_next_action()) != nullptr)
for (auto& turn: this->turns)
for (auto& action: turn.get_actions())
if (action->get_id() == id)
{
if (action->get_id() == id)
{
res = action->validate(by);
if (res == true)
this->replay->insert_action(action);
return res;
}
res = action->validate(by);
if (res == true)
this->replay->insert_action(action);
return res;
}
}
log_warning("Action to validate was not found");
// Action was not found, so it did'nt became completely validated
return false;
......@@ -153,18 +147,10 @@ void TurnHandler::completely_validate_action(const unsigned int id)
{
std::deque<Turn>::iterator it;
Action* action;
for (it = this->turns.begin(); it != this->turns.end(); ++it)
{
(*it).reset_action_iterator();
while ((action = (*it).get_next_action()) != nullptr)
{
if (action->get_id() == id)
{
action->validate_completely();
}
}
}
for (auto& turn: this->turns)
for (auto& action: turn.get_actions())
if (action->get_id() == id)
action->validate_completely();
}
bool TurnHandler::validate_turn(const unsigned int number,
......@@ -186,6 +172,11 @@ unsigned long TurnHandler::get_current_turn()
return this->current_turn;
}
std::deque<Turn>& TurnHandler::get_turns()
{
return this->turns;
}
void TurnHandler::set_next_turn_callback(t_next_turn_callback callback)
{
this->next_turn_callback = callback;
......@@ -217,20 +208,3 @@ std::ostream& operator<<(std::ostream& os, TurnHandler& turn_handler)
}
return os;
}
void TurnHandler::reset_turns_iterator()
{
this->turns_iterator = this->turns.begin();
}
Turn* TurnHandler::get_next_turn()
{
if (this->turns_iterator == this->turns.end())
{
this->reset_turns_iterator();
return nullptr;
}
std::deque<Turn>::iterator it = this->turns_iterator;
++this->turns_iterator;
return &(*it);
}
......@@ -89,8 +89,7 @@ public:
* Returns the turn we are currently at.
*/
unsigned long get_current_turn();
void reset_turns_iterator();
Turn* get_next_turn();
std::deque<Turn>& get_turns();
private:
TurnHandler(const TurnHandler&);
......@@ -103,7 +102,6 @@ private:
void next_turn();
std::deque<Turn> turns;
std::deque<Turn>::iterator turns_iterator;
unsigned long current_turn;
unsigned int turn_advancement;
......
......@@ -59,11 +59,10 @@ void GameServer::on_new_client(RemoteGameClient* new_client)
void GameServer::send_replay(RemoteGameClient* new_client)
{
Replay* replay = this->world->get_replay();
replay->reset_action_iterator();
Action* action;
Command* command;
ActionEvent* event;
while ((action = replay->get_next_action()) != nullptr)
for (auto& action: replay->get_actions())
{
event = action->get_event();
command = new Command;
......@@ -81,11 +80,10 @@ void GameServer::send_and_adjust_future_commands(RemoteGameClient* new_client)
ActionEvent* event;
TurnHandler* turn_handler = this->world->get_turn_handler();
unsigned int occupants = this->world->get_number_of_occupants();
turn_handler->reset_turns_iterator();
while ((turn = turn_handler->get_next_turn()) != nullptr)
for (auto& turn: turn_handler->get_turns())
{
turn->reset_action_iterator();
while ((action = turn->get_next_action()) != nullptr)
for (auto& action: turn.get_actions())
{
if (action->is_completely_validated() == false)
{
......@@ -107,17 +105,15 @@ void GameServer::adjust_and_revalidate_futur_commands()
TurnHandler* turn_handler = this->world->get_turn_handler();
unsigned int occupants = this->world->get_number_of_occupants();
unsigned long turn_id = this->world->get_turn_handler()->get_current_turn();
turn_handler->reset_turns_iterator();
while ((turn = turn_handler->get_next_turn()) != nullptr)
for (auto& turn: turn_handler->get_turns())
{
if (turn->get_number_of_validations() == occupants)
if (turn.get_number_of_validations() == occupants)
{
this->send_turn(turn_id, 0);
turn->validate_completely();
turn.validate_completely();
}
turn->reset_action_iterator();
while ((action = turn->get_next_action()) != nullptr)
for (auto& action: turn.get_actions())
{
if (action->is_completely_validated() == false)
{
......
......@@ -181,23 +181,6 @@ bool Map::get_layer_data(boost::property_tree::ptree& tree, std::string& data)
return false;
}
void Map::reset_layers_iterator()
{
this->layers_iterator = this->layers.begin();
}
Layer* Map::get_next_layer()
{
if (this->layers_iterator == this->layers.end())
{
this->layers_iterator = this->layers.begin();
return 0;
}
Layer* layer = *this->layers_iterator;
++this->layers_iterator;
return layer;
}
uint Map::get_height_in_pixels() const
{
return this->height;
......
......@@ -51,8 +51,6 @@ public:
uint get_width_in_pixels() const;
uint get_height_in_tiles() const;
uint get_width_in_tiles() const;
void reset_layers_iterator();
Layer* get_next_layer();
int get_max_level_for_cell(const uint cell) const;
/**
* Generates the walking map. This structure is used to know the height of
......@@ -110,8 +108,9 @@ protected:
/**
* The list of all layers. A layer can be NULL.
*/
public:
std::vector<Layer*> layers;
std::vector<Layer*>::const_iterator layers_iterator;
protected:
/**
* A representation of the map, containing 4 heights for each tile (one
* for each corner).
......
......@@ -73,18 +73,6 @@ public:
* we start the world normally to play the game at normal speed.
*/
void tick(bool force = false);
/**
* returns a pointer to the next entity of the world. If it returns NULL,
* this means that the list is over and the next call will restart from the
* beginning of the list.
* If the list is empty, it will always return NULL.
*/
Entity* get_next_entity();
/**
* Just like the other get_next_entity(), but it will also return
* NULL if the next entity's y is superior to the given y.
*/
Entity* get_next_entity(const int y);
/**
* Insert a unit in the unit list. It will also be added into the entity list.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment