Reorganize Mods.D2k

This commit is contained in:
reaperrr
2014-11-16 17:03:52 +01:00
parent 80279dc304
commit 1a032d6f6c
13 changed files with 24 additions and 24 deletions

View File

@@ -0,0 +1,72 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Reduces health points over time when the actor is placed on unsafe terrain.")]
class DamagedWithoutFoundationInfo : ITraitInfo, Requires<HealthInfo>
{
[WeaponReference]
public readonly string Weapon = "weathering";
public readonly string[] SafeTerrain = { "Concrete" };
public readonly int DamageThreshold = 50;
public object Create(ActorInitializer init) { return new DamagedWithoutFoundation(init.self, this); }
}
class DamagedWithoutFoundation : ITick, ISync, INotifyAddedToWorld
{
readonly DamagedWithoutFoundationInfo info;
readonly Health health;
readonly WeaponInfo weapon;
[Sync] int damageThreshold = 100;
[Sync] int damageTicks;
public DamagedWithoutFoundation(Actor self, DamagedWithoutFoundationInfo info)
{
this.info = info;
health = self.Trait<Health>();
weapon = self.World.Map.Rules.Weapons[info.Weapon.ToLowerInvariant()];
}
public void AddedToWorld(Actor self)
{
var safeTiles = 0;
var totalTiles = 0;
foreach (var kv in self.OccupiesSpace.OccupiedCells())
{
totalTiles++;
if (info.SafeTerrain.Contains(self.World.Map.GetTerrainInfo(kv.First).Type))
safeTiles++;
}
damageThreshold = (info.DamageThreshold * health.MaxHP + (100 - info.DamageThreshold) * safeTiles * health.MaxHP / totalTiles) / 100;
// Actors start with maximum damage applied
var delta = health.HP - damageThreshold;
if (delta > 0)
health.InflictDamage(self, self.World.WorldActor, delta, null, false);
}
public void Tick(Actor self)
{
if (health.HP <= damageThreshold || --damageTicks > 0)
return;
weapon.Impact(Target.FromActor(self), self.World.WorldActor, Enumerable.Empty<int>());
damageTicks = weapon.ReloadDelay;
}
}
}

View File

@@ -0,0 +1,78 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Rendered when the actor constructed a building.")]
public class WithBuildingPlacedOverlayInfo : ITraitInfo, Requires<RenderSpritesInfo>, Requires<IBodyOrientationInfo>
{
[Desc("Sequence name to use")]
public readonly string Sequence = "crane-overlay";
[Desc("Position relative to body")]
public readonly WVec Offset = WVec.Zero;
[Desc("Custom palette name")]
public readonly string Palette = null;
[Desc("Custom palette is a player palette BaseName")]
public readonly bool IsPlayerPalette = false;
public object Create(ActorInitializer init) { return new WithBuildingPlacedOverlay(init.self, this); }
}
public class WithBuildingPlacedOverlay : INotifyBuildComplete, INotifySold, INotifyDamageStateChanged, INotifyBuildingPlaced
{
Animation overlay;
bool buildComplete;
public WithBuildingPlacedOverlay(Actor self, WithBuildingPlacedOverlayInfo info)
{
var rs = self.Trait<RenderSprites>();
var body = self.Trait<IBodyOrientation>();
buildComplete = !self.HasTrait<Building>(); // always render instantly for units
overlay = new Animation(self.World, rs.GetImage(self));
overlay.Play(info.Sequence);
rs.Add("crane_overlay_{0}".F(info.Sequence),
new AnimationWithOffset(overlay,
() => body.LocalToWorld(info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation))),
() => !buildComplete),
info.Palette, info.IsPlayerPalette);
}
public void BuildingComplete(Actor self)
{
buildComplete = true;
}
public void Sold(Actor self) { }
public void Selling(Actor self)
{
buildComplete = false;
}
public void DamageStateChanged(Actor self, AttackInfo e)
{
overlay.ReplaceAnim(RenderSprites.NormalizeSequence(overlay, e.DamageState, overlay.CurrentSequence.Name));
}
public void BuildingPlaced(Actor self)
{
overlay.Play(overlay.CurrentSequence.Name);
}
}
}

