Advertisement
PaddyCDev

Paddy's Map Loader 0.1

Apr 3rd, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.17 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.1
  14.     Creation Date: 2013/02/04
  15.    
  16.     For updates and requests visit: http://forum.sa-mp.com/member.php?u=199151
  17. */
  18.    
  19. #define Version "0.1"
  20.  
  21. #define PML_MAX_MAPS 128
  22. #define PML_MAX_MAP_OBJECTS 500
  23. #define PML_MAX_MAP_VEHICLES 128
  24.  
  25. enum Maps
  26. {
  27.     mapname[64],
  28.     objects[PML_MAX_MAP_OBJECTS],
  29.     vehicles[PML_MAX_MAP_VEHICLES]
  30. };
  31. new MI[PML_MAX_MAPS][Maps], curmap;
  32.  
  33. /*
  34.     native LoadMTAMap(src[], Float:dist = 90.0);
  35.     native LoadSAMPMap(src[], Float:dist = 90.0);
  36.     native RemoveMapObjects(map[]);
  37.     native RemoveMapVehicles(map[]);
  38.     native RespawnMapVehicles(map[]);
  39.     native RemoveMap(map[]);
  40. */
  41.  
  42. stock pmlsplit(src[], dest[][], del = '|')
  43. {
  44.     new start, cur;
  45.     for(new i; i < strlen(src) + 1; i++)
  46.         if(src[i] == del || i == strlen(src))
  47.         {
  48.             strmid(dest[cur], src, start, i, i - start + 1);
  49.             cur++;
  50.             start = i + 1;
  51.         }
  52. }
  53.  
  54. stock GetFreeMap()
  55. {
  56.     for(new i = 1; i < PML_MAX_MAPS; i++)
  57.     {
  58.         if(MI[i][objects][0] > 0 || MI[i][vehicles][0] > 0)continue;
  59.         curmap = i;
  60.         return 1;
  61.     }
  62.     return 0;
  63. }
  64.  
  65. stock GetMapByName(map[])
  66. {
  67.     for(new i = 1; i < PML_MAX_MAPS; i++)
  68.     {
  69.         if(!strcmp(MI[i][mapname], map) && strlen(MI[i][mapname]) > 0 && strlen(map) > 0)return i;
  70.     }
  71.     return 0;
  72. }
  73.  
  74. stock LoadMTAMap(src[], Float:dist = 90.0)
  75. {
  76.     if(!fexist(src))return printf("[PML %s] LoadMTAMap(%s): This file does not exist.", Version, src);
  77.     if(strfind(src, ".map") == -1)return printf("[PML %s] LoadMTAMap(%s): You need to load a MTA .map file!", Version, src);
  78.     if(GetMapByName(src) > 0)return printf("[PML %s] LoadMTAMap(%s): A map with this name allready exists.", Version, src); // Check for a map
  79.     if(!GetFreeMap())return printf("[PML %s] LoadMTAMap(%s): Couldn't load the map (to many maps are loaded).", Version, src); // Setting the map ID
  80.     new File:srcfl = fopen(src, io_read), str[300], obj, Float:objf[6], count[2];
  81.     while(fread(srcfl, str))
  82.         if(strfind(str, "<map") == -1 && strfind(str, "</map>") == -1)
  83.         {
  84.             new substr[16], mode, clr[2][4];
  85.             if(strfind(str, "<object") != -1)mode = 1;
  86.             if(strfind(str, "<vehicle") != -1)mode = 2;
  87.             if(mode > 0)
  88.             {
  89.                 strmid(substr, str, strfind(str, "model=\"") + 7, strfind(str, "\"", false, strfind(str, "model=\"") + 7));
  90.                 obj = strval(substr); // model
  91.                 strmid(substr, str, strfind(str, "posX=\"") + 6, strfind(str, "\"", false, strfind(str, "posX=\"") + 6));
  92.                 objf[0] = floatstr(substr); // X position
  93.                 strmid(substr, str, strfind(str, "posY=\"") + 6, strfind(str, "\"", false, strfind(str, "posY=\"") + 6));
  94.                 objf[1] = floatstr(substr); // Y position
  95.                 strmid(substr, str, strfind(str, "posZ=\"") + 6, strfind(str, "\"", false, strfind(str, "posZ=\"") + 6));
  96.                 objf[2] = floatstr(substr); // Z position
  97.                 strmid(substr, str, strfind(str, "rotX=\"") + 6, strfind(str, "\"", false, strfind(str, "rotX=\"") + 6));
  98.                 objf[3] = floatstr(substr); // X rotation (objects only)
  99.                 strmid(substr, str, strfind(str, "rotY=\"") + 6, strfind(str, "\"", false, strfind(str, "rotY=\"") + 6));
  100.                 objf[4] = floatstr(substr); // Y rotation (objects only)
  101.                 strmid(substr, str, strfind(str, "rotZ=\"") + 6, strfind(str, "\"", false, strfind(str, "rotZ=\"") + 6));
  102.                 objf[5] = floatstr(substr); // Z rotation
  103.                 if(mode == 1)
  104.                 {
  105.                     MI[curmap][objects][(count[0] + 1)] = CreateObject(obj, objf[0], objf[1], objf[2], objf[3], objf[4], objf[5], dist);
  106.                     if(MI[curmap][objects][(count[0] + 1)] == INVALID_OBJECT_ID)
  107.                         printf("[PML %s] LoadMTAMap(%s): Object No. %i couldn't be loaded.", Version, src, count[0]);
  108.                     else
  109.                         count[0]++;
  110.                 }else{
  111.                     strmid(substr, str, strfind(str, "color=\"") + 7, strfind(str, "\"", false, strfind(str, "color=\"") + 7));
  112.                     pmlsplit(substr, clr, ','); // colors
  113.                     MI[curmap][vehicles][(count[1] + 1)] = AddStaticVehicle(obj, objf[0], objf[1], objf[2], objf[5], strval(clr[0]), strval(clr[1]));
  114.                     if(MI[curmap][vehicles][(count[1] + 1)] == INVALID_VEHICLE_ID)
  115.                         printf("[PML %s] LoadMTAMap(%s): Vehicle No. %i couldn't be loaded.", Version, src, count[1]);
  116.                     else
  117.                         count[1]++;
  118.                 }
  119.             }
  120.         }
  121.     fclose(srcfl);
  122.     MI[curmap][objects][0] = count[0]; // How many objects?
  123.     MI[curmap][vehicles][0] = count[1]; // How many vehicles?
  124.     format(MI[curmap][mapname], 64, src); // Set the map title
  125.     return printf("[PML %s] LoadMTAMap(%s): Loaded %i objects and %i vehicles (Map ID: %i).", Version, src, count[0], count[1], curmap);
  126. }
  127.  
  128. stock LoadSAMPMap(src[], Float:dist = 90.0)
  129. {
  130.     if(!fexist(src))return printf("[PML %s] LoadSAMPMap(%s): This file does not exist.", Version, src);
  131.     if(strfind(src, ".pml") == -1)return printf("[PML %s] LoadSAMPMap(%s): You need to load a .pml map file!", Version, src);
  132.     if(GetMapByName(src) > 0)return printf("[PML %s] LoadSAMPMap(%s): A map with this name allready exists.", Version, src); // Check for a map
  133.     if(!GetFreeMap())return printf("[PML %s] LoadSAMPMap(%s): Couldn't load the map (to many maps are loaded).", Version, src); // Setting the map ID
  134.     new File:srcfl = fopen(src, io_read), str[256], count[2];
  135.     while(fread(srcfl, str))
  136.     {
  137.         new vars[8][32];
  138.         pmlsplit(str, vars, ',');
  139.         if(strfind(str, "CreateObject(") != -1)
  140.         {
  141.             strdel(vars[0], strfind(vars[0], "CreateObject("), strfind(vars[0], "CreateObject(") + 13);
  142.             MI[curmap][objects][(count[0] + 1)] =
  143.                 CreateObject(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), floatstr(vars[5]), floatstr(vars[6]), dist);
  144.             count[0]++;
  145.         }else if(strfind(str, "CreateVehicle(") != -1)
  146.         {
  147.             strdel(vars[0], strfind(vars[0], "CreateVehicle("), strfind(vars[0], "CreateVehicle(") + 14);
  148.             if(strfind(vars[7], ");"))strdel(vars[7], strfind(vars[7], ");"), strlen(vars[7]) - 2);
  149.             MI[curmap][vehicles][(count[1] + 1)] =
  150.                 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]));
  151.             count[1]++;
  152.         }else if(strfind(str, "AddStaticVehicle(") != -1)
  153.         {
  154.             strdel(vars[0], strfind(vars[0], "AddStaticVehicle("), strfind(vars[0], "AddStaticVehicle(") + 17);
  155.             if(strfind(vars[6], ");"))strdel(vars[6], strfind(vars[6], ");"), strlen(vars[6]) - 2);
  156.             MI[curmap][vehicles][(count[1] + 1)] =
  157.                 AddStaticVehicle(strval(vars[0]), floatstr(vars[1]), floatstr(vars[2]), floatstr(vars[3]), floatstr(vars[4]), strval(vars[5]), strval(vars[6]));
  158.             count[1]++;
  159.         }else if(strfind(str, "AddStaticVehicleEx(") != -1)
  160.         {
  161.             strdel(vars[0], strfind(vars[0], "AddStaticVehicleEx("), strfind(vars[0], "AddStaticVehicleEx(") + 19);
  162.             if(strfind(vars[7], ");"))strdel(vars[7], strfind(vars[7], ");"), strlen(vars[7]) - 2);
  163.             MI[curmap][vehicles][(count[1] + 1)] =
  164.                 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]));
  165.             count[1]++;
  166.         }
  167.     }
  168.     fclose(srcfl);
  169.     MI[curmap][objects][0] = count[0]; // How many objects?
  170.     MI[curmap][vehicles][0] = count[1]; // How many vehicles?
  171.     format(MI[curmap][mapname], 64, src); // Set the map title
  172.     return printf("[PML %s] LoadSAMPMap(%s): Loaded %i objects and %i vehicles (Map ID: %i).", Version, src, count[0], count[1], curmap);
  173. }
  174.  
  175. stock RemoveMapObjects(map[])
  176. {
  177.     new mid = GetMapByName(map);
  178.     if(!mid)return printf("[PML %s] RemoveMapObjects(%s): The Map wasn't found.", Version, map);
  179.     for(new i = 1; i < MI[mid][objects][0] + 1; i++)
  180.         if(MI[mid][objects][i] != INVALID_OBJECT_ID)
  181.         {
  182.             DestroyObject(MI[mid][objects][i]);
  183.             MI[mid][objects][i] = 0;
  184.         }
  185.     MI[mid][objects][0] = 0;
  186.     return printf("[PML %s] RemoveMapObjects(%s): All objects have been removed.", Version, map);
  187. }
  188.  
  189. stock RemoveMapVehicles(map[])
  190. {
  191.     new mid = GetMapByName(map);
  192.     if(!mid)return printf("[PML %s] RemoveMapVehicles(%s): The Map wasn't found.", Version, map);
  193.     for(new i = 1; i < MI[mid][vehicles][0] + 1; i++)
  194.         if(MI[mid][vehicles][i] != INVALID_VEHICLE_ID)
  195.         {
  196.             DestroyVehicle(MI[mid][vehicles][i]);
  197.             MI[mid][vehicles][i] = 0;
  198.         }
  199.     MI[mid][vehicles][0] = 0;
  200.     return printf("[PML %s] RemoveMapVehicles(%s): All vehicles have been removed.", Version, map);
  201. }
  202.  
  203. stock RespawnMapVehicles(map[])
  204. {
  205.     new mid = GetMapByName(map);
  206.     if(!mid)return printf("[PML %s] RespawnMapVehicles(%s): The Map wasn't found.", Version, map);
  207.     for(new i = 1; i < MI[mid][vehicles][0] + 1; i++)
  208.         if(MI[mid][vehicles][i] != INVALID_VEHICLE_ID)SetVehicleToRespawn(MI[mid][vehicles][i]);
  209.     return printf("[PML %s] RespawnMapVehicles(%s): All vehicles have been respawned.", Version, map);
  210. }
  211.  
  212. stock RemoveMap(map[])
  213. {
  214.     new mid = GetMapByName(map);
  215.     if(!mid)return printf("[PML %s] RemoveMap(%s): The Map wasn't found.", Version, map);
  216.     RemoveMapObjects(map);
  217.     RemoveMapVehicles(map);
  218.     return 1;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement