_________ __ __
/ _____// |_____________ _/ |______ ____ __ __ ______
\_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
/ \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \
/_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
\/ \/ \//_____/ \/
______________________ ______________________
T H E W A R B E G I N S
Stratagus - A free fantasy real time strategy game engine
#include "network.h"#include "net_lowlevel.h"#include "stratagus.h"#include "ai.h"#include "ai_local.h"#include "interface.h"#include "pathfinder.h"#include "player.h"#include "script.h"#include "unit.h"#include "unit_manager.h"#include "unittype.h"#include "upgrade.h"Functions | |
script_ai.cpp - The AI ccl functions. | |
| static void | AiHelperInsert (std::vector< std::vector< CUnitType * > > &table, unsigned int n, CUnitType &base) |
| static std::vector< CUnitType * > | getUnitTypeFromString (const std::string &list) |
| static std::vector< CUnitType * > | getReparableUnits () |
| static std::vector< CUnitType * > | getSupplyUnits () |
| static std::vector< CUnitType * > | getRefineryUnits () |
| static void | InitAiHelper (AiHelper &aiHelper) |
| static int | CclDefineAiHelper (lua_State *l) |
| static CAiType * | GetAiTypesByName (const char *name) |
| static int | CclDefineAi (lua_State *l) |
| static void | InsertUnitTypeRequests (CUnitType *type, int count) |
| static AiRequestType * | FindInUnitTypeRequests (const CUnitType *type) |
| static int | FindInUpgradeToRequests (const CUnitType *type) |
| static void | InsertUpgradeToRequests (CUnitType *type) |
| static void | InsertResearchRequests (CUpgrade *upgrade) |
| static int | CclAiGetRace (lua_State *l) |
| static int | CclAiGetSleepCycles (lua_State *l) |
| static int | CclAiDebug (lua_State *l) |
| static int | CclAiDebugPlayer (lua_State *l) |
| static int | CclAiNeed (lua_State *l) |
| static int | CclAiSet (lua_State *l) |
| static int | CclAiWait (lua_State *l) |
| static int | CclAiPendingBuildCount (lua_State *l) |
| static int | CclAiForce (lua_State *l) |
| static int | CclAiForceRole (lua_State *l) |
| static int | CclAiReleaseForce (lua_State *l) |
| static int | CclAiCheckForce (lua_State *l) |
| static int | CclAiWaitForce (lua_State *l) |
| static int | CclAiAttackWithForce (lua_State *l) |
| static int | CclAiWaitForces (lua_State *l) |
| static int | CclAiAttackWithForces (lua_State *l) |
| static int | CclAiSleep (lua_State *l) |
| static int | CclAiResearch (lua_State *l) |
| static int | CclAiUpgradeTo (lua_State *l) |
| static int | CclAiPlayer (lua_State *l) |
| static int | CclAiSetReserve (lua_State *l) |
| static int | CclAiSetCollect (lua_State *l) |
| static int | CclAiSetBuildDepots (lua_State *l) |
| static int | CclAiDump (lua_State *l) |
| static void | CclParseBuildQueue (lua_State *l, PlayerAi *ai, int offset) |
| static int | CclDefineAiPlayer (lua_State *l) |
| static int | CclAiProcessorSetup (lua_State *l) |
| static CTCPSocket * | AiProcessorSendState (lua_State *l, char prefix) |
| static int | CclAiProcessorStep (lua_State *l) |
| static int | CclAiProcessorEnd (lua_State *l) |
| void | AiCclRegister () |
| Save the AI state. More... | |
| void AiCclRegister | ( | ) |
Save the AI state.
Register CCL features for unit-type.
|
static |
Insert new unit-type element.
| table | Table with elements. |
| n | Index to insert new into table |
| base | Base type to insert into table. |
|
static |
|
static |
Description
Attack with one single force.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, function() return AiNeed("unit-human-barracks") end, function() return AiWait("unit-human-barracks") end, function() return AiForce(1,{"unit-footman", 3}) end, function() return AiWaitForce(1) end, – Attack with Force 1 function() return AiAttackWithForce(1) end, function() return AiForce(2,{"unit-footman",5}) end, function() return AiWaitForce(2) end, – Attack with Force 2 function() return AiAttackWithForce(2) end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Attack with forces.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, function() return AiNeed("unit-human-barracks") end, function() return AiWait("unit-human-barracks") end, function() return AiNeed("unit-elven-lumber-mill") end, function() return AiWait("unit-elven-lumber-mill") end, function() return AiForce(1,{"unit-footman", 2}) end, function() return AiForce(2,{"unit-archer",1}) end, function() return AiForce(3,{"unit-archer",1,"unit-footman",3}) end, – Wait all three forces to be ready function() return AiWaitForces({1,2,3}) end, – Attack with all three forces function() return AiAttackWithForces({1,2,3}) end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Check if a force ready.
| l | Lua state. |
|
static |
Set debugging flag of AI script
| l | Lua state |
|
static |
Activate AI debugging for the given player(s) Player can be a number for a specific player "self" for current human player (ai me) "none" to disable
| l | Lua State |
|
static |
Description
Dump some AI debug information.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(3600) end, – Get the information from all the A.I players function() return AiDump() end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, function() return AiWait("unit-peasant") end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Define a force, a groups of units.
| l | Lua state. |
Example:
local simple_ai_loop = { – Sleep for 5 in game minutes function() return AiSleep(9000) end, – Repeat the instructions forever. function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, – Tell to the A.I that it needs a Town Hall. function() return AiNeed("unit-town-hall") end, – Wait until the Town Hall has been built. function() return AiWait("unit-town-hall") end, – Tell to the A.I that it needs 4 peasants function() return AiSet("unit-peasant",4) end, – Tell to the A.I that it needs a Barracks. function() return AiNeed("unit-human-barracks") end, – Tell to the A.I that it needs a Lumbermill. function() return AiNeed("unit-elven-lumber-mill") end, – Wait until the Barracks has been built. function() return AiWait("unit-human-barracks") end, – Wait until the Lumbermill has been built. function() return AiWait("unit-elven-lumber-mill") end, – Specify the force number 1 made only of 3 footmen function() return AiForce(1,{"unit-footman", 3}) end, – Specify the force number 2 made only of 2 archers function() return AiForce(2,{"unit-archer", 2}) end, – Specify the force number 3 made of 2 footmen and 1 archer function() return AiForce(3,{"unit-footman", 2, "unit-archer", 1}) end, – Wait for all three forces function() return AiWaitForce(1) end, function() return AiWaitForce(2) end, function() return AiWaitForce(3) end, – Execute the instructions in simple_ai_loop function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Define the role of a force.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, function() return AiNeed("unit-human-barracks") end, function() return AiNeed("unit-elven-lumber-mill") end, – Force 1 has the role of attacker function() return AiForceRole(1,"<u>attack</u>") end, – Force 2 has the role of defender function() return AiForceRole(2,"<u>defend</u>") end, function() return AiForce(1,{"unit-footman", 3}) end, function() return AiForce(2,{"unit-archer", 2}) end, function() return AiWaitForce(1) end, function() return AiWaitForce(2) end, – Execute the instructions in simple_ai_loop function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Get the race of the current AI player.
| l | Lua state. |
|
static |
Description
Get the number of cycles to sleep.
| l | Lua state |
Example:
local simple_ai_loop = { – Sleep for 5 in game minutes function() return AiSleep(9000) end, – Repeat the instructions forever. function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { – Get the number of cycles to sleep. function() return AiSleep(AiGetSleepCycles()) end, – Execute the instructions in simple_ai_loop function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Need a unit.
| l | Lua state. |
Example:
local simple_ai_loop = { – Sleep for 5 in game minutes function() return AiSleep(9000) end, – Repeat the instructions forever. function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, – Tell to the A.I that it needs a Town Hall. function() return AiNeed("unit-town-hall") end, – Execute the instructions in simple_ai_loop function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Get number of active build requests for a unit type.
| l | Lua State. |
|
static |
Return the player of the running AI.
| l | Lua state. |
|
static |
|
static |
AiProcessorSetup(host, port, number_of_state_variables, number_of_actions)
Connect to an AI agent running at host:port, that will consume number_of_state_variables every step and select one of number_of_actions.
|
static |
AiProcessorStep(handle, reward_since_last_call, table_of_state_variables)
|
static |
Release force.
| l | Lua state. |
|
static |
Description
Research an upgrade.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { – Get the number of cycles to sleep function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, function() return AiNeed("unit-human-barracks") end, function() return AiWait("unit-human-barracks") end, function() return AiNeed("unit-elven-lumber-mill") end, function() return AiWait("unit-elven-lumber-mill") end, function() return AiForce(1,{"unit-footman", 1, "unit-archer", 2}) end, function() return AiWaitForce(1) end, – Upgrade the archers arrow. function() return AiResearch("upgrade-arrow1") end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Set the number of units.
| l | Lua state |
Example:
local simple_ai_loop = { – Sleep for 5 in game minutes function() return AiSleep(9000) end, – Repeat the instructions forever. function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, – Tell to the A.I that it needs a Town Hall. function() return AiNeed("unit-town-hall") end, – Wait until the town-hall has been built. function() return AiWait("unit-town-hall") end, – Tell to the A.I that it needs 4 peasants function() return AiSet("unit-peasant",4) end, – Execute the instructions in simple_ai_loop function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Set AI player build.
| l | Lua state. |
|
static |
Description
Set AI player resource collect percent.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, – The peasants will focus only on gathering gold function() return AiSetCollect({0,100,0,0,0,0,0}) end, function() return AiSet("unit-peasant",4) end, function() return AiWait("unit-peasant") end, function() return AiSleep(1800) end, – The peasants will now focus 50% on gathering gold and 50% on gathering lumber function() return AiSetCollect({0,50,50,0,0,0,0}) end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Set AI player resource reserve.
| l | Lua state. |
|
static |
Description
Sleep for n cycles.
| l | Lua state. |
Example:
local simple_ai_loop = { – Sleep for 5 in game minutes function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { – Get the number of cycles to sleep function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, – Sleep for 1 in game minute function() return AiSleep(1800) end, function() return AiNeed("unit-human-blacksmith") end, function() return AiWait("unit-human-blacksmith") end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Upgrade an unit to an new unit-type.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, function() return AiNeed("unit-human-barracks") end, function() return AiWait("unit-human-barracks") end, function() return AiNeed("unit-elven-lumber-mill") end, function() return AiWait("unit-elven-lumber-mill") end, function() return AiSleep(1800) end, – Upgrade the Town Hall to Keep function() return AiUpgradeTo("unit-keep") end, function() return AiWait("unit-keep") end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Wait for a unit.
| l | Lua State. |
Example:
local simple_ai_loop = { – Sleep for 5 in game minutes function() return AiSleep(9000) end, – Repeat the instructions forever. function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, – Tell to the A.I that it needs a Town Hall. function() return AiNeed("unit-town-hall") end, – Wait until the Town Hall has been built. function() return AiWait("unit-town-hall") end, – Execute the instructions in simple_ai_loop function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Wait for a force ready.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, function() return AiNeed("unit-human-barracks") end, function() return AiWait("unit-human-barracks") end, function() return AiForce(1,{"unit-footman", 3}) end, – Wait until force 1 is completed function() return AiWaitForce(1) end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Wait for a forces ready.
| l | Lua state. |
Example:
local simple_ai_loop = { function() return AiSleep(9000) end, function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } local simple_ai = { function() return AiSleep(AiGetSleepCycles()) end, function() return AiNeed("unit-town-hall") end, function() return AiWait("unit-town-hall") end, function() return AiSet("unit-peasant",4) end, function() return AiNeed("unit-human-barracks") end, function() return AiWait("unit-human-barracks") end, function() return AiNeed("unit-elven-lumber-mill") end, function() return AiWait("unit-elven-lumber-mill") end, function() return AiForce(1,{"unit-footman", 2}) end, function() return AiForce(2,{"unit-archer",1}) end, function() return AiForce(3,{"unit-archer",1,"unit-footman",3}) end, – Wait all three forces to be ready function() return AiWaitForces({1,2,3}) end, function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Description
Define an AI engine.
| l | Lua state. |
Example:
– Those instructions will be executed forever local simple_ai_loop = { – Sleep for 5 in game minutes function() return AiSleep(9000) end, – Repeat the functions from start. function() stratagus.gameData.AIState.loop_index[1 + AiPlayer()] = 0; return false; end, } – The initial instructions the A.I has to execute local simple_ai = { function() return AiSleep(1800) end, – Sleep for 1 in game minute function() return AiNeed("unit-town-hall") end, – One Town Hall is needed function() return AiWait("unit-town-hall") end, – Wait until the Town Hall is completed function() return AiSet("unit-peasant", 4) end, – Make 4 peasants – Basic buildings function() return AiSet("unit-farm", 4) end, – Make 4 farms function() return AiWait("unit-farm") end, – Wait until all 4 farms are build. function() return AiNeed("unit-human-barracks") end, – Need a Barracks function() return AiWait("unit-human-barracks") end, – Wait until the Barracks has been built – Defense force for the base function() return AiForce(1, {"unit-footman", 3}) end, – Make a force of 3 footmen function() return AiWaitForce(1) end, – Wait until the 3 footmen are trained function() return AiForceRole(1,"defend") end, – Make this force as a defense force – Execute the instructions in simple_ai_loop function() return AiLoop(simple_ai_loop, stratagus.gameData.AIState.loop_index) end, } – function that calls the instructions in simple_ai inside DefineAi function custom_ai() return AiLoop(simple_ai,stratagus.gameData.AIState.index) end – Make an A.I for the human race that calls the function custom_ai DefineAi("example_ai","human","class_ai",custom_ai)
|
static |
Define helper for AI.
| l | Lua state. |
|
static |
Define an AI player.
| l | Lua state. |
Parse AiBuildQueue builing list
| l | Lua state. |
| ai | PlayerAi pointer which should be filled with the data. |
|
static |
Find unit-type in request table.
| type | Unit-type to be found. |
|
static |
Find unit-type in upgrade-to table.
| type | Unit-type to be found. |
|
static |
|
static |
Get sorted list of unittype with CanHarvest not null.
|
static |
Get list of unittype which can be repared.
|
static |
Get sorted list of unittype with Supply not null.
|
static |
Transform list of unit separed with coma to a true list.
Append unit-type to request table.
| upgrade | Upgrade to be appended. |
Append unit-type to request table.
| type | Unit-type to be appended. |
| count | How many unit-types to build. |
1.8.17
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.