plaguehush

Untitled

Jun 4th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Scriptname zzzContainerWeightCap extends ReferenceAlias
  2.  
  3. Actor Property PlayerRef Auto
  4. Keyword Property VendorItemArrow Auto
  5. Float fMax
  6. Float fCurrent
  7. Bool bScanContainer
  8.  
  9. string[] contweight_10
  10. string[] contweight_25
  11. string[] contweight_30
  12. string[] contweight_50
  13. string[] contweight_75
  14. string[] contweight_100
  15. string[] contweight_125
  16. string[] contweight_150
  17. string[] contweight_175
  18. string[] contweight_200
  19. string[] contweight_225
  20.  
  21.  
  22. Event OnInit()
  23. InitializeArrays()
  24. RegisterForMenu("ContainerMenu")
  25. EndEvent
  26.  
  27.  
  28. Event OnMenuOpen(String MenuName)
  29. If MenuName == "ContainerMenu"
  30. bScanContainer = True
  31. EndIf
  32. EndEvent
  33.  
  34.  
  35. Event OnMenuClose(String MenuName)
  36. If MenuName == "ContainerMenu"
  37. bScanContainer = False
  38. fCurrent = 0.0
  39. EndIf
  40. EndEvent
  41.  
  42. Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
  43. If ( akSourceContainer != None )
  44. WeightChange(False, akBaseItem, aiItemCount, akItemReference, akSourceContainer)
  45. endIF
  46. endEvent
  47.  
  48. Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
  49. If ( akDestContainer != None )
  50. WeightChange(True, akBaseItem, aiItemCount, akItemReference, akDestContainer)
  51. endIf
  52. endEvent
  53.  
  54.  
  55. Function WeightChange(Bool akAdded, Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akContainer)
  56. ; akAdded will be TRUE if an item has been added to a container, and FALSE if the player has retrieved an item from a container
  57.  
  58. Float fItemWeight
  59.  
  60. If ( UI.IsMenuOpen("ContainerMenu") && akContainer != None && akContainer as Actor == None )
  61.  
  62. ;Allow for the fact that arrows have no weight, and calculate item weight * number of items
  63. If ( akBaseItem.HasKeyword(VendorItemArrow) == 1 )
  64. fItemWeight = 0.1*aiItemCount
  65. else
  66. fItemWeight = akBaseItem.GetWeight()*aiItemCount
  67. endIf
  68.  
  69. ;Check to see if this is the first item to be handled since container was opened
  70. ;If so, get the expected maximum weight
  71. If ( bScanContainer == True )
  72. fMax = getMaxWeight(akContainer)
  73. bScanContainer = False
  74.  
  75. ;Get current weight of the container
  76. If ( akAdded == True )
  77. ;On an 'Add' event, handle the fact that it will return current weight with new item(s)
  78. ;added, NOT weight before
  79. fCurrent = weighContentsIn(akContainer) - fItemWeight
  80. Else
  81. ;On a 'Remove' event, the scanned weight will already be missing the removed item(s).
  82. fCurrent = weighContentsIn(akContainer)
  83. endIf
  84. endIf
  85.  
  86. If akAdded ;Item was placed into a container by the player
  87.  
  88. ;Compare the max allowed container weight with the current weight + item weight, and give the item
  89. ;back to the player if it would be too heavy for the container, reducing weight appropriately.
  90. If ( fCurrent + fItemWeight > fMax )
  91. ;Work out where to split the stack (if more than one item was moved)
  92. int iAcceptedItemCount = ( (fMax - fCurrent) / (fItemWeight / aiItemCount) ) As Int
  93. int iRejectedItemCount = ( aiItemCount - iAcceptedItemCount )
  94.  
  95. Debug.Notification("This won't fit")
  96.  
  97. fCurrent += fItemWeight
  98.  
  99. If ( akItemReference != None )
  100. akContainer.RemoveItem(akItemReference, iRejectedItemCount, True, PlayerRef)
  101. Else
  102. akContainer.RemoveItem(akBaseItem, iRejectedItemCount, True, PlayerRef)
  103. endIf
  104.  
  105. Else
  106. fCurrent += fItemWeight
  107. Debug.Notification("Space left in container: " + ( fMax - fCurrent ) as Int + " of " + fMax as Int)
  108. endIf
  109.  
  110. else ;Item was removed from a container by the player.
  111.  
  112. ;If this is the first run since the container weight was scanned, we already have the weight minus the removed items
  113. ;If not, minus the item weight from the container weight.
  114. If ( bScanContainer != True )
  115. fCurrent -= fItemWeight
  116. If fCurrent < 0
  117. fCurrent = 0
  118. endIf
  119. endIf
  120. Debug.Notification("Space left in container: " + ( fMax - fCurrent ) as Int + " of " + fMax as Int)
  121. endIf
  122. endIf
  123. endFunction
  124.  
  125.  
  126. Function InitializeArrays()
  127. ;Set total in [] if adding to a list
  128.  
  129. contweight_10 = new string[1]
  130. contweight_10[0] = "Clutter\\Containers\\Satchel.nif"
  131.  
  132. contweight_25 = new string[4]
  133. contweight_25[0] = "Clutter\\Containers\\MiscSackSmall.nif"
  134. contweight_25[1] = "Clutter\\Containers\\MiscSack02Small.nif"
  135. contweight_25[2] = "Clutter\\Containers\\MiscSack02SmallFlat.nif"
  136. contweight_25[3] = "Clutter\\Common\\StrongBox01.nif"
  137.  
  138. contweight_30 = new string[2]
  139. contweight_30[0] = "Clutter\\WoodFires\\SpitPotClosedLoose01.nif"
  140. contweight_30[1] = "Clutter\\WoodFires\\SpitPotClosed01.nif"
  141.  
  142. contweight_50 = new string[6]
  143. contweight_50[0] = "Architecture\\WhiteRun\\WRClutter\\WRBurialUrn01.nif"
  144. contweight_50[1] = "Clutter\\Sack01.NIF"
  145. contweight_50[2] = "Clutter\\SBurialUrn01.nif"
  146. contweight_50[3] = "Clutter\\Containers\\MiscPouch.nif"
  147. contweight_50[4] = "Clutter\\Ruins\\RuinsCanopicJar01.nif"
  148. contweight_50[5] = "Clutter\\Ruins\\RuinsCanopicJar02.nif"
  149.  
  150. contweight_75 = new string[8]
  151. contweight_75[0] = "Clutter\\Containers\\MiscSackLarge.nif"
  152. contweight_75[1] = "Clutter\\Containers\\MiscSackLargeFlat01.nif"
  153. contweight_75[2] = "Clutter\\Containers\\MiscSackLargeFlat02.nif"
  154. contweight_75[3] = "Clutter\\Containers\\MiscSackLargeFlat03.nif"
  155. contweight_75[4] = "Clutter\\Containers\\MiscSack02Large.nif"
  156. contweight_75[5] = "Clutter\\Containers\\MiscSack02LargeFlat.nif"
  157. contweight_75[6] = "Effects\\FXSpiderWebKitSkeeverDinner.nif"
  158. contweight_75[7] = "Effects\\FXSpiderWebKitDinner.nif"
  159.  
  160. contweight_100 = new string[6]
  161. contweight_100[0] = "Clutter\\Common\\EndTable01.nif"
  162. contweight_100[1] = "Clutter\\Ruins\\RuinsLargeUrnWithStand01.nif"
  163. contweight_100[2] = "Clutter\\Upperclass\\UpperEndTable01.nif"
  164. contweight_100[3] = "Furniture\\Noble\\NobleNightTable01.nif"
  165. contweight_100[4] = "Furniture\\OrcFurniture\\OrcEndTable01.nif"
  166. contweight_100[5] = "_BYOH\\Furniture\\BYOHUpperEndTable02.nif"
  167.  
  168. contweight_125 = new string[27]
  169. contweight_125[0] = "Architecture\\WhiteRun\\WRClutter\\WRCoffin01.nif"
  170. contweight_125[1] = "Architecture\\Riften\\Clutter\\RTCoffin01.nif"
  171. contweight_125[2] = "Clutter\\Barrel01.NIF"
  172. contweight_125[3] = "Clutter\\Common\\Cupboard01.nif"
  173. contweight_125[4] = "Clutter\\Common\\Dresser01.nif"
  174. contweight_125[5] = "Clutter\\Common\\Wardrobe01.nif"
  175. contweight_125[6] = "Clutter\\Common\\CommonCoffin01.nif"
  176. contweight_125[7] = "Clutter\\Containers\\SafeandLock01.nif"
  177. contweight_125[8] = "Clutter\\Dwemer\\DweDresser01.nif"
  178. contweight_125[9] = "Clutter\\Sovngarde\\SovBarrel01.nif"
  179. contweight_125[10] = "Clutter\\Upperclass\\UpperCupboard.nif"
  180. contweight_125[11] = "Clutter\\Upperclass\\UpperDresser01.nif"
  181. contweight_125[12] = "Clutter\\Upperclass\\UpperWardrobe01.nif"
  182. contweight_125[13] = "Clutter\\Windhelm\\WHcoffin01.nif"
  183. contweight_125[14] = "Dungeons\\Dwemer\\Clutter\\MrkCryptCoffin01container.nif"
  184. contweight_125[15] = "Dungeons\\Dwemer\\Clutter\\MrkCryptCoffin02container.nif"
  185. contweight_125[16] = "Furniture\\OrcFurniture\\OrcDresser01.nif"
  186. contweight_125[17] = "Dungeons\\Dwemer\\Clutter\\MrkCryptCoffin01container.nif"
  187. contweight_125[18] = "Dungeons\\Dwemer\\Clutter\\MrkCryptCoffin02container.nif"
  188. contweight_125[19] = "Furniture\\OrcFurniture\\OrcDresser01.nif"
  189. contweight_125[20] = "Furniture\\Noble\\NobleChestDrawers01.nif"
  190. contweight_125[21] = "Furniture\\Noble\\NobleChestDrawers02.nif"
  191. contweight_125[22] = "Furniture\\Noble\\NobleWardrobe01.nif"
  192. contweight_125[23] = "Furniture\\Noble\\SCoffin.nif"
  193. contweight_125[24] = "Furniture\\Noble\\SCoffinPoor.nif"
  194. contweight_125[25] = "DLC02\\Dungeons\\Apocrypha\\Animated\\ApoUrn\\ApoUrn01.nif"
  195. contweight_125[26] = "DLC02\\Clutter\\DLC2DarkElfUrn01.nif"
  196.  
  197. contweight_150 = new string[2]
  198. contweight_150[0] = "Clutter\\Dwemer\\DweContainter02.nif"
  199. contweight_150[1] = "DLC02\\Dungeons\\Apocrypha\\Animated\\ApoUrn\\ApoUrn02.nif"
  200.  
  201. contweight_175 = new string[2]
  202. contweight_175[0] = "Clutter\\Ruins\\Ruins_SmallChest.nif"
  203. contweight_175[1] = "DLC02\\Architecture\\Riekling\\DLC2RieklingTent01.nif"
  204.  
  205. contweight_200 = new string[6]
  206. contweight_200[0] = "Clutter\\Containers\\Chest01\\Chest01.nif"
  207. contweight_200[1] = "Clutter\\Falmer\\FalmerContainer.nif"
  208. contweight_200[2] = "Clutter\\Upperclass\\UpperChest01.nif"
  209. contweight_200[3] = "DLC01\\Clutter\\DLC01SnowElfChest.nif"
  210. contweight_200[4] = "DLC01\\SoulCairn\\sc_chest.nif"
  211. contweight_200[5] = "DLC01\\SoulCairn\\sc_chest02.nif"
  212.  
  213. contweight_225 = new string[5]
  214. contweight_225[0] = "Clutter\\Dwemer\\DweChest01.nif"
  215. contweight_225[1] = "Clutter\\Dwemer\\DweContainer01.nif"
  216. contweight_225[2] = "Clutter\\Falmer\\FalmerContainer02.nif"
  217. contweight_225[3] = "Clutter\\Ruins\\Ruins_LargeChest.nif"
  218. contweight_225[4] = "Furniture\\Noble\\NobleChest01.nif"
  219. endFunction
  220.  
  221.  
  222. Float Function getMaxWeight(ObjectReference oSourceContainer)
  223. ; Decide the max weight of the container
  224.  
  225. string sModelPath = oSourceContainer.GetBaseObject().GetWorldModelPath()
  226.  
  227. If contweight_10.Find(sModelPath) != -1
  228. return 10.0
  229. ElseIf contweight_25.Find(sModelPath) != -1
  230. return 25.0
  231. ElseIf contweight_30.Find(sModelPath) != -1
  232. return 30.0
  233. ElseIf contweight_50.Find(sModelPath) != -1
  234. return 50.0
  235. ElseIf contweight_75.Find(sModelPath) != -1
  236. return 75.0
  237. ElseIf contweight_100.Find(sModelPath) != -1
  238. return 100.0
  239. ElseIf contweight_125.Find(sModelPath) != -1
  240. return 125.0
  241. ElseIf contweight_150.Find(sModelPath) != -1
  242. return 150.0
  243. ElseIf contweight_175.Find(sModelPath) != -1
  244. return 175.0
  245. ElseIf contweight_200.Find(sModelPath) != -1
  246. return 200.0
  247. ElseIf contweight_225.Find(sModelPath) != -1
  248. return 225.0
  249. Else
  250. return 150.0
  251. EndIf
  252. endFunction
  253.  
  254.  
  255. Float Function weighContentsIn(ObjectReference akContainer)
  256. Float iTotal
  257.  
  258. Int iFormIndex = akContainer.GetNumItems()
  259. While iFormIndex > 0
  260. iFormIndex -= 1
  261. Form kForm = akContainer.GetNthForm(iFormIndex)
  262.  
  263. ;Allow for the fact that arrows have no weight, and calculate item weight * number of items
  264. If ( kForm.HasKeyword(VendorItemArrow) == 1 )
  265. iTotal += akContainer.GetItemCount(kForm) * 0.1
  266. else
  267. iTotal += akContainer.GetItemCount(kForm) * kForm.GetWeight()
  268. endIf
  269.  
  270. EndWhile
  271. Return iTotal
  272. endFunction
Add Comment
Please, Sign In to add comment