1
0

y_loader.inc 12 KB

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