inlines.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. FinalSun/FinalAlert 2 Mission Editor
  3. Copyright (C) 1999-2024 Electronic Arts, Inc.
  4. Authored by Matthias Wagner
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #ifndef INLINES_H_INCLUDED
  17. #define INLINES_H_INCLUDED
  18. #include "functions.h"
  19. #include "macros.h"
  20. #include "mapdata.h"
  21. #include "variables.h"
  22. #include "ovrlinline.h"
  23. #include <string>
  24. #include <vector>
  25. #include <ranges>
  26. inline BOOL isTrue(CString expr)
  27. {
  28. expr.MakeLower();
  29. if (expr == "yes" || expr == "true" || expr == "1")
  30. return TRUE;
  31. return FALSE;
  32. }
  33. inline BOOL isFalse(CString expr)
  34. {
  35. expr.MakeLower();
  36. if (expr == "no" || expr == "false" || expr == "0")
  37. return TRUE;
  38. return FALSE;
  39. }
  40. // retrieve the picture filename of a unit (as it is saved in the pics map). The returned file may not exist in the pics map (you need to do a check!).
  41. inline CString GetUnitPictureFilename(LPCTSTR lpUnitName, DWORD dwPicIndex)
  42. {
  43. CIniFile& ini = Map->GetIniFile();
  44. CString UnitName = lpUnitName;
  45. UnitName = rules.sections[lpUnitName].GetValueByName("Image", lpUnitName);
  46. if (ini.sections.find(lpUnitName) != ini.sections.end())
  47. UnitName = ini.sections[lpUnitName].GetValueByName("Image", UnitName);
  48. if (rules.sections[lpUnitName].values.find("Image") != rules.sections[lpUnitName].values.end())
  49. UnitName = rules.sections[lpUnitName].values["Image"];
  50. CString artname = UnitName;
  51. if (art.sections[UnitName].values.find("Image") != art.sections[UnitName].values.end())
  52. {
  53. if (!isTrue(g_data.sections["IgnoreArtImage"].AccessValueByName(UnitName)))
  54. artname = art.sections[UnitName].AccessValueByName("Image");
  55. }
  56. CString filename = UnitName;
  57. if (art.sections[UnitName].FindName("NewTheater") >= 0 && art.sections[UnitName].AccessValueByName("DemandLoad") != "yes")
  58. if (art.sections[UnitName].AccessValueByName("NewTheater") == "yes")
  59. filename.SetAt(1, 'T');
  60. char n[50];
  61. itoa(dwPicIndex, n, 10);
  62. if (pics.find(artname + n) != pics.end())
  63. {
  64. filename = artname; // yes, found
  65. filename += n;
  66. }
  67. else if (pics.find(artname + ".bmp") != pics.end()) // since June, 15th (Matze): Only use BMP if no SHP/VXL exists
  68. {
  69. filename = (CString)artname + ".bmp";
  70. }
  71. else
  72. filename = "";
  73. return filename;
  74. }
  75. inline CString GetParam(const CString& data, const int param)
  76. {
  77. int paramStrPos = 0;
  78. int curParam = param;
  79. while (curParam--)
  80. {
  81. auto nextComma = data.Find(',', paramStrPos);
  82. if (nextComma < 0)
  83. return CString(); // RVO; param not found
  84. paramStrPos = nextComma + 1;
  85. }
  86. auto nextComma = data.Find(',', paramStrPos);
  87. CString res = data.Mid(paramStrPos, nextComma < 0 ? data.GetLength() - paramStrPos : nextComma - paramStrPos);
  88. return res; // RVO
  89. }
  90. inline std::string GetParam(const std::string& data, const int param)
  91. {
  92. int paramStrPos = 0;
  93. int curParam = param;
  94. while (curParam--)
  95. {
  96. auto nextComma = data.find(',', paramStrPos);
  97. if (nextComma == std::string::npos)
  98. return std::string(); // RVO; param not found
  99. paramStrPos = nextComma + 1;
  100. }
  101. auto nextComma = data.find(',', paramStrPos);
  102. std::string res = data.substr(paramStrPos, nextComma == std::string::npos ? data.size() - paramStrPos : nextComma - paramStrPos);
  103. return res; // RVO
  104. }
  105. inline CString GetParam(const char* data, const int param)
  106. {
  107. return GetParam(CString(data), param);
  108. }
  109. inline std::vector<CString> Split(const CString& data, char separator)
  110. {
  111. int nextComma = -1;
  112. int lastComma = -1;
  113. const auto len = data.GetLength();
  114. std::vector<CString> res;
  115. while (lastComma < len)
  116. {
  117. nextComma = data.Find(separator, lastComma + 1);
  118. if (nextComma < 0)
  119. {
  120. res.push_back(data.Mid(lastComma + 1));
  121. break;
  122. }
  123. res.push_back(data.Mid(lastComma + 1, (nextComma - lastComma - 1)));
  124. lastComma = nextComma;
  125. }
  126. return res; // RVO
  127. }
  128. inline std::vector<CString> SplitParams(const CString& data)
  129. {
  130. return Split(data, ',');
  131. }
  132. inline CString Join(const CString& join, const std::vector<CString>& strings)
  133. {
  134. CString res;
  135. int len = 0;
  136. for (auto& s : strings)
  137. len += s.GetLength() + join.GetLength();
  138. res.Preallocate(len + 1);
  139. int remaining = strings.size();
  140. for (auto& s : strings)
  141. {
  142. res += s;
  143. if (--remaining)
  144. res += join;
  145. }
  146. return res; // RVO
  147. }
  148. inline std::string Join(const std::string& join, const std::ranges::input_range auto&& strings)
  149. {
  150. std::string res;
  151. int len = 0;
  152. for (const auto& s : strings)
  153. len += s.size() + join.size();
  154. res.reserve(len + 1);
  155. int remaining = strings.size();
  156. for (const auto& s : strings)
  157. {
  158. res += s;
  159. if (--remaining)
  160. res += join;
  161. }
  162. return res; // RVO
  163. }
  164. inline CString SetParam(const CString& data, const int param, const CString& value)
  165. {
  166. // This could be optimized, but SetParam is usually no performance issue.
  167. if (param < 0)
  168. return data;
  169. std::vector<CString> params = SplitParams(data);
  170. params.resize(max(param + 1, static_cast<int>(params.size())));
  171. params[param] = value;
  172. return Join(",", params);
  173. }
  174. [[deprecated("Instead use CMapData::ToMapCoords")]]
  175. inline void ToIso(int* x, int* y)
  176. {
  177. auto r = Map->ToMapCoords(ProjectedCoords(*x, *y));
  178. *x = r.x;
  179. *y = r.y;
  180. }
  181. [[deprecated("Instead use CMapData::ProjectCoords")]]
  182. inline void ToPhys(int* x, int* y)
  183. {
  184. auto r = Map->ProjectCoords(MapCoords(*x, *y));
  185. *x = r.x;
  186. *y = r.y;
  187. }
  188. [[deprecated("Instead use CMapData::ProjectCoords3d")]]
  189. inline void ToPhys3d(int* x, int* y)
  190. {
  191. auto r = Map->ProjectCoords3d(MapCoords(*x, *y));
  192. *x = r.x;
  193. *y = r.y;
  194. }
  195. [[deprecated("Instead use CMapData::ProjectCoords3d")]]
  196. inline void ToPhys3d(int* x, int* y, int mapZ)
  197. {
  198. auto r = Map->ProjectCoords3d(MapCoords(*x, *y), mapZ);
  199. *x = r.x;
  200. *y = r.y;
  201. }
  202. [[deprecated("Instead use CMapData::ToMapCoords3d")]]
  203. inline void ToIso3d(int* x, int* y, int mapZ)
  204. {
  205. auto r = Map->ToMapCoords3d(ProjectedCoords(*x, *y), mapZ);
  206. *x = r.x;
  207. *y = r.y;
  208. }
  209. inline BOOL isSame(CString expr1, CString expr2)
  210. {
  211. expr1.MakeLower();
  212. expr2.MakeLower();
  213. if (expr1 == expr2) return TRUE;
  214. return FALSE;
  215. }
  216. inline BOOL isIncluded(CString searchString, CString base)
  217. {
  218. searchString.MakeLower();
  219. base.MakeLower();
  220. if (searchString.Find(base) < 0) return FALSE;
  221. return TRUE;
  222. }
  223. inline BOOL isTrack(int type)
  224. {
  225. return(type >= OVRL_TRACK_BEGIN && type <= OVRL_TRACK_END);
  226. }
  227. inline BOOL isBigBridge(int type)
  228. {
  229. #ifndef RA2_MODE
  230. return((type >= 0x18 && type <= 0x19) || (type >= 0x3b && type <= 0x3c));
  231. #else
  232. return((type >= 0x18 && type <= 0x19) || (type >= 0x3b && type <= 0x3c) || (type >= 0xed && type <= 0xee));
  233. #endif
  234. }
  235. #endif