Yiin

Untitled

Nov 26th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. cell AMX_NATIVE_CALL DeleteItem(AMX* amx, cell* params)
  2. {
  3.     if((int)params[1]) {
  4.         CItem * ptr = CItem::GetItemByUID((unsigned int)params[1]);
  5.         if(ptr != NULL) {
  6.             CItem::DeleteItem(ptr);
  7.             return 1;
  8.         }
  9.     }
  10.     return 0;
  11. }
  12.  
  13. cell AMX_NATIVE_CALL CreateItem(AMX* amx, cell* params)
  14. {
  15.     // Get the name length
  16.     int iLength = 0;
  17.     cell *pAddress = NULL;
  18.     amx_GetAddr(amx, params[1], &pAddress);
  19.     amx_StrLen(pAddress, &iLength);
  20.     // Make sure the length is valid
  21.     if(!iLength)
  22.         return 0;
  23.    
  24.     // Make room for the string end
  25.     iLength++;
  26.     // Allocate the name string
  27.     char *szName = new char [iLength];
  28.     // Get the name
  29.     amx_GetString(szName, pAddress, 0, iLength);
  30.  
  31.     CItem * item = new CItem(szName, (int)params[2], (int)params[3]);
  32.  
  33.     return item->getUniqueId();
  34. }
  35.  
  36.  
  37.  
  38. //////////// kiti failai
  39.  
  40. std::deque<CItem *> gItems;
  41. std::stack<long int> gUnusedUniqueIds;
  42.  
  43. CItem * CItem::GetItemByUID(unsigned int uid)
  44. {
  45.     for(auto i = gItems.begin(); i != gItems.end(); ++i) {
  46.         if((*i)->getUniqueId() == uid) {
  47.             return (*i);
  48.         }
  49.     }
  50.     return NULL;
  51. }
  52.  
  53. void CItem::DeleteItem(CItem * item)
  54. {
  55.     deque<CItem *>::iterator result;
  56.  
  57.     result = std::find(gItems.begin(), gItems.end(), item);
  58.  
  59.     if(result != gItems.end()) {
  60.         gUnusedUniqueIds.push(item->getUniqueId());
  61.         gItems.erase(result);
  62.         delete item;
  63.     }
  64. }
  65.  
  66. void CItem::CreateItem()
  67. {
  68.     static unsigned int nextUniqueId = 1;
  69.     if(gUnusedUniqueIds.empty()) {
  70.         uniqueId = nextUniqueId++;
  71.     }
  72.     else {
  73.         uniqueId = gUnusedUniqueIds.top();
  74.         gUnusedUniqueIds.pop();
  75.     }
  76.  
  77.     gItems.push_back(this);
  78. }
  79.  
  80. CItem::CItem(string id) : itemId(id), itemCount(1), itemLevel(1)
  81. {
  82.     CreateItem();
  83. }
  84.  
  85. CItem::CItem(string id, int count) : itemId(id), itemCount(count), itemLevel(1)
  86. {
  87.     CreateItem();
  88. }
  89.  
  90. CItem::CItem(string id, int count, int level) : itemId(id), itemCount(count), itemLevel(level)
  91. {
  92.     CreateItem();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment