Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Podball Project https://sourceforge.net/projects/podball/
- -- This is wub!
- -- You may use this code freely, as a base for your own control scripts.
- -- We import the vector module.
- -- Note that vector.lua is in the same directory as this file.
- -- This works because Podball sets the Lua package path to the path given in match.info.
- local vector = require "vector"
- local debugSpam = false
- -- Constants for convenience
- local NO_TEAM = -1
- local OUR_TEAM = 0
- local OTHER_TEAM = 1
- local ACTION_NOTHING = 0
- local ACTION_MOVE = 1
- local ACTION_SHOOT = 2
- local GOAL_KEEPER_POSITION = vector(-0.45, 0)
- local FIELD_POSITIONS = {}
- local FORWARD_POSITION = vector(0.45, 0)
- local GOALTARGET = vector(1,0)
- -- Module wide variables
- local gameSettings = {}
- local matchSettings = {}
- -- Last ball owning team
- local last_owningteam = OTHER_TEAM
- -- Some useful physical constants.
- -- They will be calculated during initMatchTeam.
- -- Distance the ball travels until loosing half of his velocity.
- local pass_distance
- -- Time this will take.
- local pass_time
- ----------------------------------------
- -- This stateless function should return static info about the module.
- function getModuleInfo()
- local t = {}
- t["name"] = "wub"
- t["version"] = "1.0.0"
- t["author"] = "Me"
- t["date"] = "2014-06-01"
- t["min_pods"] = 1
- t["max_pods"] = 12
- return t
- end
- ----------------------------------------
- -- Called for every new match and once per team.
- -- For parameter gameSettingsParam, see struct GAMEINFO defined in podtypes.h.
- -- For parameter matchSettingsParam, see struct CMatchSettings defined in podball.h.
- function initMatchTeam(gameSettingsParam, matchSettingsParam)
- print("HIER KOMMT DER WUB!")
- gameSettings = gameSettingsParam
- matchSettings = matchSettingsParam
- end
- ----------------------------------------
- -- Called for every time step the engine needs a control module action for a team.
- -- Input: world - The state of the game world containing all object positions among others.
- -- See WORLDSTRUCT in podtypes.h.
- -- NOTE: In order to respect common habits of Lua, pod indices go from 1 to NPODS.
- -- This includes the variable world.ball.owningpod.
- -- Return value: Actions of all the pods of the own team:
- -- A table podActions[1..NPODS] with a vector "a" and the action "action" to perform.
- -- action is one of 0-do nothing; 1-move; 2-shoot
- local myworld = {}
- local gameframe = 0
- local lastshotframe = 0
- function getTeamActions(world)
- gameframe=gameframe+1
- myworld = world
- --print ("owningteam:", world.ball.owningteam)
- --rPrint(world.ball,nil," ")
- --print(world.time)
- -- 'vectorize' some of the tables.
- setmetatable(world.ball.q, getmetatable(vector.zero))
- setmetatable(world.ball.v, getmetatable(vector.zero))
- setmetatable(world.ball.f, getmetatable(vector.zero))
- local podActions = {}
- for i=1,gameSettings.NPODS do
- podActions[i] = {}
- podActions[i].action = ACTION_NOTHING
- podActions[i].a = vector()
- if i== 1 then
- podActions[i] , s = thinkTowart(i)
- else
- podActions[i] , s = think (i)
- end
- if debugSpam then print (i, s) end
- end
- --print (gameframe,"pod 1 speed:" , getSpeed(1)*1000)
- --print ("gameframe-lastshotframe", gameframe-lastshotframe)
- --print (world.time, last_owningteam)
- return podActions
- end
- function thinkTowart (podID)
- local s = "torwart: "
- local podAction = {}
- podAction.action = ACTION_NOTHING
- podAction.a = vector()
- if (myworld.ball.owningpod==podID and myworld.ball.owningteam == OUR_TEAM) then
- s=s.."ball wegschiessen"
- podAction.action = ACTION_SHOOT
- podAction.a = getMoveVectorTowardsXY (podID, 0.5,math.random (-0.2, 0.2))
- return podAction,s
- end
- local d = getBallDistance (podID)
- if d < 0.2 then
- --if myworld.ball.v.x < 0 then print ("<-") else print ("->") end
- s=s.. " naher ball!"
- podAction.a = getMoveVectorTowardsXY (podID,
- myworld.ball.q.x , myworld.ball.q.y)
- podAction.action = ACTION_MOVE
- return podAction,s
- end
- if d < 0.4 and getDistance (podID, -0.45,0) < 0.05 then
- s=s.. " wachsam am tor"
- podAction.a = getMoveVectorTowardsXY (podID, -0.45, myworld.ball.q.y/4 )
- podAction.action = ACTION_MOVE
- return podAction,s
- end
- if getDistance (podID, -0.45,0) < 0.05 then
- podAction.action = ACTION_NOTHING
- s=s.. " superchill"
- else
- podAction.a = getMoveVectorTowardsXY (podID, -0.45, 0 )
- podAction.action = ACTION_MOVE
- s=s.. " chill "..getDistance (podID, -0.45,0)
- return podAction,s
- end
- return podAction, "lol?"
- end
- function think (podID)
- local s = "*"
- podAction = {}
- podAction.action = ACTION_NOTHING
- podAction.a = vector()
- if (myworld.ball.owningpod==podID and getBallDistance (podID)<0.01) then
- s=s.."schuss!"
- podAction.action = ACTION_SHOOT
- podAction.a = getMoveVectorTowardsXY (podID, 0.5,0)
- lastshotframe = gameframe
- else --kein ball
- if myworld.ball.owningteam == OUR_TEAM then --jemand anders im team hat den ball
- s=s.." bewege+ "
- podAction = {}
- podAction.a = getMoveVectorTowardsXY (podID, 0.5, 0)
- podAction.action = ACTION_MOVE
- else --gar niemand im team hat gar nicht den ball
- --falls sehr nah am Ball: dann auf jeden fall den ball holen
- local d = getBallDistance (podID)
- --print (podID, d)
- if d < 0.05 then
- s=s.. " hole nahen ball"
- podAction.a = getMoveVectorTowardsXY (podID, myworld.ball.q.x, myworld.ball.q.y )
- podAction.action = ACTION_MOVE
- return podAction,s
- end
- s=s.." bewege- "
- podAction = {}
- if podID%2 == 0 then
- podAction.a = getMoveVectorTowardsXY (podID, myworld.ball.q.x, myworld.ball.q.y )
- s=s.." zum ball"
- else
- s=s.." schlange!"
- local maxSpread = math.abs (0.5+myworld.ball.q.x)/2 +0.1
- --print (maxSpread)
- local x = ((podID/gameSettings.NPODS)-0.5) /1.5
- --print (podID, maxSpread)
- --if podID%2 == 0 then x = myworld.ball.q.x end
- podAction.a = getMoveVectorTowardsXY (podID, x, math.clamp (-maxSpread, myworld.ball.q.y, maxSpread ))
- end
- podAction.action = ACTION_MOVE
- end
- end
- return podAction,s
- end
- function math.clamp(low, n, high)
- return math.min(math.max(n, low), high)
- end
- --[[
- if world.ball.owningteam == OUR_TEAM then
- last_owningteam = OUR_TEAM
- elseif world.ball.owningteam == OTHER_TEAM then
- last_owningteam = OTHER_TEAM
- end
- --]]
- function getSpeed (podID)
- local x = myworld.team.pods[podID].v.x
- local y = myworld.team.pods[podID].v.y
- return (x*x+y*y)
- end
- function getBallDistance(podID)
- local ax = myworld.team.pods[podID].q.x
- local ay = myworld.team.pods[podID].q.y
- local bx = myworld.ball.q.x
- local by = myworld.ball.q.y
- local dx = ax - bx
- local dy = ay - by
- return math.sqrt(dx * dx + dy * dy)
- end
- function getDistance (podID, bx,by)
- local ax = myworld.team.pods[podID].q.x
- local ay = myworld.team.pods[podID].q.y
- local dx = ax - bx
- local dy = ay - by
- return math.sqrt(dx * dx + dy * dy)
- end
- function getMoveVectorTowardsXY (podID, tx, ty)
- --local mx = 0
- --local my = 0
- --if myworld.team.pods[podID].q.x < tx then mx = 1 else mx = -1 end
- --if myworld.team.pods[podID].q.y < ty then my = 1 else my = -1 end
- local mx = tx-myworld.team.pods[podID].q.x
- local my = ty-myworld.team.pods[podID].q.y
- local v = vector (mx, my):normalize_inplace()
- return v
- --return myworld.team.pods[podID].q - vector (tx,ty)
- end
- --https://gist.github.com/stuby/5445834#file-rprint-lua
- function rPrint(s, l, i) -- recursive Print (structure, limit, indent)
- l = (l) or 100; i = i or ""; -- default item limit, indent string
- if (l<1) then print "ERROR: Item limit reached."; return l-1 end;
- local ts = type(s);
- if (ts ~= "table") then print (i,ts,s); return l-1 end
- print (i,ts); -- print "table"
- for k,v in pairs(s) do -- print "[KEY] VALUE"
- l = rPrint(v, l, i.."\t["..tostring(k).."]");
- if (l < 0) then break end
- end
- return l
- end
- ----------------------------------------
- -- Called once at the end of a match.
- function disposeMatchTeam()
- print("wub: dispose.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement