加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ScriptInterface.cpp 10.89 KB
一键复制 编辑 原始数据 按行查看 历史
Shaker 提交于 2014-08-12 21:17 . *提交到Git@OSC
#include "stdafx.h"
#include "TurboMir.h"
#include "gamemir.h"
#include "MainFrm.h"
void CGameMir::l_Buy( const char* item )
{
int retval;
while((retval=BuyItem(item,0))!=0)
{
if(retval==-1)
{
AddLog(crError,"脚本:购买物品【%s】失败\n",item);
return;
}
Sleep(10);
}
}
void CGameMir::l_DialogSelect( const char* text )
{
bool retval=MerchantDialogSelect(text);
if(!retval)
{
AddLog(crError,"脚本:Npc对话失败!\n");
return;
}
DWORD st=GetTickCount();
while((GetTickCount()-st)>5000)
{
if(Merchant.ID!=0)
{
return;
}
Sleep(10);
}
AddLog(crError,"脚本:等待Npc对话超时:%s\n",text);
}
void CGameMir::l_Say( const char* text )
{
AddLog(crGreen,"脚本:说话:%s\n",text);
m_Action.SendSay(text);
}
void CGameMir::l_TalkNpc( const char* npc, const char* map, int x, int y )
{
AddLog(crGreen,"脚本:在%s(%d,%d)找到npc[%s]\n",map,x,y,npc);
// 先移动到npc所在地图
l_MapMove( map, x, y );
if(m_Self.xx!=x || m_Self.yy!=y)
{
// 初始化D*
CDStar DStar(m_Map,*this);
std::string CurrentMap=m_Map.get_MapName();
std::vector<POINT> Path;//路径
QWORD retval=DStar.Init(x,y,2,8);
if(retval==0)
{
AddLog(crGreen,"脚本:DStar初始化失败\n"); // 这里应该中断脚本
return;
}
//取得路径
POINT start;
start.x=m_Self.xx;
start.y=m_Self.yy;
while ( true )
{
POINT next,search;
bool action;
start.x=m_Self.xx;
start.y=m_Self.yy;
if(abs(m_Self.xx-x)<=8 && abs(m_Self.yy-y)<=8) // 到达目的地
{
AddLog(crGreen,"脚本:到达目的地\n");
break;
}
if(CurrentMap!=m_Map.get_MapName())
{
AddLog(crGreen,"脚本:错误的过图\n"); // 这里应该中断脚本
return;
}
if ( Path.empty() )
{
if ( !DStar.GetPath( start, Path ) )
{
AddLog(crGreen,"脚本:取得路径错误\n"); // 这里应该中断脚本
return;
}
sig_SetPath(Path);
}
//在路径中查找英雄当前所在点
std::vector<POINT>::iterator pos=std::find(Path.begin(),Path.end(),start);
action=false;
if(pos!=Path.end())//找到
{
pos++;
if(pos!=Path.end())
{
next=*pos;
for(long i=0;i<8;i++)
{
search.x=start.x+xofs_walk[i];
search.y=start.y+yofs_walk[i];
if(CanMove(search.x,search.y))
{
if( search==next )
{
action=true;
m_Action.SendWalk(i);
break;
}
search.x+=xofs_walk[i];
search.y+=yofs_walk[i];
if(CanMove(search.x,search.y))
{
if( search==next )
{
action=true;
m_Action.SendRun(i);
break;
}
}
}
}
}
else
{
AddLog(crMessage," Next Fail\n");
}
}
else
{
AddLog(crMessage," 在路径中查找英雄当前所在点 fail"__FILE__"\n");
}
if(action)
{
while(!m_Action.CanAction())
Sleep(100);
}
else
{
Sleep(200);
Path.clear();
//说明我们的英雄已经偏离了路线 或者路原来的路径上被人站了 重新查找路径
}
}
}
for (itActor pos=m_ActorList.begin();pos!=m_ActorList.end();pos++)
{
//m_FrameWnd.AddLog(crGreen,"脚本:%s\n", pos->Name.c_str() );
if ( pos->Name==std::string(npc) )
{
//m_FrameWnd.AddLog(crWhite,"脚本:找到\n" );
ClickNPC(pos->ServerId);
break;
}
}
}
void CGameMir::l_Print( const char* str )
{
AddLog(crMessage,"脚本:%s\n",str);
}
void CGameMir::l_Sleep( DWORD t )
{
AddLog(crMessage,"脚本:等待%d毫秒\n",t);
Sleep(t);
}
int CGameMir::l_GetItemCount( const char* item )
{
char buf[STRING_BUFFER_LENGTH+1];
std::string itemname=item;
int cnt=0;
// 统计个数
for ( itClientItem pos=m_BagItems.begin(); pos!=m_BagItems.end(); ++pos )
{
GetDelphiString(pos->S.Name,sizeof(pos->S.Name),buf);
if ( itemname==buf )
{
cnt++;
}
}
return cnt;
}
const char* CGameMir::l_GetMapName( )
{
return m_Self.MapTitle.c_str();
}
void CGameMir::l_Disconnect( )
{
AddLog(crRed,"脚本: 离线!!\n");
Close();
}
int CGameMir::l_MapMove( const char* map, int x, int y )
{
std::string mapname=map;
PATH_LIST PathList;
POINT pnt;
pnt.x=x;
pnt.y=y;
if( BuildMapMovePath(mapname,pnt,*this,PathList)==SR_SUCCESS)
{
for ( PATH_LIST::iterator itor=PathList.begin();itor!=PathList.end();itor++)
{
m_Action.MoveTo(itor->source.pos);
int i;
for ( i=0; i<100; ++i)
{
if(itor->dest.mapname==m_Map.get_MapName())
break;
Sleep(50);
}
if ( i==100 )
{
AddLog(crRed,"脚本: 过图等待超时!!\n");
return -1;
}
}
}
else
{
AddLog(crRed,"脚本: 目标地图无法到达!!\n");
return -1;
}
return 0;
}
int CGameMir::l_StartFight( )
{
OpenFight();
return 0;
}
int CGameMir::l_UseItem( const char* item )
{
std::string itemname=item;
int i;
for (i=0;i<20;i++)
{
if( CanEat() )
break;
Sleep(50);
}
if ( i==20 )
{
AddLog(crError,"脚本: 使用%s超时!\n",itemname.c_str());
return -1;
}
EatItem(0,itemname.c_str());
AddLog(crMessage,"脚本: 使用%s.\n",itemname.c_str());
return 0;
}
typedef std::multiset<CGameMir::sMapNode> OPEN_LIST;
typedef std::list<CGameMir::sMapNode> CLOSE_LIST;
DWORD CGameMir::BuildMapMovePath(std::string mapname, POINT&destpnt, CGameMir& game, PATH_LIST& PathList)
{
std::string Target;
OPEN_LIST OpenList;
CLOSE_LIST CloseList;
// Init map
sMapNode start;
start.dist=MAXDWORD;
for(tMapList::const_iterator pos=game.m_DataManager.GetMapList().begin();
pos!=game.m_DataManager.GetMapList().end();
pos++)
{
if( pos->maptitle == mapname )
{
Target=pos->mapname;
break;
}
if( pos->mapname == mapname )
{
Target=pos->mapname;
break;
}
}
if ( Target.empty() )
{
game.AddLog(crError,"脚本: 无法检索目标地图信息!\n");
return SR_FAIL;
}
if ( Target==game.m_Map.get_MapName() )
{
PathList.clear();
if ( !(destpnt.x==0 && destpnt.y==0) )
{
sDoorLink dl;
dl.source.mapname=Target;
dl.source.pos=destpnt;
dl.dest.mapname=Target;
dl.dest.pos=destpnt;
dl.dlg_name.clear();
PathList.push_back(dl);
}
return SR_SUCCESS;
}
for(tMapList::const_iterator pos=game.m_DataManager.GetMapList().begin();
pos!=game.m_DataManager.GetMapList().end();
pos++)
{
if( pos->mapname == game.m_Map.get_MapName())
{
start.map=&(*pos);
start.dist=0;
start.active=NULL;
CloseList.push_back(start);
break;
}
}
if ( start.dist!=0 )
{
game.AddLog(crError,"脚本: 无法检索当前地图信息!\n");
return SR_FAIL;
}
// 计算当前地图各个门点的dist
DWORD dist;
CDStar dstar(game.m_Map,game);
POINT cur;
cur.x=game.m_Self.xx;
cur.y=game.m_Self.yy;
if(dstar.Init(game.m_Self.xx,game.m_Self.yy)==0)
{
game.AddLog(crError,"脚本: DStar初始化错误!\n");
return SR_FAIL;
}
for(tDoorLinkList::const_iterator posLink=start.map->DoorLinks.begin();
posLink!=start.map->DoorLinks.end();
posLink++)
{
dist=dstar.GetDist(posLink->source.pos.x,posLink->source.pos.y); //得到距离
for(tMapList::const_iterator posMap=game.m_DataManager.GetMapList().begin();
posMap!=game.m_DataManager.GetMapList().end();
posMap++)
{
if( posMap->mapname==posLink->dest.mapname )
{
//game.m_FrameWnd.AddLog(crWhite,"%d (%d,%d)\n",dist,posLink->source.pos.x,posLink->source.pos.y);
sMapNode node;
node.map=&(*posMap);
node.dist=dist;
node.active=&(*posLink);
OpenList.insert(node);
}
}
}
while (!OpenList.empty())
{
//查找开放列表中具有最小F值的方块。我们把它作为当前方块。
sMapNode MapNode;
{
OPEN_LIST::iterator itorMapNode=OpenList.begin();
MapNode=*itorMapNode;
OpenList.erase(itorMapNode);
//把它放入封闭列表。
CloseList.push_back(MapNode);
}
//game.m_FrameWnd.AddLog(crError,"OpenList %s\n",MapNode.map->mapname.c_str());
if ( MapNode.map->mapname == Target )
{
//game.m_FrameWnd.AddLog(crError,"脚本: 目标找到\n");
sMapNode*p=&MapNode;
PathList.clear();
while ( p->active!=NULL )
{
PathList.push_front(*p->active);
for(CLOSE_LIST::iterator posMapNode=CloseList.begin();posMapNode!=CloseList.end();posMapNode++)
{
if(posMapNode->map->mapname==p->active->source.mapname)
{
p=&(*posMapNode);
break;
}
}
}
if ( !(destpnt.x==0 && destpnt.y==0) )
{
sDoorLink dl;
dl.source.mapname=Target;
dl.source.pos=destpnt;
dl.dest.mapname=Target;
dl.dest.pos=destpnt;
dl.dlg_name.clear();
PathList.push_back(dl);
}
for ( PATH_LIST::iterator itor=PathList.begin();itor!=PathList.end();itor++)
{
game.AddLog(crWhite,"脚本: [%s](%d,%d) -> [%s](%d,%d)\n",
itor->source.mapname.c_str(),
itor->source.pos.x,
itor->source.pos.y,
itor->dest.mapname.c_str(),
itor->dest.pos.x,
itor->dest.pos.y
);
//game.m_Action.MoveTo(itor->source.pos);
}
return SR_SUCCESS;
}
DWORD dist_base=MapNode.dist; // 计算基本dist 就是当前地图的dist
//对当前方块的8个相邻方块的每一个?
for(tDoorLinkList::const_iterator posLink=MapNode.map->DoorLinks.begin();posLink!=MapNode.map->DoorLinks.end();posLink++)
{// 遍历Door列表
//得到距离
// 计算 当前门点的dist
dist=MAXDWORD;
for(tDoorDistList::const_iterator posDist=MapNode.map->DoorDists.begin();posDist!=MapNode.map->DoorDists.end();posDist++)
{
if ( posDist->pos1.x==MapNode.active->dest.pos.x
&& posDist->pos1.y==MapNode.active->dest.pos.y
&& posDist->pos2.x==posLink->source.pos.x
&& posDist->pos2.y==posLink->source.pos.y )
{
dist=posDist->dist+dist_base;
break;
}
}
//在CLOSE表中
//如果 估价值小于CLOSE表的估价值 更新CLOSE表中的估价值;从CLOSE表中移出节点,并放入OPEN表中;
bool inCloseList=false;
for(CLOSE_LIST::iterator posMapNode=CloseList.begin();posMapNode!=CloseList.end();posMapNode++)
{
if( posLink->dest.mapname == posMapNode->map->mapname )
{
inCloseList=true;
if (posMapNode->dist > dist)
{
sMapNode node;
node.map=posMapNode->map;
node.dist=dist;
node.active=&(*posLink);
CloseList.erase(posMapNode);
OpenList.insert(node);
}
break;
}
}
if (inCloseList) continue;
//如果它已经在开放列表了,检查到达那个方块的路径是否更优,以G值为测量值。
//更低的G值意味着更好的路径。如果找到,这个方块的父亲改为当前方块,并重新计算这个方块的G和F值。
//如果你保持开放列表按F值排序的话,可能需要重新排序来解决这个变化。
bool inOpenList=false;
for(OPEN_LIST::iterator posMapNode=OpenList.begin();posMapNode!=OpenList.end();posMapNode++)
{
if( posMapNode->map->mapname == posLink->dest.mapname )
{
inOpenList=true;
if (posMapNode->dist > dist)
{
sMapNode node;
node.map=posMapNode->map;
node.dist=dist;
node.active=&(*posLink);
OpenList.erase(posMapNode);
OpenList.insert(node);
}
break;
}
}
if (inOpenList) continue;
//如果它不在开放列表,将它添加进去。以当前方块作为其父亲。记录这个方块的F,G和H值。
for(tMapList::const_iterator posMap=game.m_DataManager.GetMapList().begin();posMap!=game.m_DataManager.GetMapList().end();posMap++)
{
if( posMap->mapname == posLink->dest.mapname )
{
sMapNode node;
node.map=&(*posMap);
node.dist=dist;
node.active=&(*posLink);
OpenList.insert(node);
}
}
}
}
return SR_FAIL;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化