--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 cem: -- Tutorial 3: Be smart! function ai.init() silent = true buyTrain(3,1) lost = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } end function posdir(b, d) table.insert(b, d) if not silent then print("Possible: " .. d) end end function trainlost(t) lost[t.ID] = lost[t.ID] + 1 return lost[t.ID] end function notlost(t) lost[t.ID] = 0 end function ai.chooseDirection(train, directions) howlost = trainlost(train) if not silent then print("How lost: "..howlost) end if howlost > 10 and math.random() > 0.5 then if not silent then print("lost going south") end return "S" end if train.passenger ~= nil then bag = {} if train.x < train.passenger.destX and directions.E ~= nil then posdir(bag, "E") end if train.x > train.passenger.destX and directions.W ~= nil then posdir(bag, "W") end if train.y < train.passenger.destY and directions.S ~= nil then posdir(bag, "S") end if train.y > train.passenger.destY and directions.N ~= nil then posdir(bag, "N") end if #bag > 0 then ch = bag[math.random(1, #bag)] if not silent then print("choosing: " .. ch) end return ch end -- fallthrough, pick at random end dirs = {} if directions.E == true then table.insert(dirs, 'E') end if directions.W == true then table.insert(dirs, 'W') end if directions.S == true then table.insert(dirs, 'S') end if directions.N == true then table.insert(dirs, 'N') end d = math.random(1, #dirs) ch = dirs[d] if not silent then print("choosing nopass: " .. ch) end return ch end function distance(x1,y1,x2,y2) return sqrt((x1-x2)^2 + (y1-y2)^2) end function ai.foundPassengers(train, passengers) min = 100000 bestpass = nil i = 1 while i <= #passengers do p = passengers[i] pdist = distance(train.x, train.y, p.destX, p.destY) if not silent then print("found pass dist "..pdist) end if pdist < min then if not silent then print(pdist.." better than "..min) end min = pdist bestpass = p end i = i + 1 end notlost(train) if bestpass == nil then if not silent then print("BADBADBAD #pass: "..#passengers) end return passengers[1] end return bestpass end function ai.foundDestination(train) dropPassenger(train) notlost(train) end function ai.enoughMoney() buyTrain(1,3) end