Counter Arithmetic

From Staredit Network Wiki
Jump to: navigation, search

It is possible to do basic mathematical operations with triggers. Refer to the map Binary Calculator for an example of add, subtract, multiply, and divide.

Binary Countoffs can be used to speed up these calculations.


Addition

Adding two counters is fairly simple. All you need to do is subtract from one counter while adding to the other. As an example, suppose you have two counters, minerals and gas, set to 10 and 8 respectively, and you need to add gas to minerals. It is possible to do this with a single trigger.

  • Conditions:
    • Current Player accumulates at least 1 gas.
  • Actions:
    • Preserve trigger.
    • Modify resources for Current Player: Subtract 1 gas.
    • Modify resources for Current Player: Add 1 ore.

This trigger will need to be allowed to run untill gas reaches zero in order to get the correct sum. The use of hyper triggers will speed up this process greatly.

If you need to have this operation finish in one trigger cycle, you can copy this trigger many times, or use a binary countoff.


Subtraction

Subtraction can be done similarly to addition. Instead of subtrating from one counter and adding to the other, subtract from both counters. A problem with this method of subtraction is that it will not handle negative numbers. If the subtrahend is larger than the minuend, the result will incorrectly be zero.

Subtraction with Negatives

Since negative values are common when dealing with subtraction, and StarCraft's counter system won't deal with them, your system will need to be able to.

The above subtraction method, with no regard for negative numbers, will attempt to keep subtracting when it is not possible to do so. For example, if you're trying to subtract 5 from 2, the system will attempt to subtract one from the result thee times when it is already zero, which will not change it.

To counter this, simply prevent the system from doing it. When the number being subtracted from reaches zero, stop. The other counter will still have a positive value. If you use your imagination and pretend that this value is negative, then this is your result.

Multiplication

For this method of multiplication, you will need additional counters. Check if either of the numbers are 0 before you start, as this indicates that the result will be zero.

If the numbers (minerals and gas) are non-zero, then do the following operations:

  1. Add the amount of gas to a Result counter and to a Storage counter.
  2. Subtract 1 from minerlals.
  3. Use the Storage counter to restore the amount of gas.
  4. Repeat steps these untill the amount of minerals is zero.

For this operation to happen in a reasonable amount of time, you will almost certainly need to use binary countoffs. If the numbers are very large, it may also be necessary to copy the entire system a few times over to speed up the calculation.

Division

Divide is similar to multiplication.

Just subtract the denominator from the numerator, and add 1 to the result. Repeat this untill the numerator reaches zero. You can store the amount subtraced during the last cycle in another counter if you want a remainder.