changeset 2682:a1d5c40777b5 draft

(svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch settings, off by defaut). An other step to AIScripts. WARNING: this is still highly experimental and has known bugs!
author truelight <truelight@openttd.org>
date Mon, 21 Nov 2005 14:28:31 +0000
parents 893c54103fbc
children b27231a02ff0
files ai/ai.c ai/ai.h ai/trolly/build.c ai/trolly/pathfinder.c ai/trolly/trolly.c command.c lang/english.txt players.c settings.c settings_gui.c variables.h
diffstat 11 files changed, 144 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/ai/ai.c
+++ b/ai/ai.c
@@ -145,6 +145,14 @@
 	/* Don't do anything if ai is disabled */
 	if (!_ai.enabled) return;
 
+	/* Don't do anything if we are a network-client
+	 *  (too bad when a client joins, he thinks the AIs are real, so it wants to control
+	 *   them.. this avoids that, while loading a network game in singleplayer, does make
+	 *   the AIs to continue ;))
+	 */
+	if (_networking && !_network_server && !_ai.network_client)
+		return;
+
 	/* New tick */
 	_ai.tick++;
 
--- a/ai/ai.h
+++ b/ai/ai.h
@@ -2,6 +2,7 @@
 #define AI_H
 
 #include "../functions.h"
+#include "../network.h"
 
 /* How DoCommands look like for an AI */
 typedef struct AICommand {
@@ -44,6 +45,37 @@
 void AI_Uninitialize(void);
 int32 AI_DoCommand(uint tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
 
+/** Is it allowed to start a new AI.
+ * This function checks some boundries to see if we should launch a new AI.
+ * @return True if we can start a new AI.
+ */
+static inline bool AI_AllowNewAI(void)
+{
+	/* If disabled, no AI */
+	if (!_ai.enabled)
+		return false;
+
+	/* If in network, but no server, no AI */
+	if (_networking && !_network_server)
+		return false;
+
+	/* If in network, and server, possible AI */
+	if (_networking && _network_server) {
+		/* Do we want AIs in multiplayer? */
+		if (!_patches.ai_in_multiplayer)
+			return false;
+
+		/* Only the NewAI is allowed... sadly enough the old AI just doesn't support this
+		 *  system, because all commands are delayed by at least 1 tick, which causes
+		 *  a big problem, because it uses variables that are only set AFTER the command
+		 *  is really executed... */
+		if (!_patches.ainew_active)
+			return false;
+	}
+
+	return true;
+}
+
 #define AI_CHANCE16(a,b)    ((uint16)     AI_Random()  <= (uint16)((65536 * a) / b))
 #define AI_CHANCE16R(a,b,r) ((uint16)(r = AI_Random()) <= (uint16)((65536 * a) / b))
 
--- a/ai/trolly/build.c
+++ b/ai/trolly/build.c
@@ -11,15 +11,16 @@
 #include "../../engine.h"
 #include "../../station.h"
 #include "../../variables.h"
+#include "../ai.h"
 
 // Build HQ
 //  Params:
 //    tile : tile where HQ is going to be build
 bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile)
 {
-	if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ)))
+	if (CmdFailed(AI_DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ)))
 		return false;
-	DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
+	AI_DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
 	return true;
 }
 
@@ -35,12 +36,12 @@
 int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
 {
 	if (type == AI_TRAIN)
-		return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
+		return AI_DoCommand(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
 
 	if (type == AI_BUS)
-		return DoCommandByTile(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
+		return AI_DoCommand(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
 
-	return DoCommandByTile(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
+	return AI_DoCommand(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
 }
 
 
@@ -69,9 +70,9 @@
 
 	// Now, simply, build the bridge!
 	if (p->ainew.tbt == AI_TRAIN)
-		return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
+		return AI_DoCommand(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
 
-	return DoCommandByTile(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
+	return AI_DoCommand(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
 }
 
 
@@ -104,7 +105,7 @@
 	if (PathFinderInfo->rail_or_road) {
 		// Tunnel code
 		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
-			cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
+			cost += AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL);
 			PathFinderInfo->position++;
 			// TODO: problems!
 			if (CmdFailed(cost)) {
@@ -135,7 +136,7 @@
 				if (old_dir != -1 && old_dir != dir) break;
 				old_dir = dir;
 				// Build the tile
-				res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
+				res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
 				if (CmdFailed(res)) {
 					// Problem.. let's just abort it all!
 					p->ainew.state = AI_STATE_NOTHING;
@@ -154,7 +155,7 @@
 	} else {
 		// Tunnel code
 		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
-			cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
+			cost += AI_DoCommand(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL);
 			PathFinderInfo->position++;
 			// TODO: problems!
 			if (CmdFailed(cost)) {
@@ -189,7 +190,7 @@
 				// There is already some road, and it is a bridge.. don't build!!!
 				if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
 					// Build the tile
-					res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
+					res = AI_DoCommand(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
 					// Currently, we ignore CMD_ERRORs!
 					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) {
 						// Problem.. let's just abort it all!
@@ -234,7 +235,7 @@
 			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
 			if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
 			// Can we build it?
-			ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
+			ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
 			if (!CmdFailed(ret)) break;
 		}
 		// We did not find a vehicle :(
@@ -252,7 +253,7 @@
 
 	if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
 
-	return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
+	return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
 }
 
 int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag)
@@ -260,12 +261,12 @@
 	static const byte _roadbits_by_dir[4] = {2,1,8,4};
 	int ret, ret2;
 	if (p->ainew.tbt == AI_TRAIN)
-		return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
+		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
 
-	ret = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
+	ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
 	if (CmdFailed(ret)) return ret;
 	// Try to build the road from the depot
-	ret2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
+	ret2 = AI_DoCommand(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
 	// If it fails, ignore it..
 	if (CmdFailed(ret2)) return ret;
 	return ret + ret2;
--- a/ai/trolly/pathfinder.c
+++ b/ai/trolly/pathfinder.c
@@ -10,6 +10,7 @@
 #include "trolly.h"
 #include "../../depot.h"
 #include "../../variables.h"
+#include "../ai.h"
 
 #define TEST_STATION_NO_DIR 0xFF
 
@@ -271,7 +272,7 @@
 				if (PathFinderInfo->rail_or_road) {
 					// Rail check
 					dir = AiNew_GetRailDirection(current->path.parent->node.tile, ctile, atile);
-					ret = DoCommandByTile(ctile, 0, dir, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
+					ret = AI_DoCommand(ctile, 0, dir, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
 					if (CmdFailed(ret)) continue;
 #ifdef AI_PATHFINDER_NO_90DEGREES_TURN
 					if (current->path.parent->parent != NULL) {
@@ -301,7 +302,7 @@
 					}
 					// Only destruct things if it is MP_CLEAR of MP_TREES
 					if (dir != 0) {
-						ret = DoCommandByTile(ctile, dir, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
+						ret = AI_DoCommand(ctile, dir, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
 						if (CmdFailed(ret)) continue;
 					}
 				}
@@ -340,7 +341,7 @@
 				if (TILES_BETWEEN(new_tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) break;
 
 				// Try building the bridge..
-				ret = DoCommandByTile(tile, new_tile, (0 << 8) + (MAX_BRIDGES / 2), DC_AUTO, CMD_BUILD_BRIDGE);
+				ret = AI_DoCommand(tile, new_tile, (0 << 8) + (MAX_BRIDGES / 2), DC_AUTO, CMD_BUILD_BRIDGE);
 				if (CmdFailed(ret)) continue;
 				// We can build a bridge here.. add him to the neighbours
 				aystar->neighbours[aystar->num_neighbours].tile = new_tile;
@@ -359,7 +360,7 @@
 				(dir == 2 && ti.tileh == 3) ||
 				(dir == 3 && ti.tileh == 9)) {
 			// Now simply check if a tunnel can be build
-			ret = DoCommandByTile(tile, (PathFinderInfo->rail_or_road?0:0x200), 0, DC_AUTO, CMD_BUILD_TUNNEL);
+			ret = AI_DoCommand(tile, (PathFinderInfo->rail_or_road?0:0x200), 0, DC_AUTO, CMD_BUILD_TUNNEL);
 			FindLandscapeHeightByTile(&ti, _build_tunnel_endtile);
 			if (!CmdFailed(ret) && (ti.tileh == 3 || ti.tileh == 6 || ti.tileh == 9 || ti.tileh == 12)) {
 				aystar->neighbours[aystar->num_neighbours].tile = _build_tunnel_endtile;
--- a/ai/trolly/trolly.c
+++ b/ai/trolly/trolly.c
@@ -32,6 +32,7 @@
 #include "../../engine.h"
 #include "../../gui.h"
 #include "../../depot.h"
+#include "../ai.h"
 
 // This function is called after StartUp. It is the init of an AI
 static void AiNew_State_FirstTime(Player *p)
@@ -78,7 +79,7 @@
 {
 	assert(p->ainew.state == AI_STATE_NOTHING);
 	// If we are done idling, start over again
-	if (p->ainew.idle == 0) p->ainew.idle = RandomRange(DAY_TICKS * 2) + DAY_TICKS;
+	if (p->ainew.idle == 0) p->ainew.idle = AI_RandomRange(DAY_TICKS * 2) + DAY_TICKS;
 	if (--p->ainew.idle == 0) {
 		// We are done idling.. what you say? Let's do something!
 		// I mean.. the next tick ;)
@@ -102,7 +103,7 @@
 		// We have no HQ yet, build one on a random place
 		// Random till we found a place for it!
 		// TODO: this should not be on a random place..
-		AiNew_Build_CompanyHQ(p, Random() % MapSize());
+		AiNew_Build_CompanyHQ(p, AI_Random() % MapSize());
 		// Enough for now, but we want to come back here the next time
 		//  so we do not change any status
 		return;
@@ -112,7 +113,7 @@
 
 	// Let's pick an action!
 	if (p->ainew.action == AI_ACTION_NONE) {
-		c = Random() & 0xFF;
+		c = AI_Random() & 0xFF;
 		if (p->current_loan > 0 &&
 				p->old_economy[1].income > AI_MINIMUM_INCOME_FOR_LOAN &&
 				c < 10) {
@@ -222,9 +223,9 @@
 
 		// Check if the rating in a city is high enough
 		//  If not, take a chance if we want to continue
-		if (t->ratings[_current_player] < 0 && CHANCE16(1,4)) return false;
+		if (t->ratings[_current_player] < 0 && AI_CHANCE16(1,4)) return false;
 
-		if (t->max_pass - t->act_pass < AI_CHECKCITY_NEEDED_CARGO && !CHANCE16(1,AI_CHECKCITY_CITY_CHANCE)) return false;
+		if (t->max_pass - t->act_pass < AI_CHECKCITY_NEEDED_CARGO && !AI_CHANCE16(1,AI_CHECKCITY_CITY_CHANCE)) return false;
 
 		// Check if we have build a station in this town the last 6 months
 		//  else we don't do it. This is done, because stat updates can be slow
@@ -259,7 +260,7 @@
 				// The rating is high.. second station...
 				//  a little chance that we still continue
 				//  But if there are 3 stations of this size, we never go on...
-				if (j == 2 && CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
+				if (j == 2 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
 				// We don't like this station :(
 				return false;
 			}
@@ -279,7 +280,7 @@
 		int count = 0;
 		int j = 0;
 
-		if (i->town != NULL && i->town->ratings[_current_player] < 0 && CHANCE16(1,4)) return false;
+		if (i->town != NULL && i->town->ratings[_current_player] < 0 && AI_CHANCE16(1,4)) return false;
 
 		// No limits on delevering stations!
 		//  Or for industry that does not give anything yet
@@ -319,7 +320,7 @@
 				j++;
 				// The rating is high.. a little chance that we still continue
 				//  But if there are 2 stations of this size, we never go on...
-				if (j == 1 && CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
+				if (j == 1 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
 				// We don't like this station :(
 				return false;
 			}
@@ -384,9 +385,9 @@
 		if (p->ainew.temp == -1) {
 			// First, we pick a random spot to search from
 			if (p->ainew.from_type == AI_CITY)
-				p->ainew.temp = RandomRange(_total_towns);
+				p->ainew.temp = AI_RandomRange(_total_towns);
 			else
-				p->ainew.temp = RandomRange(_total_industries);
+				p->ainew.temp = AI_RandomRange(_total_industries);
 		}
 
 		if (!AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.from_type)) {
@@ -419,9 +420,9 @@
 	if (p->ainew.temp == -1) {
 		// First, we pick a random spot to search to
 		if (p->ainew.to_type == AI_CITY)
-			p->ainew.temp = RandomRange(_total_towns);
+			p->ainew.temp = AI_RandomRange(_total_towns);
 		else
-			p->ainew.temp = RandomRange(_total_industries);
+			p->ainew.temp = AI_RandomRange(_total_industries);
 	}
 
 	// The same city is not allowed
@@ -993,7 +994,7 @@
 		p->ainew.state = AI_STATE_NOTHING;
 		// If the first station _was_ build, destroy it
 		if (p->ainew.temp != 0)
-			DoCommandByTile(p->ainew.from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
+			AI_DoCommand(p->ainew.from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 		return;
 	}
 	p->ainew.temp++;
@@ -1050,38 +1051,38 @@
 					dir3 = p->ainew.to_direction;
 				}
 
-				ret = DoCommandByTile(tile, _roadbits_by_dir[dir1], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+				ret = AI_DoCommand(tile, _roadbits_by_dir[dir1], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 				if (!CmdFailed(ret)) {
 					dir1 = TileOffsByDir(dir1);
 					if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) {
-						ret = DoCommandByTile(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+						ret = AI_DoCommand(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 						if (!CmdFailed(ret)) {
 							if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES))
-								DoCommandByTile(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+								AI_DoCommand(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 						}
 					}
 				}
 
-				ret = DoCommandByTile(tile, _roadbits_by_dir[dir2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+				ret = AI_DoCommand(tile, _roadbits_by_dir[dir2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 				if (!CmdFailed(ret)) {
 					dir2 = TileOffsByDir(dir2);
 					if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) {
-						ret = DoCommandByTile(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+						ret = AI_DoCommand(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 						if (!CmdFailed(ret)) {
 							if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES))
-								DoCommandByTile(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+								AI_DoCommand(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 						}
 					}
 				}
 
-				ret = DoCommandByTile(tile, _roadbits_by_dir[dir3^2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+				ret = AI_DoCommand(tile, _roadbits_by_dir[dir3^2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 				if (!CmdFailed(ret)) {
 					dir3 = TileOffsByDir(dir3);
 					if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) {
-						ret = DoCommandByTile(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+						ret = AI_DoCommand(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 						if (!CmdFailed(ret)) {
 							if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES))
-								DoCommandByTile(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
+								AI_DoCommand(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
 						}
 					}
 				}
@@ -1125,7 +1126,7 @@
 	}
 
 	p->ainew.state = AI_STATE_BUILD_VEHICLE;
-	p->ainew.idle = 1;
+	p->ainew.idle = 10;
 	p->ainew.veh_main_id = (VehicleID)-1;
 }
 
@@ -1160,11 +1161,6 @@
 	p->ainew.cur_veh++;
 	// Decrease the total counter
 	p->ainew.amount_veh--;
-	// Get the new ID
-	if (p->ainew.tbt == AI_TRAIN) {
-	} else {
-		p->ainew.veh_id = _new_roadveh_id;
-	}
 	// Go give some orders!
 	p->ainew.state = AI_STATE_GIVE_ORDERS;
 }
@@ -1178,44 +1174,51 @@
 
 	assert(p->ainew.state == AI_STATE_GIVE_ORDERS);
 
-	if (p->ainew.veh_main_id != (VehicleID)-1) {
-		DoCommandByTile(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER);
+	// Get the new ID
+	/* XXX -- Because this AI isn't using any event-system, this is VERY dangerous!
+	 *  There is no way telling if the vehicle is already bought (or delayed by the
+	 *  network), and if bought, if not an other vehicle is bought in between.. in
+	 *  other words, there is absolutely no way knowing if this id is the true
+	 *  id.. soon this will all change, but for now, we needed something to test
+	 *  on ;) -- TrueLight -- 21-11-2005 */
+	if (p->ainew.tbt == AI_TRAIN) {
+	} else {
+		p->ainew.veh_id = _new_roadveh_id;
+	}
 
-		// Skip the first order if it is a second vehicle
-		//  This to make vehicles go different ways..
-		if (p->ainew.veh_id & 1)
-			DoCommandByTile(0, p->ainew.veh_id, 0, DC_EXEC, CMD_SKIP_ORDER);
+	if (p->ainew.veh_main_id != (VehicleID)-1) {
+		AI_DoCommand(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER);
+
 		p->ainew.state = AI_STATE_START_VEHICLE;
 		return;
 	} else {
 		p->ainew.veh_main_id = p->ainew.veh_id;
 	}
 
-	// When more than 1 vehicle, we send them to different directions
+	// Very handy for AI, goto depot.. but yeah, it needs to be activated ;)
+	if (_patches.gotodepot) {
+		idx = 0;
+		order.type = OT_GOTO_DEPOT;
+		order.flags = OF_UNLOAD;
+		order.station = GetDepotByTile(p->ainew.depot_tile)->index;
+		AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+	}
+
+	idx = 0;
+	order.type = OT_GOTO_STATION;
+	order.flags = 0;
+	order.station = _m[p->ainew.to_tile].m2;
+	if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver)
+		order.flags |= OF_FULL_LOAD;
+	AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+
 	idx = 0;
 	order.type = OT_GOTO_STATION;
 	order.flags = 0;
 	order.station = _m[p->ainew.from_tile].m2;
 	if (p->ainew.tbt == AI_TRUCK && p->ainew.from_deliver)
 		order.flags |= OF_FULL_LOAD;
-	DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
-
-	idx = 1;
-	order.type = OT_GOTO_STATION;
-	order.flags = 0;
-	order.station = _m[p->ainew.to_tile].m2;
-	if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver)
-		order.flags |= OF_FULL_LOAD;
-	DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
-
-	// Very handy for AI, goto depot.. but yeah, it needs to be activated ;)
-	if (_patches.gotodepot) {
-		idx = 2;
-		order.type = OT_GOTO_DEPOT;
-		order.flags = OF_UNLOAD;
-		order.station = GetDepotByTile(p->ainew.depot_tile)->index;
-		DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
-	}
+	AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
 
 	// Start the engines!
 	p->ainew.state = AI_STATE_START_VEHICLE;
@@ -1227,9 +1230,15 @@
 {
 	assert(p->ainew.state == AI_STATE_START_VEHICLE);
 
+	// Skip the first order if it is a second vehicle
+	//  This to make vehicles go different ways..
+	if (p->ainew.cur_veh & 1)
+		AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_SKIP_ORDER);
+
 	// 3, 2, 1... go! (give START_STOP command ;))
-	DoCommandByTile(0, p->ainew.veh_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
+	AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
 	// Try to build an other vehicle (that function will stop building when needed)
+	p->ainew.idle  = 10;
 	p->ainew.state = AI_STATE_BUILD_VEHICLE;
 }
 
@@ -1239,7 +1248,7 @@
 {
 	int i;
 	for (i=0;i<AI_LOAN_REPAY;i++)
-		DoCommandByTile(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN);
+		AI_DoCommand(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN);
 	p->ainew.state = AI_STATE_ACTION_DONE;
 }
 
@@ -1268,7 +1277,7 @@
 				if (v->type == VEH_Road && IsTileDepotType(v->tile, TRANSPORT_ROAD) &&
 						(v->vehstatus&VS_STOPPED)) {
 					// We are at the depot, sell the vehicle
-					DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
+					AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
 				}
 				return;
 			}
@@ -1277,7 +1286,7 @@
 			{
 				int ret = 0;
 				if (v->type == VEH_Road)
-					ret = DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
+					ret = AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
 				// This means we can not find a depot :s
 				//				if (CmdFailed(ret))
 			}
--- a/command.c
+++ b/command.c
@@ -341,10 +341,6 @@
 
 	if (_docommand_recursive == 0) {
 		_error_message = INVALID_STRING_ID;
-		// update last build coord of player
-		if ( (x|y) != 0 && _current_player < MAX_PLAYERS) {
-			GetPlayer(_current_player)->last_build_coordinate = TileVirtXY(x, y);
-		}
 	}
 
 	_docommand_recursive++;
--- a/lang/english.txt
+++ b/lang/english.txt
@@ -1011,6 +1011,7 @@
 STR_CONFIG_PATCHES_AI_BUILDS_SHIPS                              :{LTBLUE}Disable ships for computer: {ORANGE}{STRING1}
 
 STR_CONFIG_PATCHES_AINEW_ACTIVE                                 :{LTBLUE}Enable new AI (alpha): {ORANGE}{STRING1}
+STR_CONFIG_PATCHES_AI_IN_MULTIPLAYER                            :{LTBLUE}Allow AIs in multiplayer (experimental): {ORANGE}{STRING1}
 
 STR_CONFIG_PATCHES_SERVINT_TRAINS                               :{LTBLUE}Default service interval for trains: {ORANGE}{STRING1} days/%
 STR_CONFIG_PATCHES_SERVINT_TRAINS_DISABLED                      :{LTBLUE}Default service interval for trains: {ORANGE}disabled
@@ -1175,6 +1176,7 @@
 
 TEMP_AI_IN_PROGRESS                                             :{WHITE}Welcome to this new AI, a work in progress. You should expect problems. When you happen take a screenshot and post it in the forum. Enjoy!
 TEMP_AI_ACTIVATED                                               :{WHITE}Warning: this new AI is still alpha! Currently, only trucks and busses work!
+TEMP_AI_MULTIPLAYER                                             :{WHITE}Warning: this function is still experimental. Please report any problems with it to truelight@openttd.org.
 
 ############ network gui strings
 
--- a/players.c
+++ b/players.c
@@ -528,12 +528,13 @@
 
 	// when there's a lot of computers in game, the probability that a new one starts is lower
 	if (n < (uint)_opt.diff.max_no_competitors)
-		if (n < (!_network_server ? RandomRange(_opt.diff.max_no_competitors + 2) : InteractiveRandomRange(_opt.diff.max_no_competitors + 2)) )
-		DoStartupNewPlayer(true);
+		if (n < (_network_server ? InteractiveRandomRange(_opt.diff.max_no_competitors + 2) : RandomRange(_opt.diff.max_no_competitors + 2)) )
+			/* Send a command to all clients to start  up a new AI. Works fine for Multiplayer and Singleplayer */
+			DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
 
 	// The next AI starts like the difficulty setting said, with +2 month max
 	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + 1;
-	_next_competitor_start += (!_network_server) ? RandomRange(60 * DAY_TICKS) : InteractiveRandomRange(60 * DAY_TICKS);
+	_next_competitor_start += _network_server ? InteractiveRandomRange(60 * DAY_TICKS) : RandomRange(60 * DAY_TICKS);
 }
 
 void InitializePlayers(void)
@@ -556,9 +557,7 @@
 	_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
 	if (p->name_1 != 0) GenerateCompanyName(p);
 
-	/* XXX -- For now, multiplayer AIs still aren't working, WIP! */
-	//if (_ai.enabled && (!_networking || _network_server) && _game_mode != GM_MENU && !--_next_competitor_start)
-	if (_ai.enabled && !_networking && _game_mode != GM_MENU && !--_next_competitor_start)
+	if (AI_AllowNewAI() && _game_mode != GM_MENU && !--_next_competitor_start)
 		MaybeStartNewPlayer();
 }
 
--- a/settings.c
+++ b/settings.c
@@ -942,6 +942,7 @@
 	{"wait_twoway_signal",	SDT_UINT8,	(void*)41,		&_patches.wait_twoway_signal,		NULL},
 
 	{"ainew_active",				SDT_BOOL,		(void*)false, &_patches.ainew_active,					NULL},
+	{"ai_in_multiplayer",		SDT_BOOL,		(void*)false, &_patches.ai_in_multiplayer,		NULL},
 
 	{"map_x", SDT_UINT32, (void*)8, &_patches.map_x, NULL},
 	{"map_y", SDT_UINT32, (void*)8, &_patches.map_y, NULL},
--- a/settings_gui.c
+++ b/settings_gui.c
@@ -565,6 +565,12 @@
 	return 0;
 }
 
+static int32 Ai_In_Multiplayer_Warning(int32 p1)
+{
+	if (p1 == 1) ShowErrorMessage(-1, TEMP_AI_MULTIPLAYER, 0, 0);
+	return 0;
+}
+
 static int32 PopulationInLabelActive(int32 p1)
 {
 	Town* t;
@@ -753,6 +759,7 @@
 
 static const PatchEntry _patches_ai[] = {
 	{PE_BOOL,		0, STR_CONFIG_PATCHES_AINEW_ACTIVE, "ainew_active", &_patches.ainew_active, 0, 1, 1, &AiNew_PatchActive_Warning},
+	{PE_BOOL,		0, STR_CONFIG_PATCHES_AI_IN_MULTIPLAYER, "ai_in_multiplayer", &_patches.ai_in_multiplayer, 0, 1, 1, &Ai_In_Multiplayer_Warning},
 
 	{PE_BOOL,		0, STR_CONFIG_PATCHES_AI_BUILDS_TRAINS, "ai_disable_veh_train", &_patches.ai_disable_veh_train,			0,  0,  0, NULL},
 	{PE_BOOL,		0, STR_CONFIG_PATCHES_AI_BUILDS_ROADVEH,"ai_disable_veh_roadveh",&_patches.ai_disable_veh_roadveh,		0,  0,  0, NULL},
--- a/variables.h
+++ b/variables.h
@@ -176,6 +176,7 @@
 
 	byte drag_signals_density; // many signals density
 	bool ainew_active;  // Is the new AI active?
+	bool ai_in_multiplayer; // Do we allow AIs in multiplayer
 
 	/*
 	 * New Path Finding