SmallTown3AI - Germanunkol/trAInsported GitHub Wiki
Simple AI for the challenge-map SmallTown3.
-- SmallTown3_AI-2.lua
AI="SmallTown3_AI-2 2014-02-15 by HaJo Gurt"
dbg=0
--[[
AI for trAInsported, challenge-map "SmallTown3".
2 trains with fixed routes that each stay in their area,
dropping off passengers for the other area at some exchange-point.
]]--
function ai.init()
print("AI: ".. AI)
buyTrain( 6,7, 'W')
buyTrain(10,8, 'N')
transported=0
end
function ai.enoughMoney()
print("Yay")
--buyTrain(7,1,"E") -- not useful on this map
end
function distance(x1,y1, x2,y2)
d=sqrt( (x1-x2)^2 + (y1-y2)^2 )
return d
end
function manhattanDist(x1, y1, x2, y2)
return math.abs(x1-x2) + math.abs(y1-y2)
end
function ai.foundPassengers(train,passengers)
if dbg>0 then
print( train.name, "found P.: " .. #passengers )
end
if (train.x == 7) and (train.y == 9) then
-- at exchange, only pick up passengers for hometown of train:
x1 = 7 -- center and size of town1
y1 = 5
s1 = 4.2
i = 1
while i <= #passengers do
x2 = passengers[i].destX
y2 = passengers[i].destY
--d = distance( x1, y1, x2, y2 )
d = manhattanDist( x1, y1, x2, y2 )
if dbg>0 then
print( i..")", x2, y2, ": ", d)
end
if (d<s1) and (train.name == "Train1") then
if dbg>0 then print( "T1 pickup:"..i ) end
return passengers[i]
end
if (d>s1) and (train.name == "Train2") then
if dbg>0 then print( "T2 pickup:", i ) end
return passengers[i]
end
i=i+1
end
if dbg>0 then print "no pickup" end
else -- elsewhere:
if dbg>0 then print "Normal pickup" end
return passengers[1]
end
end
function ai.foundDestination(train)
dropPassenger(train)
transported=transported+1
print( train.name .. " drop: "..transported)
end
--
function ai.mapEvent(train)
if dbg>0 then
print("Exchange station:".. train.name) -- @ (7,9)
end
if train.passenger == nil then
print(" train empty")
-- Todo: call ai.foundPassengers
else
-- only drop off passengers for the other town at exchange-point:
if train.name == "Train1" then
if train.passenger.destX > 9 then
dropPassenger(train)
end
else
if train.passenger.destX < 10 then
dropPassenger(train)
print "Train1 drop"
end
end
end
end
function ai.chooseDirection(train,dirs)
--[[
if train.passenger == nil then
print( "#", train.name, train.x, train.y, ": empty")
else
print( "#", train.name, train.x, train.y, ": " .. train.passenger.name)
end
]]--
if train.name == "Train1" then -- train #1 - upper town :
if (train.x == 8) and (train.y == 8) then return "W" end
if (train.x == 7) and (train.y == 9) then return "N" end
if (train.x == 6) and (train.y == 4) then return "E" end
if (train.x == 8) and (train.y == 7) then return "W" end
else -- train #2 - lower town:
if (train.x == 9) and (train.y == 9) then return "W" end
if (train.x == 7) and (train.y == 9) then return "E" end
if (train.x == 10) and (train.y == 8) then return "W" end
if (train.x == 10) and (train.y == 10) then return "W" end
if (train.x == 10) and (train.y == 11) then return "E" end
if (train.x == 12) and (train.y == 7) then return "E" end
if (train.x == 13) and (train.y == 8) then return "W" end
end
end
--.