____                _       __
    / __ )____  _____   | |     / /___ ___________
   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  )
 /_____/\____/____/     |__/|__/\__,_/_/  /____/

       A futuristic real-time strategy game.
          This file is part of Bos Wars.
(C) Copyright 2001-2007 by the Bos Wars and Stratagus Project. Distributed under the "GNU General Public License"

ai.cpp File Reference

The computer player AI main file. More...

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "stratagus.h"
#include "player.h"
#include "unit.h"
#include "unittype.h"
#include "upgrade.h"
#include "script.h"
#include "actions.h"
#include "map.h"
#include "pathfinder.h"
#include "ai_local.h"
#include "iolib.h"

Go to the source code of this file.

Functions

static void AiExecuteScript (void)
static void AiCheckUnits (void)
static void SaveAiPlayer (CFile *file, int plynr, PlayerAi *ai)
static void SaveAiPlayers (CFile *file)
void SaveAi (CFile *file)
 Save the AI state.
void AiInit (CPlayer *player)
 Init AI for this player.
void InitAiModule (void)
 Init AI global structures.
void CleanAi (void)
 Cleanup the AI module.
void FreeAi ()
 Free the AI resources.
static int AiRemoveFromBuilt2 (PlayerAi *pai, const CUnitType *type)
static void AiRemoveFromBuilt (PlayerAi *pai, const CUnitType *type)
static int AiReduceMadeInBuilt2 (PlayerAi *pai, const CUnitType *type)
static void AiReduceMadeInBuilt (PlayerAi *pai, const CUnitType *type)
void AiHelpMe (const CUnit *attacker, CUnit *defender)
 < Called if AI unit is attacked
void AiUnitKilled (CUnit *unit)
 Called if AI needs more farms.
void AiWorkComplete (CUnit *unit, CUnit *what)
 Called if AI unit can't build.
void AiCanNotBuild (CUnit *unit, const CUnitType *what)
 Called if AI unit can't reach building place.
void AiCanNotReach (CUnit *unit, const CUnitType *what)
 Called if an AI unit can't move.
static void AiMoveUnitInTheWay (CUnit *unit)
void AiCanNotMove (CUnit *unit)
 Called if AI unit has completed training.
void AiNeedMoreSupply (const CUnit *unit, const CUnitType *what)
 Called if AI unit has completed work.
void AiTrainingComplete (CUnit *unit, CUnit *what)
void AiEachCycle (CPlayer *player)
 Called each game cycle.
void AiEachSecond (CPlayer *player)
 Called each second.

Variables

int AiSleepCycles
 Ai sleeps # cycles.
std::vector< CAiType * > AiTypes
 List of all AI types.
AiHelper AiHelpers
 AI helper variables.
PlayerAiAiPlayer
 Current AI player.


Detailed Description

The computer player AI main file.

Definition in file ai.cpp.


Function Documentation

void AiCanNotBuild ( CUnit unit,
const CUnitType what 
)

Called if AI unit can't reach building place.

Called if building can't be build.

Parameters:
unit Pointer to unit what builds the building.
what Pointer to unit-type.

Definition at line 769 of file ai.cpp.

References _C_, CPlayer::Ai, AiReduceMadeInBuilt(), Assert, DebugPrint, CUnitType::Ident, CPlayer::Index, CUnit::Player, PlayerPerson, CPlayer::Type, CUnit::Type, UnitNumber, CUnit::X, and CUnit::Y.

Referenced by CheckCanBuild(), and StartBuilding().

void AiCanNotMove ( CUnit unit  ) 

Called if AI unit has completed training.

Called if a unit can't move. Try to move unit in the way

Parameters:
unit Pointer to unit what builds the building.

Definition at line 924 of file ai.cpp.

References CPlayer::Ai, AiMoveUnitInTheWay(), CUnit::Orders, PlaceReachable(), CUnit::Player, CUnit::Type, CUnitType::UnitType, and UnitTypeFly.

Referenced by DoActionMove().

void AiCanNotReach ( CUnit unit,
const CUnitType what 
)

Called if an AI unit can't move.

Called if building place can't be reached.

Parameters:
unit Pointer to unit what builds the building.
what Pointer to unit-type.

Definition at line 785 of file ai.cpp.

References CPlayer::Ai, AiReduceMadeInBuilt(), Assert, CUnit::Player, PlayerPerson, and CPlayer::Type.

Referenced by MoveToLocation().

static void AiCheckUnits ( void   )  [static]

void AiEachCycle ( CPlayer player  ) 

Called each game cycle.

This is called for each player, each game cycle.

Parameters:
player The player structure pointer.

Definition at line 996 of file ai.cpp.

References CPlayer::Ai, COrder::Goal, int(), AiTransportRequest::Order, CUnit::RefsDecrease(), PlayerAi::TransportRequests, and AiTransportRequest::Unit.

Referenced by PlayersEachCycle().

void AiEachSecond ( CPlayer player  ) 

Called each second.

This is called for each player each second.

Parameters:
player The player structure pointer.

Definition at line 1015 of file ai.cpp.

References CPlayer::Ai, AiCheckMagic(), AiCheckUnits(), AiExecuteScript(), AiForceManager(), AiResourceManager(), AiSendExplorers(), CYCLES_PER_SECOND, GameCycle, and PlayerAi::LastExplorationGameCycle.

Referenced by PlayersEachSecond().

static void AiExecuteScript ( void   )  [static]

Execute the AI Script.

Definition at line 166 of file ai.cpp.

References Lua, LuaCall(), and PlayerAi::Script.

Referenced by AiEachSecond().

void AiHelpMe ( const CUnit attacker,
CUnit defender 
)

< Called if AI unit is attacked

Called if a Unit is Attacked

Parameters:
attacker Pointer to attacker unit.
defender Pointer to unit that is being attacked.

Definition at line 646 of file ai.cpp.

References _C_, CPlayer::Ai, AI_MAX_ATTACKING_FORCES, AiAttackWithForceAt(), AiForce::Attacking, CUnitType::CanAttack, DebugPrint, AiForce::Defending, PlayerAi::Force, CUnitType::Ident, CPlayer::Index, int(), CUnit::Player, CUnit::Type, UnitNumber, AiForce::Units, CUnitType::UnitType, UnitTypeFly, CUnit::X, and CUnit::Y.

Referenced by HitUnit().

void AiInit ( CPlayer player  ) 

Init AI for this player.

Setup all at start.

Parameters:
player The player structure pointer.

Definition at line 423 of file ai.cpp.

References _C_, CPlayer::Ai, CPlayer::AiName, PlayerAi::AiType, AiTypes, CAiType::Class, DebugPrint, Exit(), CPlayer::Index, int(), PlayerAi::Player, CAiType::Script, and PlayerAi::Script.

Referenced by HandleCheats(), and PlayersInitAi().

static void AiMoveUnitInTheWay ( CUnit unit  )  [static]

void AiNeedMoreSupply ( const CUnit unit,
const CUnitType what 
)

Called if AI unit has completed work.

Called if the AI needs more farms.

Parameters:
unit Point to unit.
what Pointer to unit-type.

Definition at line 964 of file ai.cpp.

References CPlayer::Ai, Assert, PlayerAi::NeedSupply, CUnit::Player, PlayerPerson, and CPlayer::Type.

Referenced by HandleActionTrain().

static void AiReduceMadeInBuilt ( PlayerAi pai,
const CUnitType type 
) [static]

Reduce made unit-type from build list.

Parameters:
pai Computer AI player.
type Unit-type which is now available.

Definition at line 614 of file ai.cpp.

References AiFindUnitTypeEquiv(), AiReduceMadeInBuilt2(), Assert, AiHelper::Equiv, int(), CUnitType::Slot, UnitTypeMax, and UnitTypes.

Referenced by AiCanNotBuild(), AiCanNotReach(), and AiUnitKilled().

static int AiReduceMadeInBuilt2 ( PlayerAi pai,
const CUnitType type 
) [static]

Reduce made unit-type from build list.

Parameters:
pai Computer AI player.
type Unit-type which is now available.
Returns:
True if the unit-type could be reduced.

Definition at line 595 of file ai.cpp.

References PlayerAi::UnitTypeBuilt.

Referenced by AiReduceMadeInBuilt().

static void AiRemoveFromBuilt ( PlayerAi pai,
const CUnitType type 
) [static]

Remove unit-type from build list.

Parameters:
pai Computer AI player.
type Unit-type which is now available.

Definition at line 560 of file ai.cpp.

References AiFindUnitTypeEquiv(), AiRemoveFromBuilt2(), Assert, DebugPrint, PlayerAi::Player, ThisPlayer, UnitTypeMax, and UnitTypes.

Referenced by AiTrainingComplete(), and AiWorkComplete().

