8.14
1.3.2 Cargos
Source code at cargos.rkt
Cargos are the blocks that bots are carrying.
These are stored on the server in a hash table.
The key is the bot id, the value is the block entity.
(struct cargos (hash)) (define (make-cargos) (cargos (make-hash)))
We can load cargo for a bot and retrieve cargo for a bot.
(test-case "load and retrieve" (let ([cargos (make-cargos)] [block (entity 102 type-block (location 1 2))]) (load-cargo cargos 101 block) (check-equal? (cargo-for-bot cargos 101) block)))
The functions are basic hash table operations.
(define (cargo-for-bot cargos id) (hash-ref (cargos-hash cargos) id #f)) (define (load-cargo cargos id entity) (hash-set! (cargos-hash cargos) id entity))
When we unload cargo, the block is removed from the table and returned for later use.
(test-case "unload" (let ([cargos (make-cargos)] [block (entity 102 type-block (location 1 2))]) (load-cargo cargos 101 block) (check-equal? (unload-cargo cargos 101) block) (check-false (cargo-for-bot cargos 101))))
Another basic hash table operation.
(define (unload-cargo cargos id) (let ([entity (cargo-for-bot cargos id)]) (when entity (hash-remove! (cargos-hash cargos) id)) entity))