读书人

那位大侠能详细解释上goto_des这个寻路

发布时间: 2012-11-03 10:57:43 作者: rapoo

那位大侠能详细解释下goto_des这个寻路函数是怎么实现的, 后面的是这个函数中用到的一些内容的定义
Order goto_des(int x, int y, User * u) {
if (x == u->x[u->who] && y == u->y[u->who]) {
Order o; o.act = STOP;
return o;
}
int maptmp[boderY][boderX];
int que[boderY * boderX][2], qn=0;
int fx[5][2] = {{0,0},{-1,0},{1,0},{0,-1},{0,1}};
Order ord[5];
ord[0].act = STOP; ord[1].act = UP;
ord[2].act = DOWN; ord[3].act = LEFT;
ord[4].act = RIGHT;
fill(maptmp[0], maptmp[boderY], inf);
maptmp[y][x] = 0;
que[qn][0] = y;
que[qn++][1] = x;
for ( int i=0; i<qn; i++) {
for ( int j=1; j<=4; j++) {
int tmpy = que[i][0] + fx[j][0];
int tmpx = que[i][1] + fx[j][1];
if (tmpy >= 0 && tmpy < boderY &&
tmpx >= 0 && tmpx < boderX &&
u->mapInfo[tmpy][tmpx] == 1 && maptmp[tmpy][tmpx] == inf) {
maptmp[tmpy][tmpx] = maptmp[que[i][0]][que[i][1]] + 1;
que[qn][0] = tmpy;
que[qn++][1] = tmpx;
if (maptmp[u->y[u->who]][u->x[u->who]] != inf) {
break;
}
}
}
if (maptmp[u->y[u->who]][u->x[u->who]] != inf) {
break;
}
}
for ( int i=1; i<=4; i++) {
int tmpy = u->y[u->who] + fx[i][0];
int tmpx = u->x[u->who] + fx[i][1];
if (tmpx >= 0 && tmpx < boderX &&
tmpy >= 0 && tmpy < boderY &&
maptmp[tmpy][tmpx] + 1 == maptmp[u->y[u->who]][u->x[u->who]]) {
return ord[i];
}
}
return ord[0];
}




其中
const int inf = 0x13131313;// 极大值

//方向
const int STOP = 0;
const int UP = 1;
const int DOWN = 2;
const int LEFT = 3;
const int RIGHT = 4;
const int ATC = 5; //攻击

//指令
struct Order
{
int act;
int dir[30];
};

//边界
const int boderX = 22;
const int boderY = 17;

//地图类型
const int STAR = 0; //不可走,魔法可穿
const int LAND = 1; //可走,魔法可穿
const int TREE = 2; //不可走,魔法不可穿

struct User
{
int m[200]; //记忆空间,不会被刷新的存储位置,请把需要的信息记录在其中。
//**复赛增加接口
double m2[200]; //记忆空间,不会被刷新的存储位置,请把需要的信息记录在其中。
//**
int mapInfo[boderY][boderX]; //地图信息
int who; //当前人物
int t; //时间
int map_goods; // 地图物品数量
int map_goods_x[50]; //坐标
int map_goods_y[50];
int map_goods_time[50]; //出现时间
int map_goods_type[50]; //物品类型
//**复赛增加接口
int FPS; //平台即时帧数
int id; //当前运行人物
//**
int y[2]; //我方坐标
int x[2];
int dir[2]; //我方人物方向
int power[2]; //我方人物power
int sorce[2]; //我方分数
int goods_num[2]; //物品数量
int goods_tpye[2][50]; //物品类型
int magic_len[2]; //人物魔法长度
int magic_num[2]; //已运行魔法长度
int magic_x[2][50]; //魔法坐标数组
int magic_y[2][50];
//**复赛增加接口
int dead_cd[2]; //自杀冷却时间
int s_skill_cd[2]; //小技能冷却时间
int b_skill_cd[2]; //大技能冷却时间
//**
int ey[2]; //敌方坐标
int ex[2];
int edir[2]; //敌方人物方向
int epower[2]; //敌方人物power
int esorce[2]; //敌方分数
int egoods_num[2]; //敌方物品数量
int egoods_tpye[2][50]; //敌方物品类型
int emagic_num[2]; //敌方已运行魔法长度
int emagic_x[2][50]; //敌方魔法坐标数组
int emagic_y[2][50];
//**复赛增加接口
int edead_cd[2]; //敌方自杀冷却时间
int es_skill_cd[2]; //敌方小技能冷却时间
int eb_skill_cd[2]; //敌方大技能冷却时间
//**
};


[解决办法]
这种也只有一句句分析啥。。。
比如
if (x == u->x[u->who] && y == u->y[u->who]) {
Order o; o.act = STOP;
return o;
}
u->who 获取当前user的标识,传入x ,这个x应该是个map,或者对应的x坐标,如果x等于要移动的x,y等于要移动的y,就不移动了。act=stop...

读书人网 >C++

热点推荐