Advertisement
Guest User

INVENTORYANDITEM

a guest
Apr 29th, 2014
1,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.64 KB | None | 0 0
  1. /*$$$$$$$$ $$$$$$$ $$$$$$$$$ $$$$$ IIIIIIIIIIIII|IIIIIIIIIIIII|IIIIIIIII|
  2. $$$ $$$$$$$$ $$$ $$$$ $$$$ |IIII| |IIIIIIIIIIIII|IIII|
  3. $$$ $$$$ $$$ $$$ $$$$ $$$$ |IIII| IIII IIIIIIIII|
  4. $$$ $$$$ $$$ $$$ $$$$ $$$$ & |IIII| IIII IIIIIIIII|
  5. $$$ $$$$ $$$$$$ $$$ $$$ IIIIIIIIIIIIII| IIII IIII|
  6. $$$$$$$$$$$$$$ $$$$$$ $$$$$ entory |IIIIIIIIIIIII| IIII IIIIIIIII|M
  7.  
  8. Iventory And Item SYSTEM
  9. Inventory by Joe Staff
  10. Item by Vinpure
  11. */
  12. #include <a_samp>
  13. #include <streamer>
  14. #include <zcmd>
  15. #include <sscanf2>
  16. //====================DEFINE=======================================================================================================================================================================
  17. #define MAX_DROP_ITEMS 1000
  18. #define MAX_ITEMS 20
  19. #define MAX_ITEM_STACK 99
  20. #define MAX_ITEM_NAME 24
  21.  
  22. #define PRESSED(%0) \
  23. (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  24. //==================DIALOG
  25. #define INV_DIALOG_ID 13337
  26. #define DIALOG_CITEM 13333
  27. #define DIALOG_DROPITEM 13332
  28. //ITEM MODEL
  29. #define IPizza 1582
  30. #define IWater 1950
  31. //===========================================================================================================================================================================================
  32. new DropObject[MAX_DROP_ITEMS];
  33. new Text3D:Label[MAX_DROP_ITEMS];
  34. //==============ITEM INFO============================================================================================================================================================
  35. enum ItemData
  36. {
  37. ItemModel,//ModelID das
  38. Float:ItemPosX,
  39. Float:ItemPosY,
  40. Float:ItemPosZ,
  41. Float:ItemRotX,
  42. Float:ItemRotY,
  43. Float:ItemRotZ,
  44. ItemVWorld,
  45. ItemInterior,
  46. };
  47. new ItemInfo[MAX_DROP_ITEMS][ItemData];
  48. //========Forward===================================================================================================================================================================================
  49. forward OnPlayerPickupItem(playerid);
  50. forward OnPlayerUseItem(playerid,ItemName[]);
  51. //============NEW====================================================================================================================================================================================
  52. new gItemList[(MAX_ITEMS+1)*(MAX_ITEM_NAME+3)];
  53.  
  54. //===========================================================================================================================================================================================
  55. public OnFilterScriptInit()
  56. {
  57. print("---------Inventory And Item------------\n");
  58. print("*Inventory System By:Joe Staff---------\n");
  59. print("*Item System By:Vinpure----------------\n");
  60. print("---------------------------------------\n");
  61. return 1;
  62. }
  63. public OnFilterScriptExit()
  64. {
  65. return 1;
  66. }
  67. public OnPlayerConnect(playerid)
  68. {
  69. LoadInventory(playerid);
  70. return 1;
  71. }
  72. public OnPlayerDisconnect(playerid,reason)
  73. {
  74. SaveInventory(playerid);
  75. return 1;
  76. }
  77. //==================CMD
  78. CMD:createitem(playerid,params[])
  79. {
  80. ShowPlayerDialog(playerid,DIALOG_CITEM,1,"Create Item","Item Name:pizza|waterbottle","Create","Close");
  81. return 1;
  82. }
  83. CMD:pickupitem(playerid,params[])
  84. {
  85. OnPlayerPickupItem(playerid);
  86. return 1;
  87. }
  88. CMD:dropitem(playerid,params[])
  89. {
  90. ShowPlayerDialog(playerid,DIALOG_DROPITEM,1,"Use Item","Write Name Item you has","Use","Cancel");
  91. return 1;
  92. }
  93. CMD:useitem(playerid,params[])
  94. {
  95. UseItemInInventory(playerid);
  96. return 1;
  97. }
  98. //===========================================================================================================================================================================================
  99. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  100. {
  101. new Float:x,Float:y,Float:z;
  102. GetPlayerPos(playerid,x,y,z);
  103. InventoryOnDialogResponse(playerid, dialogid, response, inputtext);
  104. if(dialogid == DIALOG_CITEM && response == 1)
  105. {
  106. if(strcmp(inputtext[0],"pizza",true) == 0)
  107. {
  108. CreateItem(IPizza,x,y,z-1,0,0,0,0,0);
  109. }
  110. else if(strcmp(inputtext[0],"WATERBOTTLE",true) == 0)
  111. {
  112. CreateItem(IWater,x,y,z-1,0,0,0,0,0);
  113. }
  114. }
  115. //=============USE ITEM=|
  116. if(dialogid == DIALOG_DROPITEM && response == 1)
  117. {
  118. if(strcmp(inputtext[0],"pizza",true) == 0)
  119. {
  120. if(!PlayerHasItem(playerid,"Pizza")) return SendClientMessage(playerid,-1,"You no have Item Pizza");
  121. RemoveItem(playerid,"Pizza",1);
  122. DropItem(IPizza,x,y,z,0,0);
  123. }
  124. else if(strcmp(inputtext[0],"waterbottle",true) == 0)
  125. {
  126. if(!PlayerHasItem(playerid,"WaterBottle")) return SendClientMessage(playerid,-1,"You No have Item Water bottle");
  127. RemoveItem(playerid,"Waterbottle",1);
  128. DropItem(IWater,x,y,z,0,0);
  129. }
  130. }
  131. return 1;
  132. }
  133. public OnPlayerPickupItem(playerid)
  134. {
  135. for(new i = 0; i < sizeof(ItemInfo); i++)
  136. {
  137. if (IsPlayerInRangeOfPoint(playerid, 2.0,ItemInfo[i][ItemPosX],ItemInfo[i][ItemPosY],ItemInfo[i][ItemPosZ]))
  138. {
  139. if(GetPlayerVirtualWorld(playerid) == ItemInfo[i][ItemVWorld] && GetPlayerInterior(playerid) == ItemInfo[i][ItemVWorld])
  140. {
  141. ItemInfo[i][ItemPosX] = 0.0;
  142. ItemInfo[i][ItemPosY] = 0.0;
  143. ItemInfo[i][ItemPosZ] = 0.0;
  144. ItemInfo[i][ItemRotX] = 0.0;
  145. ItemInfo[i][ItemRotY] = 0.0;
  146. ItemInfo[i][ItemRotZ] = 0.0;
  147. PlayAnim(playerid, "BOMBER", "BOM_Plant", 4.0, 0, 0, 0, 0, 0, 1);
  148. if(ItemInfo[i][ItemModel] == IPizza)
  149. {
  150. Delete3DTextLabel(Label[i]);
  151. DestroyDynamicObject(DropObject[i]);
  152. AddItem(playerid,"Pizza",1);
  153. }
  154. if(ItemInfo[i][ItemModel] == IWater)
  155. {
  156. Delete3DTextLabel(Label[i]);
  157. DestroyDynamicObject(DropObject[i]);
  158. AddItem(playerid,"WaterBottle",1);
  159. }
  160. return 1;
  161. }
  162. }
  163. }
  164. return 1;
  165. }
  166. public OnPlayerUseItem(playerid,ItemName[])
  167. {
  168. if(!strcmp(ItemName,"Pizza",true))
  169. {
  170. RemoveItem(playerid,"Pizza",1);
  171. PlayAnim(playerid, "FOOD", "EAT_Burger", 4.0, 0, 0, 0, 0, 0, 1);
  172. return 1;
  173. }
  174. if(!strcmp(ItemName,"WaterBottle",true))
  175. {
  176. RemoveItem(playerid,"WaterBottle",1);
  177. PlayAnim(playerid, "BAR", "dnk_stndF_loop",4.0, 0, 0, 0, 0, 0, 1);
  178. return 1;
  179. }
  180. return 1;
  181. }
  182. //===========================================================================================================================================================================================
  183. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  184. {
  185. return 1;
  186. }
  187. //==========================================STOCK=======================================================================================================================================================================================================================================================================================
  188. //anim
  189. PlayAnim(playerid, animlib[], animname[], Float:fDelta, loop, lockx, locky, freeze, time, forcesync)
  190. {
  191. ApplyAnimation(playerid, animlib, animname, fDelta, loop, lockx, locky, freeze, time, forcesync);
  192. }
  193. //===================ITEM===================
  194. stock CreateItem(ItemM, Float:X, Float:Y, Float:Z,Float:RX,Float:RY,Float:RZ, world,interior)
  195. {
  196. if(ItemM != 0)
  197. {
  198. for(new i = 0; i < sizeof(ItemInfo); i++)
  199. {
  200. if(ItemInfo[i][ItemPosX] == 0.0 && ItemInfo[i][ItemPosY] == 0.0 && ItemInfo[i][ItemPosZ] == 0.0)
  201. {
  202. ItemInfo[i][ItemModel] = ItemM;
  203. ItemInfo[i][ItemPosX] = X;
  204. ItemInfo[i][ItemPosY] = Y;
  205. ItemInfo[i][ItemPosZ] = Z;
  206. ItemInfo[i][ItemRotX] = RX;
  207. ItemInfo[i][ItemRotY] = RY;
  208. ItemInfo[i][ItemRotZ] = RZ;
  209. ItemInfo[i][ItemVWorld] = world;
  210. ItemInfo[i][ItemInterior] = interior;
  211.  
  212. Label[i] = Create3DTextLabel("Item", 0x008080FF,X,Y,Z, 5.0,world, 0);
  213. DropObject[i] = CreateDynamicObject(ItemM, X, Y, Z,RX,RY,RZ, world);
  214.  
  215. return 1;
  216. }
  217. }
  218. return 1;
  219. }
  220. return 1;
  221. }
  222. stock DropItem(ItemM, Float:X, Float:Y, Float:Z, world, interior)
  223. {
  224.  
  225. if(ItemM != 0)
  226. {
  227. for(new i = 0; i < sizeof(ItemInfo); i++)
  228. {
  229. if(ItemInfo[i][ItemPosX] == 0.0 && ItemInfo[i][ItemPosY] == 0.0 && ItemInfo[i][ItemPosZ] == 0.0)
  230. {
  231. ItemInfo[i][ItemModel] = ItemM;
  232. ItemInfo[i][ItemPosX] = X;
  233. ItemInfo[i][ItemPosY] = Y;
  234. ItemInfo[i][ItemPosZ] = Z;
  235. ItemInfo[i][ItemVWorld] = world;
  236. ItemInfo[i][ItemInterior] = interior;
  237. Label[i] = Create3DTextLabel("Item", 0x008080FF,X,Y,Z, 5.0,world, 0);
  238. DropObject[i] = CreateDynamicObject(ItemM, X, Y, Z-1, 0.0, 0.0, 0.0, world);
  239. return 1;
  240. }
  241. }
  242. return 1;
  243. }
  244. return 1;
  245. }
  246. //===================INVENTORY===================
  247. stock _GetItemNamePVar(playerid,item)
  248. {
  249. new tmp[32];
  250. new tmp2[MAX_ITEM_NAME];
  251. format(tmp,32,"PITEMNAME%d",item);
  252. GetPVarString(playerid,tmp,tmp2,MAX_ITEM_NAME);
  253. return tmp2;
  254. }
  255. stock _SetItemNamePVar(playerid,item,ItemName[])
  256. {
  257. new tmp[MAX_ITEM_NAME];
  258. format(tmp,MAX_ITEM_NAME,"PITEMNAME%d",item);
  259. SetPVarString(playerid,tmp,ItemName);
  260. }
  261. stock _GetItemAmountPVar(playerid,item)
  262. {
  263. new tmp[16];
  264. format(tmp,16,"PITEMAMOUNT%d",item);
  265. return GetPVarInt(playerid,tmp);
  266. }
  267. stock _SetItemAmountPVar(playerid,item,Amount)
  268. {
  269. new tmp[16];
  270. format(tmp,16,"PITEMAMOUNT%d",item);
  271. SetPVarInt(playerid,tmp,Amount);
  272. }
  273. stock AddItem(playerid,ItemName[],Amount)
  274. {
  275. new slot=-1;
  276. for(new item;item<MAX_ITEMS;item++)
  277. {
  278. if(!_GetItemAmountPVar(playerid,item))
  279. {
  280. if(slot==-1)slot=item;
  281. continue;
  282. }
  283. if(!strcmp(_GetItemNamePVar(playerid,item),ItemName,true))
  284. {
  285. _SetItemAmountPVar(playerid,item,_GetItemAmountPVar(playerid,item)+Amount);
  286. if(_GetItemAmountPVar(playerid,item)<=0)_SetItemAmountPVar(playerid,item,0);
  287. if(_GetItemAmountPVar(playerid,item)>MAX_ITEM_STACK)
  288. {
  289. _SetItemAmountPVar(playerid,item,MAX_ITEM_STACK);
  290. return 2;
  291. }
  292. return 1;
  293. }
  294. }
  295. if(slot>-1)
  296. {
  297. _SetItemNamePVar(playerid,slot,ItemName);
  298. _SetItemAmountPVar(playerid,slot,Amount);
  299. if(_GetItemAmountPVar(playerid,slot)>MAX_ITEM_STACK)
  300. {
  301. _SetItemAmountPVar(playerid,slot,MAX_ITEM_STACK);
  302. return 2;
  303. }
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. stock RemoveItem(playerid,ItemName[],Amount)
  309. {
  310. for(new item;item<MAX_ITEMS;item++)
  311. {
  312. if(!_GetItemAmountPVar(playerid,item))continue;
  313. if(!strcmp(_GetItemNamePVar(playerid,item),ItemName,true))
  314. {
  315. _SetItemAmountPVar(playerid,item,_GetItemAmountPVar(playerid,item)-Amount);
  316. if(_GetItemAmountPVar(playerid,item)<=0)_SetItemAmountPVar(playerid,item,0);
  317. if(_GetItemAmountPVar(playerid,item)>MAX_ITEM_STACK)
  318. {
  319. _SetItemAmountPVar(playerid,item,MAX_ITEM_STACK);
  320. return 2;
  321. }
  322. return 1;
  323. }
  324. }
  325. return 0;
  326. }
  327. stock PlayerHasItem(playerid,ItemName[])
  328. {
  329. for(new item;item<MAX_ITEMS;item++)
  330. {
  331. if(!_GetItemAmountPVar(playerid,item))continue;
  332. if(!strcmp(_GetItemNamePVar(playerid,item),ItemName,false))return _GetItemAmountPVar(playerid,item);
  333. }
  334. return 0;
  335. }
  336. stock GetPlayerItemInfo(playerid,&idx,ItemName[],len=sizeof(ItemName),&Amount)
  337. {
  338. if(idx>=MAX_ITEMS)return 0;
  339. format(ItemName,len,_GetItemNamePVar(playerid,idx));
  340. Amount=_GetItemAmountPVar(playerid,idx);
  341. idx++;
  342. return 1;
  343. }
  344. stock ResetPlayerInventory(playerid)
  345. {
  346. for(new item;item<MAX_ITEMS;item++)_SetItemAmountPVar(playerid,item,0);
  347. }
  348. stock UseItemInInventory(playerid)
  349. {
  350. gItemList="";
  351. for(new item;item<MAX_ITEMS;item++)
  352. {
  353. if(!strlen(_GetItemNamePVar(playerid,item))||!_GetItemAmountPVar(playerid,item))continue;
  354. format(gItemList,sizeof(gItemList),"%s\n%d\t\t%s",gItemList,_GetItemAmountPVar(playerid,item),_GetItemNamePVar(playerid,item));
  355. }
  356. ShowPlayerDialog(playerid,INV_DIALOG_ID,DIALOG_STYLE_LIST,"Inventory",gItemList,"Use","Close");
  357. SetPVarInt(playerid,"PUSINGDIALOG",1);
  358. }
  359. stock SaveInventory(playerid)
  360. {
  361. gItemList="";
  362. new filename[48];
  363. GetPlayerName(playerid,filename,24);
  364. format(filename,48,"Inv/%s.inv",filename);
  365. new File:file=fopen(filename,io_write);
  366. for(new item;item<MAX_ITEMS;item++)
  367. {
  368. if(!strlen(_GetItemNamePVar(playerid,item))||!_GetItemAmountPVar(playerid,item))continue;
  369. format(gItemList,sizeof(gItemList),"%s%s\n%d\n",gItemList,_GetItemNamePVar(playerid,item),_GetItemAmountPVar(playerid,item));
  370. }
  371. fwrite(file,gItemList);
  372. fclose(file);
  373. GetPlayerName(playerid,filename,24);
  374. printf("[INV] %s[%d]'s inventory saved.",filename,playerid);
  375. }
  376. stock LoadInventory(playerid)
  377. {
  378. new tstring[48];
  379. new tstring2[12];
  380. GetPlayerName(playerid,tstring,48);
  381. format(tstring,48,"Inv/%s.inv",tstring);
  382. if(!fexist(tstring))return 0;
  383. new File:file=fopen(tstring,io_read);
  384. fread(file,tstring);
  385. while(tstring[0])
  386. {
  387. format(tstring,strlen(tstring),"%s",tstring);
  388. fread(file,tstring2);
  389. AddItem(playerid,tstring,strval(tstring2));
  390. fread(file,tstring);
  391. }
  392. fclose(file);
  393. GetPlayerName(playerid,tstring,24);
  394. printf("[INV] %s[%d]'s inventory loaded.",tstring,playerid);
  395. return 1;
  396. }
  397. InventoryOnDialogResponse(playerid, dialogid, response, inputtext[])
  398. {
  399. if(dialogid!=INV_DIALOG_ID)return 1;
  400. if(!GetPVarInt(playerid,"PUSINGDIALOG"))return 1;
  401. if(!response)return 1;
  402. format(gItemList,MAX_ITEM_NAME,inputtext[strfind(inputtext,"\t")+2]);
  403. if(CallLocalFunction("OnPlayerUseItem","is",playerid,gItemList))UseItemInInventory(playerid);
  404. else SetPVarInt(playerid,"PUSINGDIALOG",0);
  405. return 1;
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement