Skip to content
hajo4 edited this page Feb 16, 2014 · 1 revision

Simple AI for the challenge-map "TheMaze".

-- MazeRunnerAI_001.lua

  AI="MazeRunner v0.01  2014-02-15  by HaJo Gurt"
  dgb=0

--[[
AI for trAInsported, challenge-map "TheMaze".
This also works well for the map SmallTown1,
and gets SmallTown2 almost done.

Buys the train at the location of the passenger,
then does a simple maze-run "follow the right wall",
until destination is reached.
]]--

--Prevent standalone execution:
if not TRAINSPORTED then
	print("This file may only be executed by the trAInsported game.")
	return
end

-- ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####

function ai.init(map, money, maxTrains)
	followWall = "R"
	startDir   = "S"

	print("AI:", AI, "("..followWall,startDir ..")")

	print("Credits:  ",  money )
	print("Max Trains:", maxTrains )
	--rememberMap = map
	--print("Map:      ",  map.name )
	print("Map size: ",  map.height, map.width)

	transported=0
	myTrains=0
	-- buying train moved to ai.newPassenger()

	if dgb>0 then
		printMap(map)
	end
	searchMap(map)
end

function ai.enoughMoney()
	print("Yay: $", getMoney() )

	-- Todo: also check maxTrains,
	--  search for VIPs in passenger-list

	--buyTrain(1,1,"E")
end

function ai.newTrain(train)
	myTrains=myTrains+1
	print(train.name .. " starts at:", train.x, train.y)
	print("Number of trains:", myTrains)
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 printMap(map)
    str = {}
    for j = 1, map.height do
        str[j] = ""
        for i = 1, map.width do
            if map[i][j] then 
                str[j] = str[j] .. map[i][j] .. " "
            else
                str[j] = str[j] .. "- "
            end
        end
    end
	print("Map:")
    for i = 1, #str do
        print(str[i])
    end
end

function searchMap(map)
 -- go through the entire map and search for hotspots:
	rails =0
	houses=0
	spots =0
        for x = 1, map.width, 1 do
            for y = 1, map.height, 1 do
                if map[x][y] == "C" then rails =rails +1 end
                if map[x][y] == "H" then houses=houses+1 end

		-- if the field at [x][y] is "S" then print the coordinates on the screen:
                if map[x][y] == "S" then
                    print("Hotspot found at: " .. x .. ", " .. y .. "!")
			spots=spots+1
                end
            end
        end
	print("Rails:", rails, 
		"Houses:", houses,
		"Hotspots:", spots)
end


--

function ai.newPassenger(name, x, y, destX, destY, vipTime)
	if dgb>0 then
	print("newPassenger:", name, x, y, destX, destY, vipTime)
	end

	money = getMoney()
	if money >= 25 then		-- Todo: also check maxTrains
		buyTrain(x,y, startDir)	-- TheMaze: buy train next to passenger
	end

	--money = getMoney()
	--print("Cash now:", money)
end

--

function ai.foundPassengers(train,passengers)
	--print( train.name.." Hi: " .. #passengers )
	return passengers[1]
end

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


function ai.chooseDirection(train,dirs)
	if dgb>0 then
	print( "# " .. train.name, "@ X="..train.x, "Y="..train.y, "Dir="..train.dir)
 	--print( "Possible dirs:", dirs["N"],"/", dirs["E"],"/", dirs["S"],"/", dirs["W"])
 	--print( "Possible dirs:")
	--if dirs["N"] == true then print( "n" ) end
	--if dirs["E"] == true then print( "e" ) end
	--if dirs["S"] == true then print( "s" ) end
	--if dirs["W"] == true then print( "w" ) end
	end

	go = "y"
	go = train.dir

	if followWall == "R" then  -- follow the right wall:
		if (train.dir == "N") and (dirs["E"] == true) then go = "E" end
		if (train.dir == "E") and (dirs["S"] == true) then go = "S" end
		if (train.dir == "S") and (dirs["W"] == true) then go = "W" end
		if (train.dir == "W") and (dirs["N"] == true) then go = "N" end
	else	-- follow the left wall:
		if (train.dir == "N") and (dirs["W"] == true) then go = "W" end
		if (train.dir == "E") and (dirs["N"] == true) then go = "N" end
		if (train.dir == "S") and (dirs["E"] == true) then go = "E" end
		if (train.dir == "W") and (dirs["S"] == true) then go = "S" end
	end 

	if dgb>0 then print("--> " .. go) end
	return go
end

--.
Clone this wiki locally