Advertisement
Guest User

PInventory 1.1

a guest
Sep 2nd, 2011
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.43 KB | None | 0 0
  1. /*
  2. ================================================================================
  3.  
  4.                         =====[Phanto's Script]=====
  5.                         |        Inventory System |
  6.                         |            Version: 1.1 |
  7.                         |           Type: Include |
  8.                         ===========================
  9.  
  10. ================================================================================
  11.  
  12. */
  13. #if defined PInventory_Included
  14.   #endinput
  15. #endif
  16. #define PInventory_Included
  17. //==================================INCLUDES
  18. #include a_samp
  19. //==================================MAX
  20. #define MAX_ITEMS 100 //Handle max items that can be created
  21. //==================================DEBUGMODE
  22. new bool:debug_mode = false;
  23. //==================================RETURN VALUES
  24. enum
  25. {
  26. ERROR_INVALID_ITEM,
  27. SUCCESFULLY_ADDED_REMOVED,
  28. ERROR_FULL_INVENTORY,
  29. ERROR_NO_ITEM,
  30. ERROR_TOO_MANY_ITEMS,
  31. ERROR_NOT_ENOUGH_ITEMS
  32. };
  33. //==================================ITEMS
  34. enum info_items{pi_name[40], maximum};
  35. new item_list[MAX_ITEMS][info_items];
  36. new contatore;
  37. //==================================PLAYER'sINVENTORY
  38. enum inventory_items{id_inventory_item, p_amount};
  39. new player_inventory[MAX_PLAYERS][10][inventory_items];
  40. //==================================TEXTDRAWS
  41. new stock Text: pi_background, Text: pi_title, Text: pi_object_amount, Text: pi_object_list;
  42. //==================================SHOWINGINVENTORY
  43. new bool:showing_inventory[MAX_PLAYERS] = {false, ...};
  44. //================================================================INITIALISATION
  45. // Initialise PInventory
  46. //==============================================================================
  47. public OnGameModeInit()
  48. {
  49.     pi_background = TextDrawCreate(367.000000, 140.000000, "_");
  50.     TextDrawBackgroundColor(pi_background, 0);
  51.     TextDrawFont(pi_background, 1);
  52.     TextDrawLetterSize(pi_background, 0.500000, 24.299997);
  53.     TextDrawColor(pi_background, 0);
  54.     TextDrawSetOutline(pi_background, 0);
  55.     TextDrawSetProportional(pi_background, 1);
  56.     TextDrawSetShadow(pi_background, 1);
  57.     TextDrawUseBox(pi_background, 1);
  58.     TextDrawBoxColor(pi_background, 100);
  59.     TextDrawTextSize(pi_background, 628.000000, 0.000000);
  60.  
  61.     pi_title = TextDrawCreate(370.000000, 121.000000, "Inventory");
  62.     TextDrawBackgroundColor(pi_title, 255);
  63.     TextDrawFont(pi_title, 0);
  64.     TextDrawLetterSize(pi_title, 0.769999, 3.099998);
  65.     TextDrawColor(pi_title, -1);
  66.     TextDrawSetOutline(pi_title, 1);
  67.     TextDrawSetProportional(pi_title, 1);
  68.    
  69.     pi_object_list = TextDrawCreate(374.000000, 161.000000, "Empty~n~Empty~n~Empty~n~Empty~n~Empty~n~Empty~n~Empty~n~Empty~n~Empty~n~Empty~n~");
  70.     TextDrawBackgroundColor(pi_object_list, 255);
  71.     TextDrawFont(pi_object_list, 1);
  72.     TextDrawLetterSize(pi_object_list, 0.439999, 2.099999);
  73.     TextDrawColor(pi_object_list, -1);
  74.     TextDrawSetOutline(pi_object_list, 1);
  75.     TextDrawSetProportional(pi_object_list, 1);
  76.  
  77.     pi_object_amount = TextDrawCreate(606.000000, 161.000000, "~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~");
  78.     TextDrawAlignment(pi_object_amount, 3);
  79.     TextDrawBackgroundColor(pi_object_amount, 255);
  80.     TextDrawFont(pi_object_amount, 1);
  81.     TextDrawLetterSize(pi_object_amount, 0.439999, 2.099999);
  82.     TextDrawColor(pi_object_amount, -1);
  83.     TextDrawSetOutline(pi_object_amount, 1);
  84.     TextDrawSetProportional(pi_object_amount, 1);
  85.     for(new playerid; playerid<500; playerid++)
  86.     {
  87.         for(new i; i < 10; i++)
  88.         {
  89.             player_inventory[playerid][i][id_inventory_item] = -1;
  90.             player_inventory[playerid][i][p_amount] = -1;
  91.         }
  92.     }
  93.     //check_callback = funcidx("PInventory_OnPlayerDisconnect") != -1;
  94.     if (funcidx("PInventory_OnGameModeInit") != -1)
  95.     {
  96.         return CallLocalFunction("PInventory_OnGameModeInit", "");
  97.     }
  98.     return 1;
  99. }
  100. #if defined _ALS_OnGameModeInit
  101.   #undef OnGameModeInit
  102. #else
  103.   #define _ALS_OnGameModeInit
  104. #endif
  105. #define OnGameModeInit PInventory_OnGameModeInit
  106. forward PInventory_OnGameModeInit();
  107. //===============================================================ONPLAYERCONNECT
  108. // Reset inventory for connecting players
  109. //==============================================================================
  110. public OnPlayerConnect(playerid)
  111. {
  112.     Reset_Inventory(playerid);
  113.     Hide_Inventory(playerid);
  114.     if (funcidx("PInventory_OnPlayerConnect") != -1)
  115.     {
  116.         return CallLocalFunction("PInventory_OnPlayerConnect", "i", playerid);
  117.     }
  118.     return 1;
  119. }
  120. #if defined _ALS_OnPlayerConnect
  121.   #undef OnPlayerDisconnect
  122. #else
  123.   #define _ALS_OnPlayerConnect
  124. #endif
  125. #define OnPlayerDisconnect PInventory_OnPlayerConnect
  126.  
  127. forward PInventory_OnPlayerConnect(playerid);
  128. //====================================================================ITEMCREATE
  129. // Creates a new item
  130. //==============================================================================
  131. stock Create_Item(item_name[], max = 20)
  132. {
  133.     if(strlen(item_name)>40 || contatore == MAX_ITEMS) return ERROR_INVALID_ITEM;
  134.     format(item_list[contatore], 40, item_name);
  135.     item_list[contatore][maximum] = max;
  136.     if(debug_mode)
  137.         {
  138.         print(" |==========[Inventory]===========|");
  139.         printf("  Item Created: %d(ID)  ", contatore);
  140.         printf("  Item Name: %s", item_name);
  141.         printf("  Maximum Amount: %d", max);
  142.         print(" |================================|");
  143.         }
  144.     contatore++;
  145.     return contatore-1;
  146. }
  147. //==================================================================GETMAXAMOUNT
  148. // Get the Maximum amount of items that can be stored in the inventory for the
  149. // specified id.
  150. //==============================================================================
  151. stock Get_Max_Amount(item_id)
  152. {
  153.     if(item_id < 0 || item_id >= contatore) return ERROR_INVALID_ITEM;
  154.     return item_list[item_id][maximum];
  155. }
  156. //==================================================================SETMAXAMOUNT
  157. // Set the maximum amount of items of the same type that can be stored in the
  158. // inventory.
  159. //==============================================================================
  160. stock Set_Max_Amount(item_id, max)
  161. {
  162.     if(item_id < 0 || item_id >= contatore || !max) return ERROR_INVALID_ITEM;
  163.     new temp_max = item_list[item_id][maximum];
  164.     item_list[item_id][maximum] = max;
  165.     if(debug_mode)
  166.         {
  167.         print(" |==========[Inventory]===========|");
  168.         printf("  Object inventory: %d(ID)  ", item_id);
  169.         printf("  Maximum changed from %d to %d", temp_max, max);
  170.         print(" |================================|");
  171.         }
  172.     return 1;
  173. }
  174. //===============================================================AGGIUNGIOGGETTO
  175. // Add an item to player's inventory
  176. //==============================================================================
  177. stock Add_Item(playerid, item_id, amount = 1)
  178. {
  179.     if(item_id < 0 || item_id >= contatore || amount <= 0) return ERROR_INVALID_ITEM;
  180.     for(new i; i<10; i++) //Checks if there is an object of the same id and store there objects
  181.     {
  182.         if(player_inventory[playerid][i][id_inventory_item] == item_id)
  183.         {
  184.             if(player_inventory[playerid][i][p_amount] + amount > item_list[item_id][maximum])
  185.                 {
  186.                 return ERROR_TOO_MANY_ITEMS;
  187.                 }
  188.             player_inventory[playerid][i][id_inventory_item] = item_id;
  189.             player_inventory[playerid][i][p_amount] += amount;
  190.             Refresh_Inventory(playerid);
  191.             if(debug_mode)
  192.                 {
  193.                 new piname[24];
  194.                 GetPlayerName(playerid, piname, 24);
  195.                 print(" |==========[Inventory]===========|");
  196.                 printf("  Item Added: %d(ID) ", item_id);
  197.                 printf("  Player: %s(%d)", piname, playerid);
  198.                 printf("  Amount: %d ", amount);
  199.                 print(" |================================|");
  200.                 }
  201.             return SUCCESFULLY_ADDED_REMOVED;
  202.         }
  203.     }
  204.     for(new i; i<10; i++) //Add object if the inventory isn't full
  205.     {
  206.         if(player_inventory[playerid][i][id_inventory_item] == -1)
  207.             {
  208.             player_inventory[playerid][i][id_inventory_item] = item_id;
  209.             player_inventory[playerid][i][p_amount] = amount;
  210.             Refresh_Inventory(playerid);
  211.             if(debug_mode)
  212.                 {
  213.                 new piname[24];
  214.                 GetPlayerName(playerid, piname, 24);
  215.                 print(" |==========[Inventory]===========|");
  216.                 printf("  Item Added ID: %d ", item_id);
  217.                 printf("  Player: %s(%d)", piname, playerid);
  218.                 printf("  Amount: %d ", amount);
  219.                 print(" |================================|");
  220.                 }
  221.             return SUCCESFULLY_ADDED_REMOVED;
  222.             }
  223.         if(i == 9)
  224.             {
  225.             return ERROR_FULL_INVENTORY;
  226.             }
  227.     }
  228.     return 1;
  229. }
  230.  
  231. //====================================================================REMOVEITEM
  232. // Remove an item from player's inventory
  233. //==============================================================================
  234. stock Remove_Item(playerid, item_id, amount = 1)
  235. {
  236.     if(item_id < 0 || item_id >= contatore || amount <= 0) return ERROR_INVALID_ITEM;
  237.     for(new i; i<10; i++)
  238.     {
  239.         if(player_inventory[playerid][i][id_inventory_item] == item_id)
  240.             {
  241.             if(player_inventory[playerid][i][p_amount] - amount < 0)
  242.                 {
  243.                 return ERROR_NOT_ENOUGH_ITEMS;
  244.                 }
  245.             player_inventory[playerid][i][p_amount] -= amount;
  246.             if(!player_inventory[playerid][i][p_amount]) //Remove item if p_amount is less or equal to zero
  247.                 {
  248.                 player_inventory[playerid][i][id_inventory_item] = -1;
  249.                 }
  250.             Refresh_Inventory(playerid);
  251.             if(debug_mode)
  252.                 {
  253.                 new piname[24];
  254.                 GetPlayerName(playerid, piname, 24);
  255.                 print(" |==========[Inventory]===========|");
  256.                 printf("  Item Removed: %d(ID) ", item_id);
  257.                 printf("  Player: %s(%d)", piname, playerid);
  258.                 printf("  Amount: %d ", amount);
  259.                 print(" |================================|");
  260.                 }
  261.             return SUCCESFULLY_ADDED_REMOVED;
  262.             }
  263.         if(i == 9)
  264.             {
  265.             return ERROR_NO_ITEM;
  266.             }
  267.     }
  268.     return 1;
  269. }
  270. //===================================================================CHECKAMOUNT
  271. // Returns the amount of items for the object id specified in player's inventory
  272. //==============================================================================
  273. stock Check_Amount(playerid, item_id)
  274. {
  275.     if(item_id > 0 || item_id >= contatore) return ERROR_INVALID_ITEM;
  276.     for(new i; i < 10; i++)
  277.     {
  278.         if(player_inventory[playerid][i][id_inventory_item] == item_id)
  279.             return player_inventory[playerid][i][p_amount];
  280.     }
  281.     return ERROR_INVALID_ITEM;
  282. }
  283. //==============================================================REFRESHINVENTORY
  284. // Refreshes player's inventory list
  285. //==============================================================================
  286. stock Refresh_Inventory(playerid)
  287. {
  288.     new item_name_string[460], item_amount_string[256], string_temp[46], item_amount;
  289.     for(new i; i < 10; i++)
  290.         {
  291.         if(player_inventory[playerid][i][p_amount] <= 0)
  292.             {
  293.             player_inventory[playerid][i][id_inventory_item] = -1;
  294.             player_inventory[playerid][i][p_amount] = 0;
  295.             }
  296.         if(player_inventory[playerid][i][id_inventory_item] == -1)
  297.             {
  298.             continue;
  299.             }
  300.         else
  301.             {
  302.             format(string_temp, 46, "%s~w~~n~", item_list[player_inventory[playerid][i][id_inventory_item]][pi_name]);
  303.             strins(item_name_string, string_temp, strlen(item_name_string), 460);
  304.             if(player_inventory[playerid][i][p_amount] > 1)
  305.             {
  306.                 format(string_temp, 43, "%d~n~", player_inventory[playerid][i][p_amount]);
  307.                 strins(item_amount_string, string_temp, strlen(item_amount_string), 256);
  308.             }
  309.             else
  310.             {
  311.                 strins(item_amount_string, "~n~", strlen(item_amount_string), 256);
  312.             }
  313.             item_amount++;
  314.             continue;
  315.             }
  316.         }
  317.     if(!item_amount)
  318.         {
  319.         format(item_name_string, 400, "No items in inventory");
  320.         TextDrawLetterSize(pi_background, 0.500000, 5.0);
  321.         }
  322.     else
  323.         {
  324.         TextDrawLetterSize(pi_background, 0.500000, 2.8 + 2.2*item_amount);
  325.         }
  326.     TextDrawSetString(pi_object_list, item_name_string);
  327.     TextDrawSetString(pi_object_amount, item_amount_string);
  328.     if(showing_inventory[playerid])
  329.         {
  330.         Show_Inventory(playerid);
  331.         }
  332.     return 1;
  333. }
  334. //=================================================================SHOWINVENTORY
  335. // Shows inventory for a player.
  336. //==============================================================================
  337. stock Show_Inventory(playerid)
  338. {
  339.     if(!showing_inventory[playerid])
  340.         {
  341.         Refresh_Inventory(playerid);
  342.         }
  343.     TextDrawShowForPlayer(playerid, pi_background);
  344.     TextDrawShowForPlayer(playerid, pi_title);
  345.     TextDrawShowForPlayer(playerid, pi_object_list);
  346.     TextDrawShowForPlayer(playerid, pi_object_amount);
  347.     showing_inventory[playerid] = true;
  348.     return 1;
  349. }
  350. //=================================================================HIDEINVENTORY
  351. // Hide inventory for a player.
  352. //==============================================================================
  353. stock Hide_Inventory(playerid)
  354. {
  355.     TextDrawHideForPlayer(playerid, pi_background);
  356.     TextDrawHideForPlayer(playerid, pi_title);
  357.     TextDrawHideForPlayer(playerid, pi_object_list);
  358.     TextDrawHideForPlayer(playerid, pi_object_amount);
  359.     showing_inventory[playerid] = false;
  360.     return 1;
  361. }
  362. //================================================================RESETINVENTORY
  363. // Reset player's inventory
  364. //==============================================================================
  365. stock Reset_Inventory(playerid)
  366. {
  367.     for(new i; i < 10; i++)
  368.     {
  369.         player_inventory[playerid][i][id_inventory_item] = -1;
  370.         player_inventory[playerid][i][p_amount] = 0;
  371.     }
  372.     Refresh_Inventory(playerid);
  373.     return 1;
  374. }
  375. //=====================================================================DEBUGMODE
  376. // Handles debug mode of PInventory
  377. //==============================================================================
  378. stock PInventory_DebugMode(bool:mode)
  379. {
  380.     return debug_mode = mode;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement