#side Gnats
#author Matt Burkholder
#date 3/28/2
#color 00f

#type Gnat
#color ff0
#hardware
   processor 15
   engine 0.05
   constructor 1
   energy 100 5
   solar-cells 0.016
   eater 1
   syphon 2
   robot-sensor 13 10
   food-sensor 10 50
   armor 80
   repair-rate 0.05
   blaster 7 2 6

#code

#const gnat 1
#const tree 2
#const Swarm-range 30
#const target-valid-time 100
#const Get-Pregnant 0.2
#const Edge-min 10
#const Edge-max 90
#var build-progress 0
#var target-sender 0
#var target-found 0
#var wander-angle 0
#var destx 0
#var desty
#var LastEnergy 0
#var LastTime 0
#var LastDamage 0
0 1 transmit 0 4 transmit 0 7 transmit
robot-sensor-range random-angle fire-robot-sensor sync
robot-found BFound& ifg sync
CheckComms^

pi 3 / id * dup wander-angle!
0.05 engine-power! 
0.2 wander-angle polar-to-rect set-velocity sync sync sync sync
0 0 fire-food-sensor
Buzz:
do
   50 50 position v- 2dup abs 40 > swap abs 40 > or if
      angle wander-angle! else 2drop random-angle wander-angle! then
   do 
      energy food-sensor-firing-cost > if
         food-sensor-cooldown nif
            food-sensor-range wander-angle fire-food-sensor sync
               food-found if
                  food-position desty! destx!
                  0 engine-power!
               else
                  ;0.05 engine-power!
                  0.2 wander-angle polar-to-rect set-velocity
         then then energy 0.008 - engine-power!
      else 0 engine-power!
      then
   destx until-loop
   do
      destx desty seek-location sync
      CheckDamage^
   destx desty position v- norm 0.6 > while-loop
   robot-sensor-range wander-angle fire-robot-sensor sync
   robot-found BFound& ifg
   time 30 + LastTime!
   do 
      destx desty seek-location sync
      CheckDamage^
   time LastTime < while-loop
   energy 79 min LastEnergy! sync
   do energy LastEnergy > while
      CheckDamage^
      constructor-max-rate constructor-rate!
      gnat constructor-type!
      energy 79 min LastEnergy! sync
   loop
   0 destx! 0 desty!
   0 constructor-rate! 0 repair-rate!
   robot-sensor-range wander-angle fire-robot-sensor sync
   robot-found BFound& ifg
   CheckComms^
forever

CheckDamage:
   armor max-armor < if
      armor LastDamage < if
         robot-sensor-range wander-angle fire-robot-sensor sync
         robot-found BFound& ifg then
      max-repair-rate repair-rate! else 0 repair-rate! then
   armor LastDamage!
return

CheckComms:
   1 receive time > if
      1 target-sender!
      2 receive destx!
      3 receive desty!
      Swarm& jump then
   4 receive time > if
      4 target-sender!
      5 receive destx!
      6 receive desty!
      Swarm& jump then
   7 receive time > if
      7 target-sender!
      8 receive destx!
      9 receive desty!
      Swarm& jump then
return

Swarm:
   destx desty position v- norm Swarm-Range > energy 50 < or ifr
   0 constructor-rate!
do
   destx desty seek-location
   energy robot-sensor-firing-cost < robot-sensor-cooldown or nif
      robot-sensor-range destx desty position v- angle fire-robot-sensor sync 
      robot-found Attack& ifg then
time target-sender receive > until-loop
   0 target-sender!
   Buzz& jump

BFound:
   0 constructor-rate!
   robot-position 2dup desty! destx!
   1 receive time < if
      1 target-found!
      3 transmit 2 transmit
      time target-valid-time + 1 transmit
   Attack& jump then
   4 receive time < if
      4 target-found!
      6 transmit 5 transmit
      time target-valid-time + 4 transmit
   Attack& jump then
   7 receive time < if
      7 target-found!
      9 transmit 8 transmit
      time target-valid-time + 5 transmit
   then

Attack: 
   syphon-max-rate syphon-rate!
   engine-max-power engine-power!
   do
      robot-distance syphon-range min robot-direction set-syphon
      energy blaster-firing-cost > robot-distance blaster-range < and if
         robot-direction fire-blaster then
      robot-distance 1.2 - robot-direction polar-to-rect set-velocity
      armor max-armor < max-repair-rate 0 ifev repair-rate!
    
      blaster-range robot-direction fire-robot-sensor sync
   robot-found while-loop
   target-found if
      0 target-found transmit
      0 target-found! then
   0 syphon-rate!
Buzz& jump

#end