View File

@@ -0,0 +1,60 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Graphics;
using OpenRA.Mods.Common;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Rendered together with the \"make\" animation.")]
public class WithCrumbleOverlayInfo : ITraitInfo, Requires<RenderSpritesInfo>
{
[Desc("Sequence name to use")]
public readonly string Sequence = "crumble-overlay";
[Desc("Custom palette name")]
public readonly string Palette = null;
[Desc("Custom palette is a player palette BaseName")]
public readonly bool IsPlayerPalette = false;
public object Create(ActorInitializer init) { return new WithCrumbleOverlay(init, this); }
}
public class WithCrumbleOverlay : INotifyBuildComplete
{
bool buildComplete = false;
public WithCrumbleOverlay(ActorInitializer init, WithCrumbleOverlayInfo info)
{
if (init.Contains<SkipMakeAnimsInit>())
return;
var key = "make_overlay_{0}".F(info.Sequence);
var rs = init.self.Trait<RenderSprites>();
var overlay = new Animation(init.world, rs.GetImage(init.self));
// Remove the animation once it is complete
overlay.PlayThen(info.Sequence, () => init.world.AddFrameEndTask(w => rs.Remove(key)));
rs.Add(key, new AnimationWithOffset(overlay, null, () => !buildComplete),
info.Palette, info.IsPlayerPalette);
}
public void BuildingComplete(Actor self)
{
buildComplete = true;
}
}
}

View File

@@ -0,0 +1,82 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Effects;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Rendered when ProductionAirdrop is in progress.")]
public class WithDeliveryOverlayInfo : ITraitInfo, Requires<RenderSpritesInfo>, Requires<IBodyOrientationInfo>
{
[Desc("Sequence name to use")]
public readonly string Sequence = "active";
[Desc("Position relative to body")]
public readonly WVec Offset = WVec.Zero;
[Desc("Custom palette name")]
public readonly string Palette = null;
[Desc("Custom palette is a player palette BaseName")]
public readonly bool IsPlayerPalette = false;
public object Create(ActorInitializer init) { return new WithDeliveryOverlay(init.self, this); }
}
public class WithDeliveryOverlay : INotifyBuildComplete, INotifySold, INotifyDelivery
{
WithDeliveryOverlayInfo info;
Animation overlay;
bool buildComplete, delivering;
public WithDeliveryOverlay(Actor self, WithDeliveryOverlayInfo info)
{
this.info = info;
var rs = self.Trait<RenderSprites>();
var body = self.Trait<IBodyOrientation>();
buildComplete = !self.HasTrait<Building>(); // always render instantly for units
overlay = new Animation(self.World, rs.GetImage(self));
overlay.Play(info.Sequence);
rs.Add("delivery_overlay_{0}".F(info.Sequence),
new AnimationWithOffset(overlay,
() => body.LocalToWorld(info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation))),
() => !buildComplete),
info.Palette, info.IsPlayerPalette);
}
void PlayDeliveryOverlay()
{
if (delivering)
overlay.PlayThen(info.Sequence, PlayDeliveryOverlay);
}
public void BuildingComplete(Actor self)
{
self.World.AddFrameEndTask(w => w.Add(new DelayedAction(120, () =>
buildComplete = true)));
}
public void Sold(Actor self) { }
public void Selling(Actor self)
{
buildComplete = false;
}
public void IncomingDelivery(Actor self) { delivering = true; PlayDeliveryOverlay(); }
public void Delivered(Actor self) { delivering = false; }
}
}

View File

