inventory.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Player Inventories
  3. Base system by oneOCT3T
  4. To add an inventory item,
  5. 1. add to e_pInventory enumerator
  6. 2. add to GetInventoryName()
  7. 3. add (make it same name as in e_pInventory), as a MySQL field in `inventories`
  8. */
  9. enum e_pInventory {
  10. inMaterials,
  11. inCannabis,
  12. inCocaine,
  13. inMeth,
  14. inPackage,
  15. inCrates,
  16. inSeeds,
  17. inProducts,
  18. inRopes,
  19. inBlindfolds,
  20. inCigars,
  21. inSprunk,
  22. inSpraycan,
  23. inScrewdriver,
  24. inDeckOfCards,
  25. inWTC,
  26. inFishingRod,
  27. inBait,
  28. inBaitleft,
  29. inFishingLine,
  30. inStereo,
  31. inBoxOfMatches,
  32. inJerrycan,
  33. inCellphone,
  34. inCellPhoneN
  35. }
  36. //Vars
  37. static Inventory[MAX_PLAYERS][e_pInventory];
  38. static bool:ModifiedInventory[MAX_PLAYERS];
  39. //Function Prototypes
  40. forward GetInventoryName(invid);
  41. //&: SetInventory(playerid, e_pInventory:item, oper, value);
  42. forward GetInventory(playerid, e_pInventory:item);
  43. static SaveInventoryQueue();
  44. public ShowInventory(playerid, target);
  45. //Function bodies
  46. //Only shows what the player has, in a dialog
  47. public ShowInventory(playerid, target) {
  48. new string[200];
  49. for(new item; e_pInventory:item < e_pInventory; item++) {
  50. if(Inventory[playerid][item]) {
  51. format(string, sizeof(string), "%s\n%d %s", string, Inventory[playerid][item], GetInventoryName(item));
  52. }
  53. }
  54. return 1;
  55. }
  56. //Saves the current modified inventories
  57. static SaveInventoryQueue() {
  58. new query[400];
  59. foreach(new playerid: Player) {
  60. if(ModifiedInventory[playerid]) {
  61. mysql_format(sqlGameConnection, query, sizeof(query), "UPDATE inventories SET");
  62. for(new item; e_pInventory:item < e_pInventory; item++) {
  63. mysql_format(sqlGameConnection, query, sizeof(query), " \
  64. %s, `%s` = %d",
  65. query, item, Inventory[playerid][item]);
  66. }
  67. mysql_format(sqlGameConnection, query, sizeof(query), "%s WHERE ID = %d", query, PlayerInfo[playerid][ID]);
  68. mysql_pquery(sqlGameConnection, query);
  69. ModifiedInventory[playerid] = false;
  70. }
  71. }
  72. return 1;
  73. }
  74. public GetInventory(playerid, e_pInventory:item) {
  75. return Inventory[playerid][item];
  76. }
  77. //Mainly like 'overloading operators', perform things when inventory changes
  78. SetInventory(playerid, e_pInventory:item, oper = '\0', value = 0) {
  79. switch(oper) {
  80. case '+': {
  81. Inventory[playerid][item] += value;
  82. }
  83. case '-': {
  84. Inventory[playerid][item] -= value;
  85. }
  86. case '++': {
  87. Inventory[playerid][item]++;
  88. }
  89. case '--': {
  90. Inventory[playerid][item]--;
  91. }
  92. case '=': {
  93. Inventory[playerid][item] = value;
  94. }
  95. case default: {
  96. Inventory[playerid][item] = 0;
  97. }
  98. }
  99. ModifiedInventory[playerid] = true;
  100. printf("InventoryItemChange - %s (%d): %s is now %d (was %d)", PlayerName(playerid), playerid, GetInventoryName(item), Inventory[playerid][item], Inventory[playerid][item] - value);
  101. return Inventory[playerid][item];
  102. }
  103. //enum ID to inventory name
  104. public GetInventoryName(invid) {
  105. new string[20];
  106. switch(invid) {
  107. case inMaterials: return "Material(s)";
  108. case inCannabis: return "Cannabis";
  109. case inCocaine: return "Cocaine";
  110. case inMeth: return "Meth";
  111. case inPackage: return "Package(s)";
  112. case inCrates: return "Crate(s)";
  113. case inSeeds: return "Seed(s)";
  114. case inProducts: return "Product(s)";
  115. case inRopes: return "Rope(s)";
  116. case inBlindfolds: return "Blindfold(s)";
  117. case inCigars: return "Cigar(s)";
  118. case inSprunk: return "Sprunk";
  119. case inSpraycan: return "Spraycan(s)";
  120. case inScrewdriver: return "Screwdriver(s)";
  121. case inDeckOfCards: return "Deck of Card(s)";
  122. case inWTC: return "wTC";
  123. case inFishingRod: return "Fishing Rod";
  124. case inBait: return "Bait";
  125. case inBaitleft: return "Bait left";
  126. case inFishingLine: return "Fishing Line";
  127. case inStereo: return "Portable Speaker";
  128. case inBoxOfMatches: return "Matches";
  129. case inJerrycan: return "Jerrycan";
  130. case inCellphone: format(string, sizeof(string), "Phone (%d)", inCellphoneN); return string;
  131. default: "Unknown";
  132. }
  133. }