Example #1
0
 /**
  * Splice a mutation into these income slots. The mutation will split
  * one slot in twain, and affect all slots after its mutation point.
  * @param Mutation $mutation
  */
 public function splice($mutation)
 {
     if ($this->debug) {
         tracemsg("IncomeSlots::splice(" . $mutation . " @ " . simple_time($mutation->time) . ")");
     }
     if (isset($mutation->delay)) {
         // find negative splice point
         for ($i = 0; $i < count($this->_slots); $i++) {
             $slot = $this->_slots[$i];
             if ($slot->endTime === INF || $slot->endTime > $mutation->time) {
                 $newSlot = clone $slot;
                 $newSlot->start($mutation->time);
                 $slot->endTime = $mutation->time;
                 array_splice($this->_slots, $i + 1, 0, array($newSlot));
                 $spliceSlot = $i + 1;
                 break;
             }
         }
         // update income beyond negative splice point
         for ($i = $spliceSlot; $i < count($this->_slots); $i++) {
             $mutation->applyNegative($this->_slots[$i]);
         }
         // find positive splice point
         for ($i = 0; $i < count($this->_slots); $i++) {
             $slot = $this->_slots[$i];
             if ($slot->endTime === INF || $slot->endTime > $mutation->time + $mutation->delay) {
                 $newSlot = clone $slot;
                 $newSlot->start($mutation->time + $mutation->delay);
                 $slot->endTime = $mutation->time + $mutation->delay;
                 array_splice($this->_slots, $i + 1, 0, array($newSlot));
                 $spliceSlot = $i + 1;
                 break;
             }
         }
         // update income beyond positive splice point
         for ($i = $spliceSlot; $i < count($this->_slots); $i++) {
             $mutation->applyPositive($this->_slots[$i]);
         }
     } else {
         // find splice point
         for ($i = 0; $i < count($this->_slots); $i++) {
             $slot = $this->_slots[$i];
             if ($slot->endTime === INF || $slot->endTime > $mutation->time) {
                 $newSlot = clone $slot;
                 $newSlot->start($mutation->time);
                 $slot->endTime = $mutation->time;
                 array_splice($this->_slots, $i + 1, 0, array($newSlot));
                 $spliceSlot = $i + 1;
                 break;
             }
         }
         // update income beyond splice point
         for ($i = $spliceSlot; $i < count($this->_slots); $i++) {
             $mutation->apply($this->_slots[$i]);
         }
     }
 }