Guest User

Untitled

a guest
Jan 12th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.05 KB | None | 0 0
  1.  
  2. /* GTA SA-MP
  3.  * In-game MAP convertor
  4.  * By Roox 2011
  5.  */
  6.  
  7. #include <a_samp>
  8.  
  9. #define Dialog_Convertor        0
  10. //----- Change to 0 if you want to disable this MTA type
  11. #define Support_LUA             1
  12. #define Support_MTA_Race        1
  13. #define Support_MTA_DEATHMATCH  1
  14. #define Support_MTA1            1
  15.  
  16. public OnFilterScriptInit()
  17. {
  18.     return 1;
  19. }
  20.  
  21. public OnPlayerCommandText(playerid, cmdtext[])
  22. {
  23.     if(!strcmp(cmdtext, "/convert", true, 8))
  24.     {
  25.         if(strlen(cmdtext) < 9)
  26.         {
  27.             new string[128];
  28.             format(string, sizeof(string), "Napište název mapy bez koncovky .map\n\nPodporováno:");
  29.            
  30.             #if Support_LUA
  31.             strcat(string, "\nLua for MTA");
  32.             #endif
  33.            
  34.             #if Support_MTA_Race
  35.             strcat(string, "\nMTA Race");
  36.             #endif
  37.            
  38.             #if Support_MTA_DEATHMATCH
  39.             strcat(string, "\nMTA Death Match");
  40.             #endif
  41.            
  42.             #if Support_MTA1
  43.             strcat(string, "\nMTA 1.0");
  44.             #endif
  45.            
  46.             ShowPlayerDialog(playerid, Dialog_Convertor, DIALOG_STYLE_INPUT, "Map convertor:", string, "Potvrdit", "Konec");
  47.         }
  48.         else
  49.         {
  50.             PlayerConvertMap(playerid, cmdtext[9]);
  51.         }
  52.         return 1;
  53.     }
  54.     return 0;
  55. }
  56.  
  57. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  58. {
  59.     if(dialogid == Dialog_Convertor)
  60.     {
  61.         if(response)
  62.         {
  63.             if(strlen(inputtext) > 0)
  64.             {
  65.                 PlayerConvertMap(playerid, inputtext);
  66.             }
  67.         }
  68.     }
  69.     return 1;
  70. }
  71.  
  72. stock PlayerConvertMap(playerid, mapname[])
  73. {
  74.     new filename[28], pwnfile[28];
  75.     format(filename, sizeof(filename), "%s.map", mapname);
  76.     format(pwnfile, sizeof(pwnfile), "%s.pwn", mapname);
  77.    
  78.     SendClientMessage(playerid, 0xFFFF00AA, "Map convertor:");
  79.     switch(ConvertMap(filename, pwnfile))
  80.     {
  81.         case -1: SendClientMessage(playerid, 0xFFFFFFAA, "Soubor nebyl nalezen.");
  82.         case 0: SendClientMessage(playerid, 0xFFFFFFAA, "Soubor úspěšně zkonvertován. (Typ mapy: LUA)");
  83.         case 1: SendClientMessage(playerid, 0xFFFFFFAA, "Soubor úspěšně zkonvertován. (Typ mapy: MTA 1.0)");
  84.         case 2: SendClientMessage(playerid, 0xFFFFFFAA, "Soubor úspěšně zkonvertován. (Typ mapy: MTA Death Match)");
  85.         case 3: SendClientMessage(playerid, 0xFFFFFFAA, "Soubor úspěšně zkonvertován. (Typ mapy: MTA Race)");
  86.         case 4: SendClientMessage(playerid, 0xFFFFFFAA, "Tento typ MTA mapy není podporován.");
  87.     }
  88.     return 1;
  89. }
  90.  
  91. stock ConvertMap(filename[], filetowrite[])
  92. {
  93.     new File:MapFile = fopen(filename, io_read);
  94.     //-----
  95.     new bool:MapDetected = false;
  96.     new MapType = 0;
  97.     //-----
  98.     if(MapFile)
  99.     {
  100.         if(fexist(filetowrite)) fremove(filetowrite);
  101.         new File:PWNFile = fopen(filetowrite, io_write);
  102.         //-----
  103.         fwrite(PWNFile, "#include <a_samp>\n\npublic OnFilterScriptInit()\n{\n");
  104.         //-----
  105.         new strFromFile[258], strtowrite[128];
  106.         while(fread(MapFile, strFromFile, sizeof(strFromFile)))
  107.         {
  108.             StripNewLine(strFromFile);
  109.  
  110.             if(strcmp(strFromFile, "createObject", true, 12)) // Detecting LUA code
  111.             {
  112.                 if(MapDetected == false)
  113.                 {
  114.                     if(!strcmp(strFromFile, "<map edf:definitions=\"", true, 22)) // MTA 1.0 Map
  115.                     {
  116.                         #if !Support_MTA1
  117.                         fclose(PWNFile);
  118.                         fclose(MapFile);
  119.                         return 4;
  120.                         #else
  121.                
  122.                         MapDetected = true;
  123.                         MapType = 1;
  124.                         continue;
  125.                         #endif
  126.                     }
  127.                     else if(!strcmp(strFromFile, "<map mod=\"deathmatch\">", true, 21)) // Deathmatch
  128.                     {
  129.                         #if !Support_MTA_DEATHMATCH
  130.                         fclose(PWNFile);
  131.                         fclose(MapFile);
  132.                         return 4;
  133.                         #else
  134.                        
  135.                         MapDetected = true;
  136.                         MapType = 2;
  137.                         continue;
  138.                         #endif
  139.                     }
  140.                     else if(!strcmp(strFromFile, "<map mod=\"race\" version=\"", true, 25)) // Race
  141.                     {
  142.                         #if !Support_MTA_Race
  143.                         fclose(PWNFile);
  144.                         fclose(MapFile);
  145.                         return 4;
  146.                         #else
  147.                        
  148.                         MapDetected = true;
  149.                         MapType = 3;
  150.                         continue;
  151.                         #endif
  152.                     }
  153.                 }
  154.             }
  155.             else
  156.             {
  157.                 MapDetected = true;
  158.                
  159.                 #if !Support_LUA
  160.                 fclose(PWNFile);
  161.                 fclose(MapFile);
  162.                 return 4;
  163.                 #endif
  164.             }
  165.  
  166.             if(MapDetected == true)
  167.             {
  168.                 new Model = -1, Float:pos[6];
  169.                 new PosCount, bool:IsObject;
  170.                    
  171.                 switch(MapType)
  172.                 {
  173.                     case 0: // LUA for MTA
  174.                     {
  175.                         for(new a = 12; a < strlen(strFromFile); a++)
  176.                         {
  177.                             if(strFromFile[a] == '(')
  178.                             {
  179.                                 Model = strval(strFromFile[a+1]);
  180.                                 printf("Model: %d", Model);
  181.                                
  182.                                 if(Model < 10) a+=2;
  183.                                 else if(Model < 100) a += 3;
  184.                                 else if(Model < 1000) a += 4;
  185.                                 else if(Model < 10000) a += 5;
  186.                                 else a += 6;
  187.                                 continue;
  188.                             }
  189.                            
  190.                             if(Model != -1)
  191.                             {
  192.                                 if(IsCharNumeric(strFromFile[a]))
  193.                                 {
  194.                                     if(PosCount < 6) pos[PosCount] = floatstr(strFromFile[a]);
  195.                                     else continue;
  196.                                    
  197.                                     if(strcmp(strFromFile[a], "0.0", true, 3))
  198.                                     {
  199.                                         for(new t = 1; t < 10; t++)
  200.                                         {
  201.                                             if(strFromFile[a+t] == ',')
  202.                                             {
  203.                                                 a += t;
  204.                                                 break;
  205.                                             }
  206.                                         }
  207.                                     }
  208.                                     else
  209.                                     {
  210.                                         a += 2;
  211.                                     }
  212.                                    
  213.                                     printf("Pos: %f", pos[PosCount]);
  214.                                     PosCount++;
  215.                                 }
  216.                             }
  217.                         }
  218.                        
  219.                         format(strtowrite, sizeof(strtowrite), "    CreateObject(%d, %f, %f, %f, %f, %f, %f);\r", Model, pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]);
  220.                         fwrite(PWNFile, strtowrite);
  221.                     }
  222.                     case 1, 2: // MTA 1.0 & Death Match
  223.                     {
  224.                         for(new a = 0; a < strlen(strFromFile); a++)
  225.                         {
  226.                             if(IsObject == false)
  227.                             {
  228.                                 if(!strcmp(strFromFile[a], "object", true, 6))
  229.                                 {
  230.                                     IsObject = true;
  231.                                 }
  232.                                
  233.                                 continue;
  234.                             }
  235.                             else
  236.                             {
  237.                                 new savetovalue = DetectSavingValue(strFromFile, a);
  238.                                
  239.                                 if(savetovalue == 6)
  240.                                 {
  241.                                     a += 7;
  242.                                     new strfloat[18];
  243.                                     for(new k = 0; k < 18; k ++)
  244.                                     {
  245.                                         if(strFromFile[a+k] != '\"')
  246.                                         {
  247.                                             strfloat[k] = strFromFile[a+k];
  248.                                         }
  249.                                         else
  250.                                         {
  251.                                             a += k;
  252.                                             break;
  253.                                         }
  254.                                     }
  255.                                     Model = strval(strfloat);
  256.                                 }
  257.                                 else if(savetovalue != -1)
  258.                                 {
  259.                                     a += 6;
  260.                                     new strfloat[18];
  261.                                     for(new k = 0; k < 18; k ++)
  262.                                     {
  263.                                         if(strFromFile[a+k] != '\"')
  264.                                         {
  265.                                             strfloat[k] = strFromFile[a+k];
  266.                                         }
  267.                                         else
  268.                                         {
  269.                                             a += k;
  270.                                             break;
  271.                                         }
  272.                                     }
  273.                                     pos[savetovalue] = floatstr(strfloat);
  274.                                 }
  275.                             }
  276.                         }
  277.                        
  278.                         if(Model != -1)
  279.                         {
  280.                             format(strtowrite, sizeof(strtowrite), "    CreateObject(%d, %f, %f, %f, %f, %f, %f);\r", Model, pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]);
  281.                             fwrite(PWNFile, strtowrite);
  282.                         }
  283.                     }
  284.                     case 3: // MTA Race
  285.                     {
  286.                         for(new x = 0; x < strlen(strFromFile); x++)
  287.                         {
  288.                             if(!strcmp(strFromFile[x], "<object", true, 7))
  289.                             {
  290.                                 // <position>
  291.                                 fread(MapFile, strFromFile, sizeof(strFromFile));
  292.                                 StripNewLine(strFromFile);
  293.                                
  294.                                 for(new a = x; a < strlen(strFromFile); a++)
  295.                                 {
  296.                                     if(strFromFile[a-1] == '>')
  297.                                     {
  298.                                         for(new y = 0; y < 3; y++)
  299.                                         {
  300.                                             pos[PosCount] = floatstr(strFromFile[a]);
  301.                                             PosCount++;
  302.                                            
  303.                                             for(new k = 0; k < 18; k++)
  304.                                             {
  305.                                                 if(strFromFile[a+k] == ' ' || strFromFile[a+k] == '<')
  306.                                                 {
  307.                                                     a += k+1;
  308.                                                     break;
  309.                                                 }
  310.                                             }
  311.                                         }
  312.                                     }
  313.                                 }
  314.                                
  315.                                 // <rotation>
  316.                                 fread(MapFile, strFromFile, sizeof(strFromFile));
  317.                                 StripNewLine(strFromFile);
  318.  
  319.                                 for(new a = x; a < strlen(strFromFile); a++)
  320.                                 {
  321.                                     if(strFromFile[a-1] == '>')
  322.                                     {
  323.                                         for(new y = 0; y < 3; y++)
  324.                                         {
  325.                                             pos[PosCount] = floatstr(strFromFile[a]);
  326.                                             PosCount++;
  327.  
  328.                                             for(new k = 0; k < 18; k++)
  329.                                             {
  330.                                                 if(strFromFile[a+k] == ' ' || strFromFile[a+k] == '<')
  331.                                                 {
  332.                                                     a += k+1;
  333.                                                     break;
  334.                                                 }
  335.                                             }
  336.                                         }
  337.                                     }
  338.                                 }
  339.                                
  340.                                 for(new a = 3; a < 6; a++) // MTA Race have other rotations :/ Somewhat inaccurate, but works :D
  341.                                 {
  342.                                     if(pos[a] != 0.000000)
  343.                                     {
  344.                                         // pos[a] = 6.2831 + (pos[a]*0.0174);
  345.                                         pos[a] = (pos[a]+6.2831)/0.0174;
  346.                                     }
  347.                                 }
  348.  
  349.                                 // <model>
  350.                                 fread(MapFile, strFromFile, sizeof(strFromFile));
  351.                                 StripNewLine(strFromFile);
  352.  
  353.                                 for(new a = x; a < strlen(strFromFile); a++)
  354.                                 {
  355.                                     if(strFromFile[a-1] == '>')
  356.                                     {
  357.                                         for(new y = 0; y < 3; y++)
  358.                                         {
  359.                                             Model = strval(strFromFile[a]);
  360.                                         }
  361.                                     }
  362.                                 }
  363.                                
  364.                                 format(strtowrite, sizeof(strtowrite), "    CreateObject(%d, %f, %f, %f, %f, %f, %f);\r", Model, pos[0], pos[1], pos[2], pos[5], pos[4], pos[3]);
  365.                                 fwrite(PWNFile, strtowrite);
  366.                             }
  367.                         }
  368.                     }
  369.                 }
  370.             }
  371.         }
  372.        
  373.         fwrite(PWNFile, "   return 1;\n}");
  374.         fclose(PWNFile);
  375.     }
  376.     else return -1;
  377.    
  378.     fclose(MapFile);
  379.    
  380.     return MapType;
  381. }
  382.  
  383. stock StripNewLine(string[])
  384. {
  385.     new len = strlen(string);
  386.     if(string[0]==0) return ;
  387.     //-----
  388.     if ((string[len - 1] == '\n') || (string[len - 1] == '\r'))
  389.     {
  390.         string[len - 1] = 0;
  391.         if (string[0]==0) return ;
  392.         //-----
  393.         if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  394.     }
  395. }
  396.  
  397. stock IsCharNumeric(Char[])
  398. {
  399.     if( Char[0] == '0' || Char[0] == '1' || Char[0] == '2' || Char[0] == '3' || Char[0] == '4' || Char[0] == '5' ||
  400.         Char[0] == '6' || Char[0] == '7' || Char[0] == '8' || Char[0] == '9') return true;
  401.     return false;
  402. }
  403.  
  404. stock DetectSavingValue(string[], a)
  405. {
  406.     if(!strcmp(string[a], "posX=", true, 5)) return 0;
  407.     else if(!strcmp(string[a], "posY=", true, 5)) return 1;
  408.     else if(!strcmp(string[a], "posZ=", true, 5)) return 2;
  409.     else if(!strcmp(string[a], "rotX=", true, 5)) return 3;
  410.     else if(!strcmp(string[a], "rotY=", true, 5)) return 4;
  411.     else if(!strcmp(string[a], "rotZ=", true, 5)) return 5;
  412.     else if(!strcmp(string[a], "model=", true, 6)) return 6;
  413.    
  414.     return -1;
  415. }
Add Comment
Please, Sign In to add comment