@@ -0,0 +1,82 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Effects;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Rendered when a harvester is docked.")]
public class WithDockingOverlayInfo : ITraitInfo, Requires<RenderSpritesInfo>, Requires<IBodyOrientationInfo>
{
[Desc("Sequence name to use")]
public readonly string Sequence = "docking-overlay";
[Desc("Position relative to body")]
public readonly WVec Offset = WVec.Zero;
[Desc("Custom palette name")]
public readonly string Palette = null;
[Desc("Custom palette is a player palette BaseName")]
public readonly bool IsPlayerPalette = false;
public object Create(ActorInitializer init) { return new WithDockingOverlay(init.self, this); }
}
public class WithDockingOverlay : INotifyDocking, INotifyBuildComplete, INotifySold
{
WithDockingOverlayInfo info;
Animation overlay;
bool buildComplete, docked;
public WithDockingOverlay(Actor self, WithDockingOverlayInfo info)
{
this.info = info;
var rs = self.Trait<RenderSprites>();
var body = self.Trait<IBodyOrientation>();
buildComplete = !self.HasTrait<Building>(); // always render instantly for units
overlay = new Animation(self.World, rs.GetImage(self));
overlay.Play(info.Sequence);
rs.Add("docking_overlay_{0}".F(info.Sequence),
new AnimationWithOffset(overlay,
() => body.LocalToWorld(info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation))),
() => !buildComplete),
info.Palette, info.IsPlayerPalette);
}
void PlayDockingOverlay()
{
if (docked)
overlay.PlayThen(info.Sequence, PlayDockingOverlay);
}
public void BuildingComplete(Actor self)
{
self.World.AddFrameEndTask(w => w.Add(new DelayedAction(120, () =>
buildComplete = true)));
}
public void Sold(Actor self) { }
public void Selling(Actor self)
{
buildComplete = false;
}
public void Docked(Actor self, Actor harvester) { docked = true; PlayDockingOverlay(); }
public void Undocked(Actor self, Actor harvester) { docked = false; }
}
}

View File

@@ -0,0 +1,104 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Renders an animation when the Production trait of the actor is activated.",
"Works both with per player ClassicProductionQueue and per building ProductionQueue, but needs any of these.")]
public class WithProductionOverlayInfo : ITraitInfo, Requires<RenderSpritesInfo>, Requires<IBodyOrientationInfo>
{
[Desc("Sequence name to use")]
public readonly string Sequence = "production-overlay";
[Desc("Position relative to body")]
public readonly WVec Offset = WVec.Zero;
[Desc("Custom palette name")]
public readonly string Palette = null;
[Desc("Custom palette is a player palette BaseName")]
public readonly bool IsPlayerPalette = false;
public object Create(ActorInitializer init) { return new WithProductionOverlay(init.self, this); }
}
public class WithProductionOverlay : INotifyDamageStateChanged, ITick, INotifyBuildComplete, INotifySold
{
Animation overlay;
ProductionQueue queue;
bool buildComplete;
bool IsProducing
{
get { return queue != null && queue.CurrentItem() != null && !queue.CurrentPaused; }
}
public WithProductionOverlay(Actor self, WithProductionOverlayInfo info)
{
var rs = self.Trait<RenderSprites>();
var body = self.Trait<IBodyOrientation>();
buildComplete = !self.HasTrait<Building>(); // always render instantly for units
overlay = new Animation(self.World, rs.GetImage(self));
overlay.PlayRepeating(info.Sequence);
rs.Add("production_overlay_{0}".F(info.Sequence),
new AnimationWithOffset(overlay,
() => body.LocalToWorld(info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation))),
() => !IsProducing || !buildComplete),
info.Palette, info.IsPlayerPalette);
}
public void Tick(Actor self)
{
// search for the queue here once so we don't rely on order of trait initialization
if (queue == null)
{
var production = self.TraitOrDefault<Production>();
var perBuildingQueues = self.TraitsImplementing<ProductionQueue>();
queue = perBuildingQueues.FirstOrDefault(q => q.Enabled && production.Info.Produces.Contains(q.Info.Type));
if (queue == null)
{
var perPlayerQueues = self.Owner.PlayerActor.TraitsImplementing<ProductionQueue>();
queue = perPlayerQueues.FirstOrDefault(q => q.Enabled && production.Info.Produces.Contains(q.Info.Type));
}
if (queue == null)
throw new InvalidOperationException("Can't find production queues.");
}
}
public void BuildingComplete(Actor self)
{
buildComplete = true;
}
public void Sold(Actor self) { }
public void Selling(Actor self)
{
buildComplete = false;
}
public void DamageStateChanged(Actor self, AttackInfo e)
{
overlay.ReplaceAnim(RenderSprites.NormalizeSequence(overlay, e.DamageState, overlay.CurrentSequence.Name));
}
}
}

