Skip to content

Commit

Permalink
Merge pull request #1 from PhantomGamers/pr-battlebase-pr
Browse files Browse the repository at this point in the history
Battlebase Cleanup
  • Loading branch information
starfi5h authored Jan 3, 2024
2 parents 5e0bb84 + b70c814 commit 3382daf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#region

using System;
using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Networking;
Expand Down Expand Up @@ -33,75 +32,78 @@ protected override void ProcessPacket(BattleBaseSettingUpdatePacket packet, Nebu
}

var pool = factory?.defenseSystem.battleBases;
if (pool != null && packet.BattleBaseId > 0)
if (pool == null || packet.BattleBaseId <= 0)
{
var battleBase = pool[packet.BattleBaseId];
return;
}
var battleBase = pool[packet.BattleBaseId];

switch (packet.Event)
{
case BattleBaseSettingEvent.ChangeMaxChargePower:
factory.powerSystem.consumerPool[battleBase.pcId].workEnergyPerTick = (long)(5000.0 * (double)packet.Arg1 + 0.5);
break;
switch (packet.Event)
{
case BattleBaseSettingEvent.ChangeMaxChargePower:
factory.powerSystem.consumerPool[battleBase.pcId].workEnergyPerTick = (long)(5000.0 * packet.Arg1 + 0.5);
break;

case BattleBaseSettingEvent.ToggleDroneEnabled:
battleBase.constructionModule.droneEnabled = packet.Arg1 != 0f;
break;
case BattleBaseSettingEvent.ToggleDroneEnabled:
battleBase.constructionModule.droneEnabled = packet.Arg1 != 0f;
break;

case BattleBaseSettingEvent.ChangeDronesPriority:
battleBase.constructionModule.ChangeDronesPriority(factory, packet.Arg1);
break;
case BattleBaseSettingEvent.ChangeDronesPriority:
battleBase.constructionModule.ChangeDronesPriority(factory, packet.Arg1);
break;

case BattleBaseSettingEvent.ToggleCombatModuleEnabled:
battleBase.combatModule.moduleEnabled = packet.Arg1 != 0f;
break;
case BattleBaseSettingEvent.ToggleCombatModuleEnabled:
battleBase.combatModule.moduleEnabled = packet.Arg1 != 0f;
break;

case BattleBaseSettingEvent.ToggleAutoReconstruct:
battleBase.constructionModule.autoReconstruct = packet.Arg1 != 0f;
break;
case BattleBaseSettingEvent.ToggleAutoReconstruct:
battleBase.constructionModule.autoReconstruct = packet.Arg1 != 0f;
break;

case BattleBaseSettingEvent.ToggleAutoPickEnabled:
battleBase.autoPickEnabled = packet.Arg1 != 0f;
break;
case BattleBaseSettingEvent.ToggleAutoPickEnabled:
battleBase.autoPickEnabled = packet.Arg1 != 0f;
break;

case BattleBaseSettingEvent.ChangeFleetConfig:
// Copy ChangeFleetConfig without storage/inventory interactions
// This may change for future ModuleFleet sycning
var fleetIndex = 0;
var newConfigId = (int)packet.Arg1;
ref var moduleFleet = ref battleBase.combatModule.moduleFleets[fleetIndex];
for (var i = 0; i < moduleFleet.fighters.Length; i++)
case BattleBaseSettingEvent.ChangeFleetConfig:
// Copy ChangeFleetConfig without storage/inventory interactions
// This may change for future ModuleFleet syncing
const int FleetIndex = 0;
var newConfigId = (int)packet.Arg1;
ref var moduleFleet = ref battleBase.combatModule.moduleFleets[FleetIndex];
for (var i = 0; i < moduleFleet.fighters.Length; i++)
{
if (moduleFleet.fighters[i].itemId == newConfigId)
{
continue;
}
if (moduleFleet.fighters[i].count > 0)
{
if (moduleFleet.fighters[i].itemId != newConfigId)
{
if (moduleFleet.fighters[i].count > 0)
{
var itemId = 0;
var count = 0;
moduleFleet.TakeFighterFromPort(i, ref itemId, ref count);
}
moduleFleet.SetItemId(i, newConfigId);
}
var itemId = 0;
var count = 0;
moduleFleet.TakeFighterFromPort(i, ref itemId, ref count);
}
break;
moduleFleet.SetItemId(i, newConfigId);
}
break;

case BattleBaseSettingEvent.ToggleAutoReplenishFleet:
battleBase.combatModule.autoReplenishFleet = packet.Arg1 != 0f;
break;
case BattleBaseSettingEvent.ToggleAutoReplenishFleet:
battleBase.combatModule.autoReplenishFleet = packet.Arg1 != 0f;
break;

default:
Log.Warn($"BattleBaseSettingEvent: Unhandle BattleBaseSettingEvent {packet.Event}");
break;
}
case BattleBaseSettingEvent.None:
default:
Log.Warn($"BattleBaseSettingEvent: Unhandled BattleBaseSettingEvent {packet.Event}");
break;
}

//Update UI window too if the local is viewing the current Battle Base
var battleBaseWindow = UIRoot.instance.uiGame.battleBaseWindow;
if (battleBaseWindow.battleBaseId != packet.BattleBaseId || battleBaseWindow.factory?.planetId != packet.PlanetId)
{
return;
}
battleBaseWindow.eventLock = true;
battleBaseWindow.OnBattleBaseIdChange();
battleBaseWindow.eventLock = false;
//Update UI window too if the local is viewing the current Battle Base
var battleBaseWindow = UIRoot.instance.uiGame.battleBaseWindow;
if (battleBaseWindow.battleBaseId != packet.BattleBaseId || battleBaseWindow.factory?.planetId != packet.PlanetId)
{
return;
}
battleBaseWindow.eventLock = true;
battleBaseWindow.OnBattleBaseIdChange();
battleBaseWindow.eventLock = false;
}
}
6 changes: 3 additions & 3 deletions NebulaPatcher/Patches/Dynamic/UIBattleBaseWindow_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ private static void SendEvent(UIBattleBaseWindow __instance, BattleBaseSettingEv

[HarmonyPrefix]
[HarmonyPatch(nameof(UIBattleBaseWindow.OnMaxChargePowerSliderChange))]

public static bool OnMaxChargePowerSliderChange_Prefix(UIBattleBaseWindow __instance, float arg0)
{
if (!Multiplayer.IsActive || __instance.battleBaseId == 0 || __instance.factory == null || __instance.battleBase.id != __instance.battleBaseId)
if (!Multiplayer.IsActive || __instance.battleBaseId == 0 || __instance.factory == null ||
__instance.battleBase.id != __instance.battleBaseId)
{
return true;
}
Expand Down Expand Up @@ -60,7 +60,7 @@ public static bool OnDronePriorityButtonClick_Prefix(UIBattleBaseWindow __instan
return true;
}
var ratio = __instance.constructionModule.dronePriorConstructRatio;
var newRatio = ratio < 0.25f ? 0.5f : (ratio < 0.75f ? 1f : 0f);
var newRatio = ratio < 0.25f ? 0.5f : ratio < 0.75f ? 1f : 0f;
SendEvent(__instance, BattleBaseSettingEvent.ChangeDronesPriority, newRatio);
return Multiplayer.Session.LocalPlayer.IsHost;
}
Expand Down

0 comments on commit 3382daf

Please sign in to comment.