Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.nut using 1.1API
- ================
- import("pathfinder.road", "RoadPathFinder", 3);
- class Gamma extends AIController {
- }
- function Gamma::Start()
- {
- /* Globals */
- local version = 0.7.1;
- /* Initialization */
- AILog.Info("Gamma " + version + " by Matthew Brades (The idiot known as OTTDmaster to some, and hated by all)");
- AILog.Info("Thanks go to Lord Aro, Zuu & probably a few others");
- AILog.Info("0.7.1: Reaching for the flyspray");
- AILog.Warning("Initiating start procedure.");
- /* Get a list of all towns on the map. */
- local townlist = AITownList();
- AILog.Info("Townlist...Loaded.");
- /* Sort the list by population, highest population first. */
- townlist.Valuate(AITown.GetPopulation);
- townlist.Sort(AIList.SORT_BY_VALUE, false);
- AILog.Info("Townlist...Sorted.");
- /* Personal stuff. */
- if (!AICompany.SetName("Gamma " + version + "")) {
- local i = 2;
- while (!AICompany.SetName("Gamma " + version + " #" + i)) {
- i = i + 1;
- }
- }
- AILog.Info("Company...Named.");
- if (!AICompany.SetPresidentName("ZirconiumX")) {
- local i = 2;
- while (!AICompany.SetPresidentName("ZirconiumX #" + i)) {
- i = i + 1;
- }
- }
- AILog.Info("President..Named.");
- /* Pick the two towns with the highest population. */
- AILog.Info("Townlist...Done.");
- AILog.Warning("Start Proc...Done.");
- AILog.Info("==========================================");
- AILog.Warning("Starting Main Loop...Done.");
- /* Connect the towns. */
- local townid_a = townlist.Begin();
- local i = 0;
- while (!townlist.IsEnd())
- {
- i = i + 1;
- AILog.Warning("Main Loop...iteration " + i + ".");
- local townid_b = townlist.Next();
- this.BuildRoute(townid_a, townid_b);
- this.BuildRV(townid_a);
- this.BuildRV(townid_b);
- AILog.Info("Checking for events...")
- while (AIEventController.IsEventWaiting()) {
- local e = AIEventController.GetNextEvent();
- switch (e.GetEventType()) {
- case AIEvent.AI_ET_VEHICLE_CRASHED:
- local ec = AIEventVehicleCrashed.Convert(e);
- local v = ec.GetVehicleID();
- AILog.Info("Vehicle (" + v + ") crashed.");
- /* Handle the crashed vehicle */
- /* TODO: Build extra road vehicle */
- break;
- case AIEvent.AI_ET_ENGINE_PREVIEW:
- local ep = AIEventEnginePreview.Convert(e);
- local vn = ep.GetName();
- ep.AcceptPreview;
- AILog.Info("Preview of " + vn + " accepted.");
- break;
- case AIEvent.AI_ET_COMPANY_NEW:
- local cn = AIEventCompanyNew.Convert(e);
- local cid = cn.GetCompanyID();
- AILog.Info("Welcome " + AICompany.GetName(cid) + ".");
- break;
- }
- AILog.Info("Main Loop...Done");
- }
- /* Done. */
- AILog.Info("Finished.");
- AILog.Error("TODO: Standby - and manage events");
- }
- function Gamma::BuildRoute(town_a, town_b)
- {
- /* Print the names of the towns we'll try to connect. */
- AILog.Info(AITown.GetName(town_a) + " to " + AITown.GetName(town_b) + "...Connecting");
- /* Tell OpenTTD we want to build normal road (no tram tracks). */
- AIRoad.SetCurrentRoadType(AIRoad.ROADTYPE_ROAD);
- AILog.Info("Roadtype...Set");
- /* Create an instance of the pathfinder. */
- local pathfinder = RoadPathFinder();
- AILog.Info("Path...Instanced");
- /* Set the cost for making a turn extreme high. */
- pathfinder.cost.turn = 5000;
- AILog.Info("Path...Costed");
- /* Give the source and goal tiles to the pathfinder. */
- pathfinder.InitializePath([AITown.GetLocation(town_a)], [AITown.GetLocation(town_b)]);
- AILog.Info("Path...Initialized");
- /* Try to find a path. */
- local path = false;
- while (path == false) {
- path = pathfinder.FindPath(100);
- this.Sleep(1);
- }
- if (path == null) {
- /* No path was found. */
- AILog.Error("Path...Not Found");
- }
- /* If a path was found, build a road over it. */
- AILog.Info("Path...Found");
- 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("Path...Error during road construction");
- }
- } else {
- /* Build a bridge or tunnel. */
- AILog.Warning("Path...Building 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. */
- AILog.Warning("Path...Building Tunnel...Failed");
- }
- } else {
- AILog.Warning("Path...Building Bridge");
- local bridge_list = AIBridgeList_Length(AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) + 1);
- bridge_list.Valuate(AIBridge.GetMaxSpeed);
- bridge_list.Sort(AIList.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. */
- switch(AIError.GetLastError()) {
- case AIError.ERR_NOT_ENOUGH_CASH:
- local l_inter = AICompany.GetLoanInterval;
- local l_loan = AICompany.GetLoanAmount;
- AICompany.SetLoanAmount([l_loan + l_inter])
- break;
- default:
- AILog.Warning("Path...Building Bridge...Failed");
- break;
- }
- }
- }
- }
- }
- }
- path = par;
- }
- AILog.Info("Path...Built");
- }
- function Gamma::GetNeighbours4(tile_id)
- {
- /* Borrowed from TownCars */
- local list = AITileList();
- if(!AIMap.IsValidTile(tile_id))
- return list;
- local tile_x = AIMap.GetTileX(tile_id);
- local tile_y = AIMap.GetTileY(tile_id);
- list.AddTile(GetTileRelative(tile_id, -1, 0));
- list.AddTile(GetTileRelative(tile_id, 0, -1));
- list.AddTile(GetTileRelative(tile_id, 1, 0));
- list.AddTile(GetTileRelative(tile_id, 0, 1));
- return list;
- }
- function Gamma::Connectable(road_tile, adjacent_tile_to_connect)
- {
- /* Borrowed from TownCars */
- local neighbours = GetNeighbours4(road_tile);
- neighbours.RemoveTile(adjacent_tile_to_connect);
- // This function requires that road_tile is connected with at least one other road tile.
- neighbours.Valuate(AIRoad.IsRoadTile);
- if(Helper.ListValueSum(neighbours) < 1)
- return false;
- for(local neighbour_tile_id = neighbours.Begin(); neighbours.HasNext(); neighbour_tile_id = neighbours.Next())
- {
- if(AIRoad.IsRoadTile(neighbour_tile_id))
- {
- local ret = AIRoad.CanBuildConnectedRoadPartsHere(road_tile, neighbour_tile_id, adjacent_tile_to_connect);
- if(ret == 0 || ret == -1)
- return false;
- }
- }
- return true;
- }
- function Gamma::BuildRV(number, town)
- {
- /* Get locations of town */
- local loc = AITown.GetLocation(town);
- /* We want road - not tramtracks */
- AIRoad.SetCurrentRoadType(AIRoad.ROADTYPE_ROAD);
- /* Build Bus Stop */
- local neighbours = GetNeighbours4(tile_id);
- neighbours.Valuate(AIRoad.IsRoadTile);
- neighbours.KeepValue(1);
- neighbours.Valuate(CanConnectToRoad, tile_id);
- neighbours.KeepValue(1);
- neighbours.Valuate(Helper.GetMaxHeightOfTile);
- neighbours.KeepValue(Helper.GetMaxHeightOfTile(tile_id));
- /* Return false if none of the neighbours has road */
- if(neighbours.Count() == 0 || neighbours.Count() == 1)
- {
- return false;
- }
- local front = neighbours.Begin();
- local depot_front = neighbours.Next();
- if (!AIRoad.BuildDriveThroughRoadStation(loc, front, ROADVEHTYPE_BUS, AIStation.STATION_JOIN_ADJACENT))
- {
- if (!AIRoad.BuildRoadStation(loc, front, ROADVEHTYPE_BUS, AIStation.STATION_JOIN_ADJACENT))
- {
- AILog.Warning("Build Station...Failed");
- return false;
- }
- }
- /* Build Depot */
- if (!AIRoad.BuildDepot(loc, depot_front))
- {
- AILog.Warning("Build Depot...Failed");
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement