📄 cbrun.cpp
字号:
/////////////
// CBRun.cpp : v0040
// Written by : Liu Gang
// Compiler : Microsoft Visual C++ 4.2
// Copyright (C) : 1996 WayAhead Corporation
// v0010 : Nov.18.1996
// v0030 : Apr.2.1997, total changed
// v0040 : May.15.1997, fixed bugs in CreateBuild(),
// Harvest(), add some features in Repair(), Fight(), OnShip()
/////////////
// implementation file
// moving units, calculating units
// 对状态的执行模块
#include "stdafx.h"
#include "Assert.h"
#include "CBGame.h"
#include "CBCtrl.h"
#include "CBRun.h"
#include "CBMap.h"
#include "CBData.h"
#include "CBEyes.h"
#include "CBOther.h"
#include "CBDraw.h"
#include "DDCompo.h"
#include "CBBuild.h" // BUILD_
#include "CBZhenFa.h" // 阵法
#include "CBShadow.h" // SHADOW_death(), SHADOW_existent
#include "CBMini.h" // MINI_SetUnitData()
#include "CBMouse.h" // MOUSE_SetState()
#include "CBEvent.h" // 事件处理EVENT_Fire(), etc.
//////////////////////
//- TianYue
#include "Tai.h" // 本能策略
#include "Temperor.h"
#include "TGeneral.h"
//////////////////////
//////////////////////
// Li Haijun
#include "marco.h"
#include "interfa1.h"
#include "interfac.h"
#include "CBprompt.h"
//////////////////////
//////////////////////
// Dong hai, 行军算法
#include "March_n.h"
//extern BYTE MARCH_TrackForward[NUMBER_OF_TRACK];//此数组为用来存储最后由行军轨迹求出的行军路线的方向变化
#define __TY_AI__
// 调试用,为真时执行双方策略
BOOL RUN_bAUTO = FALSE;
// 调试用,为真时本关战斗结束
BOOL RUN_DEBUG_bEndGame = FALSE;
extern BOOL RUN_SOUND_bWeAreUnderAttack; // declared in CBRunOT.cpp
// 时间延时300轮
#define RUN_SOUND_ATTACKDELAY 300
//////////////////////
#include "CBRDelay.h" // defines
//////////////////////
//////////////////////
// externals
extern int MAP_nLocationNum[4]; // defined in CBMAp.cpp
extern POINT MAP_ptLocation[2][MAP_LOCATION_MAX];
extern RECT DRAW_rcScreen; // defined in CBDraw.cpp
// defined in CBCtrl.cpp
// 命令的主体的计数器
extern int CTRL_nHitCounter;
// contains the units player want to send command to
// 命令的主体
// 只有nID有用
extern struct CTRL_TASK_STRUCT CTRL_hitSrc[CTRL_TASK_LIST_MAX-1];
extern struct CTRL_TASK_SAVE_CMDUNIT CTRL_TASK_saveCmdUnit;
// defined in CBCtrl.cpp, uesed by CreateBuild()
//extern void CTRL_TASK_BuildCommand( int nFile );
extern void CTRL_TASK_BuildCommand( struct UNIT_STRUCT *pUnit );
//////////////////////
//////////////////////
// 战斗记分, Nov.6.1007
//////////////////////
struct RUN_COUNTDOWN RUN_CountDown[GAME_PLAYER_MAX];
///////////////////////////////
void RUN_EndGame( int bWin );
void RUN_DrawUpAndDown(struct CTRL_FRAME_STRUCT * pDraw);
BOOL RUN_GetNextForward(struct CTRL_FRAME_STRUCT * pDraw,struct UNIT_STRUCT * pUnit);
void RUN_MOULUE_Add();
///////////////////////////////
///////////////////////////////
// 某单元对状态的一次执行
BOOL RUN_RunUnit( int nPlayer, int nUnit );
///////////////////////////////
// 对状态的处理
///////////////////////////////
// 当状态是YIDONG时调用
// return : 当移动一格时,到达目的地时
BOOL RUN_MoveUnit( struct UNIT_STRUCT *pUnit );
// 当状态是CHANSHENG时调用,仅用于建筑生产部队
void RUN_CreateSoldier( struct UNIT_STRUCT *pUnit );
// 当状态是JIANZAO时调用,仅用于工人部队建造建筑
void RUN_CreateBuild( struct UNIT_STRUCT *pUnit );
// 当状态是SHANCHU时调用
void RUN_DestroyUnit( struct UNIT_STRUCT *pUnit );
// 当前状态是GONGJI时调用
void RUN_FightUnit( struct UNIT_STRUCT *pUnit );
// 当前状态是ZUJIAN时调用
void RUN_GroupUnit( struct UNIT_STRUCT *pUnit );
// 当前状态是JIARU时调用
void RUN_GroupAdd( struct UNIT_STRUCT *pUnit );
// 当前状态是JIESAN时调用
void RUN_GroupSub( struct UNIT_STRUCT *pUnit );
// 当前状态是XIULI时调用
void RUN_RepairBuild( struct UNIT_STRUCT *pUnit );
// 当前状态是QXSHENGCHAN时调用
void RUN_QXCreateSoldier( struct UNIT_STRUCT *pUnit );
// 当状态是QXJIANZAO时调用
void RUN_QXCreateBuild( struct UNIT_STRUCT *pUnit );
// 当状态是JIANZAOZHONG时调用
void RUN_CreatingBuild( struct UNIT_STRUCT *pUnit );
// 当状态是YUNSONG时调用
void RUN_Harvest( struct UNIT_STRUCT *pUnit );
// 当状态是HUISONG时调用
void RUN_ReturnOfTheJedi( struct UNIT_STRUCT *pUnit );
// 当状态是TISHENG时调用
void RUN_Upgrade( struct UNIT_STRUCT *pUnit );
// 当状态是QXTISHENG时调用
void RUN_QxUpgrade( struct UNIT_STRUCT *pUnit );
// 当状态是DUSE时调用
void RUN_Blocked( struct UNIT_STRUCT *pUnit );
BOOL RUN_bBlock; // 限制每轮只能有一支部队从堵塞状态恢复
// 当状态是YUNZAI时调用
void RUN_OnShip( struct UNIT_STRUCT *pUnit );
// 当状态是XIEZAI时调用
void RUN_OffShip( struct UNIT_STRUCT *pUnit );
// 当状态是ZHENXING时调用
void RUN_LineUp( struct UNIT_STRUCT *pUnit );
// 当状态是JIESHU时调用, not used now
void RUN_EndAll( struct UNIT_STRUCT *pUnit );
// 当状态是KAIMEN时调用
void RUN_OpenDoor( struct UNIT_STRUCT *pUnit );
// 当状态是JIMOU时调用
void RUN_Strategy( struct UNIT_STRUCT *pUnit );
// 当状态是HLUAN时调用
void RUN_HLuan( struct UNIT_STRUCT *pUnit );
// 当状态是MFU时调用
void RUN_MFu( struct UNIT_STRUCT *pUnit );
// 当状态是DANTIAO时调用
void RUN_FightSingle( struct UNIT_STRUCT *pUnit );
// 当状态是XIUXI时调用
void RUN_Rest( struct UNIT_STRUCT *pUnit );
///////////////////////////////
// others, defined in CBRunOT.cpp
///////////////////////////////
// 花钱
BOOL RUN_CREATE_Consume( int nPlayer, int nFile );
// 找钱
BOOL RUN_CREATE_PayBack( int nPlayer, int nFile );
// 设置单元死亡后的动画
// pUnit : 即将死亡的单元的指针
void RUN_DESTROY_SetAfterLife( struct UNIT_STRUCT *pUnit );
// 当建筑被攻击时,要着火
// pU : 被攻击的建筑的指针
void RUN_FIGHT_SetFire( struct UNIT_STRUCT *pU );
// 对于投石车,要投石
// pUnit : 投石车
// pU : 敌人
void RUN_FIGHT_SetStone( struct UNIT_STRUCT *pUnit , struct UNIT_STRUCT *pU );
// 如果是100人的步兵或弓兵,可以使用滚木计谋,攻击时使用滚木攻击
// pUnit : 投滚木的人
// pU : 敌人
void RUN_FIGHT_SetGunMu( struct UNIT_STRUCT *pUnit , struct UNIT_STRUCT *pU );
// 对于弓箭手,箭楼,战船,要射箭
// pUnit : 弓箭手
// pU : 敌人
void RUN_FIGHT_SetArrow( struct UNIT_STRUCT *pUnit , struct UNIT_STRUCT *pU );
// 计算战斗效果,死伤人数
// pUnit : 攻击方
// pUnit2 : 被攻击方
// return : TRUE if 被攻击方死了
BOOL RUN_FIGHT_CalcResult( struct UNIT_STRUCT *pUnit, struct UNIT_STRUCT *pUnit2 );
// 检测是否到达目的地了
// return : 应该转变的方向,0-1,为-1时未到达
int RUN_MOVE_IfArrived( struct UNIT_STRUCT *pUnit );
// 计算收取建筑资源的多少
// pUnit : 工人部队
// pU : 资源建筑
int RUN_HARVEST_Build( struct UNIT_STRUCT *pUnit, struct UNIT_STRUCT *pU );
//把采集的资源送回到家中
// pUnit : 工人部队
// pU : 家
// return : 为真时是回送完毕,否则超出粮仓的储量,失败
BOOL RUN_RETURN_Build( struct UNIT_STRUCT *pUnit, struct UNIT_STRUCT *pU );
// 修理建筑
BOOL RUN_REPAIR_Build( struct UNIT_STRUCT *pUnit );
// 升级预付款
BOOL RUN_UPGRADE_Pay( int nPlayer, int nTaskIDEx );
// 升级后提货
//void RUN_UPGRADE_Get( int nPlayer, int nTaskIDEx );
void RUN_UPGRADE_Get( struct UNIT_STRUCT *pUnit );
// 升级取消后找钱
// nTaskIDEx : 升级的类型
void RUN_UPGRADE_PayBack( int nPlayer, int nTaskIDEx );
///////////////////////////////
///////////////////////////////
// debug only
void RUN_MoveUnitTest( struct UNIT_STRUCT *pUnit );
///////////////////////////////
///////////////////////////////
//#define _DEBUG_OFFSET_
#ifdef _DEBUG_OFFSET_
void TestOff( struct UNIT_STRUCT *pUnit, LPCTSTR info )
{
Assert( pUnit->Draw.ptOff.x == 0 && pUnit->Draw.ptOff.y == 0 );
if( !(pUnit->Draw.ptOff.x == 0 && pUnit->Draw.ptOff.y == 0) )
{
pUnit->Draw.ptOff.x = pUnit->Draw.ptOff.y = 0;
// WriteLogFile( "off.log", info );
}
}
#endif //_DEBUG_OFFSET_
///////////////////////////////
///////////////////////////////
// 运行计数器
int RUN_nCounter = 0;
void RUN_RunUnits()
{
int i, j;
int nRun[GAME_PLAYER_MAX]={0,0,0,0,0};
//#undef __TY_AI__
BOOL nRet;
#ifdef __TY_AI__
if( RUN_bAUTO == FALSE )
{
for( int i=0; i<GAME_PLAYER_MAX; i++ )
{
if( GAME.Players[i].nType == PLAYER_TYPE_COMPUTER )
{
TY_AI( i, GAME.nID );
}
}
}
else
{
for( int i=0; i<GAME_PLAYER_MAX; i++ )
{
if( GAME.Players[i].nType == PLAYER_TYPE_COMPUTER )
{
Assert( i != GAME.nMe );
TY_AI( i, GAME.nID );
}
else if( GAME.Players[i].nType == PLAYER_TYPE_HUMAN )
{
int nMeSave = GAME.nMe;
BOOL bFind = FALSE;
for( int j=0; j<GAME_PLAYER_MAX; j++ )
{
if( GAME.Players[j].nType == PLAYER_TYPE_COMPUTER )
{
GAME.nMe = j; bFind = TRUE; break;
}
}
if( bFind == TRUE )
{
Assert( i != GAME.nMe );
TY_AI( i, GAME.nID );
}
GAME.nMe = nMeSave;
}
}
}
#endif
// 处理
CTRL_STATUS_Main();
CTRL_TASK_Main();
RUN_bBlock = 0; // 清除堵塞状态恢复标志
for( i=0; i<GAME_PLAYER_MAX; i++ )
{
for( j=0; j<PLAYER_UNIT_MAX; j++ )
{
if( RUN_RunUnit( i, j ) == TRUE )
{
nRun[i]++;
// if( i == 3 )
// OutputDebugString("a");
}
if( nRun[i] == GAME.Players[i].wUnitCounter )
break;
}
}
//save file each moment
if( RUN_nCounter%RUN_ENDGAME_DELAY == 0
|| RUN_DEBUG_bEndGame )
{
// 检测事件
nRet = EVENT_Main( GAME.nID );
int nEnd = -1;
// test if should end the game
if( GAME.nID == 5
&& ( nRet == 1 || RUN_DEBUG_bEndGame == 2 ) )
{ // 路过襄阳,刘备分支一
nEnd = 2;
RUN_DEBUG_bEndGame = FALSE;
}
else if( GAME.nID == 8
&& ( nRet == 1 || RUN_DEBUG_bEndGame == 2 ) )
{ // 长阪坡赵云逃脱
nEnd = 1;
RUN_DEBUG_bEndGame = FALSE;
}
else if( GAME.nID == 14
&& ( nRet == 2 || RUN_DEBUG_bEndGame == 2 ) )
{ // 华容道曹操逃脱,刘备分支二
nEnd = 2;
RUN_DEBUG_bEndGame = FALSE;
}
else if( GAME.nID == 29 )
{
if ( EYE_IfPlayerIsOver( 2, nRun[2] )
|| RUN_DEBUG_bEndGame == 2 )
{ // 赤壁大战刘备死亡,曹操分支一
nEnd = 2;
RUN_DEBUG_bEndGame = FALSE;
}
else if ( EYE_IfPlayerIsOver( 1, nRun[1] )
|| RUN_DEBUG_bEndGame == 1 )
{ // 赤壁大战孙权死亡,曹操分支二
nEnd = 1;
RUN_DEBUG_bEndGame = FALSE;
}
}
else if( GAME.nID == 30
&& ( nRet == 1 || RUN_DEBUG_bEndGame == 2 ) )
{ // 华容道曹操逃脱
nEnd = 1;
RUN_DEBUG_bEndGame = FALSE;
}
else if( RUN_DEBUG_bEndGame > 0 )
{ // 一般调试结束
nEnd = 1;
RUN_DEBUG_bEndGame = FALSE;
}
else if( EYE_IfPlayerIsOver( GAME.nMe, nRun[GAME.nMe] ) )
{
// July 24, 1997
// 当自己的金钱为0时,或所有建筑都被摧毁时,而且战场上没有了
// 将领,则结束游戏。
nEnd = 0;// 输了
}
else
{
BOOL bEnd = TRUE;
for( int i=0; i<GAME_PLAYER_MAX; i++ )
{
if( GAME.Players[i].nType == PLAYER_TYPE_NONE
|| GAME.nMe == i
|| GAME.Players[i].wUnitCounter == 0 )
{ // 无君主或是我自己或某君主没有单元了,不考虑
continue;
}
if( (GAME.Players[i].nType == PLAYER_TYPE_REMOTE )
&& EYE_PlayerIsAllied( GAME.nMe, i ) )
{ // 同盟者不考虑
continue;
}
// 为了绕过一个Bug,有时可能虽然将领计数器不为0,而实际将领数已经为0了。
if( nRun[i] == 0 && GAME.Players[i].wGroupCounter >= 1 )
{
for( int x = 0; x<PLAYER_GROUP_MAX; x++ )
{
if( GAME.Players[i].wGroup[x] != MAP_DATA_NONE )
{
struct UNIT_STRUCT *pU=MAP_GetUnit(GAME.Players[i].wGroup[x]);
if( EYE_IfUnitIsDead(pU) )
{
GAME.Players[i].wGroup[x] = MAP_DATA_NONE;
GAME.Players[i].wGroupCounter --;
}
}
}
}
if( EYE_IfPlayerIsOver( i, nRun[i] ) )
{ // 某个君主完蛋了,不考虑
continue;
}
bEnd = FALSE;
break;
}
if( bEnd == TRUE )
{
nEnd = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -