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

📄 gameaction.cpp

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

/*
 * name: GameAction.cpp
 *
 * desc: 游戏动作,发送给游戏服务器的数据包,现在主要完成了一些基本的动作
 *
*/

#include "StdAfx.h"
#include "GameMir.h"
#include "..\MainFrm.h"
#include ".\gamemir.h"

const DWORD MOVE_DELAY=600;
const DWORD ATTACK_DELAY=600;

/*
=======================================================================
函数名       : CGameAction
功能描述     : 构造函数
参数         : CGameMir& game
返回值       : 
=======================================================================
*/
CGameAction::CGameAction( CGameMir& game ): m_Game(game)
{
	lastMove   = 0;
	lastHit    = 0;
	m_PowerHit = false;
	m_LongHit  = false;
	m_FireHit  = false;
	m_WideHit  = false;
	m_LastAction.wCmd=0;
}

/*
=======================================================================
函数名       : ~CGameAction
功能描述     : 拆构函数
参数         : void
返回值       : 
=======================================================================
*/
CGameAction::~CGameAction(void)
{

}

/*
=======================================================================
函数名       : SendRun 
功能描述     : 发送跑步信息给服务器
参数         : int dir 人物方向
返回值       : void
=======================================================================
*/
void CGameAction::SendRun(int dir)
{
	//判断
	if(!CanAction())
	{
		// AfxMessageBox("!CanAction()");
		return;
	}
	DWORD t=GetTickCount()-lastMove;
	if( t<m_Game.m_Sets.MoveSpeed )
	{
		Sleep(m_Game.m_Sets.MoveSpeed-t);
	}

	//打数据包
	MMSG msg;
	dir &= 7;
	ZeroMemory(&msg,sizeof(MMSG));
	msg.wa = m_Game.m_Self.xx + xofs_run[dir];
	msg.wb = m_Game.m_Self.yy + yofs_run[dir];
	msg.w3 = dir;
	msg.wCmd = CM_RUN;

	//发送
	m_Game.SendMsg(msg);
	m_LastAction=msg;
	lastMove=GetTickCount();
	//m_Game.m_FrameWnd.AddLog(crMessage,"CM_RUN\n");
}

/*
=======================================================================
函数名       : SendWalk
功能描述     : 发送走路信息给服务器
参数         : int dir 人物方向
返回值       : void
=======================================================================
*/
void CGameAction::SendWalk(int dir)
{
	//判断
	if(!CanAction())
	{
		// AfxMessageBox("!CanAction()");
		return;
	}
	DWORD t=GetTickCount()-lastMove;
	if(t<m_Game.m_Sets.MoveSpeed)
	{
		Sleep(m_Game.m_Sets.MoveSpeed-t);
	}
	//打包
	MMSG msg;
	dir &= 7;
	ZeroMemory(&msg,sizeof(MMSG));
	msg.wa = m_Game.m_Self.xx + xofs_walk[dir];
	msg.wb = m_Game.m_Self.yy + yofs_walk[dir];
	msg.w3 = dir;
	msg.wCmd = CM_WALK;

	//发送
	m_Game.SendMsg(msg);
	m_LastAction=msg;
	lastMove=GetTickCount();
	//m_Game.m_FrameWnd.AddLog(crMessage,"CM_WALK\n");
}

/*
=======================================================================
函数名       : SendHit
功能描述     : 发送攻击信息给服务器
参数         : int dir 人物方向
参数         : WORD cmd
返回值       : void
=======================================================================
*/
void CGameAction::SendHit(int dir,WORD cmd)
{
	//判断
	if(!CanAction())
	{
		// AfxMessageBox("!CanAction()");
		return;
	}
	DWORD t=GetTickCount()-lastHit;
	if( t<m_Game.m_Sets.AttackSpeed )
	{
		Sleep(m_Game.m_Sets.AttackSpeed-t);
	}
	//打包
	MMSG msg;
	dir &= 7;
	ZeroMemory(&msg,sizeof(MMSG));
	msg.wa = m_Game.m_Self.xx;
	msg.wb = m_Game.m_Self.yy;
	msg.w2 = dir;
	msg.wCmd = cmd;
	//发送
	m_Game.SendMsg(msg);
	m_LastAction=msg;
	lastHit=GetTickCount();
}

/*
=======================================================================
函数名       : SendSpell
功能描述     : 发送SPELL信息给服务器
参数         : WORD cmd
参数         : WORD x
参数         : WORD y
参数         : WORD magicid
参数         : DWORD target
返回值       : void
=======================================================================
*/
void CGameAction::SendSpell( WORD cmd, WORD x, WORD y, WORD magicid, DWORD target )
{
	//判断
	if(!CanAction())
	{
		// AfxMessageBox("!CanAction()");
		return;
	}
	//打包
	MMSG msg;
	ZeroMemory(&msg,sizeof(MMSG));
	msg.wCmd = cmd;
	msg.wa = x;
	msg.wb = y;
	msg.w2 = magicid;
	msg.w1 = LOWORD(target);
	msg.w3 = HIWORD(target);
	//发送
	m_Game.SendMsg(msg);
	m_LastAction=msg;
}

/*
=======================================================================
函数名       : SendPickUp
功能描述     : 发送捡起信息给服务器
参数         : int dir 人物方向
返回值       : void
=======================================================================
*/
void CGameAction::SendPickUp(void)
{
	//打包
	MMSG msg;
	ZeroMemory(&msg,sizeof(MMSG));
	msg.wCmd = CM_PICKUP;
	msg.w1 = m_Game.m_Self.xx;
	msg.w2 = m_Game.m_Self.yy;
	//发送
	m_Game.SendMsg(msg);
}

/*
=======================================================================
函数名       : OnResult 
功能描述     : 接收攻击动作
参数         : const char * result
返回值       : void
=======================================================================
*/
void CGameAction::OnResult(const char * result)
{
	std::vector<std::string> sl;
	char buf[32];
	strncpy(buf,result,32);
	m_Game.SplitString( buf, "/", sl );
	if ( sl[0]=="GOOD" )
	{
		ActionGood();
		return;
	}
	if ( sl[0]=="FAIL" )
	{
		ActionFail();
		return;
	}
	if ( sl[0]=="PWR" )
	{
		m_PowerHit=true;
		return;
	}
	if ( sl[0]=="ULNG" )
	{
		m_LongHit=false;
		return;
	}
	if ( sl[0]=="LNG" )
	{
		m_Game.m_FrameWnd.AddLog(crGreen,"刺杀开启\n");
		m_LongHit=true;
		return;
	}
	if ( sl[0]=="UWID" )
	{
		m_WideHit=false;
		return;
	}
	if ( sl[0]=="WID" )
	{
		m_Game.m_FrameWnd.AddLog(crGreen,"半月开启\n");
		m_WideHit=true;
		return;
	}
	if ( sl[0]=="FIR" )
	{
		m_FireHit=true;
		return;
	}
	m_Game.m_FrameWnd.AddLog(crGreen,"%s\n",result);
}

/*
=======================================================================
函数名       : SendSay 
功能描述     : 发送说话信息给服务器
参数         : LPCTSTR text
返回值       : void
=======================================================================
*/
void CGameAction::SendSay(LPCTSTR text)
{
	//发送
	m_Game.SendMsg(CM_SAY,0,0,0,0,text);
}

/*
=======================================================================
函数名       : ActionGood
功能描述     : 行动成功
参数         : void
返回值       : void
=======================================================================
*/
void CGameAction::ActionGood(void)
{
	switch ( m_LastAction.wCmd )
	{
	case CM_WALK:
	case CM_RUN:
		m_Game.m_Self.xx=m_LastAction.wa;
		m_Game.m_Self.yy=m_LastAction.wb;
		m_Game.m_FrameWnd.UpdateSelfPos();
		if ( m_Game.m_Sets.SmartAccelerate )
		{
			--m_Game.m_Sets.MoveSpeed;
			if( m_Game.m_Sets.MoveSpeed<10 )
				m_Game.m_Sets.MoveSpeed=10;
		}
		break;
	case CM_FIREHIT:
		m_FireHit=false;
		if ( m_Game.m_Sets.SmartAccelerate )
		{
			--m_Game.m_Sets.AttackSpeed;
			if( m_Game.m_Sets.AttackSpeed<10 )
				m_Game.m_Sets.AttackSpeed=10;
		}
		break;
	case CM_POWERHIT:
		m_PowerHit=false;
		if ( m_Game.m_Sets.SmartAccelerate )
		{
			--m_Game.m_Sets.AttackSpeed;
			if( m_Game.m_Sets.AttackSpeed<10 )
				m_Game.m_Sets.AttackSpeed=10;
		}
		break;
	case CM_LONGHIT:
	case CM_HIT:
	case CM_HEAVYHIT:
	case CM_BIGHIT:
		if ( m_Game.m_Sets.SmartAccelerate )
		{
			--m_Game.m_Sets.AttackSpeed;
			if( m_Game.m_Sets.AttackSpeed<10 )
				m_Game.m_Sets.AttackSpeed=10;
		}
		break;
	}
	m_LastAction.wCmd=0;
}

