Skip to content

Commit

Permalink
Code simplify.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnqbob committed Oct 12, 2023
1 parent 31c8ee6 commit 2dca27b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 69 deletions.
10 changes: 4 additions & 6 deletions OpenRA.Mods.Sp/Traits/BotModules/HarvesterBotModuleSP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class HarvesterBotModuleSP : ConditionalTrait<HarvesterBotModuleSPInfo>,
readonly World world;
readonly Player player;
readonly Func<Actor, bool> unitCannotBeOrdered;
readonly HarvesterBotModuleSPInfo info;

IResourceLayer resourceLayer;
ResourceClaimLayer claimLayer;
Expand All @@ -64,7 +63,6 @@ public class HarvesterBotModuleSP : ConditionalTrait<HarvesterBotModuleSPInfo>,
public HarvesterBotModuleSP(Actor self, HarvesterBotModuleSPInfo info)
: base(info)
{
this.info = info;
world = self.World;
player = self.Owner;
unitCannotBeOrdered = a => a.Owner != self.Owner || a.IsDead || !a.IsInWorld;
Expand All @@ -76,7 +74,7 @@ protected override void TraitEnabled(Actor self)
{
resourceLayer = world.WorldActor.TraitOrDefault<IResourceLayer>();
claimLayer = world.WorldActor.TraitOrDefault<ResourceClaimLayer>();
resourseCenters = self.World.Actors.Where(a => info.ResourseCenterType.Contains(a.Info.Name)).Select(a => a.Location).OrderByDescending(c => (c - player.HomeLocation).LengthSquared).ToArray();
resourseCenters = self.World.Actors.Where(a => Info.ResourseCenterType.Contains(a.Info.Name)).Select(a => a.Location).OrderByDescending(c => (c - player.HomeLocation).LengthSquared).ToArray();

initialized = true;
}
Expand Down Expand Up @@ -132,16 +130,16 @@ Target FindNextResource(TraitPair<Harvester> harv)

foreach (var loc in resourseCenters.OrderBy(c => (c - harv.Actor.Location).LengthSquared))
{
if (world.FindActorsInCircle(world.Map.CenterOfCell(loc), info.HarvesterEnemyAvoidanceRadius).Any(a => !a.IsDead && a.IsInWorld && a.Owner.RelationshipWith(player) == PlayerRelationship.Enemy))
if (world.FindActorsInCircle(world.Map.CenterOfCell(loc), Info.HarvesterEnemyAvoidanceRadius).Any(a => !a.IsDead && a.IsInWorld && a.Owner.RelationshipWith(player) == PlayerRelationship.Enemy))
continue;

if (mobile != null && !mobile.PathFinder.PathExistsForLocomotor(mobile.Locomotor, harv.Actor.Location, loc))
continue;

var harvestable = world.Map.FindTilesInAnnulus(loc, 0, info.ResourseCenterSearchRangeInCells).Where(c => harv.Trait.CanHarvestCell(c) && claimLayer.CanClaimCell(harv.Actor, c)).ToArray();
var harvestable = world.Map.FindTilesInAnnulus(loc, 0, Info.ResourseCenterSearchRangeInCells).Where(c => harv.Trait.CanHarvestCell(c) && claimLayer.CanClaimCell(harv.Actor, c)).ToArray();

// If the resource field is rich enough then we will just stop checking and harvest.
if (harvestable.Length >= info.FavoredHarvestableCell)
if (harvestable.Length >= Info.FavoredHarvestableCell)
return Target.FromCell(world, harvestable.Random(world.LocalRandom));

// If not, we are going to find a best location by comparing the resource cells around.
Expand Down
41 changes: 19 additions & 22 deletions OpenRA.Mods.Sp/Traits/FirestromSP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public sealed class FirestromSPInfo : ConditionalTraitInfo
public sealed class FirestromSP : ConditionalTrait<FirestromSPInfo>, ITick
{
readonly int[] effectsOffet;
readonly FirestromSPInfo info;
readonly World world;
readonly Actor self;
int damageTicks;
Expand All @@ -91,8 +90,6 @@ public sealed class FirestromSP : ConditionalTrait<FirestromSPInfo>, ITick
public FirestromSP(Actor self, FirestromSPInfo info)
: base(info)
{
this.info = info;

effectsOffet = new int[info.FirestormEffectAmounts];
var offset = 1024 / info.FirestormEffectAmounts;

Expand All @@ -105,24 +102,24 @@ public FirestromSP(Actor self, FirestromSPInfo info)

void SpawnEffects()
{
for (var i = 0; i < info.FirestormEffectAmounts; i++)
for (var i = 0; i < Info.FirestormEffectAmounts; i++)
{
var sequence = info.FirestormEffectSequences.RandomOrDefault(world.LocalRandom);
var sequence = Info.FirestormEffectSequences.RandomOrDefault(world.LocalRandom);
if (sequence == null)
break;
var rotation = WRot.FromYaw(new WAngle(effectsOffet[i]));
var targetpos = self.CenterPosition + new WVec(info.FirestormEffectRange.Length, 0, 0).Rotate(rotation);
world.AddFrameEndTask(w => w.Add(new SpriteEffect(new WPos(targetpos.X, targetpos.Y, world.Map.CenterOfCell(world.Map.CellContaining(targetpos)).Z), w, info.FirestormEffectImage, sequence, info.FirestormEffectPalette)));
var targetpos = self.CenterPosition + new WVec(Info.FirestormEffectRange.Length, 0, 0).Rotate(rotation);
world.AddFrameEndTask(w => w.Add(new SpriteEffect(new WPos(targetpos.X, targetpos.Y, world.Map.CenterOfCell(world.Map.CellContaining(targetpos)).Z), w, Info.FirestormEffectImage, sequence, Info.FirestormEffectPalette)));

effectsOffet[i] = effectsOffet[i] + info.FirestormEffectRotationAngle < 1024 ? effectsOffet[i] + info.FirestormEffectRotationAngle : (effectsOffet[i] + info.FirestormEffectRotationAngle) % 1024;
effectsOffet[i] = effectsOffet[i] + Info.FirestormEffectRotationAngle < 1024 ? effectsOffet[i] + Info.FirestormEffectRotationAngle : (effectsOffet[i] + Info.FirestormEffectRotationAngle) % 1024;
}
}

void DealDamage()
{
var victims = world.FindActorsInCircle(self.CenterPosition, info.DamageMaxRange).Where(
a => info.ValidRelationships.HasRelationship(self.Owner.RelationshipWith(a.Owner))
&& (self.CenterPosition - a.CenterPosition).HorizontalLengthSquared >= info.DamageMinRange.LengthSquared); // this line has bug
var victims = world.FindActorsInCircle(self.CenterPosition, Info.DamageMaxRange).Where(
a => Info.ValidRelationships.HasRelationship(self.Owner.RelationshipWith(a.Owner))
&& (self.CenterPosition - a.CenterPosition).HorizontalLengthSquared >= Info.DamageMinRange.LengthSquared); // this line has bug

foreach (var v in victims)
{
Expand All @@ -132,9 +129,9 @@ void DealDamage()

var targetTypes = v.GetEnabledTargetTypes();

if (info.ValidTargets.Overlaps(targetTypes) && !info.InvalidTargets.Overlaps(targetTypes))
if (Info.ValidTargets.Overlaps(targetTypes) && !Info.InvalidTargets.Overlaps(targetTypes))
{
if (info.Versus.Count == 0)
if (Info.Versus.Count == 0)
continue;

var closestActiveShape = v.TraitsImplementing<HitShape>().Where(Exts.IsTraitEnabled).MinByOrDefault(t => t.DistanceFromEdge(v, v.CenterPosition));
Expand All @@ -144,12 +141,12 @@ void DealDamage()
continue;

var armor = v.TraitsImplementing<Armor>()
.Where(a => !a.IsTraitDisabled && a.Info.Type != null && info.Versus.ContainsKey(a.Info.Type) &&
.Where(a => !a.IsTraitDisabled && a.Info.Type != null && Info.Versus.ContainsKey(a.Info.Type) &&
(closestActiveShape.Info.ArmorTypes.IsEmpty || closestActiveShape.Info.ArmorTypes.Contains(a.Info.Type)))
.Select(a => info.Versus[a.Info.Type]);
.Select(a => Info.Versus[a.Info.Type]);

var damage = Common.Util.ApplyPercentageModifiers(info.Damage, armor);
v.InflictDamage(self, new Damage(damage, info.DamageTypes));
var damage = Common.Util.ApplyPercentageModifiers(Info.Damage, armor);
v.InflictDamage(self, new Damage(damage, Info.DamageTypes));
}
}
}
Expand All @@ -165,21 +162,21 @@ public void Tick(Actor self)
if (IsTraitDisabled)
return;

if (info.FirestormEffectImage != null)
if (Info.FirestormEffectImage != null)
SpawnEffects();

if (damageTicks-- <= 0)
{
DealDamage();
damageTicks = info.Damageinterval;
damageTicks = Info.Damageinterval;
}

if (soundTicks-- <= 0)
{
var launchSound = info.LaunchEffectSounds.RandomOrDefault(world.LocalRandom);
var launchSound = Info.LaunchEffectSounds.RandomOrDefault(world.LocalRandom);
if (launchSound != null && !world.ShroudObscures(self.CenterPosition) && !world.FogObscures(self.CenterPosition))
Game.Sound.Play(SoundType.World, launchSound, self.CenterPosition, info.LaunchEffectSoundVolume);
soundTicks = info.LaunchEffectSoundInterval;
Game.Sound.Play(SoundType.World, launchSound, self.CenterPosition, Info.LaunchEffectSoundVolume);
soundTicks = Info.LaunchEffectSoundInterval;
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions OpenRA.Mods.Sp/Traits/ForceFireAtLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ sealed class ForceFireAtLocationInfo : ConditionalTraitInfo, Requires<AttackBase

sealed class ForceFireAtLocation : ConditionalTrait<ForceFireAtLocationInfo>, INotifyCreated
{
readonly ForceFireAtLocationInfo info;
AttackBase[] attackBases;

public ForceFireAtLocation(ForceFireAtLocationInfo info)
: base(info)
{
this.info = info;
}
: base(info) { }

protected override void Created(Actor self)
{
Expand All @@ -48,7 +44,7 @@ protected override void TraitEnabled(Actor self)
if (ab.IsTraitDisabled)
continue;

ab.AttackTarget(Target.FromPos(self.CenterPosition + info.LocalOffset), AttackSource.Default, false, true, true);
ab.AttackTarget(Target.FromPos(self.CenterPosition + Info.LocalOffset), AttackSource.Default, false, true, true);
}

base.TraitEnabled(self);
Expand Down
18 changes: 8 additions & 10 deletions OpenRA.Mods.Sp/Traits/SpawnSparks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public override void RulesetLoaded(Ruleset rules, ActorInfo ai)

sealed class SpawnSparks : ConditionalTrait<SpawnSparksInfo>, ITick
{
readonly SpawnSparksInfo info;
readonly WeaponInfo weapon;
readonly BodyOrientation body;
readonly bool hasWeapon;
Expand All @@ -86,7 +85,6 @@ sealed class SpawnSparks : ConditionalTrait<SpawnSparksInfo>, ITick
public SpawnSparks(SpawnSparksInfo info, Actor self)
: base(info)
{
this.info = info;
hasWeapon = info.SparkWeapon != null && info.Amount > 0;
weapon = info.WeaponInfo;
body = self.TraitOrDefault<BodyOrientation>();
Expand All @@ -102,30 +100,30 @@ void ITick.Tick(Actor self)
if (--interval <= 0)
{
var epicenter = self.CenterPosition + (body != null
? body.LocalToWorld(info.LocalOffset.Rotate(body.QuantizeOrientation(self.Orientation)))
: info.LocalOffset);
? body.LocalToWorld(Info.LocalOffset.Rotate(body.QuantizeOrientation(self.Orientation)))
: Info.LocalOffset);
var world = self.World;

if (hasLaunchEffect)
{
self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(epicenter, self.World,
info.LaunchEffectImage, info.LaunchEffectSequences.Random(world.LocalRandom), info.LaunchEffectPalette)));
Info.LaunchEffectImage, Info.LaunchEffectSequences.Random(world.LocalRandom), Info.LaunchEffectPalette)));
}

if (!hasWeapon)
{
interval = info.Interval;
interval = Info.Interval;
return;
}

var map = world.Map;
var amount = info.Amount;
var amount = Info.Amount;
var offset = 1024 / amount;
for (var i = 0; i < amount; i++)
{
var rotation = WRot.FromYaw(new WAngle(i * offset));
var targetpos = epicenter + new WVec(weapon.Range.Length, 0, 0).Rotate(rotation);
var radiusTarget = Target.FromPos(new WPos(targetpos.X, targetpos.Y, info.ForceToGround ? map.CenterOfCell(map.CellContaining(targetpos)).Z : targetpos.Z));
var radiusTarget = Target.FromPos(new WPos(targetpos.X, targetpos.Y, Info.ForceToGround ? map.CenterOfCell(map.CellContaining(targetpos)).Z : targetpos.Z));

var projectileArgs = new ProjectileArgs
{
Expand Down Expand Up @@ -159,13 +157,13 @@ void ITick.Tick(Actor self)
Game.Sound.Play(SoundType.World, weapon.Report, world, epicenter, null, weapon.SoundVolume);
}

interval = info.Interval;
interval = Info.Interval;
}
}

protected override void TraitEnabled(Actor self)
{
if (info.ResetReloadWhenEnabled)
if (Info.ResetReloadWhenEnabled)
interval = 0;
}
}
Expand Down
11 changes: 4 additions & 7 deletions OpenRA.Mods.Sp/Traits/WithSupportPowerActivationExplodeWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public override void RulesetLoaded(Ruleset rules, ActorInfo ai)

public sealed class WithSupportPowerActivationExplodeWeapon : PausableConditionalTrait<WithSupportPowerActivationExplodeWeaponInfo>, INotifySupportPower, ITick
{
readonly WithSupportPowerActivationExplodeWeaponInfo info;
readonly WeaponInfo weapon;
readonly BodyOrientation body;
bool shouldAcitate;
Expand All @@ -67,8 +66,6 @@ public sealed class WithSupportPowerActivationExplodeWeapon : PausableConditiona
public WithSupportPowerActivationExplodeWeapon(Actor self, WithSupportPowerActivationExplodeWeaponInfo info)
: base(info)
{
this.info = info;

weapon = info.WeaponInfo;
burst = weapon.Burst;
body = self.TraitOrDefault<BodyOrientation>();
Expand Down Expand Up @@ -101,12 +98,12 @@ public void Tick(Actor self)
if (--fireDelay < 0)
{
var localoffset = body != null
? body.LocalToWorld(info.LocalOffset.Rotate(body.QuantizeOrientation(self.Orientation)))
: info.LocalOffset;
? body.LocalToWorld(Info.LocalOffset.Rotate(body.QuantizeOrientation(self.Orientation)))
: Info.LocalOffset;

var hitOffset = body != null
? body.LocalToWorld(info.HitOffset.Rotate(body.QuantizeOrientation(self.Orientation)))
: info.HitOffset;
? body.LocalToWorld(Info.HitOffset.Rotate(body.QuantizeOrientation(self.Orientation)))
: Info.HitOffset;

var args = new ProjectileArgs
{
Expand Down
31 changes: 13 additions & 18 deletions OpenRA.Mods.Sp/Traits/World/WeaponWeather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public override void RulesetLoaded(Ruleset rules, ActorInfo ai)

class WeaponWeather : ConditionalTrait<WeaponWeatherInfo>, ITick
{
readonly WeaponWeatherInfo info;

World world;
bool firstTick = true;
Actor firer;
Expand All @@ -70,42 +68,39 @@ class WeaponWeather : ConditionalTrait<WeaponWeatherInfo>, ITick
public int Amount { get; private set; }

public WeaponWeather(WeaponWeatherInfo info)
: base(info)
{
this.info = info;
}
: base(info) { }

void ITick.Tick(Actor self)
{
if (firstTick)
{
world = self.World;
firer = info.HasOwner ? self.Owner.PlayerActor : world.WorldActor;
firer = Info.HasOwner ? self.Owner.PlayerActor : world.WorldActor;

firstTick = false;
Interval = info.Interval.Length == 2
? world.SharedRandom.Next(info.Interval[0], info.Interval[1])
: info.Interval[0];
Interval = Info.Interval.Length == 2
? world.SharedRandom.Next(Info.Interval[0], Info.Interval[1])
: Info.Interval[0];
}

if (IsTraitDisabled || --Interval > 0)
return;

Interval = info.Interval.Length == 2
? world.SharedRandom.Next(info.Interval[0], info.Interval[1])
: info.Interval[0];
Interval = Info.Interval.Length == 2
? world.SharedRandom.Next(Info.Interval[0], Info.Interval[1])
: Info.Interval[0];

Amount = info.Amount.Length == 2
? world.SharedRandom.Next(info.Amount[0], info.Amount[1])
: info.Amount[0];
Amount = Info.Amount.Length == 2
? world.SharedRandom.Next(Info.Amount[0], Info.Amount[1])
: Info.Amount[0];

for (var i = 0; i < Amount; i++)
{
var tpos = world.Map.CenterOfCell(world.Map.ChooseRandomCell(world.SharedRandom))
+ new WVec(WDist.Zero, WDist.Zero, info.Altitude);
+ new WVec(WDist.Zero, WDist.Zero, Info.Altitude);
var target = Target.FromPos(tpos);

var weapon = info.WeaponInfos.Random(world.SharedRandom);
var weapon = Info.WeaponInfos.Random(world.SharedRandom);

var projectileArgs = new ProjectileArgs
{
Expand Down

0 comments on commit 2dca27b

Please sign in to comment.