QACheck.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SuperTux Editor
  2. // Copyright (C) 2007 Arvid Norlander <anmaster AT berlios DOT de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.Collections.Generic;
  18. using Gtk;
  19. /// <summary>
  20. /// Functions to check for common problems in levels.
  21. /// </summary>
  22. public static class QACheck
  23. {
  24. #region CheckIds
  25. /// <summary>
  26. /// Check a tile block for nonexistent tile ids.
  27. /// </summary>
  28. /// <param name="tiles">TileBlock to check</param>
  29. /// <param name="Tileset">Tileset where the ids should be defined</param>
  30. /// <returns>List of invalid tile ids.</returns>
  31. public static List<int> CheckIds(TileBlock tiles, Tileset Tileset) {
  32. List<int> invalidTiles = new List<int>();
  33. for (int y = 0; y < tiles.Height; ++y) {
  34. for (int x = 0; x < tiles.Width; ++x) {
  35. int TileId = tiles[x, y];
  36. if (!Tileset.IsValid(TileId)) {
  37. if (invalidTiles.IndexOf(TileId) == -1)
  38. invalidTiles.Add(TileId);
  39. }
  40. }
  41. }
  42. return invalidTiles;
  43. }
  44. public static void CheckIds(Application application, Sector sector, bool AlertGood) {
  45. System.Text.StringBuilder sb = new System.Text.StringBuilder("These tilemaps have bad ids in sector " + sector.Name + ":");
  46. List<int> invalidtiles;
  47. // Any bad found yet?
  48. bool bad = false;
  49. foreach (Tilemap tilemap in sector.GetObjects(typeof(Tilemap))) {
  50. invalidtiles = CheckIds(tilemap, application.CurrentLevel.Tileset);
  51. if (invalidtiles.Count != 0) {
  52. bad = true;
  53. if (String.IsNullOrEmpty(tilemap.Name))
  54. sb.Append(Environment.NewLine + "Tilemap (" + tilemap.Layer + ")");
  55. else
  56. sb.Append(Environment.NewLine + tilemap.Name + " (" + tilemap.Layer + ")");
  57. }
  58. }
  59. MessageType msgtype;
  60. string message;
  61. if (! bad) {
  62. if (! AlertGood)
  63. return;
  64. msgtype = MessageType.Info;
  65. message = "No invalid tile ids in any tilemap in sector " + sector.Name + ".";
  66. } else {
  67. msgtype = MessageType.Warning;
  68. message = sb.ToString();
  69. }
  70. MessageDialog md = new MessageDialog(null, DialogFlags.DestroyWithParent,
  71. msgtype, ButtonsType.Close, message);
  72. md.Run();
  73. md.Destroy();
  74. }
  75. #endregion CheckIds
  76. // 26 -> 83
  77. // 63 -> 70
  78. // 101 -> 93
  79. /// <summary>
  80. /// A map for replacing deprecated tiles with new ones automatically.
  81. /// </summary>
  82. private static SortedList<int, int> LevelReplaceMap = new SortedList<int, int>();
  83. /// <summary>
  84. /// Initialize the ReplaceMap.
  85. /// </summary>
  86. static QACheck() {
  87. // Keep this sorted on key to make it faster.
  88. LevelReplaceMap.Add(26, 83);
  89. LevelReplaceMap.Add(63, 70);
  90. LevelReplaceMap.Add(101, 93);
  91. }
  92. #region ReplaceDeprecatedTiles
  93. /// <summary>
  94. /// Replace deprecated tiles in tileblocks.
  95. /// </summary>
  96. /// <param name="tiles">The tileblock</param>
  97. /// <param name="TilesetFile">The TileSet file.</param>
  98. private static void ReplaceDeprecatedTiles(TileBlock tiles, string TilesetFile) {
  99. // We don't have any worldmap one currently
  100. if (TilesetFile != "images/tiles.strf")
  101. return;
  102. for (int y = 0; y < tiles.Height; ++y) {
  103. for (int x = 0; x < tiles.Width; ++x) {
  104. int TileId = tiles[x, y];
  105. if (LevelReplaceMap.ContainsKey(TileId)) {
  106. tiles[x, y] = LevelReplaceMap[TileId];
  107. LogManager.Log(LogLevel.Info, "Replaced deprecated tile {0} with {1}", TileId, LevelReplaceMap[TileId]);
  108. }
  109. }
  110. }
  111. }
  112. public static void ReplaceDeprecatedTiles(Level level) {
  113. foreach (Sector sector in level.Sectors) {
  114. foreach (Tilemap tilemap in sector.GetObjects(typeof(Tilemap)))
  115. ReplaceDeprecatedTiles(tilemap, level.TilesetFile);
  116. }
  117. }
  118. #endregion ReplaceDeprecatedTiles
  119. #region CheckDirection
  120. private static void CheckBadDirection(SimpleDirObject dirobject) {
  121. if (dirobject.Direction == SimpleDirObject.Directions.auto) {
  122. string message = String.Format("The {0} at x={1} y={2} has direction set to auto. Setting the direction of {0} objects to auto is a bad idea.",
  123. dirobject.GetType().Name, dirobject.X, dirobject.Y);
  124. MessageDialog md = new MessageDialog(null, DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Close, message);
  125. md.Run();
  126. md.Destroy();
  127. }
  128. }
  129. public static void CheckObjectDirections(Level level) {
  130. foreach (Sector sector in level.Sectors) {
  131. // This is hackish, I know
  132. foreach (SimpleDirObject dirobject in sector.GetObjects(typeof(Ispy)))
  133. CheckBadDirection(dirobject);
  134. foreach (SimpleDirObject dirobject in sector.GetObjects(typeof(DartTrap)))
  135. CheckBadDirection(dirobject);
  136. foreach (SimpleDirObject dirobject in sector.GetObjects(typeof(Dispenser)))
  137. CheckBadDirection(dirobject);
  138. }
  139. }
  140. #endregion CheckDirection
  141. public static void CheckLicense(Level level) {
  142. if (String.IsNullOrEmpty(level.License)) {
  143. MessageDialog md = new MessageDialog(null, DialogFlags.DestroyWithParent,
  144. MessageType.Warning, ButtonsType.Close, "No license is set for this level! Please make sure to fix this (setting is under Level menu -> Properties).");
  145. md.Run();
  146. md.Destroy();
  147. }
  148. }
  149. }
  150. /* EOF */