Merge pull request #6663 from reaperrr/common-stuff4

Moved more stuff to Mods.Common
This commit is contained in:
Matthias Mailänder
2014-10-05 10:10:14 +02:00
19 changed files with 49 additions and 28 deletions

View File

@@ -13,5 +13,10 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
public interface INotifyResourceClaimLost
{
void OnNotifyResourceClaimLost(Actor self, ResourceClaim claim, Actor claimer);
}
public interface INotifyChat { bool OnChat(string from, string message); }
}

View File

@@ -0,0 +1,43 @@
#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.Collections.Generic;
using OpenRA.Effects;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
namespace OpenRA.Mods.Common.Effects
{
public class ContrailFader : IEffect
{
WPos pos;
ContrailRenderable trail;
int ticks;
public ContrailFader(WPos pos, ContrailRenderable trail)
{
this.pos = pos;
this.trail = trail;
}
public void Tick(World world)
{
if (ticks++ == trail.Length)
world.AddFrameEndTask(w => w.Remove(this));
trail.Update(pos);
}
public IEnumerable<IRenderable> Render(WorldRenderer wr)
{
yield return trail;
}
}
}

View File

@@ -0,0 +1,45 @@
#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.Collections.Generic;
using OpenRA.Effects;
using OpenRA.Graphics;
namespace OpenRA.Mods.Common.Effects
{
public class Smoke : IEffect
{
readonly World world;
readonly WPos pos;
readonly CPos cell;
readonly Animation anim;
public Smoke(World world, WPos pos, string trail)
{
this.world = world;
this.pos = pos;
this.cell = world.Map.CellContaining(pos);
anim = new Animation(world, trail);
anim.PlayThen("idle",
() => world.AddFrameEndTask(w => w.Remove(this)));
}
public void Tick(World world) { anim.Tick(); }
public IEnumerable<IRenderable> Render(WorldRenderer wr)
{
if (world.FogObscures(cell))
return SpriteRenderable.None;
return anim.Render(pos, wr.Palette("effect"));
}
}
}

View File

@@ -42,8 +42,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CommonTraitsInterfaces.cs" />
<Compile Include="Effects\ContrailFader.cs" />
<Compile Include="Effects\Explosion.cs" />
<Compile Include="Effects\FloatingText.cs" />
<Compile Include="Effects\Smoke.cs" />
<Compile Include="Console\ChatCommands.cs" />
<Compile Include="Console\DevCommands.cs" />
<Compile Include="Console\HelpCommand.cs" />
@@ -52,12 +54,16 @@
<Compile Include="Graphics\BeamRenderable.cs" />
<Compile Include="Graphics\ContrailRenderable.cs" />
<Compile Include="Graphics\RangeCircleRenderable.cs" />
<Compile Include="Graphics\SpriteActorPreview.cs" />
<Compile Include="Graphics\TextRenderable.cs" />
<Compile Include="Graphics\VoxelActorPreview.cs" />
<Compile Include="Graphics\VoxelRenderable.cs" />
<Compile Include="ModChooserLoadScreen.cs" />
<Compile Include="PaletteFromFile.cs" />
<Compile Include="Traits\ProvidesRadar.cs" />
<Compile Include="ServerTraits\ColorValidator.cs" />
<Compile Include="ServerTraits\MasterServerPinger.cs" />
<Compile Include="ServerTraits\PlayerPinger.cs" />
<Compile Include="Traits\ProvidesRadar.cs" />
<Compile Include="UtilityCommands\ConvertPngToShpCommand.cs" />
<Compile Include="UtilityCommands\ConvertSpriteToPngCommand.cs" />
<Compile Include="UtilityCommands\ExtractFilesCommand.cs" />
@@ -74,10 +80,7 @@
<Compile Include="UtilityCommands\UpgradeMapCommand.cs" />
<Compile Include="UtilityCommands\UpgradeModCommand.cs" />
<Compile Include="UtilityCommands\UpgradeRules.cs" />
<Compile Include="Graphics\SpriteActorPreview.cs" />
<Compile Include="Graphics\TextRenderable.cs" />
<Compile Include="Graphics\VoxelActorPreview.cs" />
<Compile Include="Graphics\VoxelRenderable.cs" />
<Compile Include="Warheads\LeaveSmudgeWarhead.cs" />
<Compile Include="Widgets\ConfirmationDialogs.cs" />
<Compile Include="Widgets\HueSliderWidget.cs" />
<Compile Include="Widgets\LabelWithTooltipWidget.cs" />
@@ -95,7 +98,11 @@
<Compile Include="Widgets\MenuButtonWidget.cs" />
<Compile Include="Widgets\RadarWidget.cs" />
<Compile Include="Widgets\ResourceBarWidget.cs" />
<Compile Include="World\PlayMusicOnMapLoad.cs" />
<Compile Include="World\RadarPings.cs" />
<Compile Include="World\ResourceClaim.cs" />
<Compile Include="World\ResourceClaimLayer.cs" />
<Compile Include="World\SmudgeLayer.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@@ -0,0 +1,51 @@
#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.Effects;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
public class LeaveSmudgeWarhead : Warhead
{
[Desc("Size of the area. A smudge will be created in each tile.", "Provide 2 values for a ring effect (outer/inner).")]
public readonly int[] Size = { 0, 0 };
[Desc("Type of smudge to apply to terrain.")]
public readonly string[] SmudgeType = { };
public override void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers)
{
var world = firedBy.World;
var targetTile = world.Map.CellContaining(target.CenterPosition);
var smudgeLayers = world.WorldActor.TraitsImplementing<SmudgeLayer>().ToDictionary(x => x.Info.Type);
var minRange = (Size.Length > 1 && Size[1] > 0) ? Size[1] : 0;
var allCells = world.Map.FindTilesInAnnulus(targetTile, minRange, Size[0]);
// Draw the smudges:
foreach (var sc in allCells)
{
var smudgeType = world.Map.GetTerrainInfo(sc).AcceptsSmudgeType.FirstOrDefault(t => SmudgeType.Contains(t));
if (smudgeType == null) continue;
SmudgeLayer smudgeLayer;
if (!smudgeLayers.TryGetValue(smudgeType, out smudgeLayer))
throw new NotImplementedException("Unknown smudge type `{0}`".F(smudgeType));
smudgeLayer.AddSmudge(sc);
}
}
}
}