View File

@@ -0,0 +1,79 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Throws particles when the actor is destroyed that do damage on impact.")]
public class ThrowsShrapnelInfo : ITraitInfo
{
[WeaponReference]
public string[] Weapons = { };
public int[] Pieces = { 3, 10 };
public WRange[] Range = { WRange.FromCells(2), WRange.FromCells(5) };
public object Create(ActorInitializer actor) { return new ThrowsShrapnel(this); }
}
class ThrowsShrapnel : INotifyKilled
{
readonly ThrowsShrapnelInfo info;
public ThrowsShrapnel(ThrowsShrapnelInfo info)
{
this.info = info;
}
public void Killed(Actor self, AttackInfo attack)
{
foreach (var name in info.Weapons)
{
var wep = self.World.Map.Rules.Weapons[name.ToLowerInvariant()];
var pieces = self.World.SharedRandom.Next(info.Pieces[0], info.Pieces[1]);
var range = self.World.SharedRandom.Next(info.Range[0].Range, info.Range[1].Range);
for (var i = 0; pieces > i; i++)
{
var rotation = WRot.FromFacing(self.World.SharedRandom.Next(1024));
var args = new ProjectileArgs
{
Weapon = wep,
Facing = self.World.SharedRandom.Next(-1, 255),
DamageModifiers = self.TraitsImplementing<IFirepowerModifier>()
.Select(a => a.GetFirepowerModifier()),
InaccuracyModifiers = self.TraitsImplementing<IInaccuracyModifier>()
.Select(a => a.GetInaccuracyModifier()),
Source = self.CenterPosition,
SourceActor = self,
PassiveTarget = self.CenterPosition + new WVec(range, 0, 0).Rotate(rotation)
};
self.World.AddFrameEndTask(x =>
{
if (args.Weapon.Projectile != null)
{
var projectile = args.Weapon.Projectile.Create(args);
if (projectile != null)
self.World.Add(projectile);
if (args.Weapon.Report != null && args.Weapon.Report.Any())
Sound.Play(args.Weapon.Report.Random(self.World.SharedRandom), self.CenterPosition);
}
});
}
}
}
}
}

View File

@@ -0,0 +1,66 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Mods.D2k.Widgets;
using OpenRA.Mods.RA;
using OpenRA.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.D2k.Traits
{
class ChooseBuildTabOnSelectInfo : ITraitInfo
{
public readonly string BuildPaletteWidgetName = "INGAME_BUILD_PALETTE";
public object Create(ActorInitializer init) { return new ChooseBuildTabOnSelect(init, this); }
}
class ChooseBuildTabOnSelect : INotifySelection
{
readonly World world;
readonly ChooseBuildTabOnSelectInfo info;
public ChooseBuildTabOnSelect(ActorInitializer init, ChooseBuildTabOnSelectInfo info)
{
world = init.world;
this.info = info;
}
public void SelectionChanged()
{
var palette = Ui.Root.GetOrNull<BuildPaletteWidget>(info.BuildPaletteWidgetName);
if (palette == null)
return;
// Queue-per-structure
var perqueue = world.Selection.Actors.FirstOrDefault(a => a.IsInWorld && a.World.LocalPlayer == a.Owner
&& a.TraitsImplementing<ProductionQueue>().Any(q => q.Enabled));
if (perqueue != null)
{
palette.SetCurrentTab(perqueue.TraitsImplementing<ProductionQueue>().First(q => q.Enabled));
return;
}
// Queue-per-player
var types = world.Selection.Actors.Where(a => a.IsInWorld && (a.World.LocalPlayer == a.Owner))
.SelectMany(a => a.TraitsImplementing<Production>())
.SelectMany(t => t.Info.Produces)
.ToArray();
if (types.Length == 0)
return;
palette.SetCurrentTab(world.LocalPlayer.PlayerActor.TraitsImplementing<ProductionQueue>()
.FirstOrDefault(q => q.Enabled && types.Contains(q.Info.Type)));
}
}
}

