fixed teleporting to work properly

This commit is contained in:
Dave White 2003-10-14 10:38:31 +00:00
parent cefbc847d3
commit 8cb8c6f54b
3 changed files with 38 additions and 11 deletions

View File

@ -40,8 +40,10 @@ green=63
aliasof=g
[/terrain]
#grassland should contain mostly plain grassland, but occasionally
#a few rocks
[terrain]
image=grassland,grassland,grassland,grassland,grassland,grassland-flowers
image=grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland,grassland-flowers,grassland-flowers,grassland-rocks
name=grassland
char=g
green=200

View File

@ -70,11 +70,39 @@ private:
namespace detail {
struct node {
static double heuristic(const gamemap::location& src,
const gamemap::location& dst) {
return sqrt(pow(abs(dst.x-src.x),2) + pow(abs(dst.y-src.y),2));
}
node(const gamemap::location& pos, const gamemap::location& dst,
double cost, node* parent)
: parent(parent), loc(pos), g(cost),
h(sqrt(pow(abs(dst.x-pos.x),2) + pow(abs(dst.y-pos.y),2)))
double cost, node* parent,
const std::set<gamemap::location>* teleports)
: parent(parent), loc(pos), g(cost), h(heuristic(pos,dst))
{
//if there are teleport locations, correct the heuristic to
if(teleports != NULL) {
double srch = h, dsth = h;
std::set<gamemap::location>::const_iterator i;
for(i = teleports->begin(); i != teleports->end(); ++i) {
const double new_srch = heuristic(pos,*i);
const double new_dsth = heuristic(*i,dst);
if(new_srch < srch) {
srch = new_srch;
}
if(new_dsth < dsth) {
dsth = new_dsth;
}
}
if(srch + dsth + 1.0 < h) {
h = srch + dsth + 1.0;
}
}
f = g + h;
}
@ -95,7 +123,7 @@ paths::route a_star_search(const gamemap::location& src,
typedef gamemap::location location;
std::list<node> open_list, closed_list;
open_list.push_back(node(src,dst,0.0,NULL));
open_list.push_back(node(src,dst,0.0,NULL,teleports));
while(!open_list.empty()) {
@ -144,7 +172,7 @@ paths::route a_star_search(const gamemap::location& src,
}
const node nd(locs[j],dst,lowest->g+obj.cost(locs[j],lowest->g),
&*lowest);
&*lowest,teleports);
for(i = open_list.begin(); i != open_list.end(); ++i) {
if(i->loc == nd.loc && i->f <= nd.f) {

View File

@ -87,11 +87,9 @@ void play_turn(game_data& gameinfo, game_state& state_of_game,
unit u = ui->second;
const shortest_path_calculator calc(u,current_team,units,map);
const bool can_teleport = u.type().teleports() &&
map[ui->first.x][ui->first.y] == gamemap::TOWER;
const std::set<gamemap::location>* const teleports =
can_teleport ? &current_team.towers() : NULL;
u.type().teleports() ? &current_team.towers() : NULL;
paths::route route = a_star_search(ui->first,ui->second.get_goto(),
10000.0,calc,teleports);
@ -144,8 +142,7 @@ void play_turn(game_data& gameinfo, game_state& state_of_game,
if(un != units.end()) {
const shortest_path_calculator calc(un->second,current_team,
units,map);
const bool can_teleport = un->second.type().teleports() &&
map[selected_hex.x][selected_hex.y] == gamemap::TOWER;
const bool can_teleport = un->second.type().teleports();
const std::set<gamemap::location>* const teleports =
can_teleport ? &current_team.towers() : NULL;