⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scriptinterface.cpp

📁 是一个基于热血战国协议的网络游戏。现在脱机客户端先放出来给大家研究
💻 CPP
字号:

/*
 * name:		CScriptInterface.cpp
 *
 * desc:		实现文件
 *
*/

#include "stdafx.h"
#include "..\TurboMir.h"
#include "gamemir.h"
#include "..\MainFrm.h"

//////////////////////////////////////////////////////////////////////////
// Conditions 下面的是脚本的条件
//////////////////////////////////////////////////////////////////////////

/*
=======================================================================
函数名       : CheckMapName
功能描述     : 检测地图名字
参数         : void
返回值       : NULL
=======================================================================
*/
bool CScriptInterface::CheckMapName( CGameMir& game, CScriptEngine& se, boost::wsmatch& m )
{
	char buf[STRING_BUFFER_LENGTH+1];
	std::wstring op=m.str(1);
	std::string mapname=ToAnsi(m.str(2).c_str(),buf,STRING_BUFFER_LENGTH);
	bool retval=( mapname == game.m_Self.MapTitle );

	game.m_FrameWnd.AddLog(crMessage,"脚本: 检测当前地图:[%s] 结果:[%s]\n",mapname.c_str(),charbool2chstr(retval));
	if ( op == L"=" )
		return retval;
	else if ( op == L"<>" )
		return !retval;
	return false;
}

/*
=======================================================================
函数名       : CheckItemCount
功能描述     : 检测道具数量
参数         : void
返回值       : NULL
=======================================================================
*/
bool CScriptInterface::CheckItemCount( CGameMir& game, CScriptEngine& se, boost::wsmatch& m )
{
	char buf[STRING_BUFFER_LENGTH+1];
	std::string itemname=ToAnsi(m.str(1).c_str(),buf,STRING_BUFFER_LENGTH);
	std::wstring op=m.str(2);
	int value=boost::lexical_cast<int>(m.str(3));
	int cnt=0;

	//统计个数
	for ( std::list<_TCLIENTITEM>::iterator pos=game.m_BagItems.begin(); pos!=game.m_BagItems.end(); ++pos )
	{
		GetDelphiString(pos->S.Name,sizeof(pos->S.Name),buf);
		if ( itemname==buf )
		{
			cnt++;
		}
	}
	game.m_FrameWnd.AddLog(crMessage,"脚本: 物品:[%s] 数量:[%d]\n",itemname.c_str(),cnt);

	if ( op == L"=" ) return ( cnt == boost::lexical_cast<int>(value) );
	if ( op == L"<=" ) return ( cnt <= boost::lexical_cast<int>(value) );

	return false;
}

//////////////////////////////////////////////////////////////////////////
// Commands 下面的是脚本的命令
//////////////////////////////////////////////////////////////////////////

