--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 HiC: Trains = {} Passengers = {} Map = {} maxTrains = 0 ----------------- -- CALLBACKS -- ---------------- function ai.init(map, money) --print(getNumberOfLines()) Map = map --print(money.." Credits") --printMap() --printTable(Map.railList) buyTrains() print(getNumberOfLines()) end -- code to pick up passengers function ai.foundPassengers(train,passengers) -- print("Passenger: "..passengers[1].name) Passengers[passengers[1].name] = nil -- game uses returned passenger to pick up by train return passengers[1] end function ai.chooseDirection(train, directions) -- train: ID, name, x, y -- if passenger transported -> train.passenger.destX, destY, name -- directions are: N, E, S, W return chooseCleverDirection(directions) or chooseRandomDirection(directions) end -- code to drop off passengers function ai.foundDestination(train) dropPassenger(train) end function ai.enoughMoney() buyTrains() end function ai.newPassenger(name, x, y, destX, destY, vipTime) Passengers[name] = {x=x, y=y, destX=destX, destY=destY, vipTime=vipTime} --printTable(Passengers) end function ai.passengerBoarded(train,passenger) if Passengers[passenger.name] then Passengers[passenger.name] = nil end end ------------------- -- TRAIN FUNCT -- ------------------ -- CALLBACKS -- function ai.newTrain(train) table.insert(Trains,train) print("Train no."..#Trains.." purchased.") --printTable(Trains) end function buyTrains() print(#Trains.."/"..maxTrains.." on route.") if #Trains < maxTrains or 100 then buyTrain(random(Map.width), random(Map.height)) else print(getMoney().." Trillian Dollars raised, but no more trains needed") end end function chooseRandomDirection(dirs) local tempDirs = {} if dirs["N"] then table.insert(tempDirs, "N") end if dirs["S"] then table.insert(tempDirs, "S") end if dirs["E"] then table.insert(tempDirs, "E") end if dirs["W"] then table.insert(tempDirs, "W") end return tempDirs[random(#tempDirs)] end function chooseCleverDirection(directions) return nil end -------------- -- SYSTEM -- ------------- function distance(x1,y1,x2,y2) dis = sqrt( (x1-x2)^2 + (y1-y2)^2 ) return dis end function printTable(tbl, lvl) lvl = lvl or 1 local str = "" for i = 1,lvl-1 do str = str .. "\t" end if lvl > 1 then --print("Maximum Level Depth reached") else for k, v in pairs(tbl) do if type(v) == "table" then print(str,k.. "{") printTable(v, lvl + 1) print(str, "}") else print(str, k, v) end end end end function printMap() str = {} local map=Map 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 for i = 1, #str do print(str[i]) end end ---------- -- END -- ----------