Advertisement
Guest User

Untitled

a guest
Jul 19th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.92 KB | None | 0 0
  1. -- Podball Project https://sourceforge.net/projects/podball/
  2. -- This is wub!
  3. -- You may use this code freely, as a base for your own control scripts.
  4.  
  5. -- We import the vector module.
  6. -- Note that vector.lua is in the same directory as this file.
  7. -- This works because Podball sets the Lua package path to the path given in match.info.
  8. local vector = require "vector"
  9.  
  10. local debugSpam = false
  11.  
  12. -- Constants for convenience
  13. local NO_TEAM = -1
  14. local OUR_TEAM = 0
  15. local OTHER_TEAM = 1
  16. local ACTION_NOTHING = 0
  17. local ACTION_MOVE = 1
  18. local ACTION_SHOOT = 2
  19.  
  20. local GOAL_KEEPER_POSITION = vector(-0.45, 0)
  21. local FIELD_POSITIONS = {}
  22. local FORWARD_POSITION = vector(0.45, 0)
  23. local GOALTARGET = vector(1,0)
  24.  
  25. -- Module wide variables
  26. local gameSettings = {}
  27. local matchSettings = {}
  28. -- Last ball owning team
  29. local last_owningteam = OTHER_TEAM
  30.  
  31. -- Some useful physical constants.
  32. -- They will be calculated during initMatchTeam.
  33. -- Distance the ball travels until loosing half of his velocity.
  34. local pass_distance
  35. -- Time this will take.
  36. local pass_time
  37.  
  38. ----------------------------------------
  39. -- This stateless function should return static info about the module.
  40. function getModuleInfo()
  41.     local t = {}
  42.     t["name"] = "wub"
  43.     t["version"] = "1.0.0"
  44.     t["author"] = "Me"
  45.     t["date"] = "2014-06-01"
  46.     t["min_pods"] = 1
  47.     t["max_pods"] = 12
  48.     return t
  49. end
  50.  
  51. ----------------------------------------
  52. -- Called for every new match and once per team.
  53. -- For parameter gameSettingsParam, see struct GAMEINFO defined in podtypes.h.
  54. -- For parameter matchSettingsParam, see struct CMatchSettings defined in podball.h.
  55. function initMatchTeam(gameSettingsParam, matchSettingsParam)
  56.     print("HIER KOMMT DER WUB!")
  57.     gameSettings = gameSettingsParam
  58.     matchSettings = matchSettingsParam
  59. end
  60.  
  61. ----------------------------------------
  62. -- Called for every time step the engine needs a control module action for a team.
  63. -- Input: world - The state of the game world containing all object positions among others.
  64. --  See WORLDSTRUCT in podtypes.h.
  65. --  NOTE: In order to respect common habits of Lua, pod indices go from 1 to NPODS.
  66. --  This includes the variable world.ball.owningpod.
  67. -- Return value: Actions of all the pods of the own team:
  68. --  A table podActions[1..NPODS] with a vector "a" and the action "action" to perform.
  69. --  action is one of 0-do nothing; 1-move; 2-shoot
  70. local myworld = {}
  71. local gameframe = 0
  72. local lastshotframe = 0
  73. function getTeamActions(world)
  74.     gameframe=gameframe+1
  75.     myworld = world
  76.     --print ("owningteam:", world.ball.owningteam)
  77.     --rPrint(world.ball,nil," ")
  78.    
  79.  
  80.     --print(world.time)
  81.     -- 'vectorize' some of the tables.
  82.     setmetatable(world.ball.q, getmetatable(vector.zero))
  83.     setmetatable(world.ball.v, getmetatable(vector.zero))
  84.     setmetatable(world.ball.f, getmetatable(vector.zero))
  85.  
  86.    
  87.     local podActions = {}  
  88.     for i=1,gameSettings.NPODS do
  89.         podActions[i] = {}
  90.         podActions[i].action = ACTION_NOTHING
  91.         podActions[i].a = vector()
  92.         if i== 1 then
  93.             podActions[i] , s = thinkTowart(i)         
  94.         else
  95.             podActions[i] , s = think (i)          
  96.         end
  97.         if debugSpam then print (i, s) end
  98.     end
  99.    
  100.     --print (gameframe,"pod 1 speed:" ,  getSpeed(1)*1000)
  101.     --print ("gameframe-lastshotframe", gameframe-lastshotframe)
  102.     --print (world.time, last_owningteam)
  103.     return podActions
  104. end
  105.  
  106. function thinkTowart (podID)
  107.     local s = "torwart: "
  108.     local podAction = {}
  109.     podAction.action = ACTION_NOTHING
  110.     podAction.a = vector()
  111.     if (myworld.ball.owningpod==podID and myworld.ball.owningteam == OUR_TEAM) then
  112.         s=s.."ball wegschiessen"
  113.         podAction.action = ACTION_SHOOT
  114.         podAction.a = getMoveVectorTowardsXY (podID, 0.5,math.random (-0.2, 0.2))
  115.         return podAction,s
  116.     end
  117.     local d = getBallDistance (podID)
  118.     if d < 0.2 then
  119.         --if myworld.ball.v.x < 0 then print ("<-") else print ("->") end
  120.         s=s.. " naher ball!"
  121.         podAction.a = getMoveVectorTowardsXY (podID,
  122.         myworld.ball.q.x , myworld.ball.q.y)
  123.         podAction.action = ACTION_MOVE
  124.         return podAction,s
  125.     end
  126.    
  127.     if d < 0.4 and getDistance (podID, -0.45,0) < 0.05 then
  128.         s=s.. " wachsam am tor"
  129.         podAction.a = getMoveVectorTowardsXY (podID, -0.45, myworld.ball.q.y/4 )
  130.         podAction.action = ACTION_MOVE
  131.         return podAction,s
  132.     end
  133.    
  134.     if getDistance (podID, -0.45,0) < 0.05 then
  135.         podAction.action = ACTION_NOTHING
  136.         s=s.. " superchill"
  137.     else
  138.         podAction.a = getMoveVectorTowardsXY (podID, -0.45, 0 )
  139.         podAction.action = ACTION_MOVE
  140.         s=s.. " chill "..getDistance (podID, -0.45,0)
  141.         return podAction,s
  142.     end
  143.     return podAction, "lol?"
  144. end
  145.  
  146. function think (podID)
  147.     local s = "*"
  148.     podAction = {}
  149.     podAction.action = ACTION_NOTHING
  150.     podAction.a = vector()
  151.     if (myworld.ball.owningpod==podID and getBallDistance (podID)<0.01) then
  152.         s=s.."schuss!"
  153.         podAction.action = ACTION_SHOOT
  154.         podAction.a = getMoveVectorTowardsXY (podID, 0.5,0)
  155.         lastshotframe = gameframe
  156.     else    --kein ball
  157.     if myworld.ball.owningteam == OUR_TEAM then --jemand anders im team hat den ball
  158.         s=s.." bewege+ "
  159.         podAction = {}
  160.         podAction.a = getMoveVectorTowardsXY (podID, 0.5, 0)
  161.         podAction.action = ACTION_MOVE
  162.     else --gar niemand im team hat gar nicht den ball
  163.         --falls sehr nah am Ball: dann auf jeden fall den ball holen
  164.         local d = getBallDistance (podID)
  165.         --print (podID, d)
  166.         if d < 0.05 then
  167.             s=s.. " hole nahen ball"
  168.             podAction.a = getMoveVectorTowardsXY (podID, myworld.ball.q.x, myworld.ball.q.y )
  169.             podAction.action = ACTION_MOVE
  170.             return podAction,s
  171.         end
  172.        
  173.         s=s.." bewege- "
  174.         podAction = {}
  175.         if podID%2 == 0 then
  176.             podAction.a = getMoveVectorTowardsXY (podID, myworld.ball.q.x, myworld.ball.q.y )
  177.             s=s.." zum ball"
  178.         else
  179.             s=s.." schlange!"
  180.             local maxSpread = math.abs (0.5+myworld.ball.q.x)/2  +0.1
  181.             --print (maxSpread)
  182.             local x = ((podID/gameSettings.NPODS)-0.5) /1.5
  183.             --print (podID, maxSpread)
  184.             --if podID%2 == 0 then x = myworld.ball.q.x end
  185.             podAction.a = getMoveVectorTowardsXY (podID, x, math.clamp (-maxSpread, myworld.ball.q.y, maxSpread ))
  186.         end
  187.         podAction.action = ACTION_MOVE
  188.     end
  189. end
  190. return podAction,s
  191. end
  192.  
  193.  
  194. function math.clamp(low, n, high)
  195.     return math.min(math.max(n, low), high)
  196. end
  197.  
  198. --[[
  199.     if world.ball.owningteam == OUR_TEAM then
  200.         last_owningteam = OUR_TEAM
  201.     elseif world.ball.owningteam == OTHER_TEAM then
  202.         last_owningteam = OTHER_TEAM
  203.     end
  204. --]]
  205.  
  206. function getSpeed (podID)
  207.     local x = myworld.team.pods[podID].v.x
  208.     local y = myworld.team.pods[podID].v.y
  209.     return (x*x+y*y)
  210. end
  211.  
  212. function getBallDistance(podID)
  213.     local ax = myworld.team.pods[podID].q.x
  214.     local ay = myworld.team.pods[podID].q.y
  215.     local bx = myworld.ball.q.x
  216.     local by = myworld.ball.q.y
  217.    
  218.     local dx = ax - bx
  219.     local dy = ay - by
  220.     return math.sqrt(dx * dx + dy * dy)
  221. end
  222.  
  223. function getDistance (podID, bx,by)
  224.     local ax = myworld.team.pods[podID].q.x
  225.     local ay = myworld.team.pods[podID].q.y
  226.    
  227.     local dx = ax - bx
  228.     local dy = ay - by
  229.     return math.sqrt(dx * dx + dy * dy)
  230. end
  231.  
  232. function getMoveVectorTowardsXY (podID, tx, ty)
  233.     --local mx = 0
  234.     --local my = 0
  235.     --if myworld.team.pods[podID].q.x < tx then mx = 1 else mx = -1 end
  236.     --if myworld.team.pods[podID].q.y < ty then my = 1 else my = -1 end
  237.     local mx = tx-myworld.team.pods[podID].q.x
  238.     local my = ty-myworld.team.pods[podID].q.y
  239.     local v = vector (mx, my):normalize_inplace()
  240.     return v
  241.     --return myworld.team.pods[podID].q - vector (tx,ty)
  242. end
  243.  
  244. --https://gist.github.com/stuby/5445834#file-rprint-lua
  245. function rPrint(s, l, i) -- recursive Print (structure, limit, indent)
  246. l = (l) or 100; i = i or "";    -- default item limit, indent string
  247. if (l<1) then print "ERROR: Item limit reached."; return l-1 end;
  248. local ts = type(s);
  249. if (ts ~= "table") then print (i,ts,s); return l-1 end
  250. print (i,ts); -- print "table"
  251. for k,v in pairs(s) do -- print "[KEY] VALUE"
  252. l = rPrint(v, l, i.."\t["..tostring(k).."]");
  253. if (l < 0) then break end
  254. end
  255. return l
  256. end
  257.  
  258.  
  259. ----------------------------------------
  260. -- Called once at the end of a match.
  261. function disposeMatchTeam()
  262.     print("wub: dispose.")
  263. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement