Advertisement
Guest User

Spring Tank AI (tp_aibork.lua)

a guest
Aug 24th, 2014
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.92 KB | None | 0 0
  1. function gadget:GetInfo()
  2.     return {
  3.         name    = "Borked Advancer",
  4.         desc    = "forever charging onwards",
  5.         author  = "knorke",
  6.         date    = "Jan 2011",
  7.         license = "goodbye hooorses",
  8.         layer   = 82, --82 fo life?!
  9.         version = "v1",
  10.         enabled = true
  11.     }
  12. end
  13.  
  14. if (gadgetHandler:IsSyncedCode()) then
  15. ------SYNCED--------
  16. myTeam = {}
  17. unitOnMission = {} --[unitID] = wieviele frames in ruhe gelassen werden
  18. function gadget:Initialize()
  19.     counter = 0
  20.     -- Initialise AI for all teams that are set to use it
  21.     for _,t in ipairs(Spring.GetTeamList()) do
  22.         local _,_,_,ai,side = Spring.GetTeamInfo(t)
  23.         if ai and Spring.GetTeamLuaAI(t) == gadget:GetInfo().name then
  24.             --log(t, " assigned to "..gadget:GetInfo().name)
  25.             local pos = {}
  26.             local home_x,home_y,home_z = Spring.GetTeamStartPosition(t)
  27.             myTeam[t] = true
  28.             Spring.Echo ("AI playing for team " .. t .." GetTeamLuaAI: " ..  Spring.GetTeamLuaAI(t))
  29.         else
  30.             myTeam[t]=false
  31.         end
  32.     end
  33.     if (myTeam == nil) then
  34.         Spring.Echo ("Borked Advancer: not used, bye bye.")
  35.         gadgetHandler:RemoveGadget()
  36.     end
  37. end
  38.  
  39.  
  40. function gadget:GameFrame(frame)
  41.     for i in pairs(unitOnMission) do
  42.         unitOnMission[i] = unitOnMission[i] -1
  43.     if (unitOnMission[i] < 0) then unitOnMission[i] = 0 end
  44.     end
  45.     if (frame % 30 ==0) then
  46.         counter=counter+1      
  47.     end
  48.    
  49.     --if ((frame-1) % 30 ==0) then
  50. --      Spring.Echo ("counter=" .. counter)
  51. --  end
  52.    
  53.     if (frame % 60 ~=0) then return end
  54.     if (myTeam == nil) then return end
  55.     local all_units = Spring.GetAllUnits ()--  Spring.GetTeamUnits (mit nem loop)
  56.     --Spring.Echo ("denke")
  57.     if (all_units == nil) then return end
  58.     for i,unit in pairs(all_units) do      
  59.         local uteam = Spring.GetUnitTeam (unit)
  60.         if (myTeam[uteam]==true) then
  61.             local udID =Spring.GetUnitDefID(unit)
  62.             if (udID) then
  63.                 if (wantNewOrders(unit)) then
  64.                     local flagsc = Spring.GetUnitIsTransporting (unit)
  65.                     local flagscarried=0
  66.                     if (flagsc) then flagscarried=#flagsc end
  67.                     local unitDef = UnitDefs [udID]
  68.                     if (unitDef.name == "tplegocar" or unitDef.name == "tptaktak" or flagscarried>0) then
  69.                         do_flagrun (unit)
  70.                     else
  71.                         do_flagrun (unit)
  72.                         unitOnMission[unit]=100
  73.                     end
  74.                 end
  75.             end
  76.         end
  77.     end
  78.            
  79. end
  80.  
  81. function wantNewOrders (unitID)
  82.     if (unitOnMission[unitID]) then
  83.         if (unitOnMission[unitID] >0) then return false end
  84.     end
  85.     return true
  86. end
  87.  
  88. function do_flagrun (unitID)
  89.     local flagscarried = Spring.GetUnitIsTransporting (unitID)
  90.     if ((not flagscarried) or #flagscarried==0) then
  91.         nearflagID, tx, ty,tz,flagdistance = nearest_flag_from_unit (unitID)
  92.         friendID, frx,fry,frz, frienddistance = nearest_friend_from_unit (unitID)
  93.         --Spring.Echo (friendID .. " is nearest to " .. unitID)
  94.         --if (friendID) then frienddistance = Spring.GetUnitSeparation (unitID, friendID) or 999999999999 end
  95.         --if (friendID==nil) then Spring.Echo (unitID .. " got no friend") end
  96.         if (friendID and frienddistance > 400 and (flagdistance or 0) > 700 ) then
  97.             --tx,ty,tz = Spring.GetUnitPosition(friendID)
  98.             tx = frx
  99.             ty = fry
  100.             tz = frz
  101.             --Spring.Echo ("my friend is away " .. frienddistance)
  102.             --Spring.GiveOrderToUnit(unitID, CMD.MOVE , {tx, ty, tz  }, {})
  103.             Spring.GiveOrderToUnit(unitID, CMD.GUARD  , {friendID}, {})
  104.             return
  105.         else
  106.             if (nearflagID) then tx,ty,tz = Spring.GetUnitPosition(nearflagID) end
  107.         end        
  108.         if (tx and ty and tz) then
  109.             Spring.GiveOrderToUnit(unitID, CMD.MOVE , {tx, ty, tz  }, {})
  110.         end    
  111.     else    --carrying flag? run home!
  112.         local flagid, tx, ty,tz,d = nearest_homebase_from_unit (unitID)
  113.         if (tx and ty and tz) then
  114.             Spring.GiveOrderToUnit(unitID, CMD.MOVE , {tx, ty, tz  }, {})
  115.         end
  116.     end
  117. end
  118.  
  119. function do_attacknearest (unitID)
  120.     local enemy = Spring.GetUnitNearestEnemy (unitID)
  121.     if (enemy) then
  122.         Spring.GiveOrderToUnit(unitID, CMD.ATTACK  , { enemy  }, {})
  123.     end
  124. end
  125.  
  126. --nearest enemy flag actually
  127. function nearest_flag_from_unit (uID)
  128.     local nearest_flagID = nil
  129.     local nearest_flag_distance = 9999999999   
  130.     local x,y,z = Spring.GetUnitPosition(uID)  
  131.     --local uallyteam = Spring.GetUnitAllyTeam (uID)
  132.     flag=Spring.GetUnitsInCylinder (x,z, 9000)
  133.     if (flag == nil) then return nil end    --no near units at all :/
  134.     for i in pairs (flag) do
  135.         if (not units_allied (flag[i] , uID)) then
  136.             local udID =Spring.GetUnitDefID(flag[i])
  137.             if (udID) then
  138.                 local unitDef = UnitDefs [udID]
  139.                 if (unitDef.name=="tpflag") then
  140.                     local d = Spring.GetUnitSeparation (uID, flag[i])
  141.                     if (d < nearest_flag_distance) then
  142.                         nearest_flag_distance = d
  143.                         nearest_flagID = flag[i]
  144.                     end
  145.                 end
  146.             end
  147.         end
  148.     end
  149.     if (nearest_flagID~=nil) then      
  150.         local rx,ry,rz=Spring.GetUnitPosition(nearest_flagID)
  151.         return nearest_flagID, rx,ry,rz, nearest_flag_distance
  152.         else return nil
  153.     end
  154. end
  155. --***!!! combinieren zu get_nearest_unit_from_unit (uID, unitname, friend/enemy)
  156. --!!! wtf? das returned doch doch nicht die homebase sondern sucht ja schon das ziel für die ai!  <- HÄ?
  157. function nearest_homebase_from_unit (uID)
  158.     local nearest_flagID = nil
  159.     local nearest_flag_distance = 9999999999   
  160.     local x,y,z = Spring.GetUnitPosition(uID)  
  161.     local uteam = Spring.GetUnitTeam (uID)
  162.     flag=Spring.GetUnitsInCylinder (x,z, 9000, uteam)
  163.     if (flag == nil) then return nil end    --no near units at all :/
  164.     for i in pairs (flag) do
  165.         if (Spring.GetUnitTeam(flag[i]) == uteam) then
  166.             local udID =Spring.GetUnitDefID(flag[i])
  167.             if (udID) then
  168.                 local unitDef = UnitDefs [udID]
  169.                 if (unitDef.name=="tpbase") then
  170.                     local d = Spring.GetUnitSeparation (uID, flag[i])
  171.                     if (d < nearest_flag_distance) then
  172.                         nearest_flag_distance = d
  173.                         nearest_flagID = flag[i]
  174.                     end
  175.                 end
  176.             end
  177.         end
  178.     end
  179.     if (nearest_flagID~=nil) then      
  180.         local rx,ry,rz=Spring.GetUnitPosition(nearest_flagID)
  181.         return nearest_flagID, rx,ry,rz, nearest_flag_distance
  182.         else return nil
  183.     end
  184. end
  185.  
  186. function nearest_friend_from_unit (uID)
  187.     local nearest_friendID = nil
  188.     local nearest_friend_distance = 9999999999 
  189.     local x,y,z = Spring.GetUnitPosition(uID)  
  190.     local ually = Spring.GetUnitAllyTeam  (uID)
  191.     friend=Spring.GetUnitsInCylinder (x,z, 9000) --, ually geht nicht weil nur teamID geht. man könnte probieren mit nem loop alle verbündeten teamIDs zu nehmen und die tables dann zu mergen
  192.     if (friend == nil) then return nil end  --no near units at all :/
  193.     for i in pairs (friend) do
  194.         if (friend[i] ~= uID and units_allied(friend[i], uID)) then --and Spring.GetUnitAllyTeam(friend[i]) == ually
  195.             local udID =Spring.GetUnitDefID(friend[i])
  196.             if (udID) then
  197.                 local unitDef = UnitDefs [udID]
  198.                 --if (unitDef.name=="tplegocar" or unitDef.name=="tptaktak" or unitDef.name=="tpthulsa" or unitDef.name=="tpflag") then
  199.                 if (unitDef.name=="tpthulsa" or unitDef.name=="tpflag" or unitDef.name=="tplegocar") then
  200.                     local d = Spring.GetUnitSeparation (uID, friend[i])
  201.                     if (d < nearest_friend_distance) then
  202.                         nearest_friend_distance = d
  203.                         nearest_friendID = friend[i]
  204.                     end
  205.                 end
  206.             end
  207.         end
  208.     end
  209.     if (nearest_friendID~=nil) then
  210.         local rx,ry,rz=Spring.GetUnitPosition(nearest_friendID)
  211.         return nearest_friendID, rx,ry,rz, nearest_friend_distance
  212.         else return nil
  213.     end
  214. end
  215.  
  216.  
  217. function units_allied (unitID1, unitID2)
  218.     return Spring.GetUnitAllyTeam (unitID1) == Spring.GetUnitAllyTeam (unitID2)
  219. end
  220.  
  221. --2         3        4   5   6   7  
  222. --playerID, cmdType, px, py, pz, label
  223. function gadget:RecvLuaMsg(msg, playerID)
  224.     --Spring.Echo ("AI: look what i got " .. msg)
  225.     if (msg:find("MapDrawCmd",1,true)) then
  226.         local p = explode ("|", msg)
  227.         if (p[3] == "point") then
  228.             local x = p[4]
  229.             local y = p[5]
  230.             local z = p[6]
  231.             local label = p[7]
  232.         end
  233.     end
  234.    
  235. end
  236.  
  237. -- explode(seperator, string)
  238. function explode(d,p)
  239.   local t, ll
  240.   t={}
  241.   ll=0
  242.   if(#p == 1) then return {p} end
  243.     while true do
  244.       l=string.find(p,d,ll,true) -- find the next d in the string
  245.       if l~=nil then -- if "not not" found then..
  246.         table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
  247.         ll=l+1 -- save just after where we found it for searching next time.
  248.       else
  249.         table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
  250.         break -- Break at end, as it should be, according to the lua manual.
  251.       end
  252.     end
  253.   return t
  254. end
  255.  
  256.  
  257.  
  258. else ------UNSYNCED--------
  259.  
  260. --function gadget:AddConsoleLine(msg,priority)                          --does not work
  261. --  Spring.Echo ("i hear you man")
  262. --end
  263.  
  264. --function gadget:UnitDestroyed(unitID, unitDefID, teamID, attackerID)  --does not work
  265. --  Spring.Echo ("oh unit destroyed")
  266. --end
  267.  
  268. --function gadget:KeyPress(key)                                         --this works!
  269.     --Spring.Echo ("pressed key in gadget: " .. key)
  270. --end
  271.  
  272. --function gadget:MapDrawCmd(playerID, cmdType, px, py, pz, label)
  273. --  Spring.Echo ("marker!")
  274. --  Spring.Echo ("playerID: " .. playerID .. "cmdType:" .. cmdType .. "px:" .. px .. "py:" .. py .. "pz:" .. pz .."label:" .. label)   
  275. --end
  276. end --end unsynced
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement