#side Fighters
#author Devon
16 February 2002
#color 999

Defends itself with small blaster users.
Could have multiple primary types (algae and gatherers?)

How does it decide whether to make fighters?

(20020410) fixes for changes
(20021002) Fix for communications renaming. Switched to messages.

#type algae
#color 0f0

#hardware
  processor 5
  energy 20 5
  solar-cells .4
  constructor .4
  
  armor 100

#code

do
  constructor-type nif
    1 2 random-int constructor-type!
  then
  energy 10 > if
    constructor-max-rate constructor-rate!
  else
    0 constructor-rate!
  then
forever

#type fighter
#color f0d

Communicates, to help find targets.

(20021003) Cut repair rate in half and increased solar cells, to reduce starvation.
(20021025) Sends time on messages, and ignores old ones.
(20021025) Reduced range and made it a little smarter.
(20021026) Better steering.

#hardware
  processor 20
  engine .07
  
  solar-cells .15   ;c 270
  energy 100 30
  
  armor 150
  repair-rate .1   ;c 75
  
  robot-sensor 15 1
  blaster 15 1 15
  radio send receive

#code

do
  energy 10 > if
    scan^
  else
    0 engine-power!
  then
  sync
forever

#var next-scan 0
#const scan-interval 53
#const fighting-range 2

scan:
  time next-scan < ifr
  fire-robot-sensor sync
  robot-found if
    energy 20 > if
      robot-position robot-velocity robot-distance blaster-speed / vs* v+
        position v-
      robot-distance fighting-range > if
        engine-max-power engine-power!
        2dup unitize 5 vs/ engine-velocity!
      else
        0 engine-power!
      then
      robot-distance robot-radius - radius 2.5 * <= if
        angle fire-blaster
      else
        2drop
      then
    then
    robot-position time 3 1 send
    time blaster-reload-time + next-scan!
  else
    ;use radio
    0 engine-power!
    do 1 messages while
      energy 40 > if
        1 receive drop
        60 + time >= if
          position v-
          2dup norm fighting-range > if
            engine-max-power engine-power!
            2dup engine-velocity!
          then
        then
        2drop
      then
    loop
    time scan-interval + next-scan!
  then
return

#end

