Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exemple:
- // Example script for use ArrayList in PAWN
- #include <a_samp>
- #include <ArrayList> // Include ArrayList for PAWN
- // Declare new list - 'playersList'
- new ArrayList:playersList;
- public OnFilterScriptInit()
- {
- // Create new ArrayList
- // @playersList - Address
- // @ <INTEGER> - Creating list which accepting just integer values, it can be <FLOAT> too
- // @ (MAX_PLAYERS) - Creating capacity of list, that is of cource changeable
- //
- // Capacity is possible to change by following function - ArrayList::EnsureCapacity (ArrayList:ArrayListID, newcapacity);
- playersList = NewArrayList<INTEGER>(MAX_PLAYERS);
- // An example ArrayList::EnsureCapacity
- // @playersList - Address
- // @420 - New capacity value of this list
- // ArrayList::EnsureCapacity(playersList, 420);
- // Loop timer
- SetTimer("updateplayers", 1000, true);
- return (true);
- }
- public OnFilterScriptExit()
- {
- // Destroy created list
- ArrayList::Destroy(playersList);
- return (true);
- }
- public OnPlayerConnect(playerid)
- {
- // Add player's id on the list
- // This will add my id into list (playersList) on first empty slot
- ArrayList::Add(playersList, playerid);
- return (true);
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- // Remove player's id from the list
- // IndexOf will find index in array by player's id
- ArrayList::Remove(playersList, ArrayList::IndexOf(playersList, playerid));
- return (true);
- }
- // Simple timer
- forward updateplayers();
- public updateplayers()
- {
- // Check if is list size > 0
- // If yes, loop all online players
- if (ArrayList::Size(playersList) > 0)
- {
- // Just a tick
- new timer = GetTickCount();
- // Loop which will go trough all online players, just online players!
- for (new i = 0; i < ArrayList::Size(playersList); i++) // Loop just online players
- {
- // Message...
- printf ("Looped player ID - %i", ArrayList::Get (playersList, i);
- }
- // And info at end
- printf ("Loop is completed in %dms with %d players", (GetTickCount() - timer), ArrayList::Size(playersList));
- }
- else print ("Nobody is online, don't need loop"); // Jump here if is server empty, no need empty loop lol
- return (true);
- }
- Inventory:
- //
- // SA:MP Simple Inventory System with ArrayList
- //
- // Writed by Ino
- //
- // 11/07/2016
- //
- #include <a_samp>
- #include <ArrayList> // Include ArrayList for PAWN
- #include <sscanf2> // Sscanf for cmd
- #include <zcmd> // ZCMD commands
- // Defines
- #undef MAX_PLAYERS
- #define MAX_PLAYERS (50)
- #define MAX_ITEMS (10) // Max items per player in inventory
- #define INVENTORY_DIALOG (8080)
- // Enumerator
- enum E_INVENTORY
- {
- bool: i_used,
- i_item_type,
- i_item_amount
- }
- new
- PlayerInventory[MAX_PLAYERS][E_INVENTORY][MAX_ITEMS];
- // Items
- new ItemArray [] [] =
- {
- {1, "Desert Eagle"},
- {2, "Medkit"},
- {3, "Burger"}
- };
- // Declare lists
- new
- ArrayList:InventoryList[MAX_PLAYERS];
- // Callbacks
- public OnPlayerConnect(playerid)
- {
- // Create list
- InventoryList[playerid] = NewArrayList<INTEGER>(MAX_ITEMS);
- return 1;
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- // Clear array
- ClearInventory(playerid);
- // Destroy list
- ArrayList::Destroy(InventoryList[playerid]);
- return 1;
- }
- public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
- {
- if (dialogid == INVENTORY_DIALOG)
- {
- if (response)
- {
- RemoveItemFromInventory(playerid, listitem + 1);
- }
- }
- return 1;
- }
- // Functions
- stock ClearInventory(playerid)
- {
- for (new i = 0; i < MAX_ITEMS; i++)
- {
- PlayerInventory[playerid][i_used][i] = false;
- }
- ArrayList::Clear(InventoryList[playerid]);
- return 1;
- }
- stock AddItemInInventory(playerid, item_type, item_amount)
- {
- if (ArrayList::Size(InventoryList[playerid]) < MAX_ITEMS)
- {
- new
- empty_id = ArrayList::Size(InventoryList[playerid]);
- ArrayList::Add(InventoryList[playerid], empty_id);
- PlayerInventory[playerid][i_used][empty_id] = true;
- PlayerInventory[playerid][i_item_type][empty_id] = item_type;
- PlayerInventory[playerid][i_item_amount][empty_id] = item_amount;
- SendClientMessage(playerid, -1, "Item added");
- }
- else SendClientMessage(playerid, -1, "Error: Your inventory is full!");
- return 1;
- }
- stock RemoveItemFromInventory(playerid, slot)
- {
- slot = slot - 1;
- if (slot < ArrayList::Size(InventoryList[playerid]))
- {
- new
- s = ArrayList::Get(InventoryList[playerid], slot);
- PlayerInventory[playerid][i_used][s] = false;
- PlayerInventory[playerid][i_item_type][s] = -1;
- PlayerInventory[playerid][i_item_amount][s] = -1;
- ArrayList::Remove(InventoryList[playerid], s);
- SendClientMessage(playerid, -1, "Item removed");
- }
- else SendClientMessage(playerid, -1, "Error: Choosen slot is empty.");
- return 1;
- }
- stock ShowInventory(playerid)
- {
- new
- buffer[1024],
- item_name[24],
- amount,
- get_id = -1;
- if (ArrayList::Size(InventoryList[playerid]) > 0)
- {
- for (new i = 0; i < ArrayList::Size(InventoryList[playerid]); i++)
- {
- get_id = ArrayList::Get(InventoryList[playerid], i);
- format (item_name, sizeof(item_name), "%s", ItemArray[ PlayerInventory[playerid][i_item_type][get_id] - 1 ][1]);
- amount = PlayerInventory[playerid][i_item_amount][get_id];
- format (buffer, sizeof(buffer), "%s{90C3D4}%d {FFFFFF}%s (%d)\n", buffer, (i + 1), item_name, amount);
- get_id = -1;
- }
- for (new j = ArrayList::Size(InventoryList[playerid]); j < MAX_ITEMS; j++)
- {
- format (buffer, sizeof(buffer), "%s{90C3D4}%d {FFFFFF}empty slot (0)\n", buffer, (j + 1));
- }
- }
- else format (buffer, sizeof(buffer), "Your inventory is empty");
- ShowPlayerDialog(playerid, INVENTORY_DIALOG, DIALOG_STYLE_LIST, "Inventory", buffer, "Close", "");
- return 1;
- }
- // Commands
- CMD:inventory(playerid, params[])
- {
- ShowInventory(playerid);
- return 1;
- }
- CMD:add(playerid, params[])
- {
- new
- item,
- amount;
- if (!sscanf(params, "ii", item, amount))
- {
- AddItemInInventory(playerid, item, amount);
- }
- else SendClientMessage(playerid, -1, "Usage: /add [ITEM TYPE] [AMOUNT]");
- return 1;
- }
- CMD:remove(playerid, params[])
- {
- new
- slot;
- if (!sscanf(params, "i", slot))
- {
- RemoveItemFromInventory(playerid, slot);
- }
- else SendClientMessage(playerid, -1, "Usage: /remove [SLOT ID]");
- return 1;
- }
- //
- /*
- ---------------------------------------
- ArrayList in PAWN
- @author: Ino
- @version: 1.0.1
- @release: 03/06/2016
- @build: 2
- @download: github.com/Ino42O/PawnArrayList
- ---------------------------------------
- Functions list:
- * ArrayList:NewArrayList<TYPE>(capacity)
- * ArrayList::Destroy (ArrayList:ArrayListID);
- * ArrayList::IsValid (ArrayList:ArrayListID);
- * ArrayList::Add (ArrayList:ArrayListID, value);
- * ArrayList::Remove (ArrayList:ArrayListID, index);
- * ArrayList::Size (ArrayList:ArrayListID);
- * ArrayList::Capacity (ArrayList:ArrayListID);
- * ArrayList::Get (ArrayList:ArrayListID, index);
- * ArrayList::EnsureCapacity (ArrayList:ArrayListID, newcapacity);
- * ArrayList::Clear (ArrayList:ArrayListID);
- * ArrayList::IndexOf (ArrayList:ArrayListID, value);
- Changelog:
- 03-06-2016:
- * Initial release
- 12-07-2016:
- * Fixed bugs with multi-list
- Available types:
- * FLOAT
- * INTEGER
- ---------------------------------------
- */
- #include <a_samp>
- // Defines (don't change)
- #define MAX_ARRAY_LIST (0x1000)
- #define MAX_ARRAY_CAPACITY (0xFFFF)
- #define INTEGER (0x1A4)
- #define FLOAT (0x420)
- #define INVALID_ARRAY_LIST (ArrayList:-1)
- #define ArrayList:: arraylistFunction
- #define _INC_ARRAYLIST // For another includes which want use this one
- // Script
- #define ARRAYLIST_UPDATE ("03/06/2016")
- #define ARRAYLIST_VERSION ("1.0.1")
- #define ARRAYLIST_BUILD (2)
- //#define ARRAYLIST_DEBUG // unavailable right now
- // Macros
- #define NewArrayList<%0>(%1) \
- ArrayList:ArrayList::Create(%0, %1)
- // Declarations
- enum E_ARRAY_LIST
- {
- bool:al_used,
- al_capacity,
- al_type,
- al_size
- }
- static eArrayList[MAX_ARRAY_LIST][E_ARRAY_LIST];
- // Forward
- forward ArrayList:ArrayList::Create(type, capacity);
- // Function for include
- static stock findArrayListID()
- {
- new id = -1;
- for (new i = 0; i < MAX_ARRAY_LIST; i++)
- {
- if (!eArrayList[i][al_used])
- {
- id = i;
- break;
- }
- }
- return (id != -1) ? id : -1;
- }
- // Available functions
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::IsValid
- Params:
- @ArrayList:ArrayListID
- Notes:
- Check if list exist
- ----------------------------------------------------------------------------*/
- stock ArrayList::IsValid(ArrayList:ArrayListID)
- {
- return (eArrayList[_:ArrayListID][al_used]);
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::Create - NewArrayList<>()
- Params:
- @type (FLOAT / INTEGER)
- @capacity
- Notes:
- New list in heap
- ----------------------------------------------------------------------------*/
- stock ArrayList:ArrayList::Create(type, capacity)
- {
- if (capacity < 1 || capacity > MAX_ARRAY_CAPACITY)
- return INVALID_ARRAY_LIST;
- new ArrayListID = findArrayListID();
- if (ArrayListID == -1)
- return INVALID_ARRAY_LIST;
- eArrayList[_:ArrayListID][al_used] = true;
- eArrayList[_:ArrayListID][al_capacity] = capacity;
- eArrayList[_:ArrayListID][al_size] = 0;
- switch (type)
- {
- case INTEGER:
- {
- eArrayList[_:ArrayListID][al_type] = INTEGER;
- }
- case FLOAT:
- {
- eArrayList[_:ArrayListID][al_type] = FLOAT;
- }
- default:
- {
- return INVALID_ARRAY_LIST;
- }
- }
- return ArrayList:ArrayListID;
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::Destroy
- Params:
- @ArrayList:ArrayListID
- Notes:
- Destroy list if exist
- ----------------------------------------------------------------------------*/
- stock ArrayList::Destroy(ArrayList:ArrayListID)
- {
- if (!ArrayList::IsValid(ArrayListID))
- return (false);
- eArrayList[_:ArrayListID][al_used] = false;
- return (true);
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::Add
- Params:
- @ArrayList:ArrayListID
- @value
- Notes:
- Add item on first empty slot
- ----------------------------------------------------------------------------*/
- stock ArrayList::Add(ArrayList:ArrayListID, { _, Float}:var, tag = tagof(var))
- {
- if (eArrayList[_:ArrayListID][al_size] >= eArrayList[_:ArrayListID][al_capacity])
- return (false);
- eArrayList[_:ArrayListID][al_used] = true;
- new
- address = (((_:ArrayListID + 1) * 0xFFFF) + eArrayList[_:ArrayListID][al_size]),
- value[0x40];
- if (tag == tagof(Float:)) // Float
- {
- if (eArrayList[_:ArrayListID][al_type] != FLOAT)
- return (false);
- format (value, 0x40, "%f", var);
- setproperty(0, "", address, value);
- }
- else // Integer
- {
- if (eArrayList[_:ArrayListID][al_type] != INTEGER)
- return (false);
- format (value, 0x40, "%i", var);
- setproperty(0, "", address, value);
- }
- eArrayList[_:ArrayListID][al_size] ++;
- return (true);
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::Size
- Params:
- @ArrayList:ArrayListID
- Notes:
- Return size of array list
- ----------------------------------------------------------------------------*/
- stock ArrayList::Size(ArrayList:ArrayListID)
- {
- if (!ArrayList::IsValid(ArrayListID))
- return (false);
- return eArrayList[_:ArrayListID][al_size];
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::Capacity
- Params:
- @ArrayList:ArrayListID
- Notes:
- Return the capacity of list
- ----------------------------------------------------------------------------*/
- stock ArrayList::Capacity(ArrayList:ArrayListID)
- {
- if (!ArrayList::IsValid(ArrayListID))
- return (false);
- return eArrayList[_:ArrayListID][al_capacity];
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::Get
- Params:
- @ArrayList:ArrayListID
- @index
- Notes:
- Return value from choosen index
- ----------------------------------------------------------------------------*/
- stock ArrayList::Get(ArrayList:ArrayListID, index)
- {
- if (!ArrayList::IsValid(ArrayListID))
- return (false);
- if (index > ArrayList::Size(ArrayListID))
- return (false);
- new
- value[0x40],
- address = (((_:ArrayListID + 1) * 0xFFFF) + index);
- getproperty(0, "", address, value);
- strunpack(value, value, 0x40);
- switch (eArrayList[_:ArrayListID][al_type])
- {
- case INTEGER:
- {
- return strval(value);
- }
- case FLOAT:
- {
- return _:(floatstr(value));
- }
- default:
- {
- return -1;
- }
- }
- return -1;
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::EnsureCapacity
- Params:
- @ArrayList:ArrayListID
- @newcapacity
- Notes:
- This will change capacity of list
- ----------------------------------------------------------------------------*/
- stock ArrayList::EnsureCapacity(ArrayList:ArrayListID, newcapacity)
- {
- if (!ArrayList::IsValid(ArrayListID))
- return (false);
- if (newcapacity < 1 || newcapacity > MAX_ARRAY_CAPACITY)
- return (false);
- eArrayList[_:ArrayListID][al_capacity] = newcapacity;
- return (true);
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::Remove
- Params:
- @ArrayList:ArrayListID
- @position
- Notes:
- Remove value from choosen position and auto-fill that slot
- ----------------------------------------------------------------------------*/
- stock ArrayList::Remove(ArrayList:ArrayListID, position)
- {
- if (!ArrayList::IsValid(ArrayListID))
- return (false);
- if (eArrayList[_:ArrayListID][al_size] < position)
- return (false);
- for (new i = position, size = eArrayList[_:ArrayListID][al_size]; i < size;
- i++)
- {
- new
- value[0x40];
- getproperty(0, "", ((((_:ArrayListID + 1) * 0xFFFF) + i) + 1), value);
- strunpack(value, value, 0x40);
- setproperty(0, "", (((_:ArrayListID + 1) * 0xFFFF) + i), value);
- }
- eArrayList[_:ArrayListID][al_size] --;
- return (true);
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::Clear
- Params:
- @ArrayList:ArrayListID
- Notes:
- Clear all slots
- ----------------------------------------------------------------------------*/
- stock ArrayList::Clear(ArrayList:ArrayListID)
- {
- if (!ArrayList::IsValid(ArrayListID))
- return (false);
- for (new i = 0, size = eArrayList[_:ArrayListID][al_size]; i < size; i++)
- {
- deleteproperty(0, "", (i + _:ArrayListID));
- }
- eArrayList[_:ArrayListID][al_size] = 0;
- return (true);
- }
- /*----------------------------------------------------------------------------
- Function:
- ArrayList::IndexOf
- Params:
- @ArrayList:ArrayListID
- @value
- Notes:
- Return index of value
- ----------------------------------------------------------------------------*/
- stock ArrayList::IndexOf(ArrayList:ArrayListID, { _, Float}:value, tag = tagof(value))
- {
- if (!ArrayList::IsValid(ArrayListID))
- return (false);
- new
- index = -1,
- found,
- vc[0x40];
- if (tag == tagof(Float:)) // Float
- {
- if (eArrayList[_:ArrayListID][al_type] != FLOAT)
- return -1;
- format (vc, 0x40, "%f", value);
- }
- else // Integer
- {
- if (eArrayList[_:ArrayListID][al_type] != INTEGER)
- return -1;
- format (vc, 0x40, "%i", value);
- }
- for (new i = 0; i < eArrayList[_:ArrayListID][al_size]; i++)
- {
- new
- v[0x40];
- getproperty(0, "", (_:ArrayListID + i), v);
- strunpack(v, v, sizeof(v));
- if(!strcmp(vc, v))
- {
- found = true;
- index = (_:ArrayListID + i);
- break;
- }
- }
- if (found)
- return index;
- return -1;
- }
Add Comment
Please, Sign In to add comment