public void DoWork(Colony colony, RoamingJobState toiletState)
        {
            if (colony.OwnerIsOnline())
            {
                if (toiletState.GetActionEnergy(ToiletConstants.CLEAN) > 0 &&
                    toiletState.NextTimeForWork < Pipliz.Time.SecondsSinceStartDouble)
                {
                    if (TimeCycle.IsDay)
                    {
                        toiletState.SubtractFromActionEnergy(ToiletConstants.CLEAN, 0.05f);
                    }
                    else
                    {
                        toiletState.SubtractFromActionEnergy(ToiletConstants.CLEAN, 0.02f);
                    }

                    toiletState.NextTimeForWork = toiletState.RoamingJobSettings.WorkTime + Pipliz.Time.SecondsSinceStartDouble;
                }
            }
        }
        public void DoWork(Colony colony, RoamingJobState state)
        {
            if (_nextWorkTime < TimeCycle.TotalHours)
            {
                var fuel       = state.GetActionEnergy(GameLoader.NAMESPACE + ".ManaTankRefill");
                var durability = state.GetActionEnergy(GameLoader.NAMESPACE + ".ManaMachineRepair");

                if (!ColonyManagement.DecorHappiness.DecorBonuses.ContainsKey(colony))
                {
                    ColonyManagement.DecorHappiness.DecorBonuses.Add(colony, new Dictionary <string, float>());
                }

                if (fuel > 0 && durability > 0)
                {
                    ColonyManagement.DecorHappiness.DecorBonuses[colony][nameof(GoldCube)] = 10f;

                    state.SubtractFromActionEnergy(GameLoader.NAMESPACE + ".ManaTankRefill", .2f);
                    state.SubtractFromActionEnergy(GameLoader.NAMESPACE + ".ManaMachineRepair", .05f);

                    if (World.TryGetTypeAt(state.Position, out ItemTypes.ItemType itemType) && itemType.Name == GameLoader.NAMESPACE + ".GoldCubeUnlit")
                    {
                        ServerManager.TryChangeBlock(state.Position, ItemId.GetItemId(GameLoader.NAMESPACE + ".GoldCube").Id);
                    }
                }
                else
                {
                    if (ColonyManagement.DecorHappiness.DecorBonuses[colony].ContainsKey(nameof(GoldCube)))
                    {
                        ColonyManagement.DecorHappiness.DecorBonuses[colony].Remove(nameof(GoldCube));
                    }

                    if (World.TryGetTypeAt(state.Position, out ItemTypes.ItemType itemType) && itemType.Name == GameLoader.NAMESPACE + ".GoldCube")
                    {
                        ServerManager.TryChangeBlock(state.Position, ItemId.GetItemId(GameLoader.NAMESPACE + ".GoldCubeUnlit").Id);
                    }
                }

                _nextWorkTime = TimeCycle.TotalHours + 1;
            }
        }
