ThreeKingz

Inventory system by ThreeKingz aka ThePhenix

Aug 13th, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 10.96 KB | None | 0 0
  1.  
  2. /*
  3. Inventory System by ThePhenix aka ThreeKingz
  4.  
  5. */
  6. /*==============================================================================
  7. Includes
  8. ===============================================================================*/
  9. #include <a_samp>
  10. #include <zcmd>
  11. #include <sscanf2>
  12. #include <YSI\y_ini>
  13. #include <mSelection>
  14. #include <MapAndreas>
  15.  
  16.  
  17. #define SendFormattedMessage(%0,%1,%2,%3) \
  18. new text[128]; \
  19. format(text,sizeof(text),%2,%3); \
  20. SendClientMessage(%0,%1,text)
  21.  
  22.  
  23. /*==============================================================================
  24. Defines
  25. ===============================================================================*/
  26. #define MAX_INVENTORY_SLOTS 50 //Max slots to save items per player
  27. #define INV_SAVING_PATH "INV/Inventory/%s.ini" //The folder where the inventory data of each player will be stored.
  28. #define OBJ_SAVING_PATH "INV/Objects/Object%d.ini" //The folder where the objects created by the RCON will be stored.
  29. #define EMPTY_SLOT_OBJ 1239 //((Empty slot)) The object shown in mSelection for an empty slot.
  30. #define MAX_ITEMS 200 //The maximun amount of items the RCON Administrator can create.
  31. #define OBJECTS_MENU 1
  32. #define I_MENU 2
  33.  
  34. #define PRESSED(%0) \
  35.     (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  36.    
  37.  
  38. new ObjectID[MAX_ITEMS];
  39. new Text3D:LabelID[MAX_ITEMS];
  40. new ObjTimer[MAX_ITEMS];
  41.  
  42. //You can add as much models as you wish
  43. new objects[]=
  44. {
  45.     1484, 1210, 1342, 2804, 1341, 3028, 1512, 2992, 3086, 3030, 2966, 3027, 3082, 1600
  46. };
  47. /*==============================================================================
  48. Enums
  49. ===============================================================================*/
  50. enum oInfo
  51. {
  52.     Model,
  53.     Float: pX,
  54.     Float: pY,
  55.     Float: pZ
  56. }
  57. new ObjInfo[MAX_ITEMS][oInfo];
  58.  
  59.  
  60. enum iInfo
  61. {
  62.     Slot[MAX_INVENTORY_SLOTS]
  63. }
  64. new InvInfo[MAX_PLAYERS][iInfo];
  65.  
  66. /*==============================================================================
  67. Commands
  68. ===============================================================================*/
  69. CMD:citem(playerid, params[])
  70. {
  71.     if(IsPlayerAdmin(playerid))
  72.     {
  73.         ShowModelSelectionMenuEx(playerid, objects, 12, "Select object", OBJECTS_MENU, 16.0, 0.0, -55.0);
  74.     }
  75.     else
  76.     {
  77.         SendClientMessage(playerid, 0xFF00FFFF, "You need to log in as RCON Administrator");
  78.     }
  79.     return 1;
  80. }
  81. CMD:ci(playerid, params[])
  82. {
  83.     return cmd_citem(playerid, params);
  84. }
  85. CMD:myitems(playerid, params[])
  86. {
  87.     ShowModelSelectionMenuEx(playerid, InvInfo[playerid][Slot], MAX_INVENTORY_SLOTS, "Your items", I_MENU, 16.0, 0.0, -55.0);
  88.     return 1;
  89. }
  90.  
  91. COMMAND:car(playerid,params[])
  92. {
  93.         new car;
  94.         if(IsPlayerInAnyVehicle(playerid)) return 1;
  95.         if(sscanf(params, "i", car)) return SendClientMessage(playerid, -1, "USE: /car [car id]");
  96.         if(strval(params) >611 || strval(params) <400) return SendClientMessage(playerid, -1, "That id doesn't exist[400-611]");
  97.         new Float:x, Float:y, Float:z, Float:a;
  98.         GetPlayerPos(playerid, x,y,z);
  99.         GetPlayerFacingAngle(playerid, a);
  100.         car = CreateVehicle(strval(params), x, y, z,a, -1, -1, 60);
  101.         PutPlayerInVehicle(playerid, car, 0);
  102.     return 1;
  103. }
  104.  
  105. /*==============================================================================
  106. Public functions
  107. ===============================================================================*/
  108.  
  109. public OnPlayerModelSelectionEx(playerid, response, extraid, modelid)
  110. {
  111.     if(extraid == OBJECTS_MENU)
  112.     {
  113.         if(response)
  114.         {
  115.             new objectid = EmptySlot();
  116.             new Float: X, Float: Y, Float: Z;
  117.             GetPlayerPos(playerid, X, Y, Z);
  118.             MapAndreas_FindZ_For2DCoord(X, Y, Z);
  119.             ObjectID[objectid] = CreateObject(modelid, X, Y, Z, 0.0, 0.0, 96.0);
  120.             LabelID[ObjectID[objectid]] = Create3DTextLabel("Press Y to take this item", 0x20B2AAAA, X, Y, Z, 15.0, 0);
  121.             SendFormattedMessage(playerid, 0xADFF2FAA, "You have created an object successfully. [Object ID: %d | Model %d]", ObjectID[objectid], modelid);
  122.             new obj = ObjectID[objectid];
  123.             ObjInfo[obj][Model] = modelid;
  124.             ObjInfo[obj][pX] = X;
  125.             ObjInfo[obj][pY] = Y;
  126.             ObjInfo[obj][pZ] = Z;
  127.             SaveObject(obj);
  128.         }
  129.     }
  130.     return 1;
  131. }
  132.  
  133.  
  134. stock EmptySlot()
  135. {
  136.     new
  137.         i = 0;
  138.     while (i < sizeof (ObjectID) && ObjectID[i])
  139.     {
  140.         i++;
  141.     }
  142.     if (i == sizeof (ObjectID)) return -1;
  143.     return i;
  144. }
  145.  
  146. public OnFilterScriptInit()
  147. {
  148.     MapAndreas_Init(MAP_ANDREAS_MODE_FULL);
  149.     new count;
  150.     for(new i; i < MAX_ITEMS; i++)
  151.     {
  152.         if(fexist(ObjPath(i)))
  153.         {
  154.             count++;
  155.             INI_ParseFile(ObjPath(i),"loadobj_ob", .bExtra = true, .extra = i);
  156.             ObjectID[i] = CreateObject(ObjInfo[i][Model], ObjInfo[i][pX], ObjInfo[i][pY], ObjInfo[i][pZ], 0.0, 0.0, 96.0);
  157.             LabelID[ObjectID[i]] = Create3DTextLabel("Press Y to take this item", 0x20B2AAAA, ObjInfo[i][pX], ObjInfo[i][pY], ObjInfo[i][pZ], 15.0, 0);
  158.         }
  159.     }
  160.     printf("Objects loaded: %d", count);
  161.     return 1;
  162. }
  163.  
  164. public OnFilterScriptExit()
  165. {
  166.     return 1;
  167. }
  168.  
  169.  
  170.  
  171. public OnPlayerRequestClass(playerid, classid)
  172. {
  173.     SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
  174.     SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
  175.     SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
  176.     return 1;
  177. }
  178.  
  179. public OnPlayerConnect(playerid)
  180. {
  181.     if(fexist(InvPath(playerid)))
  182.     {
  183.         INI_ParseFile(InvPath(playerid),"loadinv_user", .bExtra = true, .extra = playerid);
  184.     }
  185.     else
  186.     {
  187.         for(new i; i < MAX_INVENTORY_SLOTS; i++)
  188.         {
  189.             new string[14];
  190.             format(string, sizeof(string), "Slot%d", i);
  191.             InvInfo[playerid][Slot][i] = EMPTY_SLOT_OBJ;
  192.             new INI:file = INI_Open(InvPath(playerid));
  193.             INI_SetTag(file,"Inventory Data");
  194.             INI_WriteInt(file, string, EMPTY_SLOT_OBJ);
  195.             INI_Close(file);
  196.         }
  197.     }
  198.     return 1;
  199. }
  200.  
  201. public OnPlayerDisconnect(playerid, reason)
  202. {
  203.     SaveInventory(playerid);
  204.     return 1;
  205. }
  206.  
  207. public OnPlayerSpawn(playerid)
  208. {
  209.     ApplyAnimation(playerid,"SWEET","null",0.0,0,0,0,0,0);
  210.     return 1;
  211. }
  212.  
  213. public OnPlayerDeath(playerid, killerid, reason)
  214. {
  215.     return 1;
  216. }
  217.  
  218. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  219. {
  220.     if (PRESSED(KEY_YES))
  221.     {
  222.         if(IsPlayerNearAnyObject(playerid))
  223.         {
  224.             new objectid = GetNearestObjectForPlayer(playerid);
  225.             new model = ObjInfo[objectid][Model];
  226.             new slot = FindEmptySlot(playerid);
  227.             ApplyAnimation(playerid, "BOMBER", "BOM_Plant", 4.1, 0, 1, 1, 0, 3000, 1);
  228.             SetTimerEx("TakingItem", 3000, false, "i", playerid);
  229.             SendClientMessage(playerid, 0xB4B5B7AA, "Taking item...");
  230.             InvInfo[playerid][Slot][slot] = model;
  231.             SetPVarInt(playerid, "object", objectid);
  232.         }
  233.     }
  234.     return 1;
  235. }
  236.  
  237. /*==============================================================================
  238. Functions
  239. ===============================================================================*/
  240. forward TakingItem(playerid);
  241. public TakingItem(playerid)
  242. {
  243.     new objectid = GetPVarInt(playerid, "object");
  244.     SendClientMessage(playerid, 0xB0C4DEFF, "You have successfully put that item into your inventory.");
  245.     MoveObject(objectid, ObjInfo[objectid][pX], ObjInfo[objectid][pY], ObjInfo[objectid][pZ]-100, 5.0);
  246.     Delete3DTextLabel(LabelID[objectid]);
  247.     ObjTimer[objectid] = SetTimerEx("RespawnObject", 120000, false, "i", objectid);
  248.     DeletePVar(playerid, "object");
  249.     return 1;
  250. }
  251. forward RespawnObject(objectid);
  252. public RespawnObject(objectid)
  253. {
  254.     ObjectID[objectid] = MoveObject(objectid, ObjInfo[objectid][pX], ObjInfo[objectid][pY], ObjInfo[objectid][pZ], 10.0);
  255.     LabelID[ObjectID[objectid]] = Create3DTextLabel("Press Y to take this item", 0x20B2AAAA, ObjInfo[objectid][pX], ObjInfo[objectid][pY], ObjInfo[objectid][pZ], 15.0, 0);
  256.     KillTimer(ObjTimer[objectid]);
  257.     return 1;
  258. }
  259. forward loadinv_user(playerid, name[], value[]);
  260. public loadinv_user(playerid, name[], value[])
  261. {
  262.     for(new i; i < MAX_INVENTORY_SLOTS; i++)
  263.     {
  264.         new string[7];
  265.         format(string, sizeof(string), "Slot%d", i);
  266.         INI_Int(string, InvInfo[playerid][Slot][i]);
  267.     }
  268.     return 1;
  269. }
  270. forward loadobj_obj(objectid, name[], value[]);
  271. public loadobj_obj(objectid, name[], value[])
  272. {
  273.     INI_Int("Model", ObjInfo[objectid][Model]);
  274.     INI_Float("pX", ObjInfo[objectid][pX]);
  275.     INI_Float("pY", ObjInfo[objectid][pY]);
  276.     INI_Float("pZ", ObjInfo[objectid][pZ]);
  277.     return 1;
  278. }
  279. stock SaveInventory(playerid)
  280. {
  281.     for(new i; i < MAX_INVENTORY_SLOTS; i++)
  282.     {
  283.         new string[7];
  284.         format(string, sizeof(string), "Slot%d", i);
  285.         new INI:file = INI_Open(InvPath(playerid));
  286.         INI_SetTag(file,"Inventory Data");
  287.         INI_WriteInt(file, string, InvInfo[playerid][Slot][i]);
  288.         INI_Close(file);
  289.     }
  290.     return 1;
  291. }
  292.  
  293. stock pName(playerid)
  294. {
  295.     new name[MAX_PLAYER_NAME];
  296.     GetPlayerName(playerid, name, sizeof(name));
  297.     return name;
  298. }
  299. stock InvPath(playerid)
  300. {
  301.     new str[40];
  302.     format(str,sizeof(str), INV_SAVING_PATH , pName(playerid));
  303.     return str;
  304. }
  305. stock ObjPath(object)
  306. {
  307.     new str[40];
  308.     format(str,sizeof(str), OBJ_SAVING_PATH , object);
  309.     return str;
  310. }
  311.  
  312. stock SaveObject(object)
  313. {
  314.     new INI:file = INI_Open(ObjPath(object));
  315.     INI_SetTag(file,"ObjectID Data");
  316.     INI_WriteInt(file, "Model", ObjInfo[object][Model]);
  317.     INI_WriteFloat(file, "pX", ObjInfo[object][pX]);
  318.     INI_WriteFloat(file, "pY", ObjInfo[object][pY]);
  319.     INI_WriteFloat(file, "pZ", ObjInfo[object][pZ]);
  320.     INI_Close(file);
  321.     return 1;
  322. }
  323. stock FindEmptySlot(playerid)
  324. {
  325.     new slot;
  326.     for(new i; i<MAX_INVENTORY_SLOTS; i++)
  327.     {
  328.         if(InvInfo[playerid][Slot][i] == EMPTY_SLOT_OBJ)
  329.         {
  330.             slot = i;
  331.             break;
  332.            
  333.         }
  334.     }
  335.     return slot;
  336. }
  337. stock IsPlayerNearObject(playerid, objectid, Float:range)
  338. {
  339.     new Float:X, Float:Y, Float:Z;
  340.     GetObjectPos(objectid, X, Y, Z);
  341.     if(IsPlayerInRangeOfPoint(playerid, range, X, Y, Z))return true;
  342.     return false;
  343. }
  344. stock IsPlayerNearAnyObject(playerid)
  345. {
  346.     for(new i = 0; i < MAX_ITEMS; i++)
  347.     {
  348.         if(ObjectID[i] = i)
  349.         {
  350.              if(IsPlayerNearObject(playerid, ObjectID[i], 2.4)) return true;
  351.         }
  352.     }
  353.     return false;
  354. }
  355. stock GetNearestObjectForPlayer(playerid, exception = INVALID_OBJECT_ID)
  356. {
  357.     new Float:Distance, target = -1;
  358.  
  359.     for(new v; v < MAX_ITEMS; v++) if(IsValidObject(ObjectID[v]))
  360.     {
  361.         if(ObjectID[v] != exception && (target < 0 || Distance > GetDistancePlayerObject(playerid, ObjectID[v])))
  362.         {
  363.             target = ObjectID[v];
  364.             Distance = GetDistancePlayerObject(playerid, ObjectID[v]);
  365.         }
  366.     }
  367.     return target;
  368. }
  369.  
  370. stock CheckItem(playerid, modelid)
  371. {
  372.     for(new i; i<MAX_INVENTORY_SLOTS; i++)
  373.     {
  374.         if(InvInfo[playerid][Slot][i] != modelid) return true;
  375.     }
  376.     return false;
  377. }
  378. stock GetDistancePlayerObject(playerid, objectid)
  379. {
  380.     new Float:Floats[7];
  381.  
  382.     GetPlayerPos(playerid, Floats[0], Floats[1], Floats[2]);
  383.     GetObjectPos(objectid, Floats[3], Floats[4], Floats[5]);
  384.     Floats[6] = floatsqroot((Floats[3]-Floats[0])*(Floats[3]-Floats[0])+(Floats[4]-Floats[1])*(Floats[4]-Floats[1])+(Floats[5]-Floats[2])*(Floats[5]-Floats[2]));
  385.  
  386.     return floatround(Floats[6]);
  387. }
Advertisement
Add Comment
Please, Sign In to add comment