Inventory Compression Tool

A UD script that compresses the inventory. If there is more than one of an item, it is compressed to one button. Firearms still list individual ammo counts as well as total loaded ammo.

  1. // ==UserScript==
  2. // @name Inventory Compression Tool
  3. // @namespace http://www.ray3k.com/
  4. // @description A UD script that compresses the inventory. If there is more than one of an item, it is compressed to one button. Firearms still list individual ammo counts as well as total loaded ammo.
  5. // @include http://www.urbandead.com/map.cgi*
  6. // @include http://urbandead.com/map.cgi*
  7. // @version 0.0.1.20160405031732
  8. // ==/UserScript==
  9.  
  10. //the inventory is NOT compressed when the script starts
  11. GM_setValue("inventoryIsCompressed", false);
  12.  
  13. //get the drop select menu
  14. var allDropSelects, dropSelect;
  15. allDropSelects = document.evaluate(
  16. "//select[@name='drop']",
  17. document,
  18. null,
  19. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  20. null);
  21. dropSelect = allDropSelects.snapshotItem(0);
  22.  
  23. //get the drop button
  24. var allDropInputs, dropInput;
  25. allDropInputs = document.evaluate(
  26. "//input[@value='Drop']",
  27. document,
  28. null,
  29. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  30. null);
  31. dropInput = allDropInputs.snapshotItem(0);
  32.  
  33. //if item organization is allowed
  34. compressInventory();
  35.  
  36. function findDropIndex(itemName) {
  37. for (i = 0; i < dropSelect.options.length; i++) {
  38. if (dropSelect.options[i].text.toLowerCase() == itemName.toLowerCase()) {
  39. return i;
  40. }
  41. else if (itemName.toLowerCase() == "radio" && dropSelect.options[i].text.toLowerCase().match("radio")) {
  42. return i;
  43. }
  44. }
  45. return -1;
  46. }
  47.  
  48. function calcItemQuantity(itemName) {
  49. var itemCount = 0;
  50.  
  51. var allInputs;
  52. allInputs = document.evaluate(
  53. "//input[@value='" + itemName + "']",
  54. document,
  55. null,
  56. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  57. null);
  58. var thisInput = allInputs.snapshotItem(0);
  59.  
  60. //check if the inventory has been compressed
  61. if (checkForInventoryCompression() && thisInput) {
  62. itemCount = thisInput.parentNode.textContent;
  63. itemCount = parseInt(itemCount.match(/\d/));
  64. }
  65. else {
  66. itemCount = allInputs.snapshotLength;
  67. }
  68. return itemCount;
  69. }
  70.  
  71. function checkForInventoryCompression() {
  72. if (GM_getValue("setting_compressInventory") && GM_getValue("inventoryIsCompressed", false)) {
  73. return true;
  74. }
  75. else {
  76. return false;
  77. }
  78. }
  79.  
  80. //Modifies the inventory so that each item is only listed once with a quantity
  81. function compressInventory() {
  82. var itemName, foundShotgun, foundPistol;
  83. foundShotgun = false;
  84. foundPistol = false;
  85. //foundRadio = false;
  86.  
  87. //go through each item listed in the drop menu
  88. for (var i = 0; i < dropSelect.options.length; i++) {
  89. //get the name of the current item
  90. itemName = dropSelect.options[i].text;
  91. //if item is a shotgun
  92. if (itemName.match("shotgun") && !itemName.match("shell")) {
  93. //if shotgun has been found earlier
  94. if (foundShotgun == true) {
  95. //do not compress this item any further
  96. itemName = "cancel";
  97. }
  98. else {
  99. //change the item name to the correct format
  100. itemName = "shotgun";
  101. foundShotgun = true;
  102. }
  103. }
  104. //if item is a pistol
  105. if (itemName.match("pistol") && !itemName.match("clip")) {
  106. //if pistol has been found earlier
  107. if (foundPistol == true) {
  108. //do not compress this item any further
  109. itemName = "cancel";
  110. }
  111. else {
  112. //change the item name to the correct format
  113. itemName = "pistol";
  114. foundPistol = true;
  115. }
  116. }
  117. //if item is a radio
  118. if (itemName.match("radio")) {
  119. itemName = "radio";
  120. }
  121. //if the item is in the inventory and is a valid name
  122. if (itemName != "cancel" && calcItemQuantity(itemName) > 0) {
  123. compressInventoryItem(itemName);
  124. }
  125. }
  126. //the inventory has been compressed
  127. GM_setValue("inventoryIsCompressed", true);
  128. }
  129.  
  130. //compresses a single inventory item
  131. function compressInventoryItem(itemName) {
  132. //get the quantity of the item
  133. var quantity = calcItemQuantity(itemName);
  134. //find the item buttons
  135. var allInputs, itemInput, itemForm;
  136. allInputs = document.evaluate(
  137. "//input[@value='" + itemName + "']",
  138. document,
  139. null,
  140. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  141. null);
  142. //if the item is actually in the inventory
  143. if (allInputs.snapshotLength > 0) {
  144. var ammo, thisAmmo, weaponList, isWeapon;
  145. //if it is an ammo weapon
  146. if (itemName == "shotgun" || itemName == "pistol") {
  147. ammo = 0;
  148. weaponList = "";
  149. isWeapon = true;
  150. }
  151. else {
  152. ammo = "";
  153. weaponList = "";
  154. isWeapon = false;
  155. }
  156. //go through each item button after the first one
  157. for (var i = 1; i < allInputs.snapshotLength; i++) {
  158. //get the current button
  159. itemInput = allInputs.snapshotItem(i);
  160. //get the button's corresponding form
  161. itemForm = itemInput.parentNode;
  162.  
  163. if (isWeapon) {
  164. //get the amount of ammo
  165. thisAmmo = itemForm.textContent.replace(/\D/g, "");
  166. //add this weapon's ammo to the list
  167. weaponList += ", " + thisAmmo;
  168. //increase the total ammo
  169. ammo += parseInt(thisAmmo);
  170. }
  171. //delete the form including the button
  172. itemForm.parentNode.removeChild(itemForm);
  173. }
  174. //transform the first item button
  175. itemInput = allInputs.snapshotItem(0);
  176. itemForm = itemInput.parentNode;
  177. if (isWeapon) {
  178. //format the form as a weapon
  179. thisAmmo = itemForm.textContent.replace(/\D/g, "");
  180. weaponList = thisAmmo + weaponList;
  181. ammo += parseInt(thisAmmo);
  182. itemForm.innerHTML = "";
  183. itemForm.appendChild(itemInput);
  184. itemForm.innerHTML = quantity + " x " + itemForm.innerHTML + "(" + weaponList + ") at " + ammo + " shots";
  185. }
  186. else {
  187. //format the form as a normal item
  188. itemForm.innerHTML = quantity + " x " + itemForm.innerHTML;
  189. }
  190. }
  191. }