static int AiRemoveFromBuilt2 ( PlayerAi pai,
const CUnitType type 
) [static]

Remove unit-type from build list.

Parameters:
pai Computer AI player.
type Unit-type which is now available.
Returns:
True, if unit-type was found in list.

Definition at line 537 of file ai.cpp.

References Assert, and PlayerAi::UnitTypeBuilt.

Referenced by AiRemoveFromBuilt().

void AiTrainingComplete ( CUnit unit,
CUnit what 
)

Called if training of a unit is completed.

Parameters:
unit Pointer to unit making.
what Pointer to new ready trained unit.

Definition at line 976 of file ai.cpp.

References _C_, CPlayer::Ai, AiAssignToForce(), AiCleanForces(), AiRemoveFromBuilt(), Assert, DebugPrint, CUnitType::Ident, CPlayer::Index, CUnit::Player, PlayerPerson, CPlayer::Type, CUnit::Type, UnitNumber, CUnit::X, and CUnit::Y.

Referenced by HandleActionTrain().

void AiUnitKilled ( CUnit unit  ) 

Called if AI needs more farms.

Called if a unit is killed.

Parameters:
unit Pointer to unit.

Definition at line 708 of file ai.cpp.

References _C_, CPlayer::Ai, AiReduceMadeInBuilt(), Assert, DebugPrint, CUnitType::Ident, CPlayer::Index, CUnit::Orders, CUnit::Player, PlayerPerson, CPlayer::Type, CUnit::Type, UnitActionAttack, UnitActionBuild, UnitActionBuilt, UnitActionMove, UnitActionStill, and UnitNumber.

Referenced by UnitLost().

void AiWorkComplete ( CUnit unit,
CUnit what 
)

Called if AI unit can't build.

Called if work complete (Buildings).

Parameters:
unit Pointer to unit that builds the building.
what Pointer to unit building that was built.

Definition at line 748 of file ai.cpp.

References _C_, CPlayer::Ai, AiRemoveFromBuilt(), Assert, DebugPrint, CUnitType::Ident, CPlayer::Index, CUnit::Player, PlayerPerson, CPlayer::Type, CUnit::Type, UnitNumber, CUnit::X, and CUnit::Y.

Referenced by HandleActionBuilt().

void CleanAi ( void   ) 

Cleanup the AI module.

Cleanup the AI in order to enable to restart a game.

Definition at line 486 of file ai.cpp.

References CPlayer::Ai, PlayerMax, and Players.

Referenced by CleanGame(), and FreeAi().

void FreeAi ( void   ) 

Free the AI resources.

Free all AI resources.

Definition at line 500 of file ai.cpp.

References AiResetUnitTypeEquiv(), AiTypes, AiHelper::Build, CleanAi(), AiHelper::Equiv, int(), AiHelper::Repair, AiHelper::Train, and AiHelper::UnitLimit.

Referenced by CleanModules().

void InitAiModule ( void   ) 

Init AI global structures.

Initialize global structures of the AI

Definition at line 477 of file ai.cpp.

References AiResetUnitTypeEquiv().

Referenced by InitModules(), and main().

void SaveAi ( CFile file  ) 

Save the AI state.

Save state of AI to file.

Parameters:
file Output file.

Definition at line 409 of file ai.cpp.

References DebugPrint, CFile::printf(), and SaveAiPlayers().

Referenced by SaveGame().

static void SaveAiPlayer ( CFile file,
int  plynr,
PlayerAi ai 
) [static]

static void SaveAiPlayers ( CFile file  )  [static]

Save state of player AIs.

Parameters:
file Output file.

Definition at line 395 of file ai.cpp.

References PlayerMax, Players, and SaveAiPlayer().

Referenced by SaveAi().


Variable Documentation

AI helper variables.

Definition at line 155 of file ai.cpp.

Referenced by AiCountUnitBuilders(), AiMakeUnit(), AiRepairUnit(), CclAiWait(), and CclDefineAiHelper().

Ai sleeps # cycles.

Definition at line 152 of file ai.cpp.

Referenced by CclAiGetSleepCycles(), and main().

std::vector<CAiType *> AiTypes

List of all AI types.

Definition at line 154 of file ai.cpp.

Referenced by AiInit(), CclDefineAi(), CclDefineAiPlayer(), and FreeAi().


Generated on Sat Feb 21 00:28:15 2009 for Bos Wars by  doxygen 1.5.6