Exemple #1
0
 public static Vector3 Get(TurretDef def)
 {
     return(Get(
                identifier: def.Description.Id,
                tags: def.TurretTags,
                type: typeof(Turret),
                prefab: def.Chassis.PrefabBase));
 }
 public static string getOriginal(this TurretDef def)
 {
     if (originals.TryGetValue(def.GetType(), out Dictionary <string, string> origs) == false)
     {
         return(def.ToJSON());
     }
     if (origs.TryGetValue(def.Description.Id, out string result) == false)
     {
         return(def.ToJSON());
     }
     return(result);
 }
 public static void setOriginal(this TurretDef def, string json)
 {
     if (originals.TryGetValue(def.GetType(), out Dictionary <string, string> origs) == false)
     {
         origs = new Dictionary <string, string>();
         originals.Add(def.GetType(), origs);
     }
     if (origs.ContainsKey(def.Description.Id))
     {
         origs[def.Description.Id] = json;
     }
     else
     {
         origs.Add(def.Description.Id, json);
     }
 }
            public static bool Prefix(UnitSpawnPointGameLogic __instance, SpawnableUnit spawnableUnit, MechDef ___mechDefOverride, ref PilotDef ___pilotDefOverride, VehicleDef ___vehicleDefOverride, TurretDef ___turretDefOverride)
            {
                if (ModState.PlayerGetsAIMechs && __instance.team == TeamDefinition.Player1TeamDefinitionGuid && spawnableUnit.unitType == UnitType.Mech)
                {
                    ModInit.modLog.LogMessage(
                        $"PLAYER UNIT: First mech in AIMechVariants was {ModState.AIMechs.First().mechDef.Name} with count {ModState.AIMechs.First().count}");
                    var AIMechVariants = ModState.AIMechs.OrderBy(x => x.count).ToList();
                    ModInit.modLog.LogMessage(
                        $"PLAYER UNIT: Filtered to match mechdef IDs and reordered! First mech in AIMechVariants is now {AIMechVariants.First().mechDef.Name} with count {AIMechVariants.First().count}");
                    ModState.AIMechs.First().count += 1;
                    var newMechDef = AIMechVariants.FirstOrDefault()?.mechDef;
                    newMechDef.DependenciesLoaded(1000U);

                    ___mechDefOverride = newMechDef;
                    ModInit.modLog.LogMessage(
                        $"PLAYER UNIT: mechDefOverride set to {___mechDefOverride.Description.Id}");

                    ___pilotDefOverride = spawnableUnit.Pilot;
                    ModInit.modLog.LogMessage(
                        $"PLAYER UNIT: pilotDefOverride set to {___pilotDefOverride.Description.Id}");

                    __instance.pilotDefId = spawnableUnit.PilotId;
                    ModInit.modLog.LogMessage(
                        $"PLAYER UNIT: __instance.pilotDefId is {__instance.pilotDefId}, should be {___pilotDefOverride.Description.Id}");

                    __instance.mechDefId = newMechDef.Description.Id;
                    ModInit.modLog.LogMessage(
                        $"PLAYER UNIT: __instance.mechDefId is {__instance.mechDefId}, should be {___mechDefOverride.Description.Id}");

                    __instance.unitType     = spawnableUnit.unitType;
                    ___vehicleDefOverride   = spawnableUnit.VUnit;
                    ___turretDefOverride    = spawnableUnit.TUnit;
                    __instance.vehicleDefId = spawnableUnit.UnitId;
                    __instance.turretDefId  = spawnableUnit.UnitId;
                    return(false);
                }

                return(true);
            }
        public static AbstractActor SpawnAmbushTurret(Team team, Lance ambushLance, BattleTech.Building building, Vector3 ambushOrigin)
        {
            // Randomly determine one of the spawnpairs from the current ambushdef
            List <TurretAndPilotDef> shuffledSpawns = new List <TurretAndPilotDef>();

            shuffledSpawns.AddRange(ModState.InfantryAmbushDefForContract.SpawnPool);
            shuffledSpawns.Shuffle();
            TurretAndPilotDef ambushDef = shuffledSpawns[0];

            PilotDef  pilotDef  = ModState.Combat.DataManager.PilotDefs.Get(ambushDef.PilotDefId);
            TurretDef turretDef = ModState.Combat.DataManager.TurretDefs.GetOrCreate(ambushDef.TurretDefId);

            turretDef.Refresh();

            // determine a position somewhere up the building's axis
            EncounterLayerData encounterLayerData = ModState.Combat.EncounterLayerData;
            Point cellPoint = new Point(
                ModState.Combat.MapMetaData.GetXIndex(building.CurrentPosition.x),
                ModState.Combat.MapMetaData.GetZIndex(building.CurrentPosition.z));
            MapEncounterLayerDataCell melDataCell =
                encounterLayerData.mapEncounterLayerDataCells[cellPoint.Z, cellPoint.X];
            float buildingHeight = melDataCell.GetBuildingHeight();

            float terrainHeight = ModState.Combat.MapMetaData.GetLerpedHeightAt(building.CurrentPosition, true);
            float heightDelta   = (buildingHeight - terrainHeight) * 0.7f;
            float adjustedY     = terrainHeight + heightDelta;

            Mod.Log.Debug?.Write($"At building position, terrain height is: {terrainHeight} while buildingHeight is: {buildingHeight}. " +
                                 $" Calculated 70% of building height + terrain as {adjustedY}.");

            Vector3 newPosition = building.GameRep.transform.position;

            newPosition.y = adjustedY;
            Mod.Log.Debug?.Write($"Changing transform position from: {building.GameRep.transform.position} to {newPosition}");

            /// Rotate to face the ambush origin
            Vector3    spawnDirection = Vector3.RotateTowards(building.CurrentRotation.eulerAngles, ambushOrigin, 1f, 0f);
            Quaternion spawnRotation  = Quaternion.LookRotation(spawnDirection);

            // Create the turret
            Turret turret = ActorFactory.CreateTurret(turretDef, pilotDef, team.EncounterTags, ModState.Combat, team.GetNextSupportUnitGuid(), "", null);

            turret.Init(newPosition, spawnRotation.eulerAngles.y, true);
            turret.InitGameRep(null);

            if (turret == null)
            {
                Mod.Log.Error?.Write($"Failed to spawn turretDefId: {ambushDef.TurretDefId} + pilotDefId: {ambushDef.PilotDefId} !");
            }

            Mod.Log.Debug?.Write($" Spawned trap turret, adding to team.");
            team.AddUnit(turret);
            turret.AddToTeam(team);
            turret.AddToLance(ambushLance);

            turret.BehaviorTree = BehaviorTreeFactory.MakeBehaviorTree(ModState.Combat.BattleTechGame, turret, BehaviorTreeIDEnum.CoreAITree);
            Mod.Log.Debug?.Write("Updated turret behaviorTree");

            ModState.AmbushBuildingGUIDToTurrets.Add(building.GUID, turret);
            ModState.AmbushTurretGUIDtoBuilding.Add(turret.GUID, building);

            // Associate the building withe the team
            building.AddToTeam(team);
            building.BuildingRep.IsTargetable = true;
            building.BuildingRep.SetHighlightColor(ModState.Combat, team);
            building.BuildingRep.RefreshEdgeCache();

            // Increase the building's health to the current value + turret structure
            float combinedStructure = (float)Math.Ceiling(building.CurrentStructure + turret.GetCurrentStructure(BuildingLocation.Structure));

            Mod.Log.Debug?.Write($"Setting ambush structure to: {combinedStructure} = building.currentStructure: {building.CurrentStructure} + " +
                                 $"turret.currentStructure: {turret.GetCurrentStructure(BuildingLocation.Structure)}");
            building.StatCollection.Set <float>("Structure", combinedStructure);

            // Finally notify others
            UnitSpawnedMessage message = new UnitSpawnedMessage("CJ_TRAP", turret.GUID);

            ModState.Combat.MessageCenter.PublishMessage(message);

            // Finally force the turret to be fully visible
            turret.OnPlayerVisibilityChanged(VisibilityLevel.LOSFull);

            return(turret);
        }