本文整理汇总了C++中APoint::getToHereCost方法的典型用法代码示例。如果您正苦于以下问题:C++ APoint::getToHereCost方法的具体用法?C++ APoint::getToHereCost怎么用?C++ APoint::getToHereCost使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类APoint
的用法示例。
在下文中一共展示了APoint::getToHereCost方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: AStar
void AStar(Tank*enemy,Tank*ltank)
{
APoint vfirst(enemy->MapCoordinate());
APoint vfinal(ltank->MapCoordinate());
list<APoint> openlist;
list<APoint> closelist;
openlist.push_back(vfirst); //加入到openlist,表示已经侦查过
while (!openlist.empty())
{
APoint vetex = openlist.front();
openlist.pop_front(); // 从openlist中删除
if (vetex==vfinal) //找到目标,确定第一步方向
{
Direction first=FRONT;
while (!(vetex==vfirst))
{
switch (vetex.getToSourceDirection()) //还原
{
case FRONT:
vetex = *find(closelist.begin(),closelist.end(),APoint(vetex.getX(),vetex.getY()-1));
first = RETREAT;
break;
case RETREAT:
vetex = *find(closelist.begin(),closelist.end(),APoint(vetex.getX(),vetex.getY()+1));
first = FRONT;
break;
case LEFT:
vetex = *find(closelist.begin(),closelist.end(),APoint(vetex.getX()-1,vetex.getY()));
first = RIGHT;
break;
case RIGHT:
vetex = *find(closelist.begin(),closelist.end(),APoint(vetex.getX()+1,vetex.getY()));
first = LEFT;
break;
default:
break;
}
}
enemy->setDirection(first);
return;
}
else //处理四个方向节点
{
closelist.push_back(vetex); //加入到closelist中,表示已经检测过
APoint up(vetex.getX(),vetex.getY()-1); //up 方向
if (find(openlist.begin(),openlist.end(),up)==openlist.end()&&find(closelist.begin(),closelist.end(),up)==closelist.end()&&MapValue(up.getX(),up.getY())!=CANTMOVE&&MapValue(up.getX(),up.getY())!=CANTACROSS)//不在两列表中且可以通过
{
int toCost = vetex.getToHereCost()+MapValue(up.getX(),up.getY());
int cost =toCost +Heuristic(up,vfinal); //更新 总价值=到前一节点的值+此节点的值+启发值
up.setToHereCost(toCost);
up.setCost(cost);
up.setDirection(RETREAT); //设置到前一节点的方向
openlist.push_back(up); //加入到openlist
}
APoint down(vetex.getX(),vetex.getY()+1); //down 方向
if (find(openlist.begin(),openlist.end(),down)==openlist.end()&&find(closelist.begin(),closelist.end(),down)==closelist.end()&&MapValue(down.getX(),down.getY())!=CANTMOVE&&MapValue(down.getX(),down.getY())!=CANTACROSS)//不在两列表中且可以通过
{
int toCost = vetex.getToHereCost()+MapValue(down.getX(),down.getY());
int cost = toCost+Heuristic(down,vfinal); //更新 总价值=到前一节点的值+此节点的值+启发值
down.setToHereCost(toCost);
down.setCost(cost);
down.setDirection(FRONT); //设置到前一节点的方向
openlist.push_back(down); //加入到openlist
}
APoint left(vetex.getX()-1,vetex.getY()); //left 方向
if (find(openlist.begin(),openlist.end(),left)==openlist.end()&&find(closelist.begin(),closelist.end(),left)==closelist.end()&&MapValue(left.getX(),left.getY())!=CANTMOVE&&MapValue(left.getX(),left.getY())!=CANTACROSS)//不在两列表中且可以通过
{
int toCost = vetex.getToHereCost()+MapValue(left.getX(),left.getY());
int cost = toCost+Heuristic(left,vfinal); //更新 总价值=到前一节点的值+此节点的值+启发值
left.setToHereCost(toCost);
left.setCost(cost);
left.setDirection(RIGHT); //设置到前一节点的方向
openlist.push_back(left); //加入到openlist
}
APoint right(vetex.getX()+1,vetex.getY()); //right 方向
if (find(openlist.begin(),openlist.end(),right)==openlist.end()&&find(closelist.begin(),closelist.end(),right)==closelist.end()&&MapValue(right.getX(),right.getY())!=CANTMOVE&&MapValue(right.getX(),right.getY())!=CANTACROSS)//不在两列表中且可以通过
{
int toCost = vetex.getToHereCost()+MapValue(right.getX(),right.getY());
int cost = toCost+Heuristic(right,vfinal); //更新 总价值=到前一节点的值+此节点的值+启发值
right.setToHereCost(toCost);
right.setCost(cost);
right.setDirection(LEFT); //设置到前一节点的方向
openlist.push_back(right); //加入到openlist
}
openlist.sort(compareAP);
}
}
}
示例2: NBAStar
void NBAStar(Tank*assassin,Tank*ltank)
{
vector<APoint> vec_weight;
vector<APoint> vec_bullet;
AStarWeightAdjust(vec_weight,ltank); //朝向上路径权值改变
if (ltank->getBullet().getIsFly())
{
AStarWithBulletWeightAdjust(vec_bullet,ltank); //子弹路径上权值改变
}
APoint vfirst(assassin->MapCoordinate()); //起点
APoint vfinal(ltank->MapCoordinate()); //终点
list<APoint> openlist;
list<APoint> closelist;
openlist.push_back(vfirst); //加入到openlist,表示已经侦查过
while (!openlist.empty())
{
APoint vetex = openlist.front();
openlist.pop_front(); // 从openlist中删除
if (vetex==vfinal) //找到目标,确定第一步方向
{
Direction first=FRONT;
while (!(vetex==vfirst))
{
switch (vetex.getToSourceDirection()) //还好
{
case FRONT:
vetex = *find(closelist.begin(),closelist.end(),APoint(vetex.getX(),vetex.getY()-1));
first = RETREAT;
break;
case RETREAT:
vetex = *find(closelist.begin(),closelist.end(),APoint(vetex.getX(),vetex.getY()+1));
first = FRONT;
break;
case LEFT:
vetex = *find(closelist.begin(),closelist.end(),APoint(vetex.getX()-1,vetex.getY()));
first = RIGHT;
break;
case RIGHT:
vetex = *find(closelist.begin(),closelist.end(),APoint(vetex.getX()+1,vetex.getY()));
first = LEFT;
break;
default:
break;
}
}
assassin->setDirection(first);
return;
}
else //处理四个方向节点
{
closelist.push_back(vetex); //加入到closelist中,表示已经检测过
APoint up(vetex.getX(),vetex.getY()-1); //up 方向
if (find(openlist.begin(),openlist.end(),up)==openlist.end()&&find(closelist.begin(),closelist.end(),up)==closelist.end()&&MapValue(up.getX(),up.getY())!=CANTMOVE&&MapValue(up.getX(),up.getY())!=CANTACROSS)//不在两列表中且可以通过
{
int toCost = vetex.getToHereCost()+MapValue(up.getX(),up.getY());
if (find(vec_weight.begin(),vec_weight.end(),up)!=vec_weight.end()) //路径在坦克炮口前+DANGEROUS
{
toCost += DANGEROUS;
}
if (find(vec_bullet.begin(),vec_bullet.end(),up)!=vec_bullet.end()) //路径在炮弹路线上+BULLETDANGEROUS
{
toCost += BULLETDANGEROUS;
}
int cost =toCost +Heuristic(up,vfinal); //更新 总价值=到前一节点的值+此节点的值+启发值
up.setToHereCost(toCost);
up.setCost(cost);
up.setDirection(RETREAT); //设置到前一节点的方向
openlist.push_back(up); //加入到openlist
}
APoint down(vetex.getX(),vetex.getY()+1); //down 方向
if (find(openlist.begin(),openlist.end(),down)==openlist.end()&&find(closelist.begin(),closelist.end(),down)==closelist.end()&&MapValue(down.getX(),down.getY())!=CANTMOVE&&MapValue(down.getX(),down.getY())!=CANTACROSS)//不在两列表中且可以通过
{
int toCost = vetex.getToHereCost()+MapValue(down.getX(),down.getY());
if (find(vec_weight.begin(),vec_weight.end(),down)!=vec_weight.end()) //路径在坦克炮口前+DANGEROUS
{
toCost += DANGEROUS;
}
if (find(vec_bullet.begin(),vec_bullet.end(),down)!=vec_bullet.end()) //路径在炮弹路线上+BULLETDANGEROUS
{
toCost += BULLETDANGEROUS;
}
int cost = toCost+Heuristic(down,vfinal); //更新 总价值=到前一节点的值+此节点的值+启发值
down.setToHereCost(toCost);
down.setCost(cost);
down.setDirection(FRONT); //设置到前一节点的方向
openlist.push_back(down); //加入到openlist
}
APoint left(vetex.getX()-1,vetex.getY()); //left 方向
if (find(openlist.begin(),openlist.end(),left)==openlist.end()&&find(closelist.begin(),closelist.end(),left)==closelist.end()&&MapValue(left.getX(),left.getY())!=CANTMOVE&&MapValue(left.getX(),left.getY())!=CANTACROSS)//不在两列表中且可以通过
{
int toCost = vetex.getToHereCost()+MapValue(left.getX(),left.getY());
if (find(vec_weight.begin(),vec_weight.end(),left)!=vec_weight.end()) //路径在坦克炮口前+DANGEROUS
{
toCost += DANGEROUS;
}
if (find(vec_bullet.begin(),vec_bullet.end(),left)!=vec_bullet.end()) //路径在炮弹路线上+BULLETDANGEROUS
//.........这里部分代码省略.........