Advertisement
PaddyCDev

PML - Paddy's Map Loader 0.2

Apr 4th, 2013
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.15 KB | None | 0 0
  1. /*
  2.     Paddy's Map Loader (PML) - A include that can load .map and/ or .pml map files.
  3.    
  4.     This program is distributed in the hope that it will be useful,
  5.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  6.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  7.     (The text above is not stolen from the GNU GPL xD)
  8.    
  9.     Feel free to edit this code to fit your needs.
  10.     (If you do so, please give me credits for my work)
  11.    
  12.     Author: Paddy_Corleone
  13.     Version: 0.2
  14.     Creation Date: 2013/03/04
  15.    
  16.     Changes in 0.2:
  17.     [ADD] Added LoadMTAMapToArrays/ LoadSAMPMapToArrays so you can load maps to arrays in you Gamemode (usefull for gates and stuff...)
  18.     [ADD] Added a define for vehicle respawn-rate for LoadMTAMap/ LoadMTAMapToArrays
  19.     [FIX] You can now spawn vehicles at any time with LoadMTAMap/ LoadMTAMapToArrays
  20.    
  21.     For updates and requests visit: http://forum.sa-mp.com/member.php?u=199151
  22. */
  23.  
  24. #if defined _PML_INCLUDED
  25.     #endinput
  26. #endif
  27. #define _PML_INCLUDED
  28. #define PML_VERSION "0.2"
  29. #define PML_MAX_MAPS 128
  30. #define PML_MAX_MAP_OBJECTS 500
  31. #define PML_MAX_MAP_VEHICLES 128
  32. #define PML_MTA_RESPAWN_RATE 120 // Respawn rate of MTA map vehicles in seconds
  33.  
  34. enum Maps
  35. {
  36.     mapname[64],
  37.     objects[PML_MAX_MAP_OBJECTS],
  38.     vehicles[PML_MAX_MAP_VEHICLES]
  39. };
  40. new MI[PML_MAX_MAPS][Maps], curmap;
  41.  
  42. /*
  43.     native LoadMTAMap(src[], Float:dist = 90.0);
  44.     native LoadSAMPMap(src[], Float:dist = 90.0);
  45.     native LoadMTAMapToArrays(src[], objar[], vehar[], Float:dist = 90.0);
  46.     native LoadSAMPMapToArrays(src[], objar[], vehar[], Float:dist = 90.0);
  47.     native RemoveMapObjects(map[]);
  48.     native RemoveMapVehicles(map[]);
  49.     native RespawnMapVehicles(map[]);
  50.     native RemoveMap(map[]);
  51. */
  52.  
  53. stock pmlsplit(src[], dest[][], del = '|')
  54. {
  55.     new start, cur;
  56.     for(new i; i < strlen(src) + 1; i++)
  57.         if(src[i] == del || i == strlen(src))
  58.         {
  59.             strmid(dest[cur], src, start, i, i - start + 1);
  60.             cur++;
  61.             start = i + 1;
  62.         }
  63. }
  64.  
  65. stock GetFreeMap()
  66. {
  67.     for(new i = 1; i < PML_MAX_MAPS; i++)
  68.     {
  69.         if(MI[i][objects][0] > 0 || MI[i][vehicles][0] > 0)continue;
  70.         curmap = i;
  71.         return 1;
  72.     }
  73.     return 0;
  74. }
  75.  
  76. stock GetMapByName(map[])
  77. {
  78.     for(new i = 1; i < PML_MAX_MAPS; i++)
  79.     {
  80.         if(!strcmp(MI[i][mapname], map) && strlen(MI[i][mapname]) > 0 && strlen(map) > 0)return i;
  81.     }
  82.     return 0;
  83. }
  84.  
  85. stock LoadMTAMap(src[], Float:dist = 90.0)
  86. {
  87.     if(!fexist(src))return printf("[PML %s] LoadMTAMap(%s): This file does not exist.", PML_VERSION, src);
  88.     if(strfind(src, ".map") == -1)return printf("[PML %s] LoadMTAMap(%s): You need to load a MTA .map file!", PML_VERSION, src);
  89.     if(GetMapByName(src) > 0)return printf("[PML %s] LoadMTAMap(%s): A map with this name already exists.", PML_VERSION, src); // Check for a map
  90.     if(!GetFreeMap())return printf("[PML %s] LoadMTAMap(%s): Couldn't load the map (to many maps are loaded).", PML_VERSION, src); // Setting the map ID
  91.     new File:srcfl = fopen(src, io_read), str[300], obj, Float:objf[6], count[2];
  92.     while(fread(srcfl, str))
  93.         if(strfind(str, "<map") == -1 && strfind(str, "</map>") == -1)
  94.         {
  95.             new substr[16], mode, clr[2][4];
  96.             if(strfind(str, "<object") != -1)mode = 1;
  97.             if(strfind(str, "<vehicle") != -1)mode = 2;
  98.             if(mode > 0)
  99.             {
  100.                 strmid(substr, str, strfind(str, "model=\"") + 7, strfind(str, "\"", false, strfind(str, "model=\"") + 7));
  101.                 obj = strval(substr); // model
  102.                 strmid(substr, str, strfind(str, "posX=\"") + 6, strfind(str, "\"", false, strfind(str, "posX=\"") + 6));
  103.                 objf[0] = floatstr(substr); // X position
  104.                 strmid(substr, str, strfind(str, "posY=\"") + 6, strfind(str, "\"", false, strfind(str, "posY=\"") + 6));
  105.                 objf[1] = floatstr(substr); // Y position
  106.                 strmid(substr, str, strfind(str, "posZ=\"") + 6, strfind(str, "\"", false, strfind(str, "posZ=\"") + 6));
  107.                 objf[2] = floatstr(substr); // Z position
  108.                 strmid(substr, str, strfind(str, "rotX=\"") + 6, strfind(str, "\"", false, strfind(str, "rotX=\"") + 6));
  109.                 objf[3] = floatstr(substr); // X rotation (objects only)
  110.                 strmid(substr, str, strfind(str, "rotY=\"") + 6, strfind(str, "\"", false, strfind(str, "rotY=\"") + 6));
  111.                 objf[4] = floatstr(substr); // Y rotation (objects only)
  112.                 strmid(substr, str, strfind(str, "rotZ=\"") + 6, strfind(str, "\"", false, strfind(str, "rotZ=\"") + 6));
  113.                 objf[5] = floatstr(substr); // Z rotation
  114.                 if(mode == 1)
  115.                 {
  116.                     MI[curmap][objects][(count[0] + 1)] = CreateObject(obj, objf[0], objf[1], objf[2], objf[3], objf[4], objf[5], dist);
  117.                     if(MI[curmap][objects][(count[0] + 1)] == INVALID_OBJECT_ID)
  118.                         printf("[PML %s] LoadMTAMap(%s): Object No. %i couldn't be loaded.", PML_VERSION, src, count[0]);
  119.                     else
  120.                         count[0]++;
  121.                 }else{
  122.                     strmid(substr, str, strfind(str, "color=\"") + 7, strfind(str, "\"", false, strfind(str, "color=\"") + 7));
  123.                     pmlsplit(substr, clr, ','); // colors
  124.                     MI[curmap][vehicles][(count[1] + 1)] = CreateVehicle(obj, objf[0], objf[1], objf[2], objf[5], strval(clr[0]), strval(clr[1]), PML_MTA_RESPAWN_RATE);
  125.                     if(MI[curmap][vehicles][(count[1] + 1)] == INVALID_VEHICLE_ID)
  126.                         printf("[PML %s] LoadMTAMap(%s): Vehicle No. %i couldn't be loaded.", PML_VERSION, src, count[1]);
  127.                     else
  128.                         count[1]++;
  129.                 }
  130.             }
  131.         }
  132.     fclose(srcfl);
  133.     MI[curmap][objects][0] = count[0]; // How many objects?
  134.     MI[curmap][vehicles][0] = count[1]; // How many vehicles?
  135.     format(MI[curmap][mapname], 64, src); // Set the map title
  136.     return printf("[PML %s] LoadMTAMap(%s): Loaded %i objects and %i vehicles (Map ID: %i).", PML_VERSION, src, count[0], count[1], curmap);
  137. }
  138.  
  139. stock LoadSAMPMap(src[], Float:dist = 90.0)
  140. {
  141.     if(!fexist(src))return printf("[PML %s] LoadSAMPMap(%s): This file does not exist.", PML_VERSION, src);
  142.     if(strfind(src, ".pml") == -1)return printf("[PML %s] LoadSAMPMap(%s): You need to load a .pml map file!", PML_VERSION, src);
  143.     if(GetMapByName(src) > 0)return printf("[PML %s] LoadSAMPMap(%s): A map with this name already exists.", PML_VERSION, src); // Check for a map
  144.     if(!GetFreeMap())return printf("[PML %s] LoadSAMPMap(%s): Couldn't load the map (to many maps are loaded).", PML_VERSION, src); // Setting the map ID
  145.     new File:srcfl = fopen(src, io_read), str[256], count[2];
  146.     while(fread(srcfl, str))
  147.     {
  148.         new vars[8][32];
  149.         pmlsplit(str, vars, ',');
  150.         if(strfind(str, "CreateObject(") != -1)
  151.         {
  152.             strdel(vars[0], strfind(vars[0], "CreateObject("), strfind(vars[0], "CreateObject(") + 13);
  153.             MI[curmap][objects][(count[0] + 1)] =
  154.                 CreateObject(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), floatstr(vars[5]), floatstr(vars[6]), dist);
  155.             count[0]++;
  156.         }else if(strfind(str, "CreateVehicle(") != -1)
  157.         {
  158.             strdel(vars[0], strfind(vars[0], "CreateVehicle("), strfind(vars[0], "CreateVehicle(") + 14);
  159.             if(strfind(vars[7], ");"))strdel(vars[7], strfind(vars[7], ");"), strlen(vars[7]) - 2);
  160.             MI[curmap][vehicles][(count[1] + 1)] =
  161.                 CreateVehicle(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), strval(vars[5]), strval(vars[6]), strval(vars[7]));
  162.             count[1]++;
  163.         }else if(strfind(str, "AddStaticVehicle(") != -1)
  164.         {
  165.             strdel(vars[0], strfind(vars[0], "AddStaticVehicle("), strfind(vars[0], "AddStaticVehicle(") + 17);
  166.             if(strfind(vars[6], ");"))strdel(vars[6], strfind(vars[6], ");"), strlen(vars[6]) - 2);
  167.             MI[curmap][vehicles][(count[1] + 1)] =
  168.                 AddStaticVehicle(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), strval(vars[5]), strval(vars[6]));
  169.             count[1]++;
  170.         }else if(strfind(str, "AddStaticVehicleEx(") != -1)
  171.         {
  172.             strdel(vars[0], strfind(vars[0], "AddStaticVehicleEx("), strfind(vars[0], "AddStaticVehicleEx(") + 19);
  173.             if(strfind(vars[7], ");"))strdel(vars[7], strfind(vars[7], ");"), strlen(vars[7]) - 2);
  174.             MI[curmap][vehicles][(count[1] + 1)] =
  175.                 AddStaticVehicleEx(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), strval(vars[5]), strval(vars[6]), strval(vars[7]));
  176.             count[1]++;
  177.         }
  178.     }
  179.     fclose(srcfl);
  180.     MI[curmap][objects][0] = count[0]; // How many objects?
  181.     MI[curmap][vehicles][0] = count[1]; // How many vehicles?
  182.     format(MI[curmap][mapname], 64, src); // Set the map title
  183.     return printf("[PML %s] LoadSAMPMap(%s): Loaded %i objects and %i vehicles (Map ID: %i).", PML_VERSION, src, count[0], count[1], curmap);
  184. }
  185.  
  186. stock LoadMTAMapToArrays(src[], objar[], vehar[], Float:dist = 90.0)
  187. {
  188.     if(!fexist(src))return printf("[PML %s] LoadMTAMapToArrays(%s): This file does not exist.", PML_VERSION, src);
  189.     if(strfind(src, ".map") == -1)return printf("[PML %s] LoadMTAMapToArrays(%s): You need to load a MTA .map file!", PML_VERSION, src);
  190.     if(GetMapByName(src) > 0)return printf("[PML %s] LoadMTAMapToArrays(%s): A map with this name already exists.", PML_VERSION, src); // Check for a map
  191.     if(!GetFreeMap())return printf("[PML %s] LoadMTAMapToArrays(%s): Couldn't load the map (to many maps are loaded).", PML_VERSION, src); // Setting the map ID
  192.     new File:srcfl = fopen(src, io_read), str[300], obj, Float:objf[6], count[2];
  193.     while(fread(srcfl, str))
  194.         if(strfind(str, "<map") == -1 && strfind(str, "</map>") == -1)
  195.         {
  196.             new substr[16], mode, clr[2][4];
  197.             if(strfind(str, "<object") != -1)mode = 1;
  198.             if(strfind(str, "<vehicle") != -1)mode = 2;
  199.             if(mode > 0)
  200.             {
  201.                 strmid(substr, str, strfind(str, "model=\"") + 7, strfind(str, "\"", false, strfind(str, "model=\"") + 7));
  202.                 obj = strval(substr); // model
  203.                 strmid(substr, str, strfind(str, "posX=\"") + 6, strfind(str, "\"", false, strfind(str, "posX=\"") + 6));
  204.                 objf[0] = floatstr(substr); // X position
  205.                 strmid(substr, str, strfind(str, "posY=\"") + 6, strfind(str, "\"", false, strfind(str, "posY=\"") + 6));
  206.                 objf[1] = floatstr(substr); // Y position
  207.                 strmid(substr, str, strfind(str, "posZ=\"") + 6, strfind(str, "\"", false, strfind(str, "posZ=\"") + 6));
  208.                 objf[2] = floatstr(substr); // Z position
  209.                 strmid(substr, str, strfind(str, "rotX=\"") + 6, strfind(str, "\"", false, strfind(str, "rotX=\"") + 6));
  210.                 objf[3] = floatstr(substr); // X rotation (objects only)
  211.                 strmid(substr, str, strfind(str, "rotY=\"") + 6, strfind(str, "\"", false, strfind(str, "rotY=\"") + 6));
  212.                 objf[4] = floatstr(substr); // Y rotation (objects only)
  213.                 strmid(substr, str, strfind(str, "rotZ=\"") + 6, strfind(str, "\"", false, strfind(str, "rotZ=\"") + 6));
  214.                 objf[5] = floatstr(substr); // Z rotation
  215.                 if(mode == 1)
  216.                 {
  217.                     objar[(count[0])] = MI[curmap][objects][(count[0] + 1)] = CreateObject(obj, objf[0], objf[1], objf[2], objf[3], objf[4], objf[5], dist);
  218.                     if(MI[curmap][objects][(count[0] + 1)] == INVALID_OBJECT_ID)
  219.                         printf("[PML %s] LoadMTAMapToArrays(%s): Object No. %i couldn't be loaded.", PML_VERSION, src, count[0]);
  220.                     else
  221.                         count[0]++;
  222.                 }else{
  223.                     strmid(substr, str, strfind(str, "color=\"") + 7, strfind(str, "\"", false, strfind(str, "color=\"") + 7));
  224.                     pmlsplit(substr, clr, ','); // colors
  225.                     vehar[(count[1])] = MI[curmap][vehicles][(count[1] + 1)] = CreateVehicle(obj, objf[0], objf[1], objf[2], objf[5], strval(clr[0]), strval(clr[1]), PML_MTA_RESPAWN_RATE);
  226.                     if(MI[curmap][vehicles][(count[1] + 1)] == INVALID_VEHICLE_ID)
  227.                         printf("[PML %s] LoadMTAMapToArrays(%s): Vehicle No. %i couldn't be loaded.", PML_VERSION, src, count[1]);
  228.                     else
  229.                         count[1]++;
  230.                 }
  231.             }
  232.         }
  233.     fclose(srcfl);
  234.     MI[curmap][objects][0] = count[0]; // How many objects?
  235.     MI[curmap][vehicles][0] = count[1]; // How many vehicles?
  236.     format(MI[curmap][mapname], 64, src); // Set the map title
  237.     return printf("[PML %s] LoadMTAMapToArrays(%s): Loaded %i objects and %i vehicles (Map ID: %i).", PML_VERSION, src, count[0], count[1], curmap);
  238. }
  239.  
  240. stock LoadSAMPMapToArrays(src[], objar[], vehar[], Float:dist = 90.0)
  241. {
  242.     if(!fexist(src))return printf("[PML %s] LoadSAMPMapToArrays(%s): This file does not exist.", PML_VERSION, src);
  243.     if(strfind(src, ".pml") == -1)return printf("[PML %s] LoadSAMPMapToArrays(%s): You need to load a .pml map file!", PML_VERSION, src);
  244.     if(GetMapByName(src) > 0)return printf("[PML %s] LoadSAMPMapToArrays(%s): A map with this name already exists.", PML_VERSION, src); // Check for a map
  245.     if(!GetFreeMap())return printf("[PML %s] LoadSAMPMapToArrays(%s): Couldn't load the map (to many maps are loaded).", PML_VERSION, src); // Setting the map ID
  246.     new File:srcfl = fopen(src, io_read), str[256], count[2];
  247.     while(fread(srcfl, str))
  248.     {
  249.         new vars[8][32];
  250.         pmlsplit(str, vars, ',');
  251.         if(strfind(str, "CreateObject(") != -1)
  252.         {
  253.             strdel(vars[0], strfind(vars[0], "CreateObject("), strfind(vars[0], "CreateObject(") + 13);
  254.             objar[(count[0])] = MI[curmap][objects][(count[0] + 1)] =
  255.                 CreateObject(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), floatstr(vars[5]), floatstr(vars[6]), dist);
  256.             count[0]++;
  257.         }else if(strfind(str, "CreateVehicle(") != -1)
  258.         {
  259.             strdel(vars[0], strfind(vars[0], "CreateVehicle("), strfind(vars[0], "CreateVehicle(") + 14);
  260.             if(strfind(vars[7], ");"))strdel(vars[7], strfind(vars[7], ");"), strlen(vars[7]) - 2);
  261.             vehar[(count[1])] = MI[curmap][vehicles][(count[1] + 1)] =
  262.                 CreateVehicle(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), strval(vars[5]), strval(vars[6]), strval(vars[7]));
  263.             count[1]++;
  264.         }else if(strfind(str, "AddStaticVehicle(") != -1)
  265.         {
  266.             strdel(vars[0], strfind(vars[0], "AddStaticVehicle("), strfind(vars[0], "AddStaticVehicle(") + 17);
  267.             if(strfind(vars[6], ");"))strdel(vars[6], strfind(vars[6], ");"), strlen(vars[6]) - 2);
  268.             vehar[(count[1])] = MI[curmap][vehicles][(count[1] + 1)] =
  269.                 AddStaticVehicle(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), strval(vars[5]), strval(vars[6]));
  270.             count[1]++;
  271.         }else if(strfind(str, "AddStaticVehicleEx(") != -1)
  272.         {
  273.             strdel(vars[0], strfind(vars[0], "AddStaticVehicleEx("), strfind(vars[0], "AddStaticVehicleEx(") + 19);
  274.             if(strfind(vars[7], ");"))strdel(vars[7], strfind(vars[7], ");"), strlen(vars[7]) - 2);
  275.             vehar[(count[1])] = MI[curmap][vehicles][(count[1] + 1)] =
  276.                 AddStaticVehicleEx(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), strval(vars[5]), strval(vars[6]), strval(vars[7]));
  277.             count[1]++;
  278.         }
  279.     }
  280.     fclose(srcfl);
  281.     MI[curmap][objects][0] = count[0]; // How many objects?
  282.     MI[curmap][vehicles][0] = count[1]; // How many vehicles?
  283.     format(MI[curmap][mapname], 64, src); // Set the map title
  284.     return printf("[PML %s] LoadSAMPMapToArrays(%s): Loaded %i objects and %i vehicles (Map ID: %i).", PML_VERSION, src, count[0], count[1], curmap);
  285. }
  286.  
  287. stock RemoveMapObjects(map[])
  288. {
  289.     new mid = GetMapByName(map);
  290.     if(!mid)return printf("[PML %s] RemoveMapObjects(%s): The Map wasn't found.", PML_VERSION, map);
  291.     for(new i = 1; i < MI[mid][objects][0] + 1; i++)
  292.         if(MI[mid][objects][i] != INVALID_OBJECT_ID)
  293.         {
  294.             DestroyObject(MI[mid][objects][i]);
  295.             MI[mid][objects][i] = 0;
  296.         }
  297.     MI[mid][objects][0] = 0;
  298.     return printf("[PML %s] RemoveMapObjects(%s): All objects have been removed.", PML_VERSION, map);
  299. }
  300.  
  301. stock RemoveMapVehicles(map[])
  302. {
  303.     new mid = GetMapByName(map);
  304.     if(!mid)return printf("[PML %s] RemoveMapVehicles(%s): The Map wasn't found.", PML_VERSION, map);
  305.     for(new i = 1; i < MI[mid][vehicles][0] + 1; i++)
  306.         if(MI[mid][vehicles][i] != INVALID_VEHICLE_ID)
  307.         {
  308.             DestroyVehicle(MI[mid][vehicles][i]);
  309.             MI[mid][vehicles][i] = 0;
  310.         }
  311.     MI[mid][vehicles][0] = 0;
  312.     return printf("[PML %s] RemoveMapVehicles(%s): All vehicles have been removed.", PML_VERSION, map);
  313. }
  314.  
  315. stock RespawnMapVehicles(map[])
  316. {
  317.     new mid = GetMapByName(map);
  318.     if(!mid)return printf("[PML %s] RespawnMapVehicles(%s): The Map wasn't found.", PML_VERSION, map);
  319.     for(new i = 1; i < MI[mid][vehicles][0] + 1; i++)
  320.         if(MI[mid][vehicles][i] != INVALID_VEHICLE_ID)SetVehicleToRespawn(MI[mid][vehicles][i]);
  321.     return printf("[PML %s] RespawnMapVehicles(%s): All vehicles have been respawned.", PML_VERSION, map);
  322. }
  323.  
  324. stock RemoveMap(map[])
  325. {
  326.     new mid = GetMapByName(map);
  327.     if(!mid)return printf("[PML %s] RemoveMap(%s): The Map wasn't found.", PML_VERSION, map);
  328.     RemoveMapObjects(map);
  329.     RemoveMapVehicles(map);
  330.     return 1;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement