made it so when unit is selected, pressing a number n on the keyboard will...

...show how far that unit can move in n turns
This commit is contained in:
Dave White 2003-10-19 09:20:44 +00:00
parent 5897e0a555
commit 33bc6567c1
5 changed files with 103 additions and 56 deletions

View file

@ -133,7 +133,7 @@ void find_routes(const gamemap& map, const game_data& gamedata,
int move_left,
std::map<gamemap::location,paths::route>& routes,
std::vector<team>& teams,
bool ignore_zocs, bool allow_teleport)
bool ignore_zocs, bool allow_teleport, int turns_left)
{
team& current_team = teams[u.side()-1];
@ -177,13 +177,24 @@ void find_routes(const gamemap& map, const game_data& gamedata,
//find the movement cost of this type onto the terrain
const int move_cost = u.movement_cost(map,terrain);
if(move_cost <= move_left) {
if(move_cost <= move_left ||
turns_left > 0 && move_cost <= u.total_movement()) {
int new_move_left = move_left - move_cost;
int new_turns_left = turns_left;
if(new_move_left < 0) {
--new_turns_left;
new_move_left = u.total_movement() - move_cost;
}
const int total_movement = new_turns_left*u.total_movement() +
new_move_left;
const std::map<gamemap::location,paths::route>::const_iterator
rtit = routes.find(currentloc);
//if a better route to that tile has already been found
if(rtit != routes.end() &&
rtit->second.move_left >= move_left - move_cost)
rtit->second.move_left >= total_movement)
continue;
const bool zoc = enemy_zoc(map,units,currentloc,
@ -191,13 +202,16 @@ void find_routes(const gamemap& map, const game_data& gamedata,
!ignore_zocs;
paths::route new_route = routes[loc];
new_route.steps.push_back(loc);
new_route.move_left = zoc ? 0 : move_left - move_cost;
const int zoc_move_left = zoc ? 0 : new_move_left;
new_route.move_left = u.total_movement() * new_turns_left +
zoc_move_left;
routes[currentloc] = new_route;
if(new_route.move_left > 0) {
find_routes(map,gamedata,units,u,currentloc,
new_route.move_left,routes,teams,ignore_zocs,
allow_teleport);
zoc_move_left,routes,teams,ignore_zocs,
allow_teleport,new_turns_left);
}
}
}
@ -209,7 +223,7 @@ paths::paths(const gamemap& map, const game_data& gamedata,
const std::map<gamemap::location,unit>& units,
const gamemap::location& loc,
std::vector<team>& teams,
bool ignore_zocs, bool allow_teleport)
bool ignore_zocs, bool allow_teleport, int additional_turns)
{
const std::map<gamemap::location,unit>::const_iterator i = units.find(loc);
if(i == units.end()) {
@ -220,7 +234,7 @@ paths::paths(const gamemap& map, const game_data& gamedata,
routes[loc].move_left = i->second.movement_left();
find_routes(map,gamedata,units,i->second,loc,
i->second.movement_left(),routes,teams,
ignore_zocs,allow_teleport);
ignore_zocs,allow_teleport,additional_turns);
if(i->second.can_attack()) {
gamemap::location adjacent[6];

View file

@ -42,7 +42,7 @@ struct paths
paths(const gamemap& map, const game_data& gamedata,
const std::map<gamemap::location,unit>& units,
const gamemap::location& loc, std::vector<team>& teams,
bool ignore_zocs, bool allow_teleport);
bool ignore_zocs, bool allow_teleport, int additional_turns=0);
struct route
{

View file

@ -217,13 +217,7 @@ LEVEL_RESULT play_level(game_data& gameinfo, config& terrain_config,
} else if(!replaying && team_it->is_network()) {
config cfg;
bool left_button = false, right_button = false;
gamemap::location next_unit;
paths current_paths;
paths::route current_route;
bool enemy_paths = false;
gamemap::location last_hex, selected_hex;
undo_list undo_stack, redo_stack;
turn_info turn_data;
for(;;) {
network::connection res = network::receive_data(cfg);
@ -233,10 +227,7 @@ LEVEL_RESULT play_level(game_data& gameinfo, config& terrain_config,
turn_slice(gameinfo,state_of_game,status,terrain_config,
level,video,key,gui,map,teams,player_number,
units,left_button,right_button,next_unit,
current_paths,current_route,enemy_paths,
last_hex,selected_hex,undo_stack,redo_stack,
true);
units,turn_data,true);
}
std::cerr << "replay: '" << cfg.children["turn"].front()->write() << "'\n";

View file

@ -28,6 +28,20 @@
#include <sstream>
#include <string>
namespace {
int get_turns_movement(CKey& key)
{
for(char c = '1'; c != '7'; ++c) {
if(key[c])
return c - '1';
}
return 0;
}
}
void play_turn(game_data& gameinfo, game_state& state_of_game,
gamestatus& status, config& terrain_config, config* level,
CVideo& video, CKey& key, display& gui,
@ -44,19 +58,9 @@ void play_turn(game_data& gameinfo, game_state& state_of_game,
team& current_team = teams[team_num-1];
gamemap::location next_unit;
bool left_button = false, right_button = false;
const paths_wiper wiper(gui);
paths current_paths;
paths::route current_route;
bool enemy_paths = false;
gamemap::location last_hex;
gamemap::location selected_hex;
undo_list undo_stack, redo_stack;
turn_info turn_data;
//execute gotos - first collect gotos in a list
std::vector<gamemap::location> gotos;
@ -90,10 +94,10 @@ void play_turn(game_data& gameinfo, game_state& state_of_game,
assert(route.steps.front() == *g);
gui.set_route(&route);
const size_t moves =
move_unit(&gui,map,units,teams,route.steps,&recorder,&undo_stack);
const size_t moves = move_unit(&gui,map,units,teams,route.steps,
&recorder,&turn_data.undo_stack);
if(moves > 0) {
redo_stack.clear();
turn_data.redo_stack.clear();
}
}
@ -102,10 +106,7 @@ void play_turn(game_data& gameinfo, game_state& state_of_game,
for(;;) {
const bool res = turn_slice(gameinfo,state_of_game,status,
terrain_config,level,video,key,gui,map,
teams,team_num,units,left_button,
right_button,next_unit,current_paths,
current_route,enemy_paths,last_hex,
selected_hex,undo_stack,redo_stack,false);
teams,team_num,units,turn_data,false);
if(res) {
break;
}
@ -116,13 +117,19 @@ bool turn_slice(game_data& gameinfo, game_state& state_of_game,
gamestatus& status, config& terrain_config, config* level,
CVideo& video, CKey& key, display& gui, gamemap& map,
std::vector<team>& teams, int team_num,
std::map<gamemap::location,unit>& units,
bool& left_button, bool& right_button,
gamemap::location& next_unit, paths& current_paths,
paths::route& current_route, bool& enemy_paths,
gamemap::location& last_hex, gamemap::location& selected_hex,
undo_list& undo_stack, undo_list& redo_stack, bool browse)
unit_map& units, turn_info& turn_data, bool browse)
{
bool& left_button = turn_data.left_button;
bool& right_button = turn_data.right_button;
gamemap::location& next_unit = turn_data.next_unit;
paths& current_paths = turn_data.current_paths;
paths::route& current_route = turn_data.current_route;
bool& enemy_paths = turn_data.enemy_paths;
gamemap::location& last_hex = turn_data.last_hex;
gamemap::location& selected_hex = turn_data.selected_hex;
undo_list& undo_stack = turn_data.undo_stack;
undo_list& redo_stack = turn_data.redo_stack;
team& current_team = teams[team_num-1];
int mousex, mousey;
@ -132,6 +139,31 @@ bool turn_slice(game_data& gameinfo, game_state& state_of_game,
gamemap::location new_hex = gui.hex_clicked_on(mousex,mousey);
//if the player has pressed a number, we want to show how far
//the selected unit could get in that many turns
const int new_path_turns = get_turns_movement(key);
if(new_path_turns != turn_data.path_turns) {
turn_data.path_turns = new_path_turns;
unit_map::iterator u = units.find(selected_hex);
if(u == units.end()) {
u = units.find(last_hex);
if(u != units.end() && u->second.side() == team_num) {
u = units.end();
}
} else if(u->second.side() != team_num) {
u = units.end();
}
if(u != units.end()) {
const bool ignore_zocs = u->second.type().is_skirmisher();
const bool teleport = u->second.type().teleports();
current_paths = paths(map,gameinfo,units,u->first,teams,
ignore_zocs,teleport,turn_data.path_turns);
gui.set_paths(&current_paths);
}
}
if(new_hex.valid() == false) {
current_route.steps.clear();
gui.set_route(NULL);
@ -189,7 +221,7 @@ bool turn_slice(game_data& gameinfo, game_state& state_of_game,
const unit_movement_resetter move_change(u->second);
current_paths = paths(map,gameinfo,units,new_hex,teams,
ignore_zocs,teleport);
ignore_zocs,teleport,turn_data.path_turns);
gui.set_paths(&current_paths);
enemy_paths = true;
}
@ -391,7 +423,7 @@ bool turn_slice(game_data& gameinfo, game_state& state_of_game,
const bool ignore_zocs = it->second.type().is_skirmisher();
const bool teleport = it->second.type().teleports();
current_paths = paths(map,gameinfo,units,hex,teams,
ignore_zocs,teleport);
ignore_zocs,teleport,turn_data.path_turns);
gui.set_paths(&current_paths);
unit u = it->second;
@ -888,7 +920,7 @@ bool turn_slice(game_data& gameinfo, game_state& state_of_game,
it->second.type().is_skirmisher();
const bool teleport = it->second.type().teleports();
current_paths = paths(map,gameinfo,units,
it->first,teams,ignore_zocs,teleport);
it->first,teams,ignore_zocs,teleport,turn_data.path_turns);
gui.set_paths(&current_paths);
gui.scroll_to_tile(it->first.x,it->first.y,

View file

@ -43,24 +43,34 @@ private:
display& gui_;
};
struct turn_info {
turn_info() : left_button(false), right_button(false), enemy_paths(false),
path_turns(0)
{}
bool left_button, right_button;
gamemap::location next_unit;
paths current_paths;
paths::route current_route;
bool enemy_paths;
gamemap::location last_hex;
gamemap::location selected_hex;
undo_list undo_stack;
undo_list redo_stack;
int path_turns;
};
void play_turn(game_data& gameinfo, game_state& state_of_game,
gamestatus& status, config& terrain_config, config* level,
CVideo& video, CKey& key, display& gui,
game_events::manager& events_manager, gamemap& map,
std::vector<team>& teams, int team_num,
std::map<gamemap::location,unit>& units);
std::vector<team>& teams, int team_num, unit_map& units);
bool turn_slice(game_data& gameinfo, game_state& state_of_game,
gamestatus& status, config& terrain_config, config* level,
CVideo& video, CKey& key, display& gui, gamemap& map,
std::vector<team>& teams, int team_num,
std::map<gamemap::location,unit>& units,
bool& left_button, bool& right_button,
gamemap::location& next_unit, paths& current_paths,
paths::route& current_route, bool& enemy_paths,
gamemap::location& last_hex, gamemap::location& selected_hex,
undo_list& undo_stack, undo_list& redo_stack,
bool browse_only);
unit_map& units, turn_info& turn_data, bool browse_only);
#endif