y_loader_entry.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. Legal:
  3. Version: MPL 1.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 the "License"; you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the YSI framework.
  13. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  14. Portions created by the Initial Developer are Copyright C 2011
  15. the Initial Developer. All Rights Reserved.
  16. Contributors:
  17. Y_Less
  18. koolk
  19. JoeBullet/Google63
  20. g_aSlice/Slice
  21. Misiur
  22. samphunter
  23. tianmeta
  24. maddinat0r
  25. spacemud
  26. Crayder
  27. Dayvison
  28. Ahmad45123
  29. Zeex
  30. irinel1996
  31. Yiin-
  32. Chaprnks
  33. Konstantinos
  34. Masterchen09
  35. Southclaws
  36. PatchwerkQWER
  37. m0k1
  38. paulommu
  39. udan111
  40. Thanks:
  41. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  42. ZeeX - Very productive conversations.
  43. koolk - IsPlayerinAreaEx code.
  44. TheAlpha - Danish translation.
  45. breadfish - German translation.
  46. Fireburn - Dutch translation.
  47. yom - French translation.
  48. 50p - Polish translation.
  49. Zamaroht - Spanish translation.
  50. Los - Portuguese translation.
  51. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  52. me to strive to better.
  53. Pixels^ - Running XScripters where the idea was born.
  54. Matite - Pestering me to release it and using it.
  55. Very special thanks to:
  56. Thiadmer - PAWN, whose limits continue to amaze me!
  57. Kye/Kalcor - SA:MP.
  58. SA:MP Team past, present and future - SA:MP.
  59. Optional plugins:
  60. Gamer_Z - GPS.
  61. Incognito - Streamer.
  62. Me - sscanf2, fixes2, Whirlpool.
  63. */
  64. #include "..\..\YSI_Core\y_utils"
  65. #include "..\..\YSI_Storage\y_xml"
  66. static stock
  67. XML:YSI_g_sXMLRules = NO_XML_FILE,
  68. #if defined _YSI_VISUAL_OBJECTS
  69. YSI_g_sRaceObjects[ceildiv(MAX_DYN_OBJECTS, 4)] = {-1, ...},
  70. #endif
  71. YSI_g_sCurRace,
  72. YSI_g_sCurWorld;
  73. forward Loader_Spawn();
  74. forward Loader_Check();
  75. forward Loader_Obj();
  76. /*-------------------------------------------------------------------------*//**
  77. * <remarks>
  78. * Defines the parsing rules for reading MTA XML maps.
  79. * </remarks>
  80. *//*------------------------------------------------------------------------**/
  81. Loader_Loader()
  82. {
  83. if (YSI_g_sXMLRules == NO_XML_FILE)
  84. {
  85. YSI_g_sXMLRules = XML_New();
  86. if (YSI_g_sXMLRules != NO_XML_FILE)
  87. {
  88. XML_AddHandler(YSI_g_sXMLRules, "object", "Loader_Obj");
  89. XML_AddHandler(YSI_g_sXMLRules, "checkpoint", "Loader_Check");
  90. XML_AddHandler(YSI_g_sXMLRules, "spawnpoint", "Loader_Spawn");
  91. }
  92. }
  93. return 1;
  94. }
  95. /*-------------------------------------------------------------------------*//**
  96. * <param name="filename">File to parse as an XML race file.</param>
  97. * <param name="laps">Number of laps to race for.</param>
  98. * <param name="entry">Cost of entry.</param>
  99. * <param name="countdown">Time to count down from for start.</param>
  100. * <param name="arial">Use arial checkpoints instead.</param>
  101. * <param name="fixedPrize">Set prize amounts manually.</param>
  102. * <param name="exitTime">Time allowed out a vehicle before fail.</param>
  103. * <param name="interior">The interior of the race.</param>
  104. * <param name="world">The world of the race.</param>
  105. * <param name="restart">Don't destroy the race on completion.</param>
  106. * <returns>Created race.</returns>
  107. * <remarks>
  108. * Tries to create a new race to save to then parses the file.
  109. * </remarks>
  110. *//*------------------------------------------------------------------------**/
  111. stock Loader_Parse(filename[], laps = 0, entry = 0, countdown = 3, bool:arial = false, bool:fixedPrize = true, exitTime = 0, interior = 0, world = 0, bool:restart = false)
  112. {
  113. if (!fexist(filename)) return NO_RACE;
  114. new
  115. race = Race_Create(laps, entry, countdown, arial, fixedPrize, exitTime, interior, world, restart);
  116. if (race != NO_RACE)
  117. {
  118. YSI_g_sCurRace = race;
  119. YSI_g_sCurWorld = world;
  120. XML_Parse(YSI_g_sXMLRules, filename);
  121. }
  122. YSI_g_sCurWorld = 0;
  123. YSI_g_sCurRace = NO_RACE;
  124. return race;
  125. }
  126. /*-------------------------------------------------------------------------*//**
  127. * <param name="filename">File to parse.</param>
  128. * <param name="world">World for the objects to appear in or -1 for all.</param>
  129. * <remarks>
  130. * Parses a map file, loading only the objects.
  131. * </remarks>
  132. *//*------------------------------------------------------------------------**/
  133. stock Loader_ParseObjects(filename[], world = -1)
  134. {
  135. YSI_g_sCurWorld = world;
  136. return XML_Parse(YSI_g_sXMLRules, filename);
  137. }
  138. /*-------------------------------------------------------------------------*//**
  139. * <param name="race">Race to remove objects for.</param>
  140. * <remarks>
  141. * Destroys all the objects created for a race loaded with the loader.
  142. * </remarks>
  143. *//*------------------------------------------------------------------------**/
  144. stock Loader_Unload(race)
  145. {
  146. #if defined _YSI_VISUAL_OBJECTS
  147. new
  148. race1 = race << 8,
  149. race2 = race << 16,
  150. race3 = race << 24;
  151. for (new i = 0; i < sizeof (YSI_g_sRaceObjects); i++)
  152. {
  153. if (YSI_g_sRaceObjects[i] & 0xFF000000 == race3)
  154. {
  155. YSI_g_sRaceObjects[i] |= 0xFF000000;
  156. DestroyDynamicObject((i * 4) + 3);
  157. }
  158. if (YSI_g_sRaceObjects[i] & 0x00FF0000 == race2)
  159. {
  160. YSI_g_sRaceObjects[i] |= 0x00FF0000;
  161. DestroyDynamicObject((i * 4) + 2);
  162. }
  163. if (YSI_g_sRaceObjects[i] & 0x0000FF00 == race1)
  164. {
  165. YSI_g_sRaceObjects[i] |= 0x0000FF00;
  166. DestroyDynamicObject((i * 4) + 1);
  167. }
  168. if (YSI_g_sRaceObjects[i] & 0x000000FF == race)
  169. {
  170. YSI_g_sRaceObjects[i] |= 0x000000FF;
  171. DestroyDynamicObject((i * 4));
  172. }
  173. }
  174. #else
  175. #pragma unused race
  176. #endif
  177. }
  178. /*-------------------------------------------------------------------------*//**
  179. * <param name="radians">Radian angle to convert to degrees.</param>
  180. * <returns>Float</returns>
  181. * <remarks>
  182. * Based on mtarad2deg made by Trix and fixed by Mike. Converts radians to degrees
  183. * and rationalises.
  184. * </remarks>
  185. *//*------------------------------------------------------------------------**/
  186. stock Float:Loader_Convert(Float:radians)
  187. {
  188. if (radians == 0.0)
  189. {
  190. return 0.0;
  191. }
  192. new Float:retval = (360.0 + (radians / 0.0174532925));
  193. while (retval >= 360.0) retval -= 360.0;
  194. while (retval < 0.0) retval += 360.0;
  195. return retval;
  196. }
  197. /*-------------------------------------------------------------------------*//**
  198. * <returns>CreateDynamicObject</returns>
  199. * <remarks>
  200. * Called when an end object tag is reached to create an object.
  201. * </remarks>
  202. *//*------------------------------------------------------------------------**/
  203. public Loader_Obj()
  204. {
  205. #if defined _YSI_VISUAL_OBJECTS
  206. static
  207. name[MAX_XML_ENTRY_NAME],
  208. val[MAX_XML_ENTRY_TEXT];
  209. new
  210. Float:x,
  211. Float:y,
  212. Float:z,
  213. Float:rx,
  214. Float:ry,
  215. Float:rz,
  216. model;
  217. while (XML_GetKeyValue(name, val))
  218. {
  219. if (!strcmp(name, "position", true))
  220. {
  221. new
  222. pos;
  223. x = floatstr(val);
  224. pos = chrfind(' ', val, pos);
  225. y = floatstr(val[++pos]);
  226. pos = chrfind(' ', val, pos);
  227. z = floatstr(val[++pos]);
  228. }
  229. else if (!strcmp(name, "rotation", true))
  230. {
  231. new
  232. pos;
  233. rz = floatstr(val);
  234. pos = chrfind(' ', val, pos);
  235. ry = floatstr(val[++pos]);
  236. pos = chrfind(' ', val, pos);
  237. rx = floatstr(val[++pos]);
  238. }
  239. else if (!strcmp(name, "model", true))
  240. {
  241. model = strval(val);
  242. }
  243. }
  244. new
  245. obj = CreateDynamicObject(model, x, y, z, Loader_Convert(rx), Loader_Convert(ry), Loader_Convert(rz));
  246. if (YSI_g_sCurWorld != -1)
  247. {
  248. Object_RemoveFromAllWorlds(obj);
  249. Object_AddToWorld(obj, YSI_g_sCurWorld);
  250. }
  251. #if defined _YSI_VISUAL_RACE
  252. new
  253. shift = (8 * (obj & 3)),
  254. mul = obj >>> 2;
  255. if (YSI_g_sCurRace != NO_RACE)
  256. {
  257. YSI_g_sRaceObjects[mul] = (YSI_g_sRaceObjects[mul] & ~(0xFF << shift)) | YSI_g_sCurRace << shift;
  258. }
  259. #endif
  260. return obj;
  261. #else
  262. return -1;
  263. #endif
  264. }
  265. /*-------------------------------------------------------------------------*//**
  266. * <returns>Race_AddCheckpoint</returns>
  267. * <remarks>
  268. * Called when an end checkpoint tag is reached to add a checkpoint to a race.
  269. * </remarks>
  270. *//*------------------------------------------------------------------------**/
  271. public Loader_Check()
  272. {
  273. #if defined _YSI_VISUAL_RACE
  274. if (YSI_g_sCurRace == NO_RACE) return -1;
  275. static
  276. name[MAX_XML_ENTRY_NAME],
  277. val[MAX_XML_ENTRY_TEXT];
  278. new
  279. Float:x,
  280. Float:y,
  281. Float:z;
  282. while (XML_GetKeyValue(name, val))
  283. {
  284. if (!strcmp(name, "position", true))
  285. {
  286. new
  287. pos;
  288. x = floatstr(val);
  289. pos = chrfind(' ', val, pos);
  290. y = floatstr(val[++pos]);
  291. pos = chrfind(' ', val, pos);
  292. z = floatstr(val[++pos]);
  293. }
  294. }
  295. return Race_AddCheckpoint(YSI_g_sCurRace, x, y, z);
  296. #else
  297. return -1;
  298. #endif
  299. }
  300. /*-------------------------------------------------------------------------*//**
  301. * <returns>Race_AddStart</returns>
  302. * <remarks>
  303. * Called when an end spawnpoint tag is reached to add a startpoint to a race.
  304. * </remarks>
  305. *//*------------------------------------------------------------------------**/
  306. public Loader_Spawn()
  307. {
  308. #if defined _YSI_VISUAL_RACE
  309. if (YSI_g_sCurRace == NO_RACE) return -1;
  310. static
  311. name[MAX_XML_ENTRY_NAME],
  312. val[MAX_XML_ENTRY_TEXT];
  313. new
  314. Float:x,
  315. Float:y,
  316. Float:z,
  317. Float:rz;
  318. while (XML_GetKeyValue(name, val))
  319. {
  320. if (!strcmp(name, "position", true))
  321. {
  322. new
  323. pos;
  324. x = floatstr(val);
  325. pos = chrfind(' ', val, pos);
  326. y = floatstr(val[++pos]);
  327. pos = chrfind(' ', val, pos);
  328. z = floatstr(val[++pos]);
  329. }
  330. else if (!strcmp(name, "rotation", true))
  331. {
  332. rz = floatstr(val);
  333. }
  334. }
  335. return Race_AddStart(YSI_g_sCurRace, x, y, z, rz);
  336. #else
  337. return 1;
  338. #endif
  339. }
  340. /*-------------------------------------------------------------------------*//**
  341. * <param name="trigger">Tag to trigger the callback.</param>
  342. * <param name="function">Function to call for the tag.</param>
  343. * <remarks>
  344. * Used to add custom handlers to non-default tags in the race file format.
  345. * </remarks>
  346. *//*------------------------------------------------------------------------**/
  347. stock Loader_AddHandler(trigger[], function[])
  348. {
  349. if (YSI_g_sXMLRules != NO_XML_FILE) return XML_AddHandler(YSI_g_sXMLRules, trigger, function);
  350. return 0;
  351. }
  352. /*-------------------------------------------------------------------------*//**
  353. * <returns>Current race handle.</returns>
  354. *//*------------------------------------------------------------------------**/
  355. stock Loader_GetRace()
  356. {
  357. return YSI_g_sCurRace;
  358. }