--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 aggo: function distance(x1,y1,x2,y2) res=sqrt((x1-x2)^2+(y1-y2)^2) return res end function chooseRandom(possibleDirections) local dirTable = {} if possibleDirections["N"] then table.insert(dirTable, "N") end if possibleDirections["S"] then table.insert(dirTable, "S") end if possibleDirections["E"] then table.insert(dirTable, "E") end if possibleDirections["W"] then table.insert(dirTable, "W") end return dirTable[random(#dirTable)] end function ai.init(map, money) buyTrain(1,1, 'E') acceptDist = (map.width + map.height)/3 --print(acceptDist) passengerHereSince = 0 emergency = 0 end function ai.chooseDirection(train, directions) if train.passenger==nil then return chooseRandom(directions) else passengerHereSince = passengerHereSince + 1 if passengerHereSince > 15 then emergency = emergency + 1 if emergency < 18 then if directions["E"] then return "E" end end if emergency < 22 then if directions["W"] then return "W" end end if emergency < 26 then if directions["N"] then return "N" end end if emergency < 30 then if directions["S"] then return "S" end end return chooseRandom(directions) end if random(100) < 15 then return chooseRandom(directions) end if directions["W"] or directions["E"] then horizontal = 1 end if directions["N"] or directions["S"] then vertical = 1 end if horizontal and vertical then if math.abs(train.passenger.destY - train.y) > math.abs(train.passenger.destX - train.x) then if random(100) < 66 then --print(train.passenger.destY.." "..train.y) if train.passenger.destY < train.y then return "N" else return "S" end else if train.passenger.destX < train.x then return "W" else return "E" end end end end return chooseRandom(directions) end 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 15 then if train.passenger~=nil then dropPassenger(train) end passengerHereSince = 0 return pass end if random(100) < 15 then passengerHereSince = 0 return pass end if dist < (acceptDist*train.ID) then passengerHereSince = 0 return pass end end function ai.foundDestination(train) dropPassenger(train) passengerHereSince = 0 emergency = 0 end function ai.enoughMoney() buyTrain(1,3) end