1 #region Copyright & License Information
2 /*
3  * Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
4  * This file is part of OpenRA, which is free software. It is made
5  * available to you under the terms of the GNU General Public License
6  * as published by the Free Software Foundation, either version 3 of
7  * the License, or (at your option) any later version. For more
8  * information, see COPYING.
9  */
10 #endregion
11 
12 using System.Collections.Generic;
13 using OpenRA.Graphics;
14 using OpenRA.Primitives;
15 using OpenRA.Traits;
16 
17 namespace OpenRA.Mods.Common.Traits
18 {
19 	public class ResourceTypeInfo : ITraitInfo, IMapPreviewSignatureInfo
20 	{
21 		[Desc("Sequence image that holds the different variants.")]
22 		public readonly string Image = "resources";
23 
24 		[FieldLoader.Require]
25 		[SequenceReference("Image")]
26 		[Desc("Randomly chosen image sequences.")]
27 		public readonly string[] Sequences = { };
28 
29 		[PaletteReference]
30 		[Desc("Palette used for rendering the resource sprites.")]
31 		public readonly string Palette = TileSet.TerrainPaletteInternalName;
32 
33 		[Desc("Resource index used in the binary map data.")]
34 		public readonly int ResourceType = 1;
35 
36 		[Desc("Credit value of a single resource unit.")]
37 		public readonly int ValuePerUnit = 0;
38 
39 		[Desc("Maximum number of resource units allowed in a single cell.")]
40 		public readonly int MaxDensity = 10;
41 
42 		[FieldLoader.Require]
43 		[Desc("Resource identifier used by other traits.")]
44 		public readonly string Type = null;
45 
46 		[FieldLoader.Require]
47 		[Desc("Resource name used by tooltips.")]
48 		public readonly string Name = null;
49 
50 		[FieldLoader.Require]
51 		[Desc("Terrain type used to determine unit movement and minimap colors.")]
52 		public readonly string TerrainType = null;
53 
54 		[Desc("Terrain types that this resource can spawn on.")]
55 		public readonly HashSet<string> AllowedTerrainTypes = new HashSet<string>();
56 
57 		[Desc("Allow resource to spawn under Mobile actors.")]
58 		public readonly bool AllowUnderActors = false;
59 
60 		[Desc("Allow resource to spawn under Buildings.")]
61 		public readonly bool AllowUnderBuildings = false;
62 
63 		[Desc("Allow resource to spawn on ramp tiles.")]
64 		public readonly bool AllowOnRamps = false;
65 
66 		[Desc("Harvester content pip color.")]
67 		public PipType PipColor = PipType.Yellow;
68 
IMapPreviewSignatureInfo.PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<Pair<MPos, Color>> destinationBuffer)69 		void IMapPreviewSignatureInfo.PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<Pair<MPos, Color>> destinationBuffer)
70 		{
71 			var tileSet = map.Rules.TileSet;
72 			var color = tileSet[tileSet.GetTerrainIndex(TerrainType)].Color;
73 
74 			for (var i = 0; i < map.MapSize.X; i++)
75 			{
76 				for (var j = 0; j < map.MapSize.Y; j++)
77 				{
78 					var cell = new MPos(i, j);
79 					if (map.Resources[cell].Type == ResourceType)
80 						destinationBuffer.Add(new Pair<MPos, Color>(cell, color));
81 				}
82 			}
83 		}
84 
Create(ActorInitializer init)85 		public object Create(ActorInitializer init) { return new ResourceType(this, init.World); }
86 	}
87 
88 	public class ResourceType : IWorldLoaded
89 	{
90 		public readonly ResourceTypeInfo Info;
91 		public PaletteReference Palette { get; private set; }
92 		public readonly Dictionary<string, Sprite[]> Variants;
93 
ResourceType(ResourceTypeInfo info, World world)94 		public ResourceType(ResourceTypeInfo info, World world)
95 		{
96 			Info = info;
97 			Variants = new Dictionary<string, Sprite[]>();
98 			foreach (var v in info.Sequences)
99 			{
100 				var seq = world.Map.Rules.Sequences.GetSequence(Info.Image, v);
101 				var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x));
102 				Variants.Add(v, sprites);
103 			}
104 		}
105 
WorldLoaded(World w, WorldRenderer wr)106 		public void WorldLoaded(World w, WorldRenderer wr)
107 		{
108 			Palette = wr.Palette(Info.Palette);
109 		}
110 	}
111 }
112