--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 flos: --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 flos: -- Tutorial 4: Close is good! function ai.init() buyTrain(2,4, 'E') buyTrain(5,5, 'E') buyTrain(2,3, 'E') buyTrain(4,7, 'E') lastSeenPassengers={} lastJunction = {-1,-1,-1} secondLastJunction = {-1,-1,-1} listOfLastJunctions = {} wayToStartpoint = {} achtung=0 loop=false end function ai.chooseDirection(train, possibleDirections) if(achtung>-10)then --print(achtung) end if(achtung>0 and #wayToStartpoint>0)then --print("special: search way back") nextStep = {["x"]=wayToStartpoint[1][1],["y"]=wayToStartpoint[1][2]} while(nextStep[1]==train.nextX and nextStep[2]==train.nextY or nextStep[1]==wayToStartpoint[1][1] and nextStep[2]==wayToStartpoint[1][2])do table.remove(wayToStartpoint,1) nextStep= {["x"]=wayToStartpoint[1][1],["y"]=wayToStartpoint[1][2]} end achtung=achtung-0.01 dir = direction(nextStep,train,possibleDirections) --print(dir.." | "..nextStep.x.." "..nextStep.y) return dir end --print(arrayContainsAmount(listOfLastJunctions,{train.nextX,train.nextY,test})) if(arrayContainsAmount(listOfLastJunctions,{train.nextX,train.nextY,test})>=4)then achtung=achtung+1.2 loop=true random1 = math.random(5) if(random1==5)then return "N" elseif(random1==6)then return "W" elseif(random1==7)then return "S" elseif(random1==8)then return "E" end end --print("amount of lastSeenPassengers: "..#lastSeenPassengers) if(train.passenger == nil)then --print("No passenger in train "..train.name) nearestPassenger = lowestDistance(lastSeenPassengers,train) removeSelf(lastSeenPassengers,nearestPassenger) test = direction(nearestPassenger,train,possibleDirections) else test= direction(train.passenger,train,possibleDirections) test2=test end if(lastJunction[1] == train.nextX and lastJunction[2] == train.nextY and ((lastJunction[3]==test or test==nil) or secondLastJunction[1]==lastJunction[1] and secondLastJunction[2]==lastJunction[2]))then print("Loop detected") loop=true if("W"~=test and contains(possibleDirections,"W"))then test="W" elseif("S"~=test and contains(possibleDirections,"S"))then test="S" elseif("E"~=test and contains(possibleDirections,"E"))then test="E" elseif("N"~=test and contains(possibleDirections,"N"))then test="N" end end --print(lastJunction[1].." "..lastJunction[2].." "..lastJunction[3].." | "..train.nextX.." "..train.nextY.." "..test2) secondLastJunction = lastJunction lastJunction={train.nextX,train.nextY,test} table.insert(listOfLastJunctions,lastJunction) table.insert(wayToStartpoint,1,{train.nextX,train.nextY}) if(#listOfLastJunctions>15)then table.remove(listOfLastJunctions,1) end --print(#listOfLastJunctions) if(test==test2)then achtung=achtung-1 else achtung=achtung-0.2 end loop=false return test end function ai.foundPassengers(train,passengers) -- if(#passengers > 1 or (passengers==1 and train.passenger ~= nil))then -- if(lastSeenPassengers[0]==nil)then -- table.remove(lastSeenPassengers,0) -- end -- table.insert(lastSeenPassengers,{train.x,train.y}) -- end nearestNewPassenger = lowestDistance(passengers,train) if(train.passenger ~= nil)then if((distance(nearestNewPassenger.destX,train.x,nearestNewPassenger.destY,train.y)+2 < distance(train.passenger.destX,train.x,train.passenger.destY,train.y)) or loop==true)then dropPassenger(train) end end while(lastSeenPassengers[1]==nil and #lastSeenPassengers~=0)do table.remove(lastSeenPassengers,1) --print(#lastSeenPassengers) end i=1 while(i<=#passengers)do if(train.passenger==nil and passengers[i]==nearestNewPassenger)then --do nothing else pas = {["destX"] = passengers[i].destX, ["destY"] = passengers[i].destY, ["x"] = train.x, ["y"] = train.y} --print(pas) if(arrayContains(lastSeenPassengers,pas)==false)then table.insert(lastSeenPassengers,pas) end --print(#lastSeenPassengers) end i=i+1 end return nearestNewPassenger end function ai.foundDestination(train) dropPassenger(train) end function ai.enoughMoney() if(lastSeenPassengers[1]~=nil)then buyTrain(lastSeenPassengers[1][x], lastSeenPassengers[1][y]) else buyTrain(1,3) end buyTrain(1,1) end function distance(x1,y1,x2,y2) if(x1==nil or x2==nil or y1==nil or y2==nil)then return -1 end res = (sqrt((x1-y1)^2+(x2-y2)^2))*0.2+(math.abs(x1-y1)+math.abs(x2-y2))*0.8 return res end function lowestDistance(passengers,train) act=nil dist=1000 i=1 if(passengers[1]==nil and #passengers>1)then print("Fehleralarm") elseif(passengers[1]==nil and #passengers>0)then print("Fehler: passengers in lowest distance has nil as first element") elseif(#passengers>0 and passengers[1].x == nil)then while(i<=#passengers)do d=distance(train.x,passengers[i].destX,train.y,passengers[i].destY) if(d=math.abs(diffy) and contains(dircetions,"W") bol2 = diffy>=0 and (math.abs(diffx)<=diffy or not(bol4)) and contains(dircetions,"S") bol3 = diffy<=0 and (math.abs(diffx)<=-diffy or not(bol4)) and contains(dircetions,"N") if(loop==false)then if(diffx>=0 and (diffx>=math.abs(diffy) or (math.abs(diffx)<=diffy or not(bol2 or bol3))) and contains(dircetions,"E")) then return "E" elseif(bol2) then return "S" elseif(bol3) then return "N" elseif(bol4) then return "W" else print("fail") end else if(bol2) then return "N" elseif(bol3) then return "S" elseif(bol4) then return "E" else return "W" end end print("error detected") end function removeSelf(table1,element) if(element~=nil)then i=1 while(i<=#table1)do if(table1[i]==element)then table.remove(table1, i) break end i=i+1 end end end function arrayContains(table2,element) --print("length of table2 in arrayContains: "..#table2) if(element~=nil)then i=1 while(i <= #table2)do if(table2[i]==element)then return true end i=i+1 end end return false end function arrayContainsAmount(table3,tableObject) --print("length of table2 in arrayContains: "..#table2) amount1=0 if(tableObject~=nil)then i=1 while(i <= #table3)do if(table3[i][1]==tableObject[1] and table3[i][2]==tableObject[2])then amount1=amount1+1 end i=i+1 end end return amount1 end