Beispiel #3
0
        public void DoWork(Colony colony, RoamingJobState state)
        {
            var soundUpdate = state.TempValues.GetOrDefault("SoundUpdate", Time.SecondsSinceStartDouble);

            if (soundUpdate < Time.SecondsSinceStartDouble)
            {
                AudioManager.SendAudio(state.Position.Vector, "Pandaros.Settlers.ManaPump");
                state.TempValues.Set("SoundUpdate", Time.SecondsSinceStartDouble + 16);
            }

            if (state.GetActionEnergy(GameLoader.NAMESPACE + ".ManaMachineRepair") > 0 &&
                state.NextTimeForWork < TimeCycle.TotalTime.Value.TotalMinutes &&
                RoamingJobManager.Objectives.TryGetValue(colony, out var catDic) &&
                catDic.TryGetValue(state.RoamingJobSettings.ObjectiveCategory, out var locDic))
            {
                List <RoamingJobState> manaTanks   = new List <RoamingJobState>();
                HashSet <Vector3Int>   exploredPos = new HashSet <Vector3Int>();

                foreach (var side in _applicableBlockSides)
                {
                    var offset = state.Position.GetBlockOffset(side);

                    if (locDic.TryGetValue(offset, out var roamingJobState) && roamingJobState.RoamingJobSettings.ItemIndex.Name == SettlersBuiltIn.ItemTypes.MANATANK)
                    {
                        manaTanks.Add(roamingJobState);
                    }
                }

                if (manaTanks.Count > 0)
                {
                    foreach (var side in _applicableBlockSides)
                    {
                        var offset = state.Position.GetBlockOffset(side);
                        Queue <Vector3Int> explore = new Queue <Vector3Int>();
                        explore.Enqueue(offset);
                        var maxMana = RoamingJobState.GetActionsMaxEnergy(GameLoader.NAMESPACE + ".ManaTankRefill", colony, state.RoamingJobSettings.ObjectiveCategory);

                        // walk mana pipes and find machines
                        while (explore.Count > 0)
                        {
                            offset = explore.Dequeue();

                            foreach (var exploreSide in _applicableBlockSides)
                            {
                                var exploreOffset = offset.GetBlockOffset(exploreSide);

                                if (!exploredPos.Contains(exploreOffset) &&
                                    World.TryGetTypeAt(exploreOffset, out ItemTypes.ItemType exploredItem))
                                {
                                    if (API.Items.ItemCache.CSItems.TryGetValue(exploredItem.Name, out var csExploredItem) &&
                                        csExploredItem.ConnectedBlock != null &&
                                        csExploredItem.ConnectedBlock.BlockType == "ManaPipe")
                                    {
                                        explore.Enqueue(exploreOffset);
                                        exploredPos.Add(exploreOffset);

                                        if (locDic.TryGetValue(exploreOffset, out var exploredJobState))
                                        {
                                            var existingEnergyDeficit = maxMana - exploredJobState.GetActionEnergy(GameLoader.NAMESPACE + ".ManaTankRefill");

                                            foreach (var tank in manaTanks)
                                            {
                                                var energy = tank.GetActionEnergy(GameLoader.NAMESPACE + ".ManaTankRefill");

                                                if (energy >= existingEnergyDeficit)
                                                {
                                                    tank.SubtractFromActionEnergy(GameLoader.NAMESPACE + ".ManaTankRefill", existingEnergyDeficit);
                                                    exploredJobState.ResetActionToMaxLoad(GameLoader.NAMESPACE + ".ManaTankRefill");
                                                    break;
                                                }
                                                else
                                                {
                                                    tank.SubtractFromActionEnergy(GameLoader.NAMESPACE + ".ManaTankRefill", energy);
                                                    existingEnergyDeficit = existingEnergyDeficit - energy;
                                                }

                                                state.SubtractFromActionEnergy(GameLoader.NAMESPACE + ".ManaMachineRepair", .001f);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                state.NextTimeForWork = TimeCycle.TotalTime.Value.TotalMinutes + 10;
            }
        }
Beispiel #4
0
        public static void DoWork(Colony colony, RoamingJobState machineState)
        {
            try
            {
                if (TurretSettings.ContainsKey(machineState.RoamObjective) &&
                    machineState.GetActionEnergy(MachineConstants.REPAIR) > 0 &&
                    machineState.GetActionEnergy(MachineConstants.REFUEL) > 0 &&
                    machineState.NextTimeForWork < Time.SecondsSinceStartDouble)
                {
                    var stockpile = colony.Stockpile;

                    machineState.SubtractFromActionEnergy(MachineConstants.REPAIR, TurretSettings[machineState.RoamObjective].DurabilityPerDoWork);
                    machineState.SubtractFromActionEnergy(MachineConstants.REFUEL, TurretSettings[machineState.RoamObjective].FuelPerDoWork);

                    if (machineState.GetActionEnergy(MachineConstants.RELOAD) > 0)
                    {
                        var totalDamage = TurretSettings[machineState.RoamObjective].TotalDamage;

                        var monster = MonsterTracker.Find(machineState.Position.Add(0, 1, 0),
                                                          TurretSettings[machineState.RoamObjective].Range,
                                                          totalDamage);

                        if (monster == null)
                        {
                            monster = MonsterTracker.Find(machineState.Position.Add(1, 0, 0),
                                                          TurretSettings[machineState.RoamObjective].Range,
                                                          totalDamage);
                        }

                        if (monster == null)
                        {
                            monster = MonsterTracker.Find(machineState.Position.Add(-1, 0, 0),
                                                          TurretSettings[machineState.RoamObjective].Range,
                                                          totalDamage);
                        }

                        if (monster == null)
                        {
                            monster = MonsterTracker.Find(machineState.Position.Add(0, -1, 0),
                                                          TurretSettings[machineState.RoamObjective].Range,
                                                          totalDamage);
                        }

                        if (monster == null)
                        {
                            monster = MonsterTracker.Find(machineState.Position.Add(0, 0, 1),
                                                          TurretSettings[machineState.RoamObjective].Range,
                                                          totalDamage);
                        }

                        if (monster == null)
                        {
                            monster = MonsterTracker.Find(machineState.Position.Add(0, 0, -1),
                                                          TurretSettings[machineState.RoamObjective].Range,
                                                          totalDamage);
                        }

                        if (monster != null)
                        {
                            machineState.SubtractFromActionEnergy(MachineConstants.RELOAD, TurretSettings[machineState.RoamObjective].AmmoValue);

                            if (World.TryGetTypeAt(machineState.Position.Add(0, 1, 0), out ushort above) && above == ColonyBuiltIn.ItemTypes.AIR.Id)
                            {
                                Indicator.SendIconIndicatorNear(machineState.Position.Add(0, 1, 0).Vector,
                                                                new IndicatorState(TurretSettings[machineState.RoamObjective].WorkTime,
                                                                                   TurretSettings[machineState.RoamObjective].Ammo.FirstOrDefault().Type));
                            }

                            if (TurretSettings[machineState.RoamObjective].OnShootAudio != null)
                            {
                                AudioManager.SendAudio(machineState.Position.Vector, TurretSettings[machineState.RoamObjective].OnShootAudio);
                            }

                            if (TurretSettings[machineState.RoamObjective].OnHitAudio != null)
                            {
                                AudioManager.SendAudio(monster.PositionToAimFor, TurretSettings[machineState.RoamObjective].OnHitAudio);
                            }

                            TurretSettings[machineState.RoamObjective]
                            .ProjectileAnimation
                            .SendMoveToInterpolated(machineState.Position.Vector, monster.PositionToAimFor);

                            ServerManager.SendParticleTrail(machineState.Position.Vector, monster.PositionToAimFor, 2);
                            monster.OnHit(totalDamage, machineState, ModLoader.OnHitData.EHitSourceType.Misc);
                        }
                    }

                    machineState.NextTimeForWork =
                        machineState.RoamingJobSettings.WorkTime + Time.SecondsSinceStartDouble;
                }
            }
            catch (Exception ex)
            {
                SettlersLogger.LogError(ex, $"Turret shoot for {machineState.RoamObjective}");
            }
        }
        public static void DoWork(Colony colony, RoamingJobState machineState)
        {
            if (machineState.GetActionEnergy(MachineConstants.REPAIR) > 0 &&
                machineState.GetActionEnergy(MachineConstants.RELOAD) > 0 &&
                machineState.GetActionEnergy(MachineConstants.REFUEL) > 0 &&
                machineState.NextTimeForWork < Time.SecondsSinceStartDouble)
            {
                if (!machineState.TempValues.Contains(DoorOpen))
                {
                    machineState.TempValues.Set(DoorOpen, false);
                }

                if (!_gatePositions.ContainsKey(colony))
                {
                    _gatePositions.Add(colony, new Dictionary <Vector3Int, GateState>());
                }

                var  moveGates     = new Dictionary <GateState, Vector3Int>();
                bool bossesEnabled = ColonyState.GetColonyState(colony).BossesEnabled;

                foreach (var gate in _gatePositions[colony])
                {
                    if (gate.Value.State == GatePosition.MovingClosed ||
                        gate.Value.State == GatePosition.MovingOpen)
                    {
                        continue;
                    }

                    if (World.TryGetTypeAt(gate.Key, out ushort gateType))
                    {
                        if (gate.Value.Orientation == VoxelSide.None)
                        {
                            if (gateType == GateItemXN.ItemIndex)
                            {
                                gate.Value.Orientation = VoxelSide.xMin;
                            }
                            else if (gateType == GateItemXP.ItemIndex)
                            {
                                gate.Value.Orientation = VoxelSide.xPlus;
                            }
                            else if (gateType == GateItemZN.ItemIndex)
                            {
                                gate.Value.Orientation = VoxelSide.zMin;
                            }
                            else if (gateType == GateItemZP.ItemIndex)
                            {
                                gate.Value.Orientation = VoxelSide.zPlus;
                            }
                        }

                        if (gateType != GateItem.ItemIndex &&
                            gateType != GateItemXN.ItemIndex &&
                            gateType != GateItemXP.ItemIndex &&
                            gateType != GateItemZN.ItemIndex &&
                            gateType != GateItemZP.ItemIndex)
                        {
                            switch (gate.Value.Orientation)
                            {
                            case VoxelSide.xMin:
                                ServerManager.TryChangeBlock(gate.Key, GateItemXN.ItemIndex);
                                break;

                            case VoxelSide.xPlus:
                                ServerManager.TryChangeBlock(gate.Key, GateItemXP.ItemIndex);
                                break;

                            case VoxelSide.zMin:
                                ServerManager.TryChangeBlock(gate.Key, GateItemZN.ItemIndex);
                                break;

                            case VoxelSide.zPlus:
                                ServerManager.TryChangeBlock(gate.Key, GateItemZP.ItemIndex);
                                break;

                            default:
                                ServerManager.TryChangeBlock(gate.Key, GateItemXN.ItemIndex);
                                break;
                            }
                        }
                    }

                    if (bossesEnabled)
                    {
                        if (TimeCycle.IsDay && !MonsterManager.BossActive && gate.Value.State == GatePosition.Open)
                        {
                            continue;
                        }
                    }
                    else if (TimeCycle.IsDay && gate.Value.State == GatePosition.Open)
                    {
                        continue;
                    }

                    if (bossesEnabled)
                    {
                        if ((!TimeCycle.IsDay || MonsterManager.BossActive) &&
                            gate.Value.State == GatePosition.Closed)
                        {
                            continue;
                        }
                    }
                    else if (!TimeCycle.IsDay && gate.Value.State == GatePosition.Closed)
                    {
                        continue;
                    }

                    var dis = UnityEngine.Vector3.Distance(machineState.Position.Vector, gate.Key.Vector);

                    if (dis <= 21)
                    {
                        var offset = 2;

                        if (bossesEnabled)
                        {
                            if (!TimeCycle.IsDay || MonsterManager.BossActive)
                            {
                                offset = -2;
                            }
                        }
                        else if (!TimeCycle.IsDay)
                        {
                            offset = -2;
                        }

                        moveGates.Add(gate.Value, gate.Key.Add(0, offset, 0));
                    }
                }

                foreach (var mkvp in moveGates)
                {
                    if (_gatePositions[colony].ContainsKey(mkvp.Key.Position))
                    {
                        _gatePositions[colony].Remove(mkvp.Key.Position);
                    }
                }

                foreach (var mkvp in moveGates)
                {
                    _gatePositions[colony].Add(mkvp.Value, mkvp.Key);

                    ServerManager.TryChangeBlock(mkvp.Key.Position, ColonyBuiltIn.ItemTypes.AIR.Id);

                    var newOffset = -1;

                    if (bossesEnabled)
                    {
                        if (!TimeCycle.IsDay || MonsterManager.BossActive)
                        {
                            newOffset = 1;
                        }
                    }
                    else if (!TimeCycle.IsDay)
                    {
                        newOffset = 1;
                    }

                    var newPos = mkvp.Value.Add(0, newOffset, 0).Vector;

                    switch (mkvp.Key.Orientation)
                    {
                    case VoxelSide.xMin:
                        _gateXMinusItemObjSettings.SendMoveToInterpolated(mkvp.Key.Position.Vector, newPos, TravelTime);
                        break;

                    case VoxelSide.xPlus:
                        _gateXPlusItemObjSettings.SendMoveToInterpolated(mkvp.Key.Position.Vector, newPos, TravelTime);
                        break;

                    case VoxelSide.zMin:
                        _gateZMinusItemObjSettings.SendMoveToInterpolated(mkvp.Key.Position.Vector, newPos, TravelTime);
                        break;

                    case VoxelSide.zPlus:
                        _gateZPlusItemObjSettings.SendMoveToInterpolated(mkvp.Key.Position.Vector, newPos, TravelTime);
                        break;

                    default:
                        _gateXMinusItemObjSettings.SendMoveToInterpolated(mkvp.Key.Position.Vector, newPos, TravelTime);
                        break;
                    }

                    var moveState = GatePosition.MovingClosed;

                    if (bossesEnabled)
                    {
                        if (TimeCycle.IsDay && !MonsterManager.BossActive)
                        {
                            moveState = GatePosition.MovingOpen;
                        }
                    }
                    else if (TimeCycle.IsDay)
                    {
                        moveState = GatePosition.MovingOpen;
                    }

                    mkvp.Key.State    = moveState;
                    mkvp.Key.Position = mkvp.Value;

                    var thread = new Thread(() =>
                    {
                        Thread.Sleep(8000);

                        var state = GatePosition.Closed;

                        if (bossesEnabled)
                        {
                            if (TimeCycle.IsDay && !MonsterManager.BossActive)
                            {
                                state = GatePosition.Open;
                            }
                        }
                        else if (TimeCycle.IsDay)
                        {
                            state = GatePosition.Open;
                        }

                        mkvp.Key.State = state;

                        ThreadManager.InvokeOnMainThread(() =>
                        {
                            switch (mkvp.Key.Orientation)
                            {
                            case VoxelSide.xMin:
                                ServerManager.TryChangeBlock(mkvp.Value, GateItemXN.ItemIndex);
                                break;

                            case VoxelSide.xPlus:
                                ServerManager.TryChangeBlock(mkvp.Value, GateItemXP.ItemIndex);
                                break;

                            case VoxelSide.zMin:
                                ServerManager.TryChangeBlock(mkvp.Value, GateItemZN.ItemIndex);
                                break;

                            case VoxelSide.zPlus:
                                ServerManager.TryChangeBlock(mkvp.Value, GateItemZP.ItemIndex);
                                break;

                            default:
                                ServerManager.TryChangeBlock(mkvp.Value, GateItemXN.ItemIndex);
                                break;
                            }
                        });
                    });

                    thread.IsBackground = true;
                    thread.Start();
                }

                if (moveGates.Count > 0)
                {
                    AudioManager.SendAudio(machineState.Position.Vector, GameLoader.NAMESPACE + ".GateLeverMachineAudio");
                    machineState.SubtractFromActionEnergy(MachineConstants.REPAIR, 0.01f);
                    machineState.SubtractFromActionEnergy(MachineConstants.REFUEL, 0.03f);
                }

                machineState.NextTimeForWork = machineState.RoamingJobSettings.WorkTime + Time.SecondsSinceStartDouble;
            }
        }
Beispiel #6
0
        public void DoWork(Colony colony, RoamingJobState machineState)
        {
            if (machineState.NextTimeForWork < Time.SecondsSinceStartDouble &&
                machineState.GetActionEnergy(MachineConstants.REPAIR) > 0 &&
                machineState.GetActionEnergy(MachineConstants.REFUEL) > 0)
            {
                machineState.SubtractFromActionEnergy(MachineConstants.REPAIR, 0.02f);
                machineState.SubtractFromActionEnergy(MachineConstants.REFUEL, 0.05f);

                if (World.TryGetTypeAt(machineState.Position.Add(0, -1, 0), out ItemTypes.ItemType itemBelow) &&
                    itemBelow.CustomDataNode != null &&
                    itemBelow.CustomDataNode.TryGetAs("minerIsMineable", out bool minable) &&
                    minable)
                {
                    var itemList = itemBelow.OnRemoveItems;

                    if (itemList != null && itemList.Count > 0)
                    {
                        var mineTime = itemBelow.CustomDataNode.GetAsOrDefault("minerMiningTime", machineState.RoamingJobSettings.WorkTime);
                        machineState.NextTimeForWork = mineTime + Time.SecondsSinceStartDouble;
                        var items          = machineState.TempValues.GetOrDefault <List <InventoryItem> >("MinedItems", new List <InventoryItem>());
                        int remainingItems = Math.RoundToInt(machineState.GetActionEnergy(MachineConstants.INVENTORY) / .05f) - items.Count;

                        if (remainingItems != 0)
                        {
                            for (var i = 0; i < itemList.Count; i++)
                            {
                                if (Random.NextDouble() <= itemList[i].chance)
                                {
                                    if (remainingItems != 0)
                                    {
                                        items.Add(itemList[i].item);
                                        remainingItems--;
                                        machineState.SubtractFromActionEnergy(MachineConstants.INVENTORY, .05f);
                                    }
                                }
                            }
                        }

                        machineState.TempValues.Set("MinedItems", items);

                        if (remainingItems != 0)
                        {
                            AudioManager.SendAudio(machineState.Position.Vector, GameLoader.NAMESPACE + ".MiningMachineAudio");
                            Indicator.SendIconIndicatorNear(machineState.Position.Add(0, 1, 0).Vector, new IndicatorState(mineTime, itemList.FirstOrDefault().item.Type));
                        }
                        else
                        {
                            machineState.SetActionEnergy(MachineConstants.INVENTORY, 0);
                            Indicator.SendIconIndicatorNear(machineState.Position.Add(0, 1, 0).Vector, new IndicatorState(mineTime, GameLoader.NAMESPACE + ".Inventory", true, false));
                        }
                    }
                    else
                    {
                        machineState.NextTimeForWork = machineState.RoamingJobSettings.WorkTime + Time.SecondsSinceStartDouble;
                        Indicator.SendIconIndicatorNear(machineState.Position.Add(0, 1, 0).Vector, new IndicatorState(machineState.RoamingJobSettings.WorkTime, ColonyBuiltIn.ItemTypes.ERRORIDLE.Name));
                    }
                }
                else
                {
                    machineState.NextTimeForWork = machineState.RoamingJobSettings.WorkTime + Time.SecondsSinceStartDouble;
                    Indicator.SendIconIndicatorNear(machineState.Position.Add(0, 1, 0).Vector, new IndicatorState(machineState.RoamingJobSettings.WorkTime, ColonyBuiltIn.ItemTypes.ERRORIDLE.Name));
                }
            }
        }