ai_new.c
changeset 900 27eb21ced433
parent 826 fff56bbc3606
child 919 544f374ee392
equal deleted inserted replaced
899:69db9cb6bc9d 900:27eb21ced433
   743 
   743 
   744 	p->ainew.depot_tile = 0;
   744 	p->ainew.depot_tile = 0;
   745 
   745 
   746 	for (i=2;i<p->ainew.path_info.route_length-2;i++) {
   746 	for (i=2;i<p->ainew.path_info.route_length-2;i++) {
   747 		tile = p->ainew.path_info.route[i];
   747 		tile = p->ainew.path_info.route[i];
   748 		for (j=0;j<lengthof(_tileoffs_by_dir);j++) {
   748 		for (j = 0; j < 4; j++) {
   749 			if (IS_TILETYPE(tile + _tileoffs_by_dir[j], MP_STREET)) {
   749 			if (IS_TILETYPE(tile + TileOffsByDir(j), MP_STREET)) {
   750 				// Its a street, test if it is a depot
   750 				// Its a street, test if it is a depot
   751 				if (_map5[tile + _tileoffs_by_dir[j]] & 0x20) {
   751 				if (_map5[tile + TileOffsByDir(j)] & 0x20) {
   752 					// We found a depot, is it ours? (TELL ME!!!)
   752 					// We found a depot, is it ours? (TELL ME!!!)
   753 					if (_map_owner[tile + _tileoffs_by_dir[j]] == _current_player) {
   753 					if (_map_owner[tile + TileOffsByDir(j)] == _current_player) {
   754 						// Now, is it pointing to the right direction.........
   754 						// Now, is it pointing to the right direction.........
   755 						if ((_map5[tile + _tileoffs_by_dir[j]] & 3) == (j ^ 2)) {
   755 						if ((_map5[tile + TileOffsByDir(j)] & 3) == (j ^ 2)) {
   756 							// Yeah!!!
   756 							// Yeah!!!
   757 							p->ainew.depot_tile = tile + _tileoffs_by_dir[j];
   757 							p->ainew.depot_tile = tile + TileOffsByDir(j);
   758 							p->ainew.depot_direction = j ^ 2; // Reverse direction
   758 							p->ainew.depot_direction = j ^ 2; // Reverse direction
   759 							p->ainew.state = AI_STATE_VERIFY_ROUTE;
   759 							p->ainew.state = AI_STATE_VERIFY_ROUTE;
   760 							return;
   760 							return;
   761 						}
   761 						}
   762 					}
   762 					}
   779 			continue;
   779 			continue;
   780 		}
   780 		}
   781 
   781 
   782 		tile = p->ainew.path_info.route[i];
   782 		tile = p->ainew.path_info.route[i];
   783 
   783 
   784 		for (j=0;j<lengthof(_tileoffs_by_dir);j++) {
   784 		for (j = 0; j < 4; j++) {
   785 			// It may not be placed on the road/rail itself
   785 			// It may not be placed on the road/rail itself
   786 			// And because it is not build yet, we can't see it on the tile..
   786 			// And because it is not build yet, we can't see it on the tile..
   787 			// So check the surrounding tiles :)
   787 			// So check the surrounding tiles :)
   788 			if (tile + _tileoffs_by_dir[j] == p->ainew.path_info.route[i-1] ||
   788 			if (tile + TileOffsByDir(j) == p->ainew.path_info.route[i-1] ||
   789 				tile + _tileoffs_by_dir[j] == p->ainew.path_info.route[i+1]) continue;
   789 					tile + TileOffsByDir(j) == p->ainew.path_info.route[i+1]) continue;
   790 			// Not around a bridge?
   790 			// Not around a bridge?
   791 			if (p->ainew.path_info.route_extra[i] != 0) continue;
   791 			if (p->ainew.path_info.route_extra[i] != 0) continue;
   792 			if (IS_TILETYPE(tile, MP_TUNNELBRIDGE)) continue;
   792 			if (IS_TILETYPE(tile, MP_TUNNELBRIDGE)) continue;
   793 			// Is the terrain clear?
   793 			// Is the terrain clear?
   794 			if (IS_TILETYPE(tile + _tileoffs_by_dir[j], MP_CLEAR) ||
   794 			if (IS_TILETYPE(tile + TileOffsByDir(j), MP_CLEAR) ||
   795 				IS_TILETYPE(tile + _tileoffs_by_dir[j], MP_TREES)) {
   795 					IS_TILETYPE(tile + TileOffsByDir(j), MP_TREES)) {
   796 				TileInfo ti;
   796 				TileInfo ti;
   797 				FindLandscapeHeightByTile(&ti, tile);
   797 				FindLandscapeHeightByTile(&ti, tile);
   798 				// If the current tile is on a slope (tileh != 0) then we do not allow this
   798 				// If the current tile is on a slope (tileh != 0) then we do not allow this
   799 				if (ti.tileh != 0) continue;
   799 				if (ti.tileh != 0) continue;
   800 				// Check if everything went okay..
   800 				// Check if everything went okay..
   801 				r = AiNew_Build_Depot(p, tile + _tileoffs_by_dir[j], j ^ 2, 0);
   801 				r = AiNew_Build_Depot(p, tile + TileOffsByDir(j), j ^ 2, 0);
   802 				if (r == CMD_ERROR) continue;
   802 				if (r == CMD_ERROR) continue;
   803 				// Found a spot!
   803 				// Found a spot!
   804 				p->ainew.new_cost += r;
   804 				p->ainew.new_cost += r;
   805 				p->ainew.depot_tile = tile + _tileoffs_by_dir[j];
   805 				p->ainew.depot_tile = tile + TileOffsByDir(j);
   806 				p->ainew.depot_direction = j ^ 2; // Reverse direction
   806 				p->ainew.depot_direction = j ^ 2; // Reverse direction
   807 				p->ainew.state = AI_STATE_VERIFY_ROUTE;
   807 				p->ainew.state = AI_STATE_VERIFY_ROUTE;
   808 				return;
   808 				return;
   809 			}
   809 			}
   810 		}
   810 		}
   982         	int dir1, dir2, dir3;
   982         	int dir1, dir2, dir3;
   983         	TileIndex tile;
   983         	TileIndex tile;
   984         	int i, r;
   984         	int i, r;
   985         	for (i=0;i<2;i++) {
   985         	for (i=0;i<2;i++) {
   986             	if (i == 0) {
   986             	if (i == 0) {
   987             		tile = p->ainew.from_tile + _tileoffs_by_dir[p->ainew.from_direction];
   987             		tile = p->ainew.from_tile + TileOffsByDir(p->ainew.from_direction);
   988             		dir1 = p->ainew.from_direction - 1;
   988             		dir1 = p->ainew.from_direction - 1;
   989             		if (dir1 < 0) dir1 = 3;
   989             		if (dir1 < 0) dir1 = 3;
   990             		dir2 = p->ainew.from_direction + 1;
   990             		dir2 = p->ainew.from_direction + 1;
   991             		if (dir2 > 3) dir2 = 0;
   991             		if (dir2 > 3) dir2 = 0;
   992             		dir3 = p->ainew.from_direction;
   992             		dir3 = p->ainew.from_direction;
   993             	} else {
   993             	} else {
   994             		tile = p->ainew.to_tile + _tileoffs_by_dir[p->ainew.to_direction];
   994             		tile = p->ainew.to_tile + TileOffsByDir(p->ainew.to_direction);
   995             		dir1 = p->ainew.to_direction - 1;
   995             		dir1 = p->ainew.to_direction - 1;
   996             		if (dir1 < 0) dir1 = 3;
   996             		if (dir1 < 0) dir1 = 3;
   997             		dir2 = p->ainew.to_direction + 1;
   997             		dir2 = p->ainew.to_direction + 1;
   998             		if (dir2 > 3) dir2 = 0;
   998             		if (dir2 > 3) dir2 = 0;
   999             		dir3 = p->ainew.to_direction;
   999             		dir3 = p->ainew.to_direction;
  1000             	}
  1000             	}
  1001 
  1001 
  1002             	r = DoCommandByTile(tile, _roadbits_by_dir[dir1], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1002             	r = DoCommandByTile(tile, _roadbits_by_dir[dir1], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1003 				if (r != CMD_ERROR) {
  1003 				if (r != CMD_ERROR) {
  1004 					dir1 = _tileoffs_by_dir[dir1];
  1004 					dir1 = TileOffsByDir(dir1);
  1005 					if (IS_TILETYPE(tile+dir1, MP_CLEAR) || IS_TILETYPE(tile+dir1, MP_TREES)) {
  1005 					if (IS_TILETYPE(tile+dir1, MP_CLEAR) || IS_TILETYPE(tile+dir1, MP_TREES)) {
  1006 						r = DoCommandByTile(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1006 						r = DoCommandByTile(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1007 						if (r != CMD_ERROR) {
  1007 						if (r != CMD_ERROR) {
  1008 							if (IS_TILETYPE(tile+dir1+dir1, MP_CLEAR) || IS_TILETYPE(tile+dir1+dir1, MP_TREES))
  1008 							if (IS_TILETYPE(tile+dir1+dir1, MP_CLEAR) || IS_TILETYPE(tile+dir1+dir1, MP_TREES))
  1009 								DoCommandByTile(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1009 								DoCommandByTile(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1011 					}
  1011 					}
  1012 				}
  1012 				}
  1013 
  1013 
  1014 				r = DoCommandByTile(tile, _roadbits_by_dir[dir2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1014 				r = DoCommandByTile(tile, _roadbits_by_dir[dir2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1015 				if (r != CMD_ERROR) {
  1015 				if (r != CMD_ERROR) {
  1016 					dir2 = _tileoffs_by_dir[dir2];
  1016 					dir2 = TileOffsByDir(dir2);
  1017 					if (IS_TILETYPE(tile+dir2, MP_CLEAR) || IS_TILETYPE(tile+dir2, MP_TREES)) {
  1017 					if (IS_TILETYPE(tile+dir2, MP_CLEAR) || IS_TILETYPE(tile+dir2, MP_TREES)) {
  1018 						r = DoCommandByTile(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1018 						r = DoCommandByTile(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1019 						if (r != CMD_ERROR) {
  1019 						if (r != CMD_ERROR) {
  1020 							if (IS_TILETYPE(tile+dir2+dir2, MP_CLEAR) || IS_TILETYPE(tile+dir2+dir2, MP_TREES))
  1020 							if (IS_TILETYPE(tile+dir2+dir2, MP_CLEAR) || IS_TILETYPE(tile+dir2+dir2, MP_TREES))
  1021 								DoCommandByTile(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1021 								DoCommandByTile(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1023 					}
  1023 					}
  1024 				}
  1024 				}
  1025 
  1025 
  1026 				r = DoCommandByTile(tile, _roadbits_by_dir[dir3^2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1026 				r = DoCommandByTile(tile, _roadbits_by_dir[dir3^2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1027 				if (r != CMD_ERROR) {
  1027 				if (r != CMD_ERROR) {
  1028 					dir3 = _tileoffs_by_dir[dir3];
  1028 					dir3 = TileOffsByDir(dir3);
  1029 					if (IS_TILETYPE(tile+dir3, MP_CLEAR) || IS_TILETYPE(tile+dir3, MP_TREES)) {
  1029 					if (IS_TILETYPE(tile+dir3, MP_CLEAR) || IS_TILETYPE(tile+dir3, MP_TREES)) {
  1030 						r = DoCommandByTile(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1030 						r = DoCommandByTile(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1031 						if (r != CMD_ERROR) {
  1031 						if (r != CMD_ERROR) {
  1032 							if (IS_TILETYPE(tile+dir3+dir3, MP_CLEAR) || IS_TILETYPE(tile+dir3+dir3, MP_TREES))
  1032 							if (IS_TILETYPE(tile+dir3+dir3, MP_CLEAR) || IS_TILETYPE(tile+dir3+dir3, MP_TREES))
  1033 								DoCommandByTile(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1033 								DoCommandByTile(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
  1059 			return;
  1059 			return;
  1060 		}
  1060 		}
  1061 	}
  1061 	}
  1062 
  1062 
  1063 	// There is a bus on the tile we want to build road on... idle till he is gone! (BAD PERSON! :p)
  1063 	// There is a bus on the tile we want to build road on... idle till he is gone! (BAD PERSON! :p)
  1064 	if (!EnsureNoVehicle(p->ainew.depot_tile + _tileoffs_by_dir[p->ainew.depot_direction]))
  1064 	if (!EnsureNoVehicle(p->ainew.depot_tile + TileOffsByDir(p->ainew.depot_direction)))
  1065 		return;
  1065 		return;
  1066 
  1066 
  1067 	res = AiNew_Build_Depot(p, p->ainew.depot_tile, p->ainew.depot_direction, DC_EXEC);
  1067 	res = AiNew_Build_Depot(p, p->ainew.depot_tile, p->ainew.depot_direction, DC_EXEC);
  1068     if (res == CMD_ERROR) {
  1068     if (res == CMD_ERROR) {
  1069 		DEBUG(ai,0)("[AiNew - BuildDepot] Strange but true... depot can not be build!");
  1069 		DEBUG(ai,0)("[AiNew - BuildDepot] Strange but true... depot can not be build!");