Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ================================================================================
- =====[Phanto's Script]=====
- | Inventory System |
- | Version: 1.1 |
- | Type: Include |
- ===========================
- ================================================================================
- */
- #if defined PInventory_Included
- #endinput
- #endif
- #define PInventory_Included
- //==================================INCLUDES
- #include a_samp
- //==================================MAX
- #define MAX_ITEMS 100 //Handle max items that can be created
- //==================================DEBUGMODE
- new bool:debug_mode = false;
- //==================================RETURN VALUES
- enum
- {
- ERROR_INVALID_ITEM,
- SUCCESFULLY_ADDED_REMOVED,
- ERROR_FULL_INVENTORY,
- ERROR_NO_ITEM,
- ERROR_TOO_MANY_ITEMS,
- ERROR_NOT_ENOUGH_ITEMS
- };
- //==================================ITEMS
- enum info_items{pi_name[40], maximum};
- new item_list[MAX_ITEMS][info_items];
- new contatore;
- //==================================PLAYER'sINVENTORY
- enum inventory_items{id_inventory_item, p_amount};
- new player_inventory[MAX_PLAYERS][10][inventory_items];
- //==================================TEXTDRAWS
- new stock Text: pi_background, Text: pi_title, Text: pi_object_amount, Text: pi_object_list;
- //==================================SHOWINGINVENTORY
- new bool:showing_inventory[MAX_PLAYERS] = {false, ...};
- //================================================================INITIALISATION
- // Initialise PInventory
- //==============================================================================
- public OnGameModeInit()
- {
- pi_background = TextDrawCreate(367.000000, 140.000000, "_");
- TextDrawBackgroundColor(pi_background, 0);
- TextDrawFont(pi_background, 1);
- TextDrawLetterSize(pi_background, 0.500000, 24.299997);
- TextDrawColor(pi_background, 0);
- TextDrawSetOutline(pi_background, 0);
- TextDrawSetProportional(pi_background, 1);
- TextDrawSetShadow(pi_background, 1);
- TextDrawUseBox(pi_background, 1);
- TextDrawBoxColor(pi_background, 100);
- TextDrawTextSize(pi_background, 628.000000, 0.000000);
- pi_title = TextDrawCreate(370.000000, 121.000000, "Inventory");
- TextDrawBackgroundColor(pi_title, 255);
- TextDrawFont(pi_title, 0);
- TextDrawLetterSize(pi_title, 0.769999, 3.099998);
- TextDrawColor(pi_title, -1);
- TextDrawSetOutline(pi_title, 1);
- TextDrawSetProportional(pi_title, 1);
- 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~");
- TextDrawBackgroundColor(pi_object_list, 255);
- TextDrawFont(pi_object_list, 1);
- TextDrawLetterSize(pi_object_list, 0.439999, 2.099999);
- TextDrawColor(pi_object_list, -1);
- TextDrawSetOutline(pi_object_list, 1);
- TextDrawSetProportional(pi_object_list, 1);
- pi_object_amount = TextDrawCreate(606.000000, 161.000000, "~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~");
- TextDrawAlignment(pi_object_amount, 3);
- TextDrawBackgroundColor(pi_object_amount, 255);
- TextDrawFont(pi_object_amount, 1);
- TextDrawLetterSize(pi_object_amount, 0.439999, 2.099999);
- TextDrawColor(pi_object_amount, -1);
- TextDrawSetOutline(pi_object_amount, 1);
- TextDrawSetProportional(pi_object_amount, 1);
- for(new playerid; playerid<500; playerid++)
- {
- for(new i; i < 10; i++)
- {
- player_inventory[playerid][i][id_inventory_item] = -1;
- player_inventory[playerid][i][p_amount] = -1;
- }
- }
- //check_callback = funcidx("PInventory_OnPlayerDisconnect") != -1;
- if (funcidx("PInventory_OnGameModeInit") != -1)
- {
- return CallLocalFunction("PInventory_OnGameModeInit", "");
- }
- return 1;
- }
- #if defined _ALS_OnGameModeInit
- #undef OnGameModeInit
- #else
- #define _ALS_OnGameModeInit
- #endif
- #define OnGameModeInit PInventory_OnGameModeInit
- forward PInventory_OnGameModeInit();
- //===============================================================ONPLAYERCONNECT
- // Reset inventory for connecting players
- //==============================================================================
- public OnPlayerConnect(playerid)
- {
- Reset_Inventory(playerid);
- Hide_Inventory(playerid);
- if (funcidx("PInventory_OnPlayerConnect") != -1)
- {
- return CallLocalFunction("PInventory_OnPlayerConnect", "i", playerid);
- }
- return 1;
- }
- #if defined _ALS_OnPlayerConnect
- #undef OnPlayerDisconnect
- #else
- #define _ALS_OnPlayerConnect
- #endif
- #define OnPlayerDisconnect PInventory_OnPlayerConnect
- forward PInventory_OnPlayerConnect(playerid);
- //====================================================================ITEMCREATE
- // Creates a new item
- //==============================================================================
- stock Create_Item(item_name[], max = 20)
- {
- if(strlen(item_name)>40 || contatore == MAX_ITEMS) return ERROR_INVALID_ITEM;
- format(item_list[contatore], 40, item_name);
- item_list[contatore][maximum] = max;
- if(debug_mode)
- {
- print(" |==========[Inventory]===========|");
- printf(" Item Created: %d(ID) ", contatore);
- printf(" Item Name: %s", item_name);
- printf(" Maximum Amount: %d", max);
- print(" |================================|");
- }
- contatore++;
- return contatore-1;
- }
- //==================================================================GETMAXAMOUNT
- // Get the Maximum amount of items that can be stored in the inventory for the
- // specified id.
- //==============================================================================
- stock Get_Max_Amount(item_id)
- {
- if(item_id < 0 || item_id >= contatore) return ERROR_INVALID_ITEM;
- return item_list[item_id][maximum];
- }
- //==================================================================SETMAXAMOUNT
- // Set the maximum amount of items of the same type that can be stored in the
- // inventory.
- //==============================================================================
- stock Set_Max_Amount(item_id, max)
- {
- if(item_id < 0 || item_id >= contatore || !max) return ERROR_INVALID_ITEM;
- new temp_max = item_list[item_id][maximum];
- item_list[item_id][maximum] = max;
- if(debug_mode)
- {
- print(" |==========[Inventory]===========|");
- printf(" Object inventory: %d(ID) ", item_id);
- printf(" Maximum changed from %d to %d", temp_max, max);
- print(" |================================|");
- }
- return 1;
- }
- //===============================================================AGGIUNGIOGGETTO
- // Add an item to player's inventory
- //==============================================================================
- stock Add_Item(playerid, item_id, amount = 1)
- {
- if(item_id < 0 || item_id >= contatore || amount <= 0) return ERROR_INVALID_ITEM;
- for(new i; i<10; i++) //Checks if there is an object of the same id and store there objects
- {
- if(player_inventory[playerid][i][id_inventory_item] == item_id)
- {
- if(player_inventory[playerid][i][p_amount] + amount > item_list[item_id][maximum])
- {
- return ERROR_TOO_MANY_ITEMS;
- }
- player_inventory[playerid][i][id_inventory_item] = item_id;
- player_inventory[playerid][i][p_amount] += amount;
- Refresh_Inventory(playerid);
- if(debug_mode)
- {
- new piname[24];
- GetPlayerName(playerid, piname, 24);
- print(" |==========[Inventory]===========|");
- printf(" Item Added: %d(ID) ", item_id);
- printf(" Player: %s(%d)", piname, playerid);
- printf(" Amount: %d ", amount);
- print(" |================================|");
- }
- return SUCCESFULLY_ADDED_REMOVED;
- }
- }
- for(new i; i<10; i++) //Add object if the inventory isn't full
- {
- if(player_inventory[playerid][i][id_inventory_item] == -1)
- {
- player_inventory[playerid][i][id_inventory_item] = item_id;
- player_inventory[playerid][i][p_amount] = amount;
- Refresh_Inventory(playerid);
- if(debug_mode)
- {
- new piname[24];
- GetPlayerName(playerid, piname, 24);
- print(" |==========[Inventory]===========|");
- printf(" Item Added ID: %d ", item_id);
- printf(" Player: %s(%d)", piname, playerid);
- printf(" Amount: %d ", amount);
- print(" |================================|");
- }
- return SUCCESFULLY_ADDED_REMOVED;
- }
- if(i == 9)
- {
- return ERROR_FULL_INVENTORY;
- }
- }
- return 1;
- }
- //====================================================================REMOVEITEM
- // Remove an item from player's inventory
- //==============================================================================
- stock Remove_Item(playerid, item_id, amount = 1)
- {
- if(item_id < 0 || item_id >= contatore || amount <= 0) return ERROR_INVALID_ITEM;
- for(new i; i<10; i++)
- {
- if(player_inventory[playerid][i][id_inventory_item] == item_id)
- {
- if(player_inventory[playerid][i][p_amount] - amount < 0)
- {
- return ERROR_NOT_ENOUGH_ITEMS;
- }
- player_inventory[playerid][i][p_amount] -= amount;
- if(!player_inventory[playerid][i][p_amount]) //Remove item if p_amount is less or equal to zero
- {
- player_inventory[playerid][i][id_inventory_item] = -1;
- }
- Refresh_Inventory(playerid);
- if(debug_mode)
- {
- new piname[24];
- GetPlayerName(playerid, piname, 24);
- print(" |==========[Inventory]===========|");
- printf(" Item Removed: %d(ID) ", item_id);
- printf(" Player: %s(%d)", piname, playerid);
- printf(" Amount: %d ", amount);
- print(" |================================|");
- }
- return SUCCESFULLY_ADDED_REMOVED;
- }
- if(i == 9)
- {
- return ERROR_NO_ITEM;
- }
- }
- return 1;
- }
- //===================================================================CHECKAMOUNT
- // Returns the amount of items for the object id specified in player's inventory
- //==============================================================================
- stock Check_Amount(playerid, item_id)
- {
- if(item_id > 0 || item_id >= contatore) return ERROR_INVALID_ITEM;
- for(new i; i < 10; i++)
- {
- if(player_inventory[playerid][i][id_inventory_item] == item_id)
- return player_inventory[playerid][i][p_amount];
- }
- return ERROR_INVALID_ITEM;
- }
- //==============================================================REFRESHINVENTORY
- // Refreshes player's inventory list
- //==============================================================================
- stock Refresh_Inventory(playerid)
- {
- new item_name_string[460], item_amount_string[256], string_temp[46], item_amount;
- for(new i; i < 10; i++)
- {
- if(player_inventory[playerid][i][p_amount] <= 0)
- {
- player_inventory[playerid][i][id_inventory_item] = -1;
- player_inventory[playerid][i][p_amount] = 0;
- }
- if(player_inventory[playerid][i][id_inventory_item] == -1)
- {
- continue;
- }
- else
- {
- format(string_temp, 46, "%s~w~~n~", item_list[player_inventory[playerid][i][id_inventory_item]][pi_name]);
- strins(item_name_string, string_temp, strlen(item_name_string), 460);
- if(player_inventory[playerid][i][p_amount] > 1)
- {
- format(string_temp, 43, "%d~n~", player_inventory[playerid][i][p_amount]);
- strins(item_amount_string, string_temp, strlen(item_amount_string), 256);
- }
- else
- {
- strins(item_amount_string, "~n~", strlen(item_amount_string), 256);
- }
- item_amount++;
- continue;
- }
- }
- if(!item_amount)
- {
- format(item_name_string, 400, "No items in inventory");
- TextDrawLetterSize(pi_background, 0.500000, 5.0);
- }
- else
- {
- TextDrawLetterSize(pi_background, 0.500000, 2.8 + 2.2*item_amount);
- }
- TextDrawSetString(pi_object_list, item_name_string);
- TextDrawSetString(pi_object_amount, item_amount_string);
- if(showing_inventory[playerid])
- {
- Show_Inventory(playerid);
- }
- return 1;
- }
- //=================================================================SHOWINVENTORY
- // Shows inventory for a player.
- //==============================================================================
- stock Show_Inventory(playerid)
- {
- if(!showing_inventory[playerid])
- {
- Refresh_Inventory(playerid);
- }
- TextDrawShowForPlayer(playerid, pi_background);
- TextDrawShowForPlayer(playerid, pi_title);
- TextDrawShowForPlayer(playerid, pi_object_list);
- TextDrawShowForPlayer(playerid, pi_object_amount);
- showing_inventory[playerid] = true;
- return 1;
- }
- //=================================================================HIDEINVENTORY
- // Hide inventory for a player.
- //==============================================================================
- stock Hide_Inventory(playerid)
- {
- TextDrawHideForPlayer(playerid, pi_background);
- TextDrawHideForPlayer(playerid, pi_title);
- TextDrawHideForPlayer(playerid, pi_object_list);
- TextDrawHideForPlayer(playerid, pi_object_amount);
- showing_inventory[playerid] = false;
- return 1;
- }
- //================================================================RESETINVENTORY
- // Reset player's inventory
- //==============================================================================
- stock Reset_Inventory(playerid)
- {
- for(new i; i < 10; i++)
- {
- player_inventory[playerid][i][id_inventory_item] = -1;
- player_inventory[playerid][i][p_amount] = 0;
- }
- Refresh_Inventory(playerid);
- return 1;
- }
- //=====================================================================DEBUGMODE
- // Handles debug mode of PInventory
- //==============================================================================
- stock PInventory_DebugMode(bool:mode)
- {
- return debug_mode = mode;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement