AwDod

Arraylist.inc

Oct 24th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 15.90 KB | None | 0 0
  1. Exemple:
  2.  
  3. // Example script for use ArrayList in PAWN
  4.  
  5. #include <a_samp>
  6. #include <ArrayList> // Include ArrayList for PAWN
  7.  
  8. // Declare new list - 'playersList'
  9. new ArrayList:playersList;
  10.  
  11. public OnFilterScriptInit()
  12. {
  13.     // Create new ArrayList
  14.  
  15.     // @playersList - Address
  16.     // @ <INTEGER> - Creating list which accepting just integer values, it can be <FLOAT> too
  17.     // @ (MAX_PLAYERS) - Creating capacity of list, that is of cource changeable
  18.     //
  19.     // Capacity is possible to change by following function - ArrayList::EnsureCapacity (ArrayList:ArrayListID, newcapacity);
  20.     playersList = NewArrayList<INTEGER>(MAX_PLAYERS);
  21.  
  22.     // An example ArrayList::EnsureCapacity
  23.  
  24.     // @playersList - Address
  25.     // @420 - New capacity value of this list
  26.  
  27.     //      ArrayList::EnsureCapacity(playersList, 420);
  28.  
  29.  
  30.     // Loop timer
  31.     SetTimer("updateplayers", 1000, true);
  32.     return (true);
  33. }
  34.  
  35. public OnFilterScriptExit()
  36. {
  37.     // Destroy created list
  38.     ArrayList::Destroy(playersList);
  39.     return (true);
  40. }
  41.  
  42. public OnPlayerConnect(playerid)
  43. {
  44.     // Add player's id on the list
  45.     // This will add my id into list (playersList) on first empty slot
  46.     ArrayList::Add(playersList, playerid);
  47.     return (true);
  48. }
  49.  
  50. public OnPlayerDisconnect(playerid, reason)
  51. {
  52.     // Remove player's id from the list
  53.     // IndexOf will find index in array by player's id
  54.     ArrayList::Remove(playersList, ArrayList::IndexOf(playersList, playerid));
  55.     return (true);
  56. }
  57.  
  58. // Simple timer
  59. forward updateplayers();
  60. public updateplayers()
  61. {
  62.     // Check if is list size > 0
  63.     // If yes, loop all online players
  64.     if (ArrayList::Size(playersList) > 0)
  65.     {
  66.         // Just a tick
  67.         new timer = GetTickCount();
  68.  
  69.         // Loop which will go trough all online players, just online players!
  70.         for (new i = 0; i < ArrayList::Size(playersList); i++) // Loop just online players
  71.         {
  72.             // Message...
  73.             printf ("Looped player ID - %i", ArrayList::Get (playersList, i);
  74.         }
  75.  
  76.         // And info at end
  77.         printf ("Loop is completed in %dms with %d players", (GetTickCount() - timer), ArrayList::Size(playersList));
  78.  
  79.     }
  80.     else print ("Nobody is online, don't need loop"); // Jump here if is server empty, no need empty loop lol
  81.     return (true);
  82. }
  83.  
  84. Inventory:
  85.  
  86. //
  87. //      SA:MP Simple Inventory System with ArrayList
  88. //     
  89. //      Writed by Ino
  90. //
  91. //      11/07/2016
  92. //
  93.  
  94. #include <a_samp>
  95. #include <ArrayList> // Include ArrayList for PAWN
  96. #include <sscanf2> // Sscanf for cmd
  97. #include <zcmd> // ZCMD commands
  98.  
  99.  
  100. // Defines
  101. #undef MAX_PLAYERS
  102. #define MAX_PLAYERS             (50)
  103. #define MAX_ITEMS               (10) // Max items per player in inventory
  104. #define INVENTORY_DIALOG        (8080)
  105.  
  106.  
  107. // Enumerator
  108. enum E_INVENTORY
  109. {
  110.     bool:   i_used,
  111.             i_item_type,
  112.             i_item_amount
  113. }
  114. new
  115.     PlayerInventory[MAX_PLAYERS][E_INVENTORY][MAX_ITEMS];
  116.  
  117.  
  118. // Items
  119. new ItemArray [] [] =
  120. {
  121.     {1, "Desert Eagle"},
  122.     {2, "Medkit"},
  123.     {3, "Burger"}
  124. };
  125.  
  126.  
  127. // Declare lists
  128. new
  129.     ArrayList:InventoryList[MAX_PLAYERS];
  130.  
  131.  
  132. // Callbacks
  133. public OnPlayerConnect(playerid)
  134. {
  135.     // Create list
  136.     InventoryList[playerid] = NewArrayList<INTEGER>(MAX_ITEMS);
  137.     return 1;
  138. }
  139.  
  140. public OnPlayerDisconnect(playerid, reason)
  141. {
  142.     // Clear array
  143.     ClearInventory(playerid);
  144.  
  145.     // Destroy list
  146.     ArrayList::Destroy(InventoryList[playerid]);
  147.     return 1;
  148. }
  149.  
  150. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  151. {
  152.     if (dialogid == INVENTORY_DIALOG)
  153.     {
  154.         if (response)
  155.         {
  156.             RemoveItemFromInventory(playerid, listitem + 1);
  157.         }
  158.     }
  159.     return 1;
  160. }
  161.  
  162.  
  163.  
  164. // Functions
  165. stock ClearInventory(playerid)
  166. {
  167.     for (new i = 0; i < MAX_ITEMS; i++)
  168.     {
  169.         PlayerInventory[playerid][i_used][i] = false;
  170.     }
  171.     ArrayList::Clear(InventoryList[playerid]);
  172.     return 1;
  173. }
  174.  
  175. stock AddItemInInventory(playerid, item_type, item_amount)
  176. {
  177.     if (ArrayList::Size(InventoryList[playerid]) < MAX_ITEMS)
  178.     {
  179.         new
  180.             empty_id = ArrayList::Size(InventoryList[playerid]);
  181.  
  182.         ArrayList::Add(InventoryList[playerid], empty_id);
  183.  
  184.         PlayerInventory[playerid][i_used][empty_id] = true;
  185.         PlayerInventory[playerid][i_item_type][empty_id] = item_type;
  186.         PlayerInventory[playerid][i_item_amount][empty_id] = item_amount;
  187.  
  188.         SendClientMessage(playerid, -1, "Item added");
  189.     }
  190.     else SendClientMessage(playerid, -1, "Error: Your inventory is full!");
  191.     return 1;
  192. }
  193.  
  194. stock RemoveItemFromInventory(playerid, slot)
  195. {
  196.     slot = slot - 1;
  197.  
  198.     if (slot < ArrayList::Size(InventoryList[playerid]))
  199.     {
  200.         new
  201.             s = ArrayList::Get(InventoryList[playerid], slot);
  202.  
  203.         PlayerInventory[playerid][i_used][s] = false;
  204.         PlayerInventory[playerid][i_item_type][s] = -1;
  205.         PlayerInventory[playerid][i_item_amount][s] = -1;
  206.  
  207.         ArrayList::Remove(InventoryList[playerid], s);
  208.  
  209.         SendClientMessage(playerid, -1, "Item removed");
  210.     }
  211.     else SendClientMessage(playerid, -1, "Error: Choosen slot is empty.");
  212.     return 1;
  213. }
  214.  
  215. stock ShowInventory(playerid)
  216. {
  217.     new
  218.         buffer[1024],
  219.         item_name[24],
  220.         amount,
  221.         get_id = -1;
  222.  
  223.     if (ArrayList::Size(InventoryList[playerid]) > 0)
  224.     {
  225.         for (new i = 0; i < ArrayList::Size(InventoryList[playerid]); i++)
  226.         {
  227.             get_id = ArrayList::Get(InventoryList[playerid], i);
  228.  
  229.             format (item_name, sizeof(item_name), "%s", ItemArray[ PlayerInventory[playerid][i_item_type][get_id] - 1 ][1]);
  230.             amount = PlayerInventory[playerid][i_item_amount][get_id];
  231.             format (buffer, sizeof(buffer), "%s{90C3D4}%d {FFFFFF}%s (%d)\n", buffer, (i + 1), item_name, amount);
  232.  
  233.             get_id = -1;
  234.         }
  235.         for (new j = ArrayList::Size(InventoryList[playerid]); j < MAX_ITEMS; j++)
  236.         {      
  237.             format (buffer, sizeof(buffer), "%s{90C3D4}%d {FFFFFF}empty slot (0)\n", buffer, (j + 1));
  238.         }
  239.     }
  240.     else format (buffer, sizeof(buffer), "Your inventory is empty");
  241.  
  242.     ShowPlayerDialog(playerid, INVENTORY_DIALOG, DIALOG_STYLE_LIST, "Inventory", buffer, "Close", "");
  243.     return 1;
  244. }
  245.  
  246.  
  247. // Commands
  248. CMD:inventory(playerid, params[])
  249. {
  250.     ShowInventory(playerid);
  251.     return 1;
  252. }
  253.  
  254. CMD:add(playerid, params[])
  255. {
  256.     new
  257.         item,
  258.         amount;
  259.  
  260.     if (!sscanf(params, "ii", item, amount))
  261.     {  
  262.         AddItemInInventory(playerid, item, amount);
  263.     }
  264.     else SendClientMessage(playerid, -1, "Usage: /add [ITEM TYPE] [AMOUNT]");
  265.     return 1;
  266. }
  267.  
  268. CMD:remove(playerid, params[])
  269. {
  270.     new
  271.         slot;
  272.  
  273.     if (!sscanf(params, "i", slot))
  274.     {  
  275.         RemoveItemFromInventory(playerid, slot);
  276.     }
  277.     else SendClientMessage(playerid, -1, "Usage: /remove [SLOT ID]");
  278.     return 1;
  279. }
  280.  
  281.  
  282. //
  283.  
  284. /*
  285. ---------------------------------------
  286.  
  287.             ArrayList in PAWN  
  288.  
  289.             @author: Ino
  290.             @version: 1.0.1
  291.             @release: 03/06/2016
  292.             @build: 2  
  293.             @download: github.com/Ino42O/PawnArrayList
  294.  
  295. ---------------------------------------
  296.  
  297.     Functions list:
  298.  
  299.         * ArrayList:NewArrayList<TYPE>(capacity)
  300.         * ArrayList::Destroy (ArrayList:ArrayListID);
  301.         * ArrayList::IsValid (ArrayList:ArrayListID);
  302.         * ArrayList::Add (ArrayList:ArrayListID, value);
  303.         * ArrayList::Remove (ArrayList:ArrayListID, index);
  304.         * ArrayList::Size (ArrayList:ArrayListID);
  305.         * ArrayList::Capacity (ArrayList:ArrayListID);
  306.         * ArrayList::Get (ArrayList:ArrayListID, index);
  307.         * ArrayList::EnsureCapacity (ArrayList:ArrayListID, newcapacity);
  308.         * ArrayList::Clear (ArrayList:ArrayListID);
  309.         * ArrayList::IndexOf (ArrayList:ArrayListID, value);
  310.  
  311.    
  312.  
  313.     Changelog:
  314.  
  315.         03-06-2016:
  316.             * Initial release
  317.  
  318.         12-07-2016:
  319.             * Fixed bugs with multi-list
  320.  
  321.  
  322.  
  323.     Available types:
  324.  
  325.         * FLOAT
  326.         * INTEGER
  327.  
  328. ---------------------------------------
  329. */
  330.  
  331. #include <a_samp>
  332.  
  333. // Defines (don't change)
  334. #define MAX_ARRAY_LIST          (0x1000)
  335. #define MAX_ARRAY_CAPACITY      (0xFFFF)
  336. #define INTEGER                 (0x1A4)
  337. #define FLOAT                   (0x420)
  338. #define INVALID_ARRAY_LIST      (ArrayList:-1)
  339. #define ArrayList::             arraylistFunction
  340.  
  341. #define _INC_ARRAYLIST // For another includes which want use this one
  342.  
  343. // Script
  344. #define ARRAYLIST_UPDATE        ("03/06/2016")
  345. #define ARRAYLIST_VERSION       ("1.0.1")
  346. #define ARRAYLIST_BUILD         (2)
  347. //#define ARRAYLIST_DEBUG       // unavailable right now
  348.  
  349.  
  350. // Macros
  351. #define NewArrayList<%0>(%1) \
  352.     ArrayList:ArrayList::Create(%0, %1)
  353.  
  354.  
  355. // Declarations
  356. enum E_ARRAY_LIST
  357. {
  358.     bool:al_used,
  359.          al_capacity,
  360.          al_type,
  361.          al_size
  362. }
  363. static eArrayList[MAX_ARRAY_LIST][E_ARRAY_LIST];
  364.  
  365.  
  366.  
  367. // Forward
  368. forward ArrayList:ArrayList::Create(type, capacity);
  369.  
  370.  
  371. // Function for include
  372. static stock findArrayListID()
  373. {
  374.     new id = -1;
  375.  
  376.     for (new i = 0; i < MAX_ARRAY_LIST; i++)
  377.     {
  378.         if (!eArrayList[i][al_used])
  379.         {
  380.             id = i;
  381.             break;
  382.         }
  383.     }
  384.  
  385.     return (id != -1) ? id : -1;
  386. }
  387.  
  388.  
  389. // Available functions
  390. /*----------------------------------------------------------------------------
  391.  
  392.     Function:
  393.         ArrayList::IsValid
  394.  
  395.     Params:
  396.         @ArrayList:ArrayListID
  397.  
  398.     Notes:
  399.         Check if list exist
  400.  
  401. ----------------------------------------------------------------------------*/
  402. stock ArrayList::IsValid(ArrayList:ArrayListID)
  403. {
  404.     return (eArrayList[_:ArrayListID][al_used]);
  405. }
  406.  
  407.  
  408. /*----------------------------------------------------------------------------
  409.  
  410.     Function:
  411.         ArrayList::Create - NewArrayList<>()
  412.  
  413.     Params:
  414.         @type (FLOAT / INTEGER)
  415.         @capacity
  416.  
  417.     Notes:
  418.         New list in heap
  419.  
  420. ----------------------------------------------------------------------------*/
  421. stock ArrayList:ArrayList::Create(type, capacity)
  422. {
  423.     if (capacity < 1 || capacity > MAX_ARRAY_CAPACITY)
  424.         return INVALID_ARRAY_LIST;
  425.  
  426.     new ArrayListID = findArrayListID();
  427.  
  428.     if (ArrayListID == -1)
  429.         return INVALID_ARRAY_LIST;
  430.  
  431.     eArrayList[_:ArrayListID][al_used] = true;
  432.     eArrayList[_:ArrayListID][al_capacity] = capacity;
  433.     eArrayList[_:ArrayListID][al_size] = 0;
  434.  
  435.     switch (type)
  436.     {
  437.         case INTEGER:
  438.         {
  439.             eArrayList[_:ArrayListID][al_type] = INTEGER;
  440.         }
  441.         case FLOAT:
  442.         {
  443.             eArrayList[_:ArrayListID][al_type] = FLOAT;
  444.         }
  445.         default:
  446.         {
  447.             return INVALID_ARRAY_LIST;
  448.         }
  449.     }
  450.  
  451.     return ArrayList:ArrayListID;
  452. }
  453.  
  454.  
  455. /*----------------------------------------------------------------------------
  456.  
  457.     Function:
  458.         ArrayList::Destroy
  459.  
  460.     Params:
  461.         @ArrayList:ArrayListID
  462.  
  463.     Notes:
  464.         Destroy list if exist
  465.  
  466. ----------------------------------------------------------------------------*/
  467. stock ArrayList::Destroy(ArrayList:ArrayListID)
  468. {
  469.     if (!ArrayList::IsValid(ArrayListID))
  470.         return (false);
  471.  
  472.     eArrayList[_:ArrayListID][al_used] = false;
  473.  
  474.     return (true);
  475. }
  476.  
  477.  
  478. /*----------------------------------------------------------------------------
  479.  
  480.     Function:
  481.         ArrayList::Add
  482.  
  483.     Params:
  484.         @ArrayList:ArrayListID
  485.         @value
  486.  
  487.     Notes:
  488.         Add item on first empty slot
  489.  
  490. ----------------------------------------------------------------------------*/
  491. stock ArrayList::Add(ArrayList:ArrayListID, { _, Float}:var, tag = tagof(var))
  492. {
  493.     if (eArrayList[_:ArrayListID][al_size] >= eArrayList[_:ArrayListID][al_capacity])
  494.         return (false);
  495.    
  496.     eArrayList[_:ArrayListID][al_used] = true;
  497.  
  498.     new
  499.         address = (((_:ArrayListID + 1) * 0xFFFF) + eArrayList[_:ArrayListID][al_size]),
  500.         value[0x40];
  501.  
  502.     if (tag == tagof(Float:)) // Float
  503.     {
  504.         if (eArrayList[_:ArrayListID][al_type] != FLOAT)
  505.             return (false);
  506.  
  507.         format (value, 0x40, "%f", var);
  508.         setproperty(0, "", address, value);
  509.     }
  510.     else // Integer
  511.     {
  512.         if (eArrayList[_:ArrayListID][al_type] != INTEGER)
  513.             return (false);
  514.  
  515.         format (value, 0x40, "%i", var);
  516.         setproperty(0, "", address, value);
  517.     }
  518.  
  519.     eArrayList[_:ArrayListID][al_size] ++;
  520.     return (true);
  521. }
  522.  
  523.  
  524. /*----------------------------------------------------------------------------
  525.  
  526.     Function:
  527.         ArrayList::Size
  528.  
  529.     Params:
  530.         @ArrayList:ArrayListID
  531.  
  532.     Notes:
  533.         Return size of array list
  534.  
  535. ----------------------------------------------------------------------------*/
  536. stock ArrayList::Size(ArrayList:ArrayListID)
  537. {
  538.     if (!ArrayList::IsValid(ArrayListID))
  539.         return (false);
  540.  
  541.     return eArrayList[_:ArrayListID][al_size];
  542. }
  543.  
  544.  
  545. /*----------------------------------------------------------------------------
  546.  
  547.     Function:
  548.         ArrayList::Capacity
  549.  
  550.     Params:
  551.         @ArrayList:ArrayListID
  552.  
  553.     Notes:
  554.         Return the capacity of list
  555.  
  556. ----------------------------------------------------------------------------*/
  557. stock ArrayList::Capacity(ArrayList:ArrayListID)
  558. {
  559.     if (!ArrayList::IsValid(ArrayListID))
  560.         return (false);
  561.  
  562.     return eArrayList[_:ArrayListID][al_capacity];
  563. }
  564.  
  565.  
  566. /*----------------------------------------------------------------------------
  567.  
  568.     Function:
  569.         ArrayList::Get
  570.  
  571.     Params:
  572.         @ArrayList:ArrayListID
  573.         @index
  574.  
  575.     Notes:
  576.         Return value from choosen index
  577.  
  578. ----------------------------------------------------------------------------*/
  579. stock ArrayList::Get(ArrayList:ArrayListID, index)
  580. {
  581.     if (!ArrayList::IsValid(ArrayListID))
  582.         return (false);
  583.  
  584.     if (index > ArrayList::Size(ArrayListID))
  585.         return (false);
  586.  
  587.     new
  588.         value[0x40],
  589.         address = (((_:ArrayListID + 1) * 0xFFFF) + index);
  590.  
  591.     getproperty(0, "", address, value);
  592.     strunpack(value, value, 0x40);
  593.    
  594.     switch (eArrayList[_:ArrayListID][al_type])
  595.     {
  596.         case INTEGER:
  597.         {
  598.             return strval(value);
  599.         }
  600.         case FLOAT:
  601.         {
  602.             return _:(floatstr(value));
  603.         }
  604.         default:
  605.         {
  606.             return -1;
  607.         }
  608.     }
  609.  
  610.     return -1;
  611. }
  612.  
  613.  
  614. /*----------------------------------------------------------------------------
  615.  
  616.     Function:
  617.         ArrayList::EnsureCapacity
  618.  
  619.     Params:
  620.         @ArrayList:ArrayListID
  621.         @newcapacity
  622.  
  623.     Notes:
  624.         This will change capacity of list
  625.  
  626. ----------------------------------------------------------------------------*/
  627. stock ArrayList::EnsureCapacity(ArrayList:ArrayListID, newcapacity)
  628. {
  629.     if (!ArrayList::IsValid(ArrayListID))
  630.         return (false);
  631.  
  632.     if (newcapacity < 1 || newcapacity > MAX_ARRAY_CAPACITY)
  633.         return (false);
  634.  
  635.     eArrayList[_:ArrayListID][al_capacity] = newcapacity;
  636.  
  637.     return (true);
  638. }
  639.  
  640.  
  641. /*----------------------------------------------------------------------------
  642.  
  643.     Function:
  644.         ArrayList::Remove
  645.  
  646.     Params:
  647.         @ArrayList:ArrayListID
  648.         @position
  649.  
  650.     Notes:
  651.         Remove value from choosen position and auto-fill that slot
  652.  
  653. ----------------------------------------------------------------------------*/
  654. stock ArrayList::Remove(ArrayList:ArrayListID, position)
  655. {
  656.     if (!ArrayList::IsValid(ArrayListID))
  657.         return (false);
  658.  
  659.     if (eArrayList[_:ArrayListID][al_size] < position)
  660.         return (false);
  661.  
  662.     for (new i = position, size = eArrayList[_:ArrayListID][al_size]; i < size;
  663.         i++)
  664.     {
  665.         new
  666.             value[0x40];
  667.  
  668.         getproperty(0, "", ((((_:ArrayListID + 1) * 0xFFFF) + i) + 1), value);
  669.         strunpack(value, value, 0x40);
  670.         setproperty(0, "", (((_:ArrayListID + 1) * 0xFFFF) + i), value);
  671.     }
  672.  
  673.     eArrayList[_:ArrayListID][al_size] --;
  674.     return (true);
  675. }
  676.  
  677.  
  678. /*----------------------------------------------------------------------------
  679.  
  680.     Function:
  681.         ArrayList::Clear
  682.  
  683.     Params:
  684.         @ArrayList:ArrayListID
  685.  
  686.     Notes:
  687.         Clear all slots
  688.  
  689. ----------------------------------------------------------------------------*/
  690. stock ArrayList::Clear(ArrayList:ArrayListID)
  691. {
  692.     if (!ArrayList::IsValid(ArrayListID))
  693.         return (false);
  694.  
  695.     for (new i = 0, size = eArrayList[_:ArrayListID][al_size]; i < size; i++)
  696.     {
  697.         deleteproperty(0, "", (i + _:ArrayListID));
  698.     }
  699.     eArrayList[_:ArrayListID][al_size] = 0;
  700.     return (true);
  701. }
  702.  
  703.  
  704. /*----------------------------------------------------------------------------
  705.  
  706.     Function:
  707.         ArrayList::IndexOf
  708.  
  709.     Params:
  710.         @ArrayList:ArrayListID
  711.         @value
  712.  
  713.     Notes:
  714.         Return index of value
  715.  
  716. ----------------------------------------------------------------------------*/
  717. stock ArrayList::IndexOf(ArrayList:ArrayListID, { _, Float}:value, tag = tagof(value))
  718. {
  719.     if (!ArrayList::IsValid(ArrayListID))
  720.         return (false);
  721.  
  722.     new
  723.         index = -1,
  724.         found,
  725.         vc[0x40];
  726.  
  727.  
  728.     if (tag == tagof(Float:)) // Float
  729.     {
  730.         if (eArrayList[_:ArrayListID][al_type] != FLOAT)
  731.             return -1;
  732.  
  733.         format (vc, 0x40, "%f", value);
  734.     }
  735.     else // Integer
  736.     {
  737.         if (eArrayList[_:ArrayListID][al_type] != INTEGER)
  738.             return -1;
  739.  
  740.         format (vc, 0x40, "%i", value);
  741.     }
  742.    
  743.  
  744.     for (new i = 0; i < eArrayList[_:ArrayListID][al_size]; i++)
  745.     {
  746.         new
  747.             v[0x40];
  748.  
  749.         getproperty(0, "", (_:ArrayListID + i), v);
  750.         strunpack(v, v, sizeof(v));
  751.  
  752.         if(!strcmp(vc, v))
  753.         {
  754.             found = true;
  755.             index = (_:ArrayListID + i);
  756.             break;
  757.         }
  758.     }
  759.  
  760.     if (found)
  761.         return index;
  762.  
  763.     return -1;
  764. }
Add Comment
Please, Sign In to add comment