1.3 The Code
1.3.2 Client
1.3.2.1 Connection
1.3.2.2 Bot
1.3.2.3 Step
1.3.2.4 Tactics
1.3.2.5 Wandering
1.3.2.6 TCP Client
1.3.2.7 Viewer
1.3.2.8 TCP Viewer
8.14
1.3.2.4 Tactics🔗

Source code at tactics.rkt

This is a collection of functions for use in implementing an action strategy.

The direction change chance is the chance of making a random direction change when a bot is wandering around the world. This is set to a 20% chance.

(define direction-change-chance (make-parameter 0.2))

The choice structure is used to return information about what a strategy has chosen for the next action.

(struct choice (type parameter direction))

When a strategy chooses to move a bot, the choice parameter is the destination.

(test-case:
 "choose move"
 (let ([choice (choose-move direction-west direction-east
                            (step #f request-move #f #t (bot #f (location 2 1) #f #f)))])
   (check-equal? (choice-type choice) request-move)
   (check-equal? (choice-parameter choice) (location 1 1))
   (check-equal? (choice-direction choice) direction-west)))
(define (move-failed? step)
  (and (equal? (step-request-type step) request-move)
                  (not (step-success? step))))
(define (choose-move chosen-direction current-direction step)
  (let ([direction
         (if (move-failed? step)
             (change-direction (step-bot step) current-direction)
             chosen-direction)])
    (choice request-move
            (direction (step-location step))
            direction)))

When a strategy chooses to take a block, the choice parameter is block id. The next move direction is the direction to the block.

(test-case:
 "choose take"
 (let ([choice (choose-take
                (bot (entity 101 type-bot) (location 1 1) #f #f)
                (occupant (entity 102 type-block) (location 1 2)))])
   (check-equal? (choice-type choice) request-take)
   (check-equal? (choice-parameter choice) 102)
   (check-equal? (choice-direction choice) direction-north)))
(define (choose-take bot block)
  (let ([take-direction (direction-from (bot-location bot) (occupant-location block))])
    (choice request-take (occupant-id block) take-direction)))

When a strategy chooses to transfer a block to a base, the choice parameter is the base id. The next move direction is away from the base.

(test-case:
 "choose transfer"
 (let* ([base (occupant (entity 102 type-base) (location 1 2))]
        [choice (choose-transfer
                 (bot (entity 101 type-bot) (location 1 1) #f (list base))
                 base)])
   (check-equal? (choice-type choice) request-transfer)
   (check-equal? (choice-parameter choice) 102)
   (check-equal? (choice-direction choice) direction-south)))
(define (choose-transfer bot base)
  (choice request-transfer (occupant-id base)
          (direction-from (occupant-location base) (bot-location bot))))