Psymetrix

ObjectEdit0.1

Mar 16th, 2012
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 14.44 KB | None | 0 0
  1. /*
  2.     ObjectEdit By Psymetrix
  3.  
  4.     Information:
  5.         This script allows more control over SA:MP's in-built object editor.
  6.        
  7.         Comments with the prefix 'NOTE:' require further changes or edits.
  8.  
  9.     Dialog structure:
  10.         [+] Create
  11.             [*] Insert model ID
  12.             [*] Insert object ID (Clone object)
  13.             [*] Select (Clone object)
  14.         [+] Remove
  15.             [*] Insert object ID
  16.             [*] Select
  17.         [+] Edit
  18.             [*] Insert object ID
  19.             [*] Select
  20.         [+] Project
  21.             [*] Save
  22.             [*] Load
  23.             [*] Export
  24. */
  25.  
  26. #define FILTERSCRIPT
  27.  
  28. #include <a_samp>
  29.  
  30. #define EDITOR_VERSION                                                     "0.1"
  31.  
  32. #define DIALOG_MENU                                                         5666
  33. #define DIALOG_CREATE                                              DIALOG_MENU+1
  34. #define DIALOG_REMOVE                                              DIALOG_MENU+2
  35. #define DIALOG_EDIT                                                DIALOG_MENU+3
  36. #define DIALOG_CREATE_MODEL                                        DIALOG_MENU+4
  37. #define DIALOG_CREATE_ID                                           DIALOG_MENU+5
  38. #define DIALOG_REMOVE_ID                                           DIALOG_MENU+6
  39. #define DIALOG_EDIT_ID                                             DIALOG_MENU+7
  40. #define DIALOG_PROJECT                                             DIALOG_MENU+8
  41. #define DIALOG_SAVE                                                DIALOG_MENU+9
  42. #define DIALOG_LOAD                                               DIALOG_MENU+10
  43. #define DIALOG_EXPORT                                             DIALOG_MENU+11
  44. #define DIALOG_NEW_PROJECT                                        DIALOG_MENU+12
  45.  
  46. #define EDIT_MODE_NONE                                                         0
  47. #define EDIT_MODE_REMOVE                                                       1
  48. #define EDIT_MODE_CLONE                                                        2
  49. #define EDIT_MODE_EDIT                                                         3
  50.  
  51. new ObjectModel[MAX_OBJECTS] = {INVALID_OBJECT_ID, ...};
  52.  
  53. new EditMode[MAX_PLAYERS] = {EDIT_MODE_NONE, ...};
  54.  
  55. forward Delay_SelectObject(playerid);
  56. forward Delay_EditObject(playerid, objectid);
  57.  
  58. Editor_CreateObject(model, Float:x, Float:y, Float:z, Float:rotx, Float:roty, Float:rotz)
  59. {
  60.     new objectid;
  61.  
  62.     objectid = CreateObject(model, x, y, z, rotx, roty, rotz);
  63.  
  64.     ObjectModel[objectid] = model;
  65.  
  66.     return objectid;
  67. }
  68.  
  69. Editor_RemoveObject(objectid)
  70. {
  71.     if (!IsValidObject(objectid) || ObjectModel[objectid] == INVALID_OBJECT_ID) return 0;
  72.     DestroyObject(objectid);
  73.  
  74.     ObjectModel[objectid] = INVALID_OBJECT_ID;
  75.     return 1;
  76. }
  77.  
  78. Editor_DuplicateObject(objectid)
  79. {
  80.     new Float:x, Float:y, Float:z, Float:rotx, Float:roty, Float:rotz;
  81.     GetObjectPos(objectid, x, y, z);
  82.     GetObjectRot(objectid, rotx, roty, rotz);
  83.     Editor_CreateObject(ObjectModel[objectid], x, y, z, rotx, roty, rotz);
  84. }
  85.  
  86. Editor_RemoveEditorObjects()
  87. {
  88.     for (new i; i < MAX_OBJECTS; i++)
  89.     {
  90.         Editor_RemoveObject(i);
  91.     }
  92. }
  93.  
  94. Editor_PlayerSelectObject(playerid)
  95.     SetTimerEx("Delay_SelectObject", 200, false, "i", playerid);
  96.  
  97. public Delay_SelectObject(playerid)
  98.     SelectObject(playerid);
  99.  
  100. Editor_PlayerEditObject(playerid, objectid)
  101.     SetTimerEx("Delay_EditObject", 200, false, "ii", playerid, objectid);
  102.  
  103. public Delay_EditObject(playerid, objectid)
  104.     EditObject(playerid, objectid);
  105.  
  106. ShowDialog(playerid, dialogid)
  107. {
  108.     EditMode[playerid] = EDIT_MODE_NONE;
  109.     switch(dialogid)
  110.     {
  111.         // Main editor dialog
  112.         case DIALOG_MENU:          ShowPlayerDialog(playerid, DIALOG_MENU, DIALOG_STYLE_LIST, "ObjectEdit "EDITOR_VERSION"", "[+] Create\n[+] Remove\n[+] Edit\n[+] Project", "Select", "Cancel");
  113.         // DIALOG_MENU response
  114.         case DIALOG_CREATE:        ShowPlayerDialog(playerid, DIALOG_CREATE, DIALOG_STYLE_LIST, "ObjectEdit "EDITOR_VERSION" - Create", "[*] Insert model ID\n[*] Insert object ID (Clone object)\n[*] Select (Clone object)", "Select", "Back");
  115.         case DIALOG_REMOVE:        ShowPlayerDialog(playerid, DIALOG_REMOVE, DIALOG_STYLE_LIST, "ObjectEdit "EDITOR_VERSION" - Remove", "[*] Insert object ID\n[*] Select", "Select", "Back");
  116.         case DIALOG_EDIT:          ShowPlayerDialog(playerid, DIALOG_EDIT, DIALOG_STYLE_LIST, "ObjectEdit "EDITOR_VERSION" - Edit", "[*] Insert object ID\n[*] Select", "Select", "Back");
  117.         // DIALOG_CREATE response
  118.         case DIALOG_CREATE_MODEL:  ShowPlayerDialog(playerid, DIALOG_CREATE_MODEL, DIALOG_STYLE_INPUT, "ObjectEdit "EDITOR_VERSION" - Create", "Please insert an object model ID.", "Create", "Back");
  119.         case DIALOG_CREATE_ID:     ShowPlayerDialog(playerid, DIALOG_CREATE_ID, DIALOG_STYLE_INPUT, "ObjectEdit "EDITOR_VERSION" - Create", "Please insert an object ID.", "Clone", "Back");
  120.         // DIALOG_REMOVE response
  121.         case DIALOG_REMOVE_ID:     ShowPlayerDialog(playerid, DIALOG_REMOVE_ID, DIALOG_STYLE_INPUT, "ObjectEdit "EDITOR_VERSION" - Remove", "Please insert an object ID.", "Remove", "Back");
  122.         // DIALOG_EDIT response
  123.         case DIALOG_EDIT_ID:       ShowPlayerDialog(playerid, DIALOG_EDIT_ID, DIALOG_STYLE_INPUT, "ObjectEdit "EDITOR_VERSION" - Edit", "Please insert an object ID.", "Edit", "Back");
  124.         // Project dialog
  125.         case DIALOG_PROJECT:       ShowPlayerDialog(playerid, DIALOG_PROJECT, DIALOG_STYLE_LIST, "ObjectEdit "EDITOR_VERSION" - Project", "[*] Save\n[*] Load\n[*] Export\n[*] New", "Select", "Back");
  126.         // DIALOG_PROJECT response
  127.         case DIALOG_SAVE:          ShowPlayerDialog(playerid, DIALOG_SAVE, DIALOG_STYLE_INPUT, "ObjectEdit "EDITOR_VERSION" - Save", "Please give a name for this project.\n\nDo not use file extensions.\n\nIf the project file already exists, it will be overwritten.", "Save", "Back");
  128.         case DIALOG_LOAD:          ShowPlayerDialog(playerid, DIALOG_LOAD, DIALOG_STYLE_INPUT, "ObjectEdit "EDITOR_VERSION" - Load", "Please give a project name to open.\n\nDo not use file extensions.\n\nPlease save your work.\nOpening a project will remove all existing objects\nadded by this editor.", "Load", "Back");
  129.         case DIALOG_EXPORT:        ShowPlayerDialog(playerid, DIALOG_EXPORT, DIALOG_STYLE_INPUT, "ObjectEdit "EDITOR_VERSION" - Export", "Please give a name for this export.\n\nDo not use file extensions.\n\nIf the export file already exists, it will be overwritten.", "Export", "Back");
  130.         case DIALOG_NEW_PROJECT:   ShowPlayerDialog(playerid, DIALOG_NEW_PROJECT, DIALOG_STYLE_MSGBOX, "ObjectEdit "EDITOR_VERSION" - New", "Do you really want to start a new project?\n\nThis will delete all objects added by the editor\nand cannot be undone!", "New", "Back");
  131.     }
  132. }
  133.  
  134. split(const strsrc[], strdest[][], delimiter)
  135. {
  136.     new i, li;
  137.     new aNum;
  138.     new len;
  139.     while(i <= strlen(strsrc))
  140.     {
  141.         if(strsrc[i]==delimiter || i==strlen(strsrc))
  142.         {
  143.             len = strmid(strdest[aNum], strsrc, li, i, 128);
  144.             strdest[aNum][len] = 0;
  145.             li = i+1;
  146.             aNum++;
  147.         }
  148.         i++;
  149.     }
  150.     return 1;
  151. }
  152.  
  153. public OnFilterScriptInit()
  154. {
  155.     return 1;
  156. }
  157.  
  158. public OnFilterScriptExit()
  159. {
  160.     Editor_RemoveEditorObjects();
  161.     return 1;
  162. }
  163.  
  164. public OnPlayerCommandText(playerid, cmdtext[])
  165. {
  166.     if (strcmp(cmdtext, "/oedit", true) == 0)
  167.     {
  168.         ShowDialog(playerid, DIALOG_MENU);
  169.         return 1;
  170.     }
  171.     return 0;
  172. }
  173.  
  174. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  175. {
  176.     // Main editor dialog
  177.     if (dialogid == DIALOG_MENU)
  178.     {
  179.         if (response)
  180.         {
  181.             switch (listitem)
  182.             {
  183.                 // Create
  184.                 case 0: ShowDialog(playerid, DIALOG_CREATE);
  185.                 // Remove
  186.                 case 1: ShowDialog(playerid, DIALOG_REMOVE);
  187.                 // Edit
  188.                 case 2: ShowDialog(playerid, DIALOG_EDIT);
  189.                 // Project
  190.                 case 3: ShowDialog(playerid, DIALOG_PROJECT);
  191.             }
  192.         }
  193.         return 1;
  194.     }  
  195.     // Create object dialog
  196.     if (dialogid == DIALOG_CREATE)
  197.     {
  198.         if (!response) ShowDialog(playerid, DIALOG_MENU);
  199.         else
  200.         {
  201.             switch (listitem)
  202.             {
  203.                 case 0: ShowDialog(playerid, DIALOG_CREATE_MODEL);
  204.                 case 1: ShowDialog(playerid, DIALOG_CREATE_ID);
  205.                 case 2:
  206.                 {
  207.                     Editor_PlayerSelectObject(playerid);
  208.                     EditMode[playerid] = EDIT_MODE_CLONE;
  209.                 }
  210.             }
  211.         }
  212.         return 1;
  213.     }  
  214.     // Remove object dialog
  215.     if (dialogid == DIALOG_REMOVE)
  216.     {
  217.         if (!response) ShowDialog(playerid, DIALOG_MENU);
  218.         else
  219.         {
  220.             switch (listitem)
  221.             {
  222.                 case 0: ShowDialog(playerid, DIALOG_REMOVE_ID);
  223.                 case 1:
  224.                 {
  225.                     Editor_PlayerSelectObject(playerid);
  226.                     EditMode[playerid] = EDIT_MODE_REMOVE;
  227.                 }
  228.             }
  229.         }
  230.         return 1;
  231.     }  
  232.     // Edit object dialog
  233.     if (dialogid == DIALOG_EDIT)
  234.     {
  235.         if (!response) ShowDialog(playerid, DIALOG_MENU);
  236.         else
  237.         {
  238.             switch (listitem)
  239.             {
  240.                 case 0: ShowDialog(playerid, DIALOG_EDIT_ID);
  241.                 case 1:
  242.                 {
  243.                     Editor_PlayerSelectObject(playerid);
  244.                     EditMode[playerid] = EDIT_MODE_EDIT;
  245.                 }
  246.             }
  247.         }
  248.         return 1;
  249.     }  
  250.     // Project object dialog
  251.     if (dialogid == DIALOG_PROJECT)
  252.     {
  253.         if (!response) ShowDialog(playerid, DIALOG_MENU);
  254.         else
  255.         {
  256.             switch (listitem)
  257.             {
  258.                 case 0: ShowDialog(playerid, DIALOG_SAVE);
  259.                 case 1: ShowDialog(playerid, DIALOG_LOAD);
  260.                 case 2: ShowDialog(playerid, DIALOG_EXPORT);
  261.                 case 3: ShowDialog(playerid, DIALOG_NEW_PROJECT);
  262.             }
  263.         }
  264.         return 1;
  265.     }  
  266.     // Create object by model
  267.     if (dialogid == DIALOG_CREATE_MODEL)
  268.     {
  269.         if (!response || !inputtext[0]) ShowDialog(playerid, DIALOG_CREATE);
  270.         else
  271.         {
  272.             new Float:x, Float:y, Float:z;
  273.             GetPlayerPos(playerid, x, y, z);
  274.             Editor_CreateObject(strval(inputtext), x, y, z, 0.0, 0.0, 0.0);
  275.             ShowDialog(playerid, DIALOG_CREATE);
  276.         }
  277.         return 1;
  278.     }  
  279.     // Create object by object ID (clone)
  280.     if (dialogid == DIALOG_CREATE_ID)
  281.     {
  282.         if (!response || !inputtext[0]) ShowDialog(playerid, DIALOG_CREATE);
  283.         else
  284.         {
  285.             Editor_DuplicateObject(strval(inputtext));
  286.             ShowDialog(playerid, DIALOG_CREATE);
  287.         }
  288.         return 1;
  289.     }  
  290.     // Remove object by object ID
  291.     if (dialogid == DIALOG_REMOVE_ID)
  292.     {
  293.         if (!response || !inputtext[0]) ShowDialog(playerid, DIALOG_REMOVE);
  294.         else
  295.         {
  296.             Editor_RemoveObject(strval(inputtext));
  297.             ShowDialog(playerid, DIALOG_REMOVE);
  298.         }
  299.         return 1;
  300.     }  
  301.     // Edit object by object ID
  302.     if (dialogid == DIALOG_EDIT_ID)
  303.     {
  304.         if (!response || !inputtext[0]) ShowDialog(playerid, DIALOG_EDIT);
  305.         else
  306.         {
  307.             Editor_PlayerEditObject(playerid, strval(inputtext));
  308.             EditMode[playerid] = EDIT_MODE_EDIT;
  309.         }
  310.         return 1;
  311.     }  
  312.     // Save project
  313.     if (dialogid == DIALOG_SAVE)
  314.     {
  315.         if (!response || !inputtext[0]) ShowDialog(playerid, DIALOG_PROJECT);
  316.         else
  317.         {
  318.             new File:pFile, string[128];
  319.             format(string, sizeof(string), "%s.txt", inputtext);
  320.             pFile = fopen(string, io_write);
  321.             if (pFile)
  322.             {
  323.                 new Float:x, Float:y, Float:z, Float:rotx, Float:roty, Float:rotz;
  324.                 for (new i; i < MAX_OBJECTS; i++)
  325.                 {
  326.                     if (ObjectModel[i] && IsValidObject(i))
  327.                     {
  328.                         GetObjectPos(i, x, y, z);
  329.                         GetObjectRot(i, rotx, roty, rotz);
  330.                         format(string, sizeof(string), "%d, %f, %f, %f, %f, %f, %f\r\n", ObjectModel[i], x, y, z, rotx, roty, rotz);
  331.                         fwrite(pFile, string);
  332.                     }
  333.                 }
  334.                 fclose(pFile);
  335.                 ShowDialog(playerid, DIALOG_PROJECT);
  336.             }
  337.         }
  338.         return 1;      
  339.     }  
  340.     // Load project
  341.     if (dialogid == DIALOG_LOAD)
  342.     {
  343.         if (!response || !inputtext[0]) ShowDialog(playerid, DIALOG_PROJECT);
  344.         else
  345.         {
  346.             new File:pFile, string[128];
  347.             format(string, sizeof(string), "%s.txt", inputtext);
  348.  
  349.             if (fexist(string))
  350.             {
  351.                 Editor_RemoveEditorObjects();
  352.                 new params[7][10];
  353.                 pFile = fopen(string, io_read);
  354.                 if (pFile)
  355.                 {
  356.                     while (fread(pFile, string))
  357.                     {
  358.                         split(string, params, ',');
  359.                         Editor_CreateObject(strval(params[0]), floatstr(params[1]), floatstr(params[2]), floatstr(params[3]), floatstr(params[4]), floatstr(params[5]), floatstr(params[6]));
  360.                     }
  361.                     fclose(pFile);
  362.                     ShowDialog(playerid, DIALOG_PROJECT);
  363.                 }
  364.             }
  365.         }
  366.         return 1;
  367.     }      
  368.     // Export project
  369.     if (dialogid == DIALOG_EXPORT)
  370.     {
  371.         if (!response || !inputtext[0]) ShowDialog(playerid, DIALOG_PROJECT);
  372.         else
  373.         {
  374.             // NOTE: eFile and string2 need changed back to oFile and string
  375.             // and the warning removed.
  376.             new File:eFile, string2[128];
  377.             format(string2, sizeof(string2), "%s.pwn", inputtext);
  378.             eFile = fopen(string2, io_write);
  379.             if (eFile)
  380.             {
  381.                 fwrite(eFile, "#define FILTERSCRIPT\r\n\n");               
  382.                 fwrite(eFile, "#include <a_samp>\r\n\n");              
  383.                 fwrite(eFile, "public OnGameFilterScriptInit()\r\n");
  384.                 fwrite(eFile, "{\r\n");
  385.                 new Float:x, Float:y, Float:z, Float:rotx, Float:roty, Float:rotz;
  386.                 for (new i; i < MAX_OBJECTS; i++)
  387.                 {
  388.                     if (ObjectModel[i] != INVALID_OBJECT_ID && IsValidObject(i))
  389.                     {
  390.                         GetObjectPos(i, x, y, z);
  391.                         GetObjectRot(i, rotx, roty, rotz);
  392.                         format(string2, sizeof(string2), "\tCreateObject(%d,%f,%f,%f,%f,%f,%f);\r\n", ObjectModel[i], x, y, z, rotx, roty, rotz);
  393.                         fwrite(eFile, string2);
  394.                     }
  395.                 }
  396.                 fwrite(eFile, "\treturn 1;\r\n");
  397.                 fwrite(eFile, "}\r\n");
  398.                 fclose(eFile);
  399.                 ShowDialog(playerid, DIALOG_PROJECT);
  400.             }
  401.         }
  402.         return 1;
  403.     }
  404.    
  405.     // New project
  406.     if (dialogid == DIALOG_NEW_PROJECT)
  407.     {
  408.         if (response) Editor_RemoveEditorObjects();
  409.         else ShowDialog(playerid, DIALOG_PROJECT);
  410.     }
  411.     return 0;
  412. }
  413.  
  414. public OnPlayerSelectObject(playerid, type, objectid, modelid, Float:fX, Float:fY, Float:fZ)
  415. {
  416.     // We don't handle player objects in this script
  417.     if (type != SELECT_OBJECT_GLOBAL_OBJECT) return 0;
  418.  
  419.     if (EditMode[playerid] == EDIT_MODE_REMOVE)
  420.     {
  421.         Editor_RemoveObject(objectid);
  422.         CancelEdit(playerid);
  423.         ShowDialog(playerid, DIALOG_REMOVE);
  424.     }
  425.     else if (EditMode[playerid] == EDIT_MODE_CLONE)
  426.     {
  427.         Editor_DuplicateObject(objectid);
  428.         CancelEdit(playerid);
  429.         ShowDialog(playerid, DIALOG_CREATE);
  430.     }
  431.     else if (EditMode[playerid] == EDIT_MODE_EDIT)
  432.     {
  433.         Editor_PlayerEditObject(playerid, objectid);
  434.     }
  435.     return 1;
  436. }
  437.  
  438. public OnPlayerEditObject(playerid, playerobject, objectid, response, Float:fX, Float:fY, Float:fZ, Float:fRotX, Float:fRotY, Float:fRotZ)
  439. {
  440.     // We don't handle player objects in this script
  441.     if (playerobject) return;
  442.  
  443.     if(response)
  444.     {
  445.         if (response == EDIT_RESPONSE_FINAL)
  446.         {
  447.             // Update the object
  448.             SetObjectPos(objectid, fX, fY, fZ);
  449.             SetObjectRot(objectid, fRotX, fRotY, fRotZ);
  450.             if (EditMode[playerid] == EDIT_MODE_EDIT)
  451.             {
  452.                 ShowDialog(playerid, DIALOG_EDIT);
  453.             }
  454.         }
  455.     }
  456.     else
  457.     {
  458.         // Undo last movements
  459.         new Float:x, Float:y, Float:z, Float:rotx, Float:roty, Float:rotz;
  460.         GetObjectPos(objectid, x, y, z);
  461.         GetObjectRot(objectid, rotx, roty, rotz);
  462.         SetObjectPos(objectid, x, y, z);
  463.         SetObjectRot(objectid, rotx, roty, rotz);
  464.         if (EditMode[playerid] == EDIT_MODE_EDIT)
  465.         {
  466.             ShowDialog(playerid, DIALOG_EDIT);
  467.         }
  468.     }
  469. }
Advertisement
Add Comment
Please, Sign In to add comment