public static AbstractActor SpawnAmbushMech(Team team, Lance ambushLance, Vector3 ambushOrigin, Vector3 spawnPos, Quaternion spawnRot, List <MechAndPilotDef> spawnPool)
        {
            // Randomly determine one of the spawnpairs from the current ambushdef
            List <MechAndPilotDef> shuffledSpawns = new List <MechAndPilotDef>();

            shuffledSpawns.AddRange(spawnPool);
            shuffledSpawns.Shuffle();

            MechAndPilotDef ambushDef = shuffledSpawns[0];

            PilotDef pilotDef = ModState.Combat.DataManager.PilotDefs.Get(ambushDef.PilotDefId);
            MechDef  mechDef  = ModState.Combat.DataManager.MechDefs.Get(ambushDef.MechDefId);

            mechDef.Refresh();

            // Adjust position so we don't spawn in the ground.
            spawnPos.y = ModState.Combat.MapMetaData.GetLerpedHeightAt(spawnPos, true);

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

            Mech mech = ActorFactory.CreateMech(mechDef, pilotDef, team.EncounterTags, ModState.Combat, team.GetNextSupportUnitGuid(), "", null);

            mech.Init(spawnPos, spawnRotation.eulerAngles.y, true);
            mech.InitGameRep(null);
            Mod.Log.Debug?.Write($"Spawned mech {CombatantUtils.Label(mech)} at position: {spawnPos}");

            if (mech == null)
            {
                Mod.Log.Error?.Write($"Failed to spawn mechDefId: {ambushDef.MechDefId} / pilotDefId: {ambushDef.PilotDefId} !");
            }

            Mod.Log.Debug?.Write($" Spawned ambush mech, adding to team: {team} and lance: {ambushLance}");
            team.AddUnit(mech);
            mech.AddToTeam(team);
            mech.AddToLance(ambushLance);

            mech.BehaviorTree = BehaviorTreeFactory.MakeBehaviorTree(ModState.Combat.BattleTechGame, mech, BehaviorTreeIDEnum.CoreAITree);
            Mod.Log.Debug?.Write("Enabled mech behavior tree");

            UnitSpawnedMessage message = new UnitSpawnedMessage("CJ_MECH", mech.GUID);

            ModState.Combat.MessageCenter.PublishMessage(message);

            return(mech);
        }
        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);
        }
Exemplo n.º 3
0
        public void TestCreateTree()
        {
            var config = new Config
            {
                Name = "selector",
                Id   = 1,
                Args = new List <string> {
                    "11"
                },
                SubConfigs = new List <Config>
                {
                    new Config
                    {
                        Name = "sequence",
                        Id   = 2,
                        Args = new List <string> {
                            "12"
                        },
                        SubConfigs = new List <Config>
                        {
                            new Config
                            {
                                Name = "selector",
                                Id   = 4,
                                Args = new List <string> {
                                    "14"
                                },
                            },

                            new Config
                            {
                                Name = "selector",
                                Id   = 5,
                                Args = new List <string> {
                                    "15", "17"
                                },
                            }
                        }
                    },

                    new Config
                    {
                        Name = "not",
                        Id   = 3,
                        Args = new List <string> {
                            "13"
                        },
                        SubConfigs = new List <Config>
                        {
                            new Config
                            {
                                Name = "selector",
                                Id   = 6,
                                Args = new List <string> {
                                    "16"
                                },
                            }
                        }
                    }
                }
            };

            var behaviorTreeFactory = new BehaviorTreeFactory();

            BehaviorTree.BehaviorTree behaviorTree = behaviorTreeFactory.CreateTree(config);
            var blackBoard = new BlackBoard();

            Assert.IsTrue(behaviorTree.Run(blackBoard));
        }
Exemplo n.º 4
0
 protected override TBTActionPrioritizedSelector GetDecisionTree()
 {
     return(BehaviorTreeFactory.GetDecisionTree());
 }