View File

@@ -0,0 +1,173 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Used to render spice with round borders.")]
public class D2kResourceLayerInfo : TraitInfo<D2kResourceLayer> { }
public class D2kResourceLayer : ResourceLayer
{
[Flags] enum ClearSides : byte
{
None = 0x0,
Left = 0x1,
Top = 0x2,
Right = 0x4,
Bottom = 0x8,
TopLeft = 0x10,
TopRight = 0x20,
BottomLeft = 0x40,
BottomRight = 0x80,
All = 0xFF
}
static readonly Dictionary<string, int[]> Variants = new Dictionary<string, int[]>()
{
{ "cleara", new[] { 0, 50 } },
{ "clearb", new[] { 1, 51 } },
{ "clearc", new[] { 43, 52 } },
{ "cleard", new[] { 0, 53 } },
};
static readonly Dictionary<ClearSides, int> SpriteMap = new Dictionary<ClearSides, int>()
{
{ ClearSides.None, 0 },
{ ClearSides.Left | ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 2 },
{ ClearSides.Top | ClearSides.Right | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 3 },
{ ClearSides.Left | ClearSides.Bottom | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 4 },
{ ClearSides.Right | ClearSides.Bottom | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 5 },
{ ClearSides.Left | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 6 },
{ ClearSides.Right | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 7 },
{ ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 8 },
{ ClearSides.Bottom | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 9 },
{ ClearSides.Left | ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft, 10 },
{ ClearSides.Top | ClearSides.Right | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomRight, 11 },
{ ClearSides.Left | ClearSides.Bottom | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 12 },
{ ClearSides.Right | ClearSides.Bottom | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 13 },
{ ClearSides.Left | ClearSides.Top | ClearSides.Right | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 14 },
{ ClearSides.Left | ClearSides.Right | ClearSides.Bottom | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 15 },
{ ClearSides.Left | ClearSides.Top | ClearSides.Bottom | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 16 },
{ ClearSides.Top | ClearSides.Right | ClearSides.Bottom | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 17 },
{ ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight, 18 },
{ ClearSides.Right | ClearSides.TopRight | ClearSides.BottomRight, 19 },
{ ClearSides.Left | ClearSides.TopLeft | ClearSides.BottomLeft, 20 },
{ ClearSides.Bottom | ClearSides.BottomLeft | ClearSides.BottomRight, 21 },
{ ClearSides.TopLeft, 22 },
{ ClearSides.TopRight, 23 },
{ ClearSides.BottomLeft, 24 },
{ ClearSides.BottomRight, 25 },
{ ClearSides.Left | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 26 },
{ ClearSides.Right | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 27 },
{ ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomRight, 28 },
{ ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft, 29 },
{ ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 30 },
{ ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 31 },
{ ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomRight, 32 },
{ ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft, 33 },
{ ClearSides.TopRight | ClearSides.BottomRight, 34 },
{ ClearSides.TopLeft | ClearSides.TopRight, 35 },
{ ClearSides.TopRight | ClearSides.BottomLeft, 36 },
{ ClearSides.TopLeft | ClearSides.BottomLeft, 37 },
{ ClearSides.BottomLeft | ClearSides.BottomRight, 38 },
{ ClearSides.TopLeft | ClearSides.BottomRight, 39 },
{ ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 40 },
{ ClearSides.Left | ClearSides.Right | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 41 },
{ ClearSides.Top | ClearSides.Bottom | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 42 },
{ ClearSides.All, 44 },
{ ClearSides.Left | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomLeft, 46 },
{ ClearSides.Right | ClearSides.TopLeft | ClearSides.TopRight | ClearSides.BottomRight, 47 },
{ ClearSides.Bottom | ClearSides.TopRight | ClearSides.BottomLeft | ClearSides.BottomRight, 48 },
{ ClearSides.Bottom | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 49 },
};
ClearSides FindClearSides(ResourceType t, CPos p)
{
var ret = ClearSides.None;
if (render[p + new CVec(0, -1)].Type != t)
ret |= ClearSides.Top | ClearSides.TopLeft | ClearSides.TopRight;
if (render[p + new CVec(-1, 0)].Type != t)
ret |= ClearSides.Left | ClearSides.TopLeft | ClearSides.BottomLeft;
if (render[p + new CVec(1, 0)].Type != t)
ret |= ClearSides.Right | ClearSides.TopRight | ClearSides.BottomRight;
if (render[p + new CVec(0, 1)].Type != t)
ret |= ClearSides.Bottom | ClearSides.BottomLeft | ClearSides.BottomRight;
if (render[p + new CVec(-1, -1)].Type != t)
ret |= ClearSides.TopLeft;
if (render[p + new CVec(1, -1)].Type != t)
ret |= ClearSides.TopRight;
if (render[p + new CVec(-1, 1)].Type != t)
ret |= ClearSides.BottomLeft;
if (render[p + new CVec(1, 1)].Type != t)
ret |= ClearSides.BottomRight;
return ret;
}
void UpdateRenderedTileInner(CPos p)
{
var t = render[p];
if (t.Density > 0)
{
var clear = FindClearSides(t.Type, p);
int index;
if (clear == ClearSides.None)
{
var sprites = Variants[t.Variant];
var frame = t.Density > t.Type.Info.MaxDensity / 2 ? 1 : 0;
t.Sprite = t.Type.Variants.First().Value[sprites[frame]];
}
else if (SpriteMap.TryGetValue(clear, out index))
t.Sprite = t.Type.Variants.First().Value[index];
else
t.Sprite = null;
}
else
t.Sprite = null;
render[p] = t;
}
protected override void UpdateRenderedSprite(CPos p)
{
// Need to update neighbouring tiles too
UpdateRenderedTileInner(p);
UpdateRenderedTileInner(p + new CVec(-1, -1));
UpdateRenderedTileInner(p + new CVec(0, -1));
UpdateRenderedTileInner(p + new CVec(1, -1));
UpdateRenderedTileInner(p + new CVec(-1, 0));
UpdateRenderedTileInner(p + new CVec(1, 0));
UpdateRenderedTileInner(p + new CVec(-1, 1));
UpdateRenderedTileInner(p + new CVec(0, 1));
UpdateRenderedTileInner(p + new CVec(1, 1));
}
protected override string ChooseRandomVariant(ResourceType t)
{
return Variants.Keys.Random(Game.CosmeticRandom);
}
}
}

View File

@@ -0,0 +1,59 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.IO;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
class FogPaletteFromR8Info : ITraitInfo
{
[Desc("Internal palette name")]
public readonly string Name = null;
[Desc("Filename to load")]
public readonly string Filename = null;
[Desc("Palette byte offset")]
public readonly long Offset = 0;
public readonly bool AllowModifiers = true;
public readonly bool InvertColor = false;
public object Create(ActorInitializer init) { return new FogPaletteFromR8(this); }
}
class FogPaletteFromR8 : ILoadsPalettes
{
readonly FogPaletteFromR8Info info;
public FogPaletteFromR8(FogPaletteFromR8Info info) { this.info = info; }
public void LoadPalettes(WorldRenderer wr)
{
var colors = new uint[Palette.Size];
using (var s = GlobalFileSystem.Open(info.Filename))
{
s.Seek(info.Offset, SeekOrigin.Begin);
for (var i = 0; i < Palette.Size; i++)
{
var packed = s.ReadUInt16();
// Fog is rendered with half opacity
colors[i] = (uint)((255 << 24) | ((packed & 0xF800) << 7) | ((packed & 0x7E0) << 4) | ((packed & 0x1f) << 2));
if (info.InvertColor)
colors[i] ^= 0x00FFFFFF;
}
}
wr.AddPalette(info.Name, new ImmutablePalette(colors), info.AllowModifiers);
}
}
}

View File

@@ -0,0 +1,57 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.IO;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
class PaletteFromR8Info : ITraitInfo
{
[Desc("Internal palette name")]
public readonly string Name = null;
[Desc("Filename to load")]
public readonly string Filename = null;
[Desc("Palette byte offset")]
public readonly long Offset = 0;
public readonly bool AllowModifiers = true;
public readonly bool InvertColor = false;
public object Create(ActorInitializer init) { return new PaletteFromR8(this); }
}
class PaletteFromR8 : ILoadsPalettes
{
readonly PaletteFromR8Info info;
public PaletteFromR8(PaletteFromR8Info info) { this.info = info; }
public void LoadPalettes(WorldRenderer wr)
{
var colors = new uint[Palette.Size];
using (var s = GlobalFileSystem.Open(info.Filename))
{
s.Seek(info.Offset, SeekOrigin.Begin);
for (var i = 0; i < Palette.Size; i++)
{
var packed = s.ReadUInt16();
colors[i] = (uint)((255 << 24) | ((packed & 0xF800) << 8) | ((packed & 0x7E0) << 5) | ((packed & 0x1f) << 3));
if (info.InvertColor)
colors[i] ^= 0x00FFFFFF;
}
}
wr.AddPalette(info.Name, new ImmutablePalette(colors), info.AllowModifiers);
}
}
}

View File

@@ -0,0 +1,69 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Create a palette by applying a scale and offset to the colors in another palette.")]
class PaletteFromScaledPaletteInfo : ITraitInfo
{
[Desc("Internal palette name")]
public readonly string Name = null;
[Desc("The name of the palette to base off.")]
public readonly string BasePalette = null;
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
[Desc("Amount to scale the base palette colors by.")]
public readonly float Scale = 1.0f;
[Desc("Amount to offset the base palette colors by.")]
public readonly int Offset = 0;
public object Create(ActorInitializer init) { return new PaletteFromScaledPalette(this); }
}
class PaletteFromScaledPalette : ILoadsPalettes
{
readonly PaletteFromScaledPaletteInfo info;
public PaletteFromScaledPalette(PaletteFromScaledPaletteInfo info) { this.info = info; }
public void LoadPalettes(WorldRenderer wr)
{
var remap = new ScaledPaletteRemap(info.Scale, info.Offset);
wr.AddPalette(info.Name, new ImmutablePalette(wr.Palette(info.BasePalette).Palette, remap), info.AllowModifiers);
}
}
class ScaledPaletteRemap : IPaletteRemap
{
readonly float scale;
readonly int offset;
public ScaledPaletteRemap(float scale, int offset)
{
this.scale = scale;
this.offset = offset;
}
public Color GetRemappedColor(Color original, int index)
{
return Color.FromArgb(original.A,
(int)Exts.Clamp((int)(scale * original.R + offset), 0, 255),
(int)Exts.Clamp((int)(scale * original.G + offset), 0, 255),
(int)Exts.Clamp((int)(scale * original.B + offset), 0, 255));
}
}
}