quantityconversions.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. class QuantityConversions
  2. {
  3. static string GetItemQuantityText( EntityAI item, bool showMax = false )
  4. {
  5. string quantity_text = "";
  6. if ( item.IsInherited( InventoryItem) )
  7. {
  8. ItemBase item_base;
  9. Class.CastTo(item_base, item);
  10. float quantity = item_base.GetQuantity();
  11. int ammo;
  12. if ( item.IsMagazine() )
  13. {
  14. Magazine magazine_item;
  15. Class.CastTo(magazine_item, item);
  16. ammo = magazine_item.GetAmmoCount();
  17. return ammo.ToString();
  18. }
  19. else if ( item.IsInherited( ItemBook) )
  20. {
  21. return "";
  22. }
  23. int stack_max = item.GetQuantityMax();
  24. //int max = item.ConfigGetInt("varQuantityMax");
  25. //string unit = item.ConfigGetString("stackedUnit");
  26. if (stack_max > 0)
  27. {
  28. if (stack_max == 1)
  29. {
  30. if (quantity > 1)
  31. {
  32. if (showMax)
  33. quantity_text = string.Format("%1/%2", quantity.ToString(), stack_max.ToString() );
  34. //quantity_text = string.Format("%i/%i", quantity, stack_max );
  35. else
  36. quantity_text = quantity.ToString();
  37. }
  38. else
  39. {
  40. quantity_text = "";
  41. }
  42. }
  43. else
  44. {
  45. if (showMax)
  46. quantity_text = string.Format("%1/%2", quantity.ToString(), stack_max.ToString() );
  47. //quantity_text = string.Format("%i/%i", quantity, stack_max );
  48. else
  49. quantity_text = quantity.ToString();
  50. // if (unit == "ml")
  51. // {
  52. // float liters = round(quantity / 1000.0);
  53. // if ( quantity < 2000 )
  54. // {
  55. // liters = ( round( (quantity / 100.0) ) ) / 10;
  56. // }
  57. // quantity_text = ftoa(liters) + "l";
  58. // }
  59. // else
  60. // {
  61. // quantity_text = itoa(quantity) + unit;
  62. // }
  63. }
  64. }
  65. }
  66. return quantity_text;
  67. }
  68. static float GetItemQuantity(InventoryItem item)
  69. {
  70. float quantity = 0;
  71. if ( item.IsInherited(InventoryItem))
  72. {
  73. ItemBase item_base;
  74. Class.CastTo(item_base, item);
  75. if (item_base)
  76. {
  77. if (item.IsMagazine())
  78. {
  79. Magazine magazine_item;
  80. Class.CastTo(magazine_item, item);
  81. quantity = magazine_item.GetAmmoCount();
  82. }
  83. else
  84. {
  85. quantity = item_base.GetQuantity();
  86. }
  87. }
  88. }
  89. return quantity;
  90. }
  91. static float GetItemQuantityMax(InventoryItem item)
  92. {
  93. float quantity = 0;
  94. if (item.IsInherited(InventoryItem))
  95. {
  96. ItemBase item_base;
  97. Class.CastTo(item_base, item);
  98. if (item_base)
  99. {
  100. if (item.IsMagazine())
  101. {
  102. Magazine magazine_item;
  103. Class.CastTo(magazine_item, item);
  104. quantity = magazine_item.GetAmmoMax();
  105. }
  106. else
  107. {
  108. quantity = item_base.GetQuantityMax();
  109. }
  110. }
  111. }
  112. return quantity;
  113. }
  114. static void GetItemQuantity( InventoryItem item, out float q_cur, out float q_min, out float q_max )
  115. {
  116. if ( item.IsInherited( InventoryItem ) )
  117. {
  118. ItemBase item_base;
  119. Class.CastTo(item_base, item);
  120. if ( item.IsMagazine() )
  121. {
  122. Magazine magazine_item;
  123. Class.CastTo(magazine_item, item);
  124. q_cur = magazine_item.GetAmmoCount();
  125. q_min = 0;
  126. q_max = magazine_item.GetAmmoMax();
  127. }
  128. else
  129. {
  130. q_cur = item_base.GetQuantity();
  131. q_min = item_base.GetQuantityMin();
  132. q_max = item_base.GetQuantityMax();
  133. }
  134. }
  135. }
  136. static int HasItemQuantity( notnull EntityAI item )
  137. {
  138. ItemBase ib;
  139. if ( Class.CastTo(ib, item) )
  140. {
  141. if ( item.IsMagazine() )
  142. return QUANTITY_COUNT;
  143. if ( !ib.m_CanShowQuantity )
  144. return QUANTITY_HIDDEN;
  145. int max = item.GetQuantityMax();
  146. if ( max > 0 )
  147. {
  148. if ( ib.m_HasQuantityBar )
  149. {
  150. return QUANTITY_PROGRESS;
  151. }
  152. else
  153. {
  154. if (max == 1)
  155. {
  156. return QUANTITY_HIDDEN;
  157. }
  158. else
  159. {
  160. return QUANTITY_COUNT;
  161. }
  162. }
  163. }
  164. }
  165. return QUANTITY_HIDDEN;
  166. }
  167. }