Esempio n. 1
0
        /// <summary>
        /// Catch All for vehicle related Things spawned in. Handles GodMode placing of vehicle buildings, corrects immovable spawn locations, and registers air defenses
        /// </summary>
        /// <param name="newThing"></param>
        /// <param name="loc"></param>
        /// <param name="map"></param>
        /// <param name="rot"></param>
        /// <param name="__result"></param>
        /// <param name="wipeMode"></param>
        /// <param name="respawningAfterLoad"></param>
        /// <returns></returns>
        public static bool RegisterThingSpawned(Thing newThing, ref IntVec3 loc, Map map, Rot4 rot, Thing __result, WipeMode wipeMode, bool respawningAfterLoad)
        {
            if (newThing.def is VehicleBuildDef def)
            {
                if (!VehicleMod.settings.debug.debugSpawnVehicleBuildingGodMode && newThing.HitPoints == newThing.MaxHitPoints)
                {
                    VehiclePawn vehiclePawn = VehicleSpawner.GenerateVehicle(def.thingToSpawn, newThing.Faction);                    // (VehiclePawn)PawnGenerator.GeneratePawn(def.thingToSpawn);

                    if (def.soundBuilt != null)
                    {
                        def.soundBuilt.PlayOneShot(new TargetInfo(loc, map, false));
                    }
                    VehiclePawn vehicleSpawned = (VehiclePawn)GenSpawn.Spawn(vehiclePawn, loc, map, rot, WipeMode.FullRefund, false);
                    vehicleSpawned.Rename();
                    __result = vehicleSpawned;
                    AchievementsHelper.TriggerVehicleConstructionEvent(vehicleSpawned);
                    return(false);
                }
            }
            else if (newThing is VehiclePawn vehicle)
            {
                bool standable = true;
                foreach (IntVec3 c in vehicle.PawnOccupiedCells(loc, rot))
                {
                    if (!c.InBounds(map) || (vehicle.IsBoat() ? GenGridVehicles.Impassable(c, map) : GenGrid.Impassable(c, map)))
                    {
                        standable = false;
                        break;
                    }
                }
                bool validator(IntVec3 c)
                {
                    foreach (IntVec3 c2 in vehicle.PawnOccupiedCells(c, rot))
                    {
                        if (vehicle.IsBoat() ? GenGridVehicles.Impassable(c, map) : GenGrid.Impassable(c, map))
                        {
                            return(false);
                        }
                    }
                    return(true);
                }

                if (standable)
                {
                    return(true);
                }
                if (!CellFinder.TryFindRandomCellNear(loc, map, 20, validator, out IntVec3 newLoc, 100))
                {
                    Log.Error($"Unable to find location to spawn {newThing.LabelShort} after 100 attempts. Aborting spawn.");
                    return(false);
                }
                loc = newLoc;
            }
            else if (newThing is Pawn pawn && !pawn.Dead)
            {
                try
                {
                    var positionManager = map.GetCachedMapComponent <VehiclePositionManager>();
                    if (positionManager.PositionClaimed(loc))
                    {
                        VehiclePawn inPlaceVehicle = positionManager.ClaimedBy(loc);
                        CellRect    occupiedRect   = inPlaceVehicle.OccupiedRect().ExpandedBy(1);
                        Rand.PushState();
                        for (int i = 0; i < 3; i++)
                        {
                            IntVec3 newLoc = occupiedRect.EdgeCells.Where(c => GenGrid.InBounds(c, map) && GenGrid.Standable(c, map)).RandomElementWithFallback(inPlaceVehicle.Position);
                            if (occupiedRect.EdgeCells.Contains(newLoc))
                            {
                                loc = newLoc;
                                break;
                            }
                            occupiedRect = occupiedRect.ExpandedBy(1);
                        }
                        Rand.PopState();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error($"Pawn {newThing.Label} could not be readjusted for spawn location. Exception={ex.Message}");
                }
            }
Esempio n. 2
0
 public static bool LocationRestrictedBySize(this VehiclePawn pawn, IntVec3 dest)
 {
     return(CellRect.CenteredOn(dest, pawn.def.Size.x, pawn.def.Size.z).NotNullAndAny(c2 => pawn.IsBoat() ? (!c2.InBoundsShip(pawn.Map) || GenGridVehicles.Impassable(c2, pawn.Map)) :
                                                                                      (!c2.InBounds(pawn.Map) || MultithreadHelper.ImpassableReverseThreaded(c2, pawn.Map, pawn))) &&
            CellRect.CenteredOn(dest, pawn.def.Size.z, pawn.def.Size.x).NotNullAndAny(c2 => pawn.IsBoat()
                                                                                                                                                                                                                          ? (!c2.InBoundsShip(pawn.Map) || GenGridVehicles.Impassable(c2, pawn.Map)) :
                                                                                      (!c2.InBounds(pawn.Map) || MultithreadHelper.ImpassableReverseThreaded(c2, pawn.Map, pawn))));
 }