Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- info.nut
- ==================================================================================================
- class SampleAI extends AIInfo
- {
- function GetAuthor() { return "Newbie AI Writer"; }
- function GetName() { return "SampleAI"; }
- function GetDescription() { return "An example AI by following the wiki tutorial"; }
- function GetVersion() { return 1; }
- function MinVersionToLoad() { return 1; }
- function GetDate() { return "2007-03-17"; }
- function GetShortName() { return "SAAI"; }
- function CreateInstance() { return "SampleAI"; }
- function GetAPIVersion() { return "1.0"; }
- }
- /* Tell the core we are an AI */
- RegisterAI(SampleAI());
- ==================================================================================================
- main.nut
- ==================================================================================================
- import("pathfinder.road", "RoadPathFinder", 3);
- class SampleAI extends AIController {
- }
- function SampleAI::Start()
- {
- /* Get a list of all towns on the map. */
- local townlist = AITownList();
- /* Sort the list by population, highest population first. */
- townlist.Valuate(AITown.GetPopulation);
- townlist.Sort(AIAbstractList.SORT_BY_VALUE, false);
- /* Pick the two towns with the highest population. */
- local townid_a = townlist.Begin();
- local townid_b = townlist.Next();
- /* Print the names of the towns we'll try to connect. */
- AILog.Info("Going to connect " + AITown.GetName(townid_a) + " to " + AITown.GetName(townid_b));
- /* Tell OpenTTD we want to build normal road (no tram tracks). */
- AIRoad.SetCurrentRoadType(AIRoad.ROADTYPE_ROAD);
- /* Create an instance of the pathfinder. */
- local pathfinder = RoadPathFinder();
- /* Give the source and goal tiles to the pathfinder. */
- pathfinder.InitializePath([AITown.GetLocation(townid_a)], [AITown.GetLocation(townid_b)]);
- /* Try to find a path. */
- local path = false;
- local i = 0;
- while (path == false) {
- path = pathfinder.FindPath(100);
- i++;
- if (i >= 100) {
- path = null;
- break;
- }
- SampleAI.Sleep(1);
- AILog.Info("Zzzz");
- }
- if (path == null) {
- /* No path was found. */
- AILog.Warning("Ooops: FindPath returned null");
- AILog.Warning("TODO: Find another town");
- SampleAI.Stop()
- }
- /* If a path was found, build a road over it. */
- while (path != null) {
- local par = path.GetParent();
- if (par != null) {
- local last_node = path.GetTile();
- if (AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) == 1 ) {
- if (!AIRoad.BuildRoad(path.GetTile(), par.GetTile())) {
- /* An error occured while building a piece of road. TODO: handle it.
- * Note that is can also be the case that the road was already build. */
- AILog.Warning("Ooops: BuildRoad returned null");
- }
- } else {
- /* Build a bridge or tunnel. */
- if (!AIBridge.IsBridgeTile(path.GetTile()) && !AITunnel.IsTunnelTile(path.GetTile())) {
- /* If it was a road tile, demolish it first. Do this to work around expended roadbits. */
- if (AIRoad.IsRoadTile(path.GetTile())) AITile.DemolishTile(path.GetTile());
- if (AITunnel.GetOtherTunnelEnd(path.GetTile()) == par.GetTile()) {
- if (!AITunnel.BuildTunnel(AIVehicle.VT_ROAD, path.GetTile())) {
- /* An error occured while building a tunnel. TODO: handle it. */
- }
- } else {
- local bridge_list = AIBridgeList_Length(AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) + 1);
- bridge_list.Valuate(AIBridge.GetMaxSpeed);
- bridge_list.Sort(AIAbstractList.SORT_BY_VALUE, false);
- if (!AIBridge.BuildBridge(AIVehicle.VT_ROAD, bridge_list.Begin(), path.GetTile(), par.GetTile())) {
- /* An error occured while building a bridge. TODO: handle it. */
- }
- }
- }
- }
- }
- path = par;
- }
- AILog.Info("Done");
- }
- function SampleAI::Stop()
- {
- AILog.Error("Panic!"); // :p
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement