SmallTown12AI - Germanunkol/trAInsported GitHub Wiki

Simple AI for both of the challenge-maps SmallTown1 and SmallTown2.

-- SmallTown12_AI-2.lua
-- AI for trAInsported, challenge-maps "SmallTown1" + "SmallTown2"

  AI="SmallTown12_AI v.2  2014-02-15  by HaJo Gurt"
  dgb=0

function ai.init()
	print("AI:".. AI)

	buyTrain(3,4, 'W')	-- SmallTown1+2
	transported=0
	tour=1
end

function ai.enoughMoney()
	print("Yay")
	--buyTrain(1,1,"E")	-- not possible on these maps
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 dgb>0 then
		print( train.name.." found PSX " .. #passengers )
	end
	return passengers[1]
end

function ai.foundDestination(train)
	dropPassenger(train)
	transported=transported+1
	print( train.name .. " drop: "..transported)
end


function ai.chooseDirection(train,dirs)
	if dgb>0 then
		print( "# " .. train.name .. " @ X="..train.x, "Y="..train.y )
	end

-- SmallTown1:
-- simple round-course:
	if (train.x == 7) and (train.y == 7) then
		return "W"
	end

	if (train.x == 3) and (train.y == 4) then
		return "E"
	end

-- SmallTown2:
-- uses 2 different routes
-- tour1: get passengers from west part of town (as for SmallTown1)
-- tour2: after west part of town is empty, get psx from hotspot
-- switch after no psx was found at end of tour1.
	if (train.passenger == nil) and (train.x == 3) and (train.y == 1) then
		tour=2
		print( "Now tour", tour )
	end

	if (train.passenger == nil) and (tour == 1) and
	   (train.x == 5) and (train.y == 4) then
		return "W"
	end

end

--.