View File

@@ -0,0 +1,51 @@
#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 OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
class PlayMusicOnMapLoadInfo : ITraitInfo
{
public readonly string Music = null;
public readonly bool Loop = false;
public object Create(ActorInitializer init) { return new PlayMusicOnMapLoad(init.world, this); }
}
class PlayMusicOnMapLoad : IWorldLoaded
{
readonly PlayMusicOnMapLoadInfo info;
readonly World world;
public PlayMusicOnMapLoad(World world, PlayMusicOnMapLoadInfo info)
{
this.world = world;
this.info = info;
}
public void WorldLoaded(World world, WorldRenderer wr)
{
PlayMusic();
}
void PlayMusic()
{
var onComplete = info.Loop ? (Action)PlayMusic : () => {};
if (Game.Settings.Sound.MapMusic &&
world.Map.Rules.Music.ContainsKey(info.Music))
Sound.PlayMusicThen(world.Map.Rules.Music[info.Music], onComplete);
}
}
}

View File

@@ -0,0 +1,20 @@
#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
namespace OpenRA.Mods.Common
{
public sealed class ResourceClaim
{
public readonly Actor Claimer;
public CPos Cell;
public ResourceClaim(Actor claimer, CPos cell) { Claimer = claimer; Cell = cell; }
}
}

View File

@@ -0,0 +1,128 @@
#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.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Allows harvesters to coordinate their operations. Attach this to the world actor.")]
public sealed class ResourceClaimLayerInfo : TraitInfo<ResourceClaimLayer> { }
public sealed class ResourceClaimLayer : IWorldLoaded
{
Dictionary<CPos, ResourceClaim> claimByCell;
Dictionary<Actor, ResourceClaim> claimByActor;
private void MakeClaim(Actor claimer, CPos cell)
{
UnclaimByActor(claimer);
UnclaimByCell(cell, claimer);
claimByActor[claimer] = claimByCell[cell] = new ResourceClaim(claimer, cell);
}
private void Unclaim(ResourceClaim claim, Actor claimer)
{
if (claimByActor.Remove(claim.Claimer) & claimByCell.Remove(claim.Cell))
{
if (claim.Claimer.Destroyed) return;
if (!claim.Claimer.IsInWorld) return;
if (claim.Claimer.IsDead()) return;
claim.Claimer.Trait<INotifyResourceClaimLost>().OnNotifyResourceClaimLost(claim.Claimer, claim, claimer);
}
}
public void WorldLoaded(World w, WorldRenderer wr)
{
// NOTE(jsd): 32 seems a sane default initial capacity for the total # of harvesters in a game. Purely a guesstimate.
claimByCell = new Dictionary<CPos, ResourceClaim>(32);
claimByActor = new Dictionary<Actor, ResourceClaim>(32);
}
/// <summary>
/// Attempt to claim the resource at the cell for the given actor.
/// </summary>
/// <param name="claimer"></param>
/// <param name="cell"></param>
/// <returns></returns>
public bool ClaimResource(Actor claimer, CPos cell)
{
// Has anyone else claimed this point?
ResourceClaim claim;
if (claimByCell.TryGetValue(cell, out claim))
{
// Same claimer:
if (claim.Claimer == claimer) return true;
// This is to prevent in-fighting amongst friendly harvesters:
if (claimer.Owner == claim.Claimer.Owner) return false;
if (claimer.Owner.Stances[claim.Claimer.Owner] == Stance.Ally) return false;
// If an enemy/neutral claimed this, don't respect that claim:
}
// Either nobody else claims this point or an enemy/neutral claims it:
MakeClaim(claimer, cell);
return true;
}
/// <summary>
/// Release the last resource claim made on this cell.
/// </summary>
/// <param name="cell"></param>
public void UnclaimByCell(CPos cell, Actor claimer)
{
ResourceClaim claim;
if (claimByCell.TryGetValue(cell, out claim))
Unclaim(claim, claimer);
}
/// <summary>
/// Release the last resource claim made by this actor.
/// </summary>
/// <param name="claimer"></param>
public void UnclaimByActor(Actor claimer)
{
ResourceClaim claim;
if (claimByActor.TryGetValue(claimer, out claim))
Unclaim(claim, claimer);
}
/// <summary>
/// Is the cell location <paramref name="cell"/> claimed for harvesting by any other actor?
/// </summary>
/// <param name="self"></param>
/// <param name="cell"></param>
/// <returns>true if already claimed by an ally that isn't <paramref name="self"/>; false otherwise.</returns>
public bool IsClaimedByAnyoneElse(Actor self, CPos cell, out ResourceClaim claim)
{
if (claimByCell.TryGetValue(cell, out claim))
{
// Same claimer:
if (claim.Claimer == self) return false;
// This is to prevent in-fighting amongst friendly harvesters:
if (self.Owner == claim.Claimer.Owner) return true;
if (self.Owner.Stances[claim.Claimer.Owner] == Stance.Ally) return true;
// If an enemy/neutral claimed this, don't respect that claim and fall through:
}
else
{
// No claim.
claim = null;
}
return false;
}
}
}

View File

@@ -0,0 +1,145 @@
#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.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Attach this to the world actor.", "Order of the layers defines the Z sorting.")]
public class SmudgeLayerInfo : ITraitInfo
{
public readonly string Type = "Scorch";
[Desc("Sprite sequence name")]
public readonly string Sequence = "scorch";
public readonly int SmokePercentage = 25;
[Desc("Sprite sequence name")]
public readonly string SmokeType = "smoke_m";
public readonly string Palette = "terrain";
public object Create(ActorInitializer init) { return new SmudgeLayer(this); }
}
public class SmudgeLayer : IRenderOverlay, IWorldLoaded, ITickRender
{
struct Smudge
{
public string Type;
public int Depth;
public Sprite Sprite;
}
public SmudgeLayerInfo Info;
Dictionary<CPos, Smudge> tiles;
Dictionary<CPos, Smudge> dirty;
Dictionary<string, Sprite[]> smudges;
World world;
public SmudgeLayer(SmudgeLayerInfo info)
{
this.Info = info;
}
public void WorldLoaded(World w, WorldRenderer wr)
{
world = w;
tiles = new Dictionary<CPos, Smudge>();
dirty = new Dictionary<CPos, Smudge>();
smudges = new Dictionary<string, Sprite[]>();
var types = world.Map.SequenceProvider.Sequences(Info.Sequence);
foreach (var t in types)
{
var seq = world.Map.SequenceProvider.GetSequence(Info.Sequence, t);
var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x));
smudges.Add(t, sprites);
}
// Add map smudges
foreach (var s in w.Map.Smudges.Value.Where(s => smudges.Keys.Contains(s.Type)))
{
var smudge = new Smudge
{
Type = s.Type,
Depth = s.Depth,
Sprite = smudges[s.Type][s.Depth]
};
tiles.Add((CPos)s.Location, smudge);
}
}
public void AddSmudge(CPos loc)
{
if (Game.CosmeticRandom.Next(0, 100) <= Info.SmokePercentage)
world.AddFrameEndTask(w => w.Add(new Smoke(w, world.Map.CenterOfCell(loc), Info.SmokeType)));
if (!dirty.ContainsKey(loc) && !tiles.ContainsKey(loc))
{
// No smudge; create a new one
var st = smudges.Keys.Random(world.SharedRandom);
dirty[loc] = new Smudge { Type = st, Depth = 0, Sprite = smudges[st][0] };
}
else
{
// Existing smudge; make it deeper
var tile = dirty.ContainsKey(loc) ? dirty[loc] : tiles[loc];
var maxDepth = smudges[tile.Type].Length;
if (tile.Depth < maxDepth - 1)
{
tile.Depth++;
tile.Sprite = smudges[tile.Type][tile.Depth];
}
dirty[loc] = tile;
}
}
public void TickRender(WorldRenderer wr, Actor self)
{
var remove = new List<CPos>();
foreach (var kv in dirty)
{
if (!self.World.FogObscures(kv.Key))
{
tiles[kv.Key] = kv.Value;
remove.Add(kv.Key);
}
}
foreach (var r in remove)
dirty.Remove(r);
}
public void Render(WorldRenderer wr)
{
var pal = wr.Palette(Info.Palette);
foreach (var kv in tiles)
{
if (!wr.Viewport.VisibleCells.Contains(kv.Key))
continue;
if (world.ShroudObscures(kv.Key))
continue;
new SpriteRenderable(kv.Value.Sprite, world.Map.CenterOfCell(kv.Key),
WVec.Zero, -511, pal, 1f, true).Render(wr); // TODO ZOffset is ignored
}
}
}
}