/*
=======================================================================
函数名       : JumpToLine
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::JumpToLine( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result )
{
	if (se.Jump(boost::lexical_cast<int>(m.str(1))))
		return SR_SUCCESS|SR_NOMOVE;
	return SR_FAIL;
}

/*
=======================================================================
函数名       : JumpToLabel
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::JumpToLabel( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result )
{
	if (se.Jump(m.str(1)))
		return SR_SUCCESS|SR_NOMOVE;
	return SR_FAIL;
}

/*
=======================================================================
函数名       : Wait
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::Wait( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result )
{
	int isleeptime=boost::lexical_cast<int>(m.str(1));

	game.m_FrameWnd.AddLog(crMessage,"脚本: 睡眠时间%d\n",isleeptime);

	Sleep(isleeptime);
	return SR_SUCCESS;
}

/*
=======================================================================
函数名       : ShowMessage
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::ShowMessage( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result )
{
	char buf[STRING_BUFFER_LENGTH+1];
	game.m_FrameWnd.AddLog(crMessage,"脚本: 输出消息:%s\n",ToAnsi(m.str(1).c_str(),buf,STRING_BUFFER_LENGTH));
	return SR_SUCCESS;
}

/*
=======================================================================
函数名       : DisConnect
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::DisConnect( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result )
{
	game.Close();
	return SR_SUCCESS;
}

/*
=======================================================================
函数名       : DoNothing
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::DoNothing( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result )
{
	char buf[STRING_BUFFER_LENGTH+1];
	for(size_t i=1;i<m.size();i++)
	{
		game.m_FrameWnd.AddLog(crMessage,"脚本: 参数%d=%s\n",i,ToAnsi(m.str(i).c_str(),buf,STRING_BUFFER_LENGTH));
	}
	return SR_SUCCESS;
}

/*
=======================================================================
函数名       : MapMove
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::MapMove( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result )
{
	char buf[STRING_BUFFER_LENGTH+1];
	std::string mapname=ToAnsi(m.str(1).c_str(),buf,STRING_BUFFER_LENGTH);

	PATH_LIST PathList;
	POINT pnt;
	pnt.x=boost::lexical_cast<LONG>(m.str(2));
	pnt.y=boost::lexical_cast<LONG>(m.str(3));
	if( BuildMapMovePath(mapname,pnt,game,PathList)==SR_SUCCESS)
	{
		for ( PATH_LIST::iterator itor=PathList.begin();itor!=PathList.end();itor++)
		{
			game.m_Action.MoveTo(itor->source.pos);
			int i;
			for ( i=0; i<100; ++i)
			{
				if(itor->dest.mapname==game.m_Map.get_MapName())
					break;
				Sleep(50);
			}
			if ( i==100 )
			{
				game.m_FrameWnd.AddLog(crRed,"脚本: 过图等待超时!!\n");
				return SR_FAIL;
			}
		}
	}
	else
	{
		game.m_FrameWnd.AddLog(crRed,"脚本: 目标地图无法到达!!\n");
		return SR_FAIL;
	}
	return SR_SUCCESS;
}

/*
=======================================================================
函数名       : BuildMapMovePath
功能描述     : 
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::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++)
	{
		//game.m_FrameWnd.AddLog(crMessage,"%s[%s]\n",pos->maptitle.c_str(),pos->mapname.c_str());
		if( pos->maptitle == mapname )
		{
			Target=pos->mapname;
			break;
		}
	}
	if ( Target.empty() )
	{
		game.m_FrameWnd.AddLog(crError,"脚本: 无法检索目标地图信息!\n");
		return SR_FAIL;
	}
	if ( Target==game.m_Map.get_MapName() )
	{
		_TDOORLINK 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;
	}
	//game.m_FrameWnd.AddLog(crMessage,"脚本: 移动到 %s .\n",Target.c_str());
	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.m_FrameWnd.AddLog(crError,"脚本: 无法检索当前地图信息!\n");
		return SR_FAIL;
	}
	// 计算当前地图各个门点的dist
	DWORD dist;
	CGameDStar 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.m_FrameWnd.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 )
			{
				//game.m_FrameWnd.AddLog(crWhite,"[%s](%d,%d) -> [%s](%d,%d)\n",
				//	p->active->source.mapname.c_str(),
				//	p->active->source.pos.x,
				//	p->active->source.pos.y,
				//	p->active->dest.mapname.c_str(),
				//	p->active->dest.pos.x,
				//	p->active->dest.pos.y
				//	);
				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;
					}
				}
			}
			for ( PATH_LIST::iterator itor=PathList.begin();itor!=PathList.end();itor++)
			{
				game.m_FrameWnd.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;
}

/*
=======================================================================
函数名       : StartFight
功能描述     : 开始战斗
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::StartFight( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result ) 
{
	game.m_AutoFight.Go();
	return SR_SUCCESS;
}

/*
=======================================================================
函数名       : UseItem
功能描述     : 使用道具
参数         : void
返回值       : NULL
=======================================================================
*/
DWORD CScriptInterface::UseItem( CGameMir& game, CScriptEngine& se, boost::wsmatch& m, bool& result ) 
{
	char buf[STRING_BUFFER_LENGTH+1];
	std::string itemname=ToAnsi(m.str(1).c_str(),buf,STRING_BUFFER_LENGTH);
	int i;
	for (i=0;i<20;i++)
	{
		if( game.CanEat() )
			break;
		Sleep(50);
	}
	if ( i==20 )
	{
		game.m_FrameWnd.AddLog(crError,"脚本: 使用%s超时!\n",itemname.c_str());
		return SR_FAIL;
	}
	game.EatItem(0,itemname.c_str());
	game.m_FrameWnd.AddLog(crMessage,"脚本: 使用%s.\n",itemname.c_str());
	return SR_SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -