[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[bos-dev] Ai patch



Hi,

what do you think about the attached patch? This might need some polishing, but I would like to get feedback on the general idea how this is written.

What it should do: make addition of AIs very easy (just write a new file and place it into the scripts/ais/ directory).

Gorm
Index: ai.lua
===================================================================
--- ai.lua	(revision 637)
+++ ai.lua	(working copy)
@@ -28,6 +28,21 @@
 --	$Id$
 --
 
+-- The list of registered AIs in BOS
+-- Every AI has an entry name: {name, fun, initfun}
+-- See at RegisterAi() for a description what these are
+AiList = {}
+
+-- Function to register an AI to BOS
+-- Parameters:
+-- name    : Name of the AI without the leading "ai-"
+-- fun     : main AI function
+-- initfun : initialization function, can be obmitted
+function RegisterAi(name, fun, initfun)
+  DefineAi("ai-" .. name, "*", "ai-" .. name, fun)
+  AiList[name] = {name, fun, initfun}
+end
+
 DefineAiHelper(
   --
   -- Unit can build which buildings.
@@ -66,75 +81,39 @@
 local player
 
 function AiLoop(loop_funcs, loop_pos)
-    local ret
+  local ret
 
-    player = AiPlayer() + 1
-    while (true) do
-    	ret = loop_funcs[loop_pos[player]]()
-	if (ret) then
-	    break
-	end
-	loop_pos[player] = loop_pos[player] + 1
+  player = AiPlayer() + 1
+  while (true) do
+  	ret = loop_funcs[loop_pos[player]]()
+    if (ret) then
+     break
     end
-    return true
+    loop_pos[player] = loop_pos[player] + 1
+  end
+  return true
 end
 
+-- Execute all AI init scripts
 function InitAiScripts()
-  ai_pos = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
-  ai_loop_pos = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
+  for key,value in next,AiList do
+    -- check if this AI actually has an init script
+    if (value[3] ~= nil) then
+      value[3]()
+    end
+  end
 end
 
-local ai_loop_funcs = {
-  function() print("Looping !"); return false end,
-  function() return AiForce(1, {"unit-assault", 20, 
-                                "unit-grenadier", 8, 
-                                "unit-bazoo", 8}) end,
-  function() return AiWaitForce(1) end,  -- wait until attack party is completed
-  function() return AiSleep(50*GameSettings.Difficulty) end,
-  function() return AiAttackWithForce(1) end,
-  function() ai_loop_pos[player] = 0; return false end,
-}
+-- Find and load all Ais
+local list
+local i
+local f
+list = ListFilesInDirectory("scripts/ais/")
 
-local ai_funcs = {
-  function() AiDebug(false) return false end,
-  function() return AiSleep(AiGetSleepCycles()) end,
-  function() return AiNeed("unit-vault") end,
-  function() return AiSet("unit-engineer", 10) end,
-  function() return AiWait("unit-vault") end,
-
-  function() return AiNeed("unit-camp") end,
-  function() return AiWait("unit-camp") end,
-  function() return AiForce(0, {"unit-assault", 10}) end,
-  function() return AiWaitForce(0) end, 
-  function() return AiNeed("unit-camp") end,
-  function() return AiSleep(125*GameSettings.Difficulty) end,
-  function() return AiNeed("unit-camp") end,
-  
-  function() return AiForce(1, {"unit-assault", 10}) end,
-  function() return AiWaitForce(1) end,
-  function() return AiSleep(50*GameSettings.Difficulty) end, 
-  function() return AiAttackWithForce(1) end,
-
-  function() return AiForce(0, {"unit-assault", 20}) end,
-  function() return AiNeed("unit-rfac") end,
-  function() return AiResearch("upgrade-expl") end,
-  function() return AiForce(1, {"unit-assault", 20, "unit-grenadier", 8}) end,
-  function() return AiWaitForce(1) end, 
-  function() return AiAttackWithForce(1) end,
-
-  function() return AiResearch("upgrade-expl2") end,
-  function() return AiLoop(ai_loop_funcs, ai_loop_pos) end,
-}
-
-function AiRush()
---    print(AiPlayer() .. " position ".. ai_pos[AiPlayer() + 1]);
-    return AiLoop(ai_funcs, ai_pos)
+for i,f in list do
+  if(string.find(f, "^.*%.lua$")) then
+    print("Loading AI: " .. f)
+    Load("scripts/ais/" .. f)
+  end
 end
 
-DefineAi("ai-rush", "*", "ai-rush", AiRush)
-
-function AiPassive()
-end
-
-DefineAi("ai-passive", "*", "ai-passive", AiPassive)
-
Index: ais/rush.lua
===================================================================
--- ais/rush.lua	(revision 0)
+++ ais/rush.lua	(revision 0)
@@ -0,0 +1,85 @@
+--            ____            
+--           / __ )____  _____
+--          / __  / __ \/ ___/
+--         / /_/ / /_/ (__  ) 
+--        /_____/\____/____/  
+--
+--  Invasion - Battle of Survival                  
+--   A GPL'd futuristic RTS game
+--
+--	ai.lua		-	Define the AI.
+--
+--	(c) Copyright 2000-2006 by Lutz Sammer
+--
+--      This program is free software; you can redistribute it and/or modify
+--      it under the terms of the GNU General Public License as published by
+--      the Free Software Foundation; either version 2 of the License, or
+--      (at your option) any later version.
+--  
+--      This program is distributed in the hope that it will be useful,
+--      but WITHOUT ANY WARRANTY; without even the implied warranty of
+--      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--      GNU General Public License for more details.
+--  
+--      You should have received a copy of the GNU General Public License
+--      along with this program; if not, write to the Free Software
+--      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+--
+--	$Id: rush.lua 626 2006-11-25 18:10:03Z feb $
+--
+
+function InitAiScripts_rush()
+  print("Called rush init method\n");
+  ai_rush_pos = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
+  ai_rush_loop_pos = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
+end
+
+local ai_rush_loop_funcs = {
+  function() print("Looping !"); return false end,
+  function() return AiForce(1, {"unit-assault", 20, 
+                                "unit-grenadier", 8, 
+                                "unit-bazoo", 8}) end,
+  function() return AiWaitForce(1) end,  -- wait until attack party is completed
+  function() return AiSleep(50*GameSettings.Difficulty) end,
+  function() return AiAttackWithForce(1) end,
+  function() ai_rush_loop_pos[player] = 0; return false end,
+}
+
+local ai_rush_funcs = {
+  function() AiDebug(false) return false end,
+  function() return AiSleep(AiGetSleepCycles()) end,
+  function() return AiNeed("unit-vault") end,
+  function() return AiSet("unit-engineer", 10) end,
+  function() return AiWait("unit-vault") end,
+
+  function() return AiNeed("unit-camp") end,
+  function() return AiWait("unit-camp") end,
+  function() return AiForce(0, {"unit-assault", 10}) end,
+  function() return AiWaitForce(0) end, 
+  function() return AiNeed("unit-camp") end,
+  function() return AiSleep(125*GameSettings.Difficulty) end,
+  function() return AiNeed("unit-camp") end,
+  
+  function() return AiForce(1, {"unit-assault", 10}) end,
+  function() return AiWaitForce(1) end,
+  function() return AiSleep(50*GameSettings.Difficulty) end, 
+  function() return AiAttackWithForce(1) end,
+
+  function() return AiForce(0, {"unit-assault", 20}) end,
+  function() return AiNeed("unit-rfac") end,
+  function() return AiResearch("upgrade-expl") end,
+  function() return AiForce(1, {"unit-assault", 20, "unit-grenadier", 8}) end,
+  function() return AiWaitForce(1) end, 
+  function() return AiAttackWithForce(1) end,
+
+  function() return AiResearch("upgrade-expl2") end,
+  function() return AiLoop(ai_rush_loop_funcs, ai_loop_pos) end,
+}
+
+function AiRush()
+--    print(AiPlayer() .. " position ".. ai_rush_pos[AiPlayer() + 1]);
+    return AiLoop(ai_rush_funcs, ai_rush_pos)
+end
+
+RegisterAi("rush", AiRush, InitAiScripts_rush)
+
Index: ais/passive.lua
===================================================================
--- ais/passive.lua	(revision 0)
+++ ais/passive.lua	(revision 0)
@@ -0,0 +1,35 @@
+--            ____            
+--           / __ )____  _____
+--          / __  / __ \/ ___/
+--         / /_/ / /_/ (__  ) 
+--        /_____/\____/____/  
+--
+--  Invasion - Battle of Survival                  
+--   A GPL'd futuristic RTS game
+--
+--	ai.lua		-	Define the AI.
+--
+--	(c) Copyright 2000-2006 by Lutz Sammer
+--
+--      This program is free software; you can redistribute it and/or modify
+--      it under the terms of the GNU General Public License as published by
+--      the Free Software Foundation; either version 2 of the License, or
+--      (at your option) any later version.
+--  
+--      This program is distributed in the hope that it will be useful,
+--      but WITHOUT ANY WARRANTY; without even the implied warranty of
+--      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--      GNU General Public License for more details.
+--  
+--      You should have received a copy of the GNU General Public License
+--      along with this program; if not, write to the Free Software
+--      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+--
+--	$Id: ai.lua 626 2006-11-25 18:10:03Z feb $
+--
+
+function AiPassive()
+end
+
+RegisterAi("passive", AiPassive)
+