--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 popsb: rememberPassengers = {} rememberMap = nil rememberMaxTrains= 0 function ai.init(map, money, maxTrains) rememberMap = map -- printMap(rememberMap) if sqrt(map.width * map.height) <= maxTrains then rememberMaxTrains = sqrt(map.width * map.height) else rememberMaxTrains = maxTrains end x = random(map.width) y = random(map.height) while money >= 25 do -- 25c is cost of one train buyTrain(x, y) money = money - 25 rememberMaxTrains = maxTrains - 1 end end function ai.enoughMoney(money) x = random(rememberMap.width) y = random(rememberMap.height) while money >= 25 and rememberMaxTrains > 0 do -- 25c is cost of one train buyTrain(x, y) money = money - 25 rememberMaxTrains = rememberMaxTrains -1 -- print("Bought new train!") end end function chooseSmart(train, possibleDirections) if train.passenger then -- print("train.passenger destination:", train.passenger.destX, train.passenger.destY) -- print("train.pos:", train.x, train.y) if possibleDirections["N"] and train.passenger.destY+1 < train.y and random() < .7 then return "N" end if possibleDirections["S"] and train.passenger.destY-1 > train.y and random() < .7 then return "S" end if possibleDirections["E"] and train.passenger.destX+1 > train.x and random() < .7 then return "E" end if possibleDirections["W"] and train.passenger.destX-1 < train.x and random() < .7 then return "W" end end end function chooseRandom(train, possibleDirections) tbl = {} if possibleDirections["N"] then tbl[#tbl+1] = "N" end if possibleDirections["S"] then tbl[#tbl+1] = "S" end if possibleDirections["E"] then tbl[#tbl+1] = "E" end if possibleDirections["W"] then tbl[#tbl+1] = "W" end ret = tbl[random(#tbl)] return ret end function ai.chooseDirection(train, possibleDirections) -- choose a direction that makes sense. -- otherwise, return a random direction: return chooseSmart(train, possibleDirections) or chooseRandom(train, possibleDirections) end function ai.blocked(train, possibleDirections, lastDirection) return chooseSmart(train, possibleDirections) or chooseRandom(train, possibleDirections) end function ai.foundPassengers(train, passengers) if train.passenger then return nil end for k, p in pairs(passengers) do --print('Found passenger '..p.name..".") if p.name:find("VIP") then --print("found VIP!") return p end end return passengers[1] end function ai.foundDestination(train) --print(train.name..": Bye "..train.passenger.name) dropPassenger(train) --print("I have " .. getMoney().." Credits.") end function ai.passengerBoarded(train, passenger) --print("I see you! " .. train.ID .. " " .. passenger) end function ai.newPassenger(name, x, y, destX, destY) rememberPassengers[name] = {x=x,y=y,destX=destX,destY=destY} -- printTable(rememberPassengers) end function boolTableFind(tbl, pattern) return nil end function printMap(map) str = {} for j = 1, map.height do str[j] = "" for i = 1, map.width do if map[i][j] == "C" then str[j] = str[j] .. map[i][j] .. " " else str[j] = str[j] .. "- " end end end for i = 1, #str do print(str[i]) end end