Skip to content

Commit

Permalink
Halve output goo after summing instead of summing the halves
Browse files Browse the repository at this point in the history
# What

We were summing up halves of the inputs which meant we lost all fractional parts due to working with integers so `(5/2) + (5/2) = 4`. By summing and then halving we get the expected output `(5 + 5) / 2 = 5`
  • Loading branch information
hypnoshock committed Nov 3, 2023
1 parent fb12d98 commit 3e4778b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions contracts/src/rules/BuildingRule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,19 @@ contract BuildingRule is Rule {
} else {
require(inputQty[i] == 1, "equipable input item must have qty=1");
}
availableInputAtoms[0] = availableInputAtoms[0] + (inputAtoms[0] * uint32(inputQty[i])) / 2;
availableInputAtoms[1] = availableInputAtoms[1] + (inputAtoms[1] * uint32(inputQty[i])) / 2;
availableInputAtoms[2] = availableInputAtoms[2] + (inputAtoms[2] * uint32(inputQty[i])) / 2;
availableInputAtoms[0] += inputAtoms[0] * uint32(inputQty[i]);
availableInputAtoms[1] += inputAtoms[1] * uint32(inputQty[i]);
availableInputAtoms[2] += inputAtoms[2] * uint32(inputQty[i]);
}

// Halve the available goo.
// NOTE: At a cost of some extra gas we halve the sum of atoms instead of summing the halves. Because we
// are dealing with integers (5/2) + (5/2) would equal 4 instead of 5.
// If we REALLY needed to gas golf perhaps bit shifting once right would be cheaper than dividing by 2?
availableInputAtoms[0] /= 2;
availableInputAtoms[1] /= 2;
availableInputAtoms[2] /= 2;

// require that the total number of each atom in the total number of
// output items is less than half of the total input of each atom
require(
Expand Down

0 comments on commit 3e4778b

Please sign in to comment.