New AI attacks.
This commit is contained in:
parent
685341d157
commit
af53cdfac3
6 changed files with 200 additions and 35 deletions
|
@ -89,7 +89,7 @@ local void AiCheckUnits(void)
|
|||
//
|
||||
for( queue=AiPlayer->UnitTypeBuilded; queue; queue=queue->Next ) {
|
||||
counter[queue->Type->Type]+=queue->Want;
|
||||
DebugLevel0Fn("Already in build queue: %s %d/%d\n" _C_
|
||||
DebugLevel3Fn("Already in build queue: %s %d/%d\n" _C_
|
||||
queue->Type->Ident _C_ queue->Made _C_ queue->Want);
|
||||
}
|
||||
unit_types_count=AiPlayer->Player->UnitTypesCount;
|
||||
|
@ -102,7 +102,7 @@ local void AiCheckUnits(void)
|
|||
t=AiPlayer->UnitTypeRequests[i].Table[0]->Type;
|
||||
x=AiPlayer->UnitTypeRequests[i].Count;
|
||||
if( x>unit_types_count[t]+counter[t] ) { // Request it.
|
||||
DebugLevel0Fn("Need %s *%d\n" _C_
|
||||
DebugLevel3Fn("Need %s *%d\n" _C_
|
||||
AiPlayer->UnitTypeRequests[i].Table[0]->Ident,x);
|
||||
AiAddUnitTypeRequest(AiPlayer->UnitTypeRequests[i].Table[0],
|
||||
x-unit_types_count[t]-counter[t]);
|
||||
|
@ -121,7 +121,7 @@ local void AiCheckUnits(void)
|
|||
t=aiut->Type->Type;
|
||||
x=aiut->Want;
|
||||
if( x>unit_types_count[t]+counter[t] ) { // Request it.
|
||||
DebugLevel0Fn("Force %d need %s * %d\n" _C_ i _C_
|
||||
DebugLevel3Fn("Force %d need %s * %d\n" _C_ i _C_
|
||||
aiut->Type->Ident,x);
|
||||
AiAddUnitTypeRequest(aiut->Type,
|
||||
x-unit_types_count[t]-counter[t]);
|
||||
|
@ -234,8 +234,16 @@ local void AiReduceMadeInBuilded(const PlayerAi* pai,const UnitType* type)
|
|||
*/
|
||||
global void AiHelpMe(Unit* unit)
|
||||
{
|
||||
DebugLevel0Fn("%d(%s) attacked at %d,%d" _C_
|
||||
PlayerAi* pai;
|
||||
|
||||
DebugLevel0Fn("%d(%s) attacked at %d,%d\n" _C_
|
||||
UnitNumber(unit) _C_ unit->Type->Ident _C_ unit->X _C_ unit->Y);
|
||||
|
||||
pai=unit->Player->Ai;
|
||||
if( !pai->Force[0].Attacking ) { // Send force 0 defending
|
||||
AiAttackWithForceAt(0,unit->X,unit->Y);
|
||||
pai->Force[0].Defending=1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -336,7 +344,7 @@ global void AiEachFrame(Player* player)
|
|||
global void AiEachSecond(Player* player)
|
||||
{
|
||||
|
||||
DebugLevel0Fn("%d:\n" _C_ player->Player);
|
||||
DebugLevel3Fn("%d:\n" _C_ player->Player);
|
||||
|
||||
AiPlayer=player->Ai;
|
||||
//
|
||||
|
@ -347,11 +355,14 @@ global void AiEachSecond(Player* player)
|
|||
// Look if everything is fine.
|
||||
//
|
||||
AiCheckUnits();
|
||||
|
||||
//
|
||||
// Handle the resource manager.
|
||||
//
|
||||
AiResourceManager();
|
||||
//
|
||||
// Handle the force manager.
|
||||
//
|
||||
AiForceManager();
|
||||
}
|
||||
|
||||
//@}
|
||||
|
|
|
@ -27,8 +27,10 @@
|
|||
|
||||
#include "freecraft.h"
|
||||
|
||||
#include "unittype.h"
|
||||
#include "unit.h"
|
||||
#include "ai_local.h"
|
||||
#include "actions.h"
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
-- Variables
|
||||
|
@ -155,6 +157,89 @@ global void AiAssignToForce(Unit* unit)
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Attack at position with force.
|
||||
**
|
||||
** @param force Force number to attack with.
|
||||
** @param x X tile map position to be attacked.
|
||||
** @param y Y tile map position to be attacked.
|
||||
*/
|
||||
global void AiAttackWithForceAt(int force,int x,int y)
|
||||
{
|
||||
const AiUnit* aiunit;
|
||||
|
||||
AiPlayer->Force[force].Attacking=1;
|
||||
aiunit=AiPlayer->Force[force].Units;
|
||||
|
||||
//
|
||||
// Send all units in the force to enemy.
|
||||
//
|
||||
while( aiunit ) {
|
||||
CommandAttack(aiunit->Unit, x, y, NULL,FlushCommands);
|
||||
aiunit=aiunit->Next;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Attack opponent with force.
|
||||
**
|
||||
** @param force Force number to attack with.
|
||||
*/
|
||||
global void AiAttackWithForce(int force)
|
||||
{
|
||||
const AiUnit* aiunit;
|
||||
const Unit* enemy;
|
||||
|
||||
AiPlayer->Force[force].Attacking=1;
|
||||
aiunit=AiPlayer->Force[force].Units;
|
||||
|
||||
enemy = AttackUnitsInDistance(aiunit->Unit, 1000);
|
||||
if (!enemy) {
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Send all units in the force to enemy.
|
||||
//
|
||||
while( aiunit ) {
|
||||
CommandAttack(aiunit->Unit, enemy->X, enemy->Y, NULL,FlushCommands);
|
||||
aiunit=aiunit->Next;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Entry point of force manager, perodic called.
|
||||
*/
|
||||
global void AiForceManager(void)
|
||||
{
|
||||
int force;
|
||||
|
||||
//
|
||||
// Look if our defenders still have enemies in range.
|
||||
//
|
||||
for( force=0; force<AI_MAX_FORCES; ++force ) {
|
||||
if( AiPlayer->Force[force].Defending ) {
|
||||
const AiUnit* aiunit;
|
||||
|
||||
//
|
||||
// Look if still enemies in attack range.
|
||||
//
|
||||
aiunit=AiPlayer->Force[force].Units;
|
||||
while( aiunit ) {
|
||||
if( AttackUnitsInReactRange(aiunit->Unit) ) {
|
||||
break;
|
||||
}
|
||||
aiunit=aiunit->Next;
|
||||
}
|
||||
if( !aiunit ) { // No enemies go home.
|
||||
DebugLevel0Fn("FIXME: not written, should send force home\n");
|
||||
AiPlayer->Force[force].Defending=0;
|
||||
AiPlayer->Force[force].Attacking=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//@}
|
||||
|
||||
#endif // } NEW_AI
|
||||
|
|
|
@ -168,9 +168,10 @@ typedef struct _ai_force_ AiForce;
|
|||
*/
|
||||
struct _ai_force_ {
|
||||
int Completed; /// Flag saying force is complete build
|
||||
int Defending; /// Flag saying force is defending
|
||||
int Attacking; /// Flag saying force is attacking
|
||||
AiUnitType* UnitTypes; /// Count and types of unit-type
|
||||
AiUnit* Units; /// Units in the force.
|
||||
AiUnit* Units; /// Units in the force
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -204,7 +205,7 @@ typedef struct _player_ai_ {
|
|||
|
||||
// forces
|
||||
#define AI_MAX_FORCES 10 /// How many forces are supported
|
||||
AiForce Force[AI_MAX_FORCES]; /// Forces controlled by AI.
|
||||
AiForce Force[AI_MAX_FORCES]; /// Forces controlled by AI
|
||||
|
||||
// resource manager
|
||||
|
||||
|
@ -315,8 +316,14 @@ extern int AiFindBuildingPlace(const Unit*, const UnitType * , int *, int *);
|
|||
//
|
||||
// Forces
|
||||
//
|
||||
/// Assign a new unit to a force.
|
||||
/// Assign a new unit to a force
|
||||
extern void AiAssignToForce(Unit* unit);
|
||||
/// Attack with force at position
|
||||
extern void AiAttackWithForceAt(int force,int x,int y);
|
||||
/// Attack with force
|
||||
extern void AiAttackWithForce(int force);
|
||||
/// Periodic called force manager handler
|
||||
extern void AiForceManager(void);
|
||||
|
||||
//@}
|
||||
|
||||
|
|
|
@ -174,16 +174,16 @@ local void AiRequestFarms(void)
|
|||
return;
|
||||
}
|
||||
|
||||
DebugLevel0Fn("Must build: %s " _C_ type->Ident);
|
||||
DebugLevel3Fn("Must build: %s " _C_ type->Ident);
|
||||
//
|
||||
// Check if resources available.
|
||||
//
|
||||
if( (c=AiCheckUnitTypeCosts(type)) ) {
|
||||
DebugLevel0("- no resources\n");
|
||||
DebugLevel3("- no resources\n");
|
||||
AiPlayer->NeededMask|=c;
|
||||
return;
|
||||
} else {
|
||||
DebugLevel0("- enough resources\n");
|
||||
DebugLevel3("- enough resources\n");
|
||||
if( AiMakeUnit(type) ) {
|
||||
queue=malloc(sizeof(*AiPlayer->UnitTypeBuilded));
|
||||
queue->Next=AiPlayer->UnitTypeBuilded;
|
||||
|
@ -473,6 +473,25 @@ local int AiHarvest(Unit * unit)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
** Assign worker to haul oil.
|
||||
*/
|
||||
local int AiHaulOil(Unit * unit)
|
||||
{
|
||||
Unit *dest;
|
||||
|
||||
DebugLevel3Fn("%d\n", UnitNumber(unit));
|
||||
dest = FindOilPlatform(unit->Player, unit->X, unit->Y);
|
||||
if (!dest) {
|
||||
DebugLevel0Fn("oil platform not reachable\n");
|
||||
return 0;
|
||||
}
|
||||
DebugCheck(unit->Type!=UnitTypeHumanTanker && unit->Type!=UnitTypeOrcTanker);
|
||||
CommandHaulOil(unit, dest,FlushCommands);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
** Assign workers to collect resources.
|
||||
**
|
||||
|
@ -519,16 +538,22 @@ local void AiCollectResources(void)
|
|||
if (table[i]->Orders[0].Action != UnitActionBuild
|
||||
&& table[i]->OrderCount==1 ) {
|
||||
switch( c ) {
|
||||
case 1:
|
||||
case GoldCost:
|
||||
if (table[i]->Orders[0].Action != UnitActionMineGold ) {
|
||||
AiMineGold(table[i]);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
case WoodCost:
|
||||
if (table[i]->Orders[0].Action != UnitActionHarvest ) {
|
||||
AiHarvest(table[i]);
|
||||
}
|
||||
break;
|
||||
|
||||
case OilCost:
|
||||
if (table[i]->Orders[0].Action != UnitActionHaulOil ) {
|
||||
AiHaulOil(table[i]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
DebugCheck( 1 );
|
||||
}
|
||||
|
@ -566,12 +591,15 @@ local void AiCollectResources(void)
|
|||
continue;
|
||||
}
|
||||
switch( c ) {
|
||||
case 1:
|
||||
case GoldCost:
|
||||
AiMineGold(table[i]);
|
||||
break;
|
||||
case 2:
|
||||
case WoodCost:
|
||||
AiHarvest(table[i]);
|
||||
break;
|
||||
case OilCost:
|
||||
AiHaulOil(table[i]);
|
||||
break;
|
||||
default:
|
||||
DebugCheck( 1 );
|
||||
}
|
||||
|
|
|
@ -464,9 +464,11 @@ local SCM CclAiWait(SCM value)
|
|||
AiUnitTypeTable* autt;
|
||||
UnitType* type;
|
||||
|
||||
printf("Wait: ");
|
||||
gh_display(value);
|
||||
gh_newline();
|
||||
IfDebug(
|
||||
printf("Wait: ");
|
||||
gh_display(value);
|
||||
gh_newline();
|
||||
);
|
||||
|
||||
type=CclGetUnitType(value);
|
||||
if( !(autt=FindInRequests(type)) ) {
|
||||
|
@ -496,9 +498,11 @@ local SCM CclAiForce(SCM list)
|
|||
int count;
|
||||
int force;
|
||||
|
||||
printf("Force: ");
|
||||
gh_display(list);
|
||||
gh_newline();
|
||||
IfDebug(
|
||||
printf("Force: ");
|
||||
gh_display(list);
|
||||
gh_newline();
|
||||
);
|
||||
|
||||
force=gh_scm2int(gh_car(list));
|
||||
if( force<0 || force>=AI_MAX_FORCES ) {
|
||||
|
@ -551,9 +555,11 @@ local SCM CclAiWaitForce(SCM value)
|
|||
{
|
||||
int force;
|
||||
|
||||
printf("Wait-Force: ");
|
||||
gh_display(value);
|
||||
gh_newline();
|
||||
IfDebug(
|
||||
printf("Wait-Force: ");
|
||||
gh_display(value);
|
||||
gh_newline();
|
||||
);
|
||||
|
||||
force=gh_scm2int(value);
|
||||
if( force<0 || force>=AI_MAX_FORCES ) {
|
||||
|
@ -573,10 +579,20 @@ local SCM CclAiWaitForce(SCM value)
|
|||
*/
|
||||
local SCM CclAiAttackWithForce(SCM value)
|
||||
{
|
||||
printf("Attack: ");
|
||||
gh_display(value);
|
||||
gh_newline();
|
||||
// FIXME: more later!
|
||||
int force;
|
||||
|
||||
IfDebug(
|
||||
printf("Attack: ");
|
||||
gh_display(value);
|
||||
gh_newline();
|
||||
);
|
||||
|
||||
force=gh_scm2int(value);
|
||||
if( force<0 || force>=AI_MAX_FORCES ) {
|
||||
errl("Force out of range",value);
|
||||
}
|
||||
|
||||
AiAttackWithForce(force);
|
||||
|
||||
return SCM_BOOL_F;
|
||||
}
|
||||
|
@ -591,10 +607,12 @@ local SCM CclAiSleep(SCM value)
|
|||
static int fc;
|
||||
int i;
|
||||
|
||||
printf("Sleep: ");
|
||||
gh_display(value);
|
||||
printf(" %d %d",fc,FrameCounter);
|
||||
gh_newline();
|
||||
IfDebug(
|
||||
printf("Sleep: ");
|
||||
gh_display(value);
|
||||
printf(" %d %d",fc,FrameCounter);
|
||||
gh_newline();
|
||||
);
|
||||
|
||||
i=gh_scm2int(value);
|
||||
if( fc ) {
|
||||
|
@ -616,6 +634,14 @@ local SCM CclAiSleep(SCM value)
|
|||
*/
|
||||
local SCM CclAiResearch(SCM value)
|
||||
{
|
||||
IfDebug(
|
||||
printf("Research: ");
|
||||
gh_display(value);
|
||||
gh_newline();
|
||||
);
|
||||
|
||||
DebugLevel0Fn("FIXME:\n");
|
||||
|
||||
return SCM_BOOL_F;
|
||||
}
|
||||
|
||||
|
@ -626,6 +652,14 @@ local SCM CclAiResearch(SCM value)
|
|||
*/
|
||||
local SCM CclAiUpgradeTo(SCM value)
|
||||
{
|
||||
IfDebug(
|
||||
printf("Upgrade-to: ");
|
||||
gh_display(value);
|
||||
gh_newline();
|
||||
);
|
||||
|
||||
DebugLevel0Fn("FIXME:\n");
|
||||
|
||||
return SCM_BOOL_F;
|
||||
}
|
||||
|
||||
|
|
|
@ -549,7 +549,7 @@ global Unit* AttackUnitsInRange(const Unit* unit)
|
|||
IfDebug(
|
||||
|
||||
if( !unit->Type->CanAttack && !unit->Type->Tower ) {
|
||||
DebugLevel0("Should be handled by caller?\n");
|
||||
DebugLevel0Fn("Should be handled by caller?\n");
|
||||
abort();
|
||||
return NoUnitP;
|
||||
}
|
||||
|
@ -576,7 +576,7 @@ global Unit* AttackUnitsInReactRange(const Unit* unit)
|
|||
type=unit->Type;
|
||||
IfDebug(
|
||||
if( !type->CanAttack && !type->Tower ) {
|
||||
DebugLevel0("Should be handled by caller?\n");
|
||||
DebugLevel0Fn("Should be handled by caller?\n");
|
||||
abort();
|
||||
return NoUnitP;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue