BuildingType.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using MobiusEditor.Interface;
  15. using MobiusEditor.Render;
  16. using System;
  17. using System.Drawing;
  18. namespace MobiusEditor.Model
  19. {
  20. public class BuildingType : ICellOverlapper, ICellOccupier, ITechnoType, IBrowsableType
  21. {
  22. public sbyte ID { get; private set; }
  23. public string Name { get; private set; }
  24. public string DisplayName { get; private set; }
  25. public string Tilename { get; private set; }
  26. public Rectangle OverlapBounds => new Rectangle(Point.Empty, new Size(OccupyMask.GetLength(1), OccupyMask.GetLength(0)));
  27. public bool[,] OccupyMask { get; private set; }
  28. public bool[,] BaseOccupyMask { get; private set; }
  29. public Size Size { get; private set; }
  30. public bool HasBib { get; private set; }
  31. public string OwnerHouse { get; private set; }
  32. public TheaterType[] Theaters { get; private set; }
  33. public bool IsFake { get; private set; }
  34. public bool HasTurret { get; private set; }
  35. public string FactoryOverlay { get; private set; }
  36. public Image Thumbnail { get; set; }
  37. public BuildingType(sbyte id, string name, string textId, bool[,] occupyMask, bool hasBib, string ownerHouse, TheaterType[] theaters, bool isFake, bool hasTurret, string factoryOverlay)
  38. {
  39. ID = id;
  40. Name = isFake ? (name.Substring(0, name.Length - 1) + "f") : name;
  41. DisplayName = Globals.TheGameTextManager[textId];
  42. Tilename = name;
  43. BaseOccupyMask = occupyMask;
  44. Size = new Size(BaseOccupyMask.GetLength(1), BaseOccupyMask.GetLength(0));
  45. HasBib = hasBib;
  46. OwnerHouse = ownerHouse;
  47. Theaters = theaters;
  48. IsFake = isFake;
  49. HasTurret = hasTurret;
  50. FactoryOverlay = factoryOverlay;
  51. if (HasBib)
  52. {
  53. OccupyMask = new bool[BaseOccupyMask.GetLength(0) + 1, BaseOccupyMask.GetLength(1)];
  54. for (var i = 0; i < BaseOccupyMask.GetLength(0) - 1; ++i)
  55. {
  56. for (var j = 0; j < BaseOccupyMask.GetLength(1); ++j)
  57. {
  58. OccupyMask[i, j] = BaseOccupyMask[i, j];
  59. }
  60. }
  61. for (var j = 0; j < OccupyMask.GetLength(1); ++j)
  62. {
  63. OccupyMask[OccupyMask.GetLength(0) - 2, j] = true;
  64. OccupyMask[OccupyMask.GetLength(0) - 1, j] = true;
  65. }
  66. }
  67. else
  68. {
  69. OccupyMask = BaseOccupyMask;
  70. }
  71. }
  72. public BuildingType(sbyte id, string name, string textId, bool[,] occupyMask, bool hasBib, string ownerHouse, bool isFake, bool hasTurret, string factoryOverlay)
  73. : this(id, name, textId, occupyMask, hasBib, ownerHouse, null, isFake, hasTurret, factoryOverlay)
  74. {
  75. }
  76. public BuildingType(sbyte id, string name, string textId, bool[,] occupyMask, bool hasBib, string ownerHouse)
  77. : this(id, name, textId, occupyMask, hasBib, ownerHouse, null, false, false, null)
  78. {
  79. }
  80. public BuildingType(sbyte id, string name, string textId, bool[,] occupyMask, bool hasBib, string ownerHouse, TheaterType[] theaters)
  81. : this(id, name, textId, occupyMask, hasBib, ownerHouse, theaters, false, false, null)
  82. {
  83. }
  84. public BuildingType(sbyte id, string name, string textId, bool[,] occupyMask, bool hasBib, string ownerHouse, bool isFake)
  85. : this(id, name, textId, occupyMask, hasBib, ownerHouse, null, isFake, false, null)
  86. {
  87. }
  88. public BuildingType(sbyte id, string name, string textId, bool[,] occupyMask, bool hasBib, string ownerHouse, bool isFake, bool hasTurret)
  89. : this(id, name, textId, occupyMask, hasBib, ownerHouse, null, isFake, hasTurret, null)
  90. {
  91. }
  92. public override bool Equals(object obj)
  93. {
  94. if (obj is BuildingType)
  95. {
  96. return this == obj;
  97. }
  98. else if (obj is sbyte)
  99. {
  100. return ID == (sbyte)obj;
  101. }
  102. else if (obj is string)
  103. {
  104. return string.Equals(Name, obj as string, StringComparison.OrdinalIgnoreCase);
  105. }
  106. return base.Equals(obj);
  107. }
  108. public override int GetHashCode()
  109. {
  110. return ID.GetHashCode();
  111. }
  112. public override string ToString()
  113. {
  114. return Name;
  115. }
  116. public void Init(GameType gameType, TheaterType theater, HouseType house, DirectionType direction)
  117. {
  118. var mockBuilding = new Building()
  119. {
  120. Type = this,
  121. House = house,
  122. Strength = 256,
  123. Direction = direction
  124. };
  125. var render = MapRenderer.Render(gameType, theater, Point.Empty, Globals.TileSize, Globals.TileScale, mockBuilding);
  126. if (!render.Item1.IsEmpty)
  127. {
  128. var buildingPreview = new Bitmap(render.Item1.Width, render.Item1.Height);
  129. using (var g = Graphics.FromImage(buildingPreview))
  130. {
  131. render.Item2(g);
  132. }
  133. Thumbnail = buildingPreview;
  134. }
  135. }
  136. }
  137. }