Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dysfunction of PowerDownBotModuleCA on loading saved game #55

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
user.config
engine

# Visual Studio
Release
bin
obj
*.ncb
*.vcproj*
*.suo
*.user
*.sln.cache
*.manifest
*.CodeAnalysisLog.xml
*.lastcodeanalysissucceeded
_ReSharper.*/
/.vs

# Visual Studio Code
/.vscode/settings.json

# backup files by various editors
*~
*.orig
\#*
.*.sw?

# Monodevelop
*.pidb
*.userprefs

# Mac OS X
.DS_Store

# SublimeText
*.sublime-project
*.sublime-workspace

# NUnit
/TestResult.xml
/lib/

# Support directory
/Support

# IntelliJ files
.idea
.vs
71 changes: 49 additions & 22 deletions OpenRA.Mods.CA/Traits/BotModules/PowerDownBotModuleCA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@

namespace OpenRA.Mods.CA.Traits
{
[Desc("Manages AI powerdown.")]
[TraitLocation(SystemActors.Player)]
[Desc("Manages AI powerdown.",
"You need to use PowerMultiplier on toggle control only on related buildings, for calculation of this bot module")]
public class PowerDownBotModuleCAInfo : ConditionalTraitInfo
{
[Desc("Delay (in ticks) between toggling powerdown.")]
public readonly int Interval = 150;

[Desc("Order string that used for powerdown.")]
public readonly string OrderName = "PowerDown";

public override object Create(ActorInitializer init) { return new PowerDownBotModuleCA(init.Self, this); }
}

public class PowerDownBotModuleCA : ConditionalTrait<PowerDownBotModuleCAInfo>, IBotTick
public class PowerDownBotModuleCA : ConditionalTrait<PowerDownBotModuleCAInfo>, IBotTick, IGameSaveTraitData
{
readonly World world;
readonly Player player;

PowerManager playerPower;
int toggleTick;

readonly Func<Actor, bool> isToggledBuildingsValid;

// We keep a list to track toggled buildings for performance.
List<BuildingPowerWrapper> toggledBuildings;
List<BuildingPowerWrapper> toggledBuildings = new List<BuildingPowerWrapper>();

class BuildingPowerWrapper
sealed class BuildingPowerWrapper
{
public int ExpectedPowerChanging;
public Actor Actor;
Expand All @@ -56,26 +57,21 @@ public PowerDownBotModuleCA(Actor self, PowerDownBotModuleCAInfo info)
{
world = self.World;
player = self.Owner;
toggledBuildings = new List<BuildingPowerWrapper>();
isToggledBuildingsValid = a => a.Owner == self.Owner && !a.IsDead && a.IsInWorld;

isToggledBuildingsValid = a => a != null && a.Owner == self.Owner && !a.IsDead && a.IsInWorld;
}

protected override void Created(Actor self)
{
// Special case handling is required for the Player actor.
// Created is called before Player.PlayerActor is assigned,
// so we must query player traits from self, which refers
// for bot modules always to the Player actor.
playerPower = self.TraitOrDefault<PowerManager>();
playerPower = self.Owner.PlayerActor.TraitOrDefault<PowerManager>();
}

protected override void TraitEnabled(Actor self)
{
toggleTick = world.LocalRandom.Next(Info.Interval);
toggledBuildings = new List<BuildingPowerWrapper>();
}

int GetTogglePowerChanging(Actor a)
static int GetTogglePowerChanging(Actor a)
{
var powerChangingIfToggled = 0;
var powerTraits = a.TraitsImplementing<Power>().Where(t => !t.IsTraitDisabled).ToArray();
Expand All @@ -100,7 +96,7 @@ IEnumerable<Actor> GetToggleableBuildings(IBot bot)

IEnumerable<BuildingPowerWrapper> GetOnlineBuildings(IBot bot)
{
List<BuildingPowerWrapper> toggleableBuildings = new List<BuildingPowerWrapper>();
var toggleableBuildings = new List<BuildingPowerWrapper>();

foreach (var a in GetToggleableBuildings(bot))
{
Expand All @@ -121,13 +117,13 @@ void IBotTick.BotTick(IBot bot)
}

var power = playerPower.ExcessPower;
List<Actor> togglingBuildings = new List<Actor>();
var togglingBuildings = new List<Actor>();

// When there is extra power, check if AI can toggle on
if (power > 0)
{
toggledBuildings = toggledBuildings.Where(bpw => isToggledBuildingsValid(bpw.Actor)).OrderByDescending(bpw => bpw.ExpectedPowerChanging).ToList();
for (int i = 0; i < toggledBuildings.Count; i++)
for (var i = 0; i < toggledBuildings.Count; i++)
{
var bpw = toggledBuildings[i];
if (power + bpw.ExpectedPowerChanging < 0)
Expand Down Expand Up @@ -156,11 +152,42 @@ void IBotTick.BotTick(IBot bot)
}

if (togglingBuildings.Count > 0)
{
bot.QueueOrder(new Order(Info.OrderName, null, false, groupedActors: togglingBuildings.ToArray()));
}
bot.QueueOrder(new Order("PowerDown", null, false, groupedActors: togglingBuildings.ToArray()));

toggleTick = Info.Interval;
}

List<MiniYamlNode> IGameSaveTraitData.IssueTraitData(Actor self)
{
if (IsTraitDisabled)
return null;

var data = new List<MiniYamlNode>();
foreach (var tb in toggledBuildings.Where(td => isToggledBuildingsValid(td.Actor)))
data.Add(new MiniYamlNode(FieldSaver.FormatValue(tb.Actor.ActorID), FieldSaver.FormatValue(tb.ExpectedPowerChanging)));

return new List<MiniYamlNode>()
{
new MiniYamlNode("ToggledBuildings", new MiniYaml("", data))
};
}

void IGameSaveTraitData.ResolveTraitData(Actor self, List<MiniYamlNode> data)
{
if (self.World.IsReplay)
return;

var toggledBuildingsNode = data.FirstOrDefault(n => n.Key == "ToggledBuildings");
if (toggledBuildingsNode != null)
{
foreach (var n in toggledBuildingsNode.Value.Nodes)
{
var a = self.World.GetActorById(FieldLoader.GetValue<uint>(n.Key, n.Key));

if (isToggledBuildingsValid(a))
toggledBuildings.Add(new BuildingPowerWrapper(a, FieldLoader.GetValue<int>(n.Key, n.Value.Value)));
}
}
}
}
}
Loading