/*
=======================================================================
函数名       : ActionFail
功能描述     : 行动失败
参数         : void
返回值       : void
=======================================================================
*/
void CGameAction::ActionFail(void)
{
	switch ( m_LastAction.wCmd )
	{
	case CM_WALK:
	case CM_RUN:
		if ( m_Game.m_Sets.SmartAccelerate )
		{
			m_Game.m_Sets.MoveSpeed+=20;
			if( m_Game.m_Sets.MoveSpeed>2000 )
				m_Game.m_Sets.MoveSpeed=2000;
		}
		break;
	case CM_POWERHIT:
	case CM_LONGHIT:
	case CM_HIT:
	case CM_HEAVYHIT:
	case CM_BIGHIT:
		if ( m_Game.m_Sets.SmartAccelerate )
		{
			m_Game.m_Sets.AttackSpeed+=20;
			if( m_Game.m_Sets.AttackSpeed>2000 )
				m_Game.m_Sets.AttackSpeed=2000;
		}
		break;
	}
	m_LastAction.wCmd=0;
}


/*
=======================================================================
函数名       : MoveTo 
功能描述     : 移动到某点
参数         : int x
参数         : int y
返回值       : void
=======================================================================
*/
void CGameAction::MoveTo(int x, int y)
{
	POINT pnt;
	pnt.x = x;
	pnt.y = y;
	MoveTo(pnt);
}

/*
=======================================================================
函数名       : MoveTo
功能描述     : 移动到某点
参数         : const POINT& target
返回值       : void
=======================================================================
*/
void CGameAction::MoveTo(const POINT& target )
{
	//m_Game.m_FrameWnd.AddLog(crMessage,"MoveTo(%d,%d)\n",target.x,target.y);
	if(m_Game.m_Self.xx==target.x && m_Game.m_Self.yy==target.y)
	{
		m_Game.m_FrameWnd.AddLog(crMessage,"m_Game.m_Self.xx==target.x && m_Game.m_Self.yy==target.y\n");
		return;
	}
	
	//初始化D*
	CGameDStar DStar(m_Game.m_Map,m_Game);
	std::string CurrentMap=m_Game.m_Map.get_MapName();
	std::vector<POINT> Path;//路径
	u64 retval=DStar.Init(target.x,target.y);
	if(retval==0)
	{
		AfxMessageBox("DStar 初始化失败");
		return;
	}

	//取得路径
	POINT start;
	start.x=m_Game.m_Self.xx;
	start.y=m_Game.m_Self.yy;
	while ( true )
	{
		POINT next,search;
		bool action;
		start.x=m_Game.m_Self.xx;
		start.y=m_Game.m_Self.yy;
		if(start==target) //到达目的地
		{
			return;
		}
		if(CurrentMap!=m_Game.m_Map.get_MapName())
		{
			return;
		}
		if ( Path.empty() )
		{
			if ( !DStar.GetPath( start, Path ) )
				return;
			m_Game.m_FrameWnd.m_wndMap.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(m_Game.CanMove(search.x,search.y))
					{
						if( search==next )
						{
							action=true;
							SendWalk(i);//发送走路
							break;
						}
						search.x+=xofs_walk[i];
						search.y+=yofs_walk[i];
						if(m_Game.CanMove(search.x,search.y))
						{
							if( search==next )
							{
								action=true;
								SendRun(i);//发送跑步
								break;
							}
						}
					}
				}
			}
			else
			{
				m_Game.m_FrameWnd.AddLog(crMessage," Next Fail\n");
			}
		}
		else
		{
			m_Game.m_FrameWnd.AddLog(crMessage," 在路径中查找英雄当前所在点 fail"__FILE__"\n");
		}
		if(action)
		{
			//m_Game.m_FrameWnd.AddLog(crMessage," action\n");
			while(!CanAction())
				Sleep(100);
		}
		else
		{
			//m_Game.m_FrameWnd.AddLog(crMessage," not action\n");
			Sleep(200);
			Path.clear();
			//说明我们的英雄已经偏离了路线 或者路原来的路径上被人站了 重新查找路径
		}
	}
}

/*
=======================================================================
函数名       : CanLongHit
功能描述     : 可以刺杀
参数         : void
返回值       : bool
=======================================================================
*/
bool CGameAction::CanLongHit(void) 
{ 
	return m_LongHit; 
}

/*
=======================================================================
函数名       : CanWideHit
功能描述     : 可以半月
参数         : void
返回值       : bool
=======================================================================
*/
inline bool CGameAction::CanWideHit(void)
{ 
	return m_WideHit; 
}  

/*
=======================================================================
函数名       : CanPowerHit
功能描述     : 可以攻杀
参数         : void
返回值       : bool
=======================================================================
*/
bool CGameAction::CanPowerHit(void)
{ 
	return m_PowerHit;
}

/*
=======================================================================
函数名       : CanFireHit
功能描述     : 可以烈火
参数         : void
返回值       : bool
=======================================================================
*/
bool CGameAction::CanFireHit(void) 
{ 
	return m_FireHit; 
}  

/*
=======================================================================
函数名       : CanAction
功能描述     : 可以行动
参数         : void
返回值       : bool
=======================================================================
*/
inline bool CGameAction::CanAction(void)
{ 
	return ( m_LastAction.wCmd==0 ); 
}

⌨️ 快捷键说明

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