--Prevent standalone execution: if not TRAINSPORTED then print("To prevent players from harm, this file may only be executed by the trAInsported game.") return end --AI by Brunius: -- FOLLOW MY TRACERS! --V2 includes better turning AI decision making, and will go for a random hotspot when it has no passengers. --V3 will include - go for the nearest hotspot when no passengers. function ai.init(map, money, maxTrains) buyTrain(1,1, 'E') trainNo = 1 cashMoney = money hotSpots = {} hnum = 0 for x = 1, map.width, 1 do for y = 1, map.height, 1 do if map[x][y] == "S" then -- if the field at [x][y] is "S" then print the coordinates on the screen: print("Hotspot found at: " .. x .. ", " .. y) hotSpots[hnum] = {x, y} hnum = hnum+1 end end end end function ai.enoughMoney(money) cashMoney = cashMoney + money buyTrain (1,1) trainNo = trainNo + 1 print("BruniusAI - Buying new train") print("This is train no." .. trainNo) print("Total money accumulated: " .. cashMoney) end function ai.foundPassengers(train, passengers) pass = nil dist = 100 i = 1 while i <= #passengers do d = distance(train.x, train.y, passengers[i].destX, passengers[i].destY) if d train.x-1) then print(train.name .. " same X grid as target") if train.passenger.destX < train.x then return "W" else return "E" end elseif (train.passenger.destY < train.y+1 and train.passenger.destY > train.y-1) then print(train.name .. " same Y grid as target") if train.passenger.destY < train.y then return "N" else return "S" end end -- dir = math.random(2) -- if dir <= 1 then -- print("Pass - E/W selected") -- if train.passenger.destX < train.x then -- return "W" -- else -- return "E" -- end -- elseif dir <= 2 then -- print("Pass - N/S selected") -- if train.passenger.destY < train.y then -- return "N" -- else -- return "S" -- end -- else -- print(train.name .. " pass " .. dir) -- end end end function distance (x1, y1, x2, y2) res = sqrt((x1-x2)^2+(y1-y2)^2) return res end