splitting too big ammo boxes upon changing ammo; also, properly dropping too much ammo after same action

This commit is contained in:
FateJH 2018-05-17 18:32:18 -04:00
parent 2aabf9abe7
commit c838ff3168
2 changed files with 82 additions and 19 deletions

View file

@ -40,6 +40,8 @@ object AmmoBox {
* Accepting an `AmmoBox` object that has an uncertain amount of ammunition in it,
* create multiple `AmmoBox` objects where none contain more than the maximum capacity for that ammunition type,
* and the sum of all objects' capacities is the original object's capacity.
* The first element in the returned value is always the same object as the input object.
* Even if the original ammo object is not split, a list comprised of that same original object is returned.
* @param box an `AmmoBox` object of unspecified capacity
* @return a `List` of `AmmoBox` objects with correct capacities
*/
@ -48,7 +50,8 @@ object AmmoBox {
val boxCap : Int = box.Capacity
val maxCap : Int = ammoDef.Capacity
val splitCap : Int = boxCap / maxCap
val list : List[AmmoBox] = List.fill(splitCap)(new AmmoBox(ammoDef))
box.Capacity = math.min(box.Capacity, maxCap)
val list : List[AmmoBox] = if(splitCap == 0) { Nil } else { box +: List.fill(splitCap - 1)(new AmmoBox(ammoDef)) }
val leftover = boxCap - maxCap * splitCap
if(leftover > 0) {
list :+ AmmoBox(ammoDef, leftover)