9ab57657d815841068dd9593b0aeb0a696dc4ea4
[8sync.git] / demos / actors / robotscanner.scm
1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2016 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of 8sync.
5 ;;;
6 ;;; 8sync is free software: you can redistribute it and/or modify it
7 ;;; under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation, either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; 8sync is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU Lesser General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with 8sync.  If not, see <http://www.gnu.org/licenses/>.
18
19 ;;; =====================================================================
20 ;;; Robot Scanner test demo (from XUDD, originally).
21 ;;; 
22 ;;; Here's the premise.  There's a warehouse full of droids, some
23 ;;; infected, and some not.  The SecurityRobot is being sent in to clean
24 ;;; up the mess.  It's capable of sending a message that infected droids
25 ;;; are susceptible to responding in a predictable way.  Once it has
26 ;;; identified that a droid is infected, it shoots it full of holes till
27 ;;; the droid is terminated.  The SecurityRobot goes from room to room
28 ;;; till things are cleared out.
29 ;;; 
30 ;;; Overseeing the operation is the "overseer".  The security robot keeps
31 ;;; the overseer up to date on its progress as it goes.  (For this demo,
32 ;;; the overseer is also responsible for initializing the world and
33 ;;; reporting info back to the user.)
34 ;;; =====================================================================
35
36 (use-modules (8sync systems actors)
37              (oop goops)
38              (ice-9 match))
39
40 (set! *random-state* (random-state-from-platform))
41
42 (define room-structure
43   ;; A list of (clean-droids infected-droids)
44   '((3 1)
45     (0 2)
46     (8 5)
47     (5 0)
48     (2 1)))
49
50 (define-simple-actor <overseer>
51   (init-world
52    (lambda (actor message)
53      ;; Porting mostly straight up from super-imperative XUDD code.
54      (define previous-room #f)
55      (define first-room #f)
56
57      ;; Set up all rooms
58      (for-each
59       (match-lambda
60         ((clean-droids infected-droids)
61          ;; Create this room
62          (define room (create-actor* actor <warehouse-room> "room"))
63          (define* (init-droid #:key infected)
64            (define droid (create-actor* actor <droid> "droid"
65                                         #:infected infected
66                                         #:room room))
67            (<-wait actor droid 'register-with-room))
68
69          ;; Link rooms.
70          ;; Couldn't this just be folded into the warehouse room init?
71          ;; I guess it stress tests more the message sending process
72          (when previous-room
73            (<- actor previous-room 'set-next-room
74                #:id room)
75            (<- actor room 'set-previous-room
76                #:id previous-room))
77
78          ;; Set up clean droids in the room
79          (for-each
80           (lambda _
81             (init-droid #:infected #f))
82           (iota clean-droids))
83
84          ;; Set up infected droids in the room
85          (for-each
86           (lambda _
87             (init-droid #:infected #t))
88           (iota clean-droids))
89
90          (set! previous-room room)
91          (if (not first-room)
92              (set! first-room room))))
93       room-structure)
94
95      ;; Add security robot
96      (let ((security-robot
97             (create-actor actor <security-robot>)))
98        (<- actor security-robot 'begin-mission
99            #:starting-room first-room
100            #:overseer (actor-id actor)))))
101
102   (transmission
103    (lambda* (actor message #:key text)
104      (display text)
105      (newline))))
106
107
108 ;;; A room full of robots.
109 (define-class <warehouse-room> (<actor>)
110   (droids #:init-value '())
111   (next-room #:init-value #f)
112   (previous-room #:init-value #f)
113
114   (actions
115    #:allocation #:each-subclass
116    #:init-value
117    (build-actions
118     (set-next-room
119      (lambda* (actor message #:key id)
120        "Set the room following this"
121        (slot-set! actor 'next-room id)))
122
123     (set-previous-room
124      (lambda* (actor message #:key id)
125        "Set the room previous to this"
126        (slot-set! actor 'previous-room id)))
127
128     (get-next-room
129      (lambda (actor message)
130        "Return a reference to the link following this"
131        (<-reply actor message (slot-ref actor 'next-room))))
132
133     (get-previous-room
134      (lambda (actor message)
135        "Return a reference to the link preceding this"
136        (<-reply actor message (slot-ref actor 'previous-room))))
137
138     (list-droids
139      (lambda (actor message)
140        "Return a list of all the droid ids we know of in this room"
141        (<-reply actor message
142                 #:droid-ids (slot-ref actor 'droids))))
143
144     (register-droid
145      (lambda* (actor message #:key droid-id)
146        "Register a droid as being in this room"
147        (slot-set! actor 'droids
148                   (cons droid-id
149                         (slot-ref actor 'droids))))))))
150
151
152 ;;; A droid that may or may not be infected!
153 ;;; What will happen?  Stay tuned!
154 (define-class <droid> (<actor>)
155   (infected #:init-keyword #:infected)
156   (room #:init-keyword #:room)
157   (hp #:init-value 50)
158
159   (actions
160    #:allocation #:each-subclass
161    #:init-value
162    (build-actions
163     (register-with-room
164      (lambda (actor message)
165        "Register ourselves as being in a room"
166        (let ((room-id (slot-ref actor 'room)))
167          (<-wait actor room-id
168                  'register-droid
169                  #:droid-id (actor-id actor))
170          (format #t "Droid ~a registered with room ~a\n"
171                  (actor-id-actor actor)
172                  (address-actor-id room-id)))))
173
174     (infection-expose
175      (lambda (actor message)
176        "Leak whether or not we're infected to a security droid"
177        (<-reply actor message (slot-ref actor 'infected))))
178
179     (get-shot
180      (lambda (actor message)
181        "Get shot by bullets"
182        (let* ((damage (random 60))
183               (new-hp (- (slot-ref actor 'hp) damage))
184               (alive (> new-hp 0)))
185          ;; Set our health to the new value
186          (slot-set! actor 'hp new-hp)
187          (<-reply actor message
188                   #:hp-left new-hp
189                   #:damage-taken damage
190                   #:alive alive)
191          (when (not alive)
192            (format #t "~a: *Kaboom!*\n" (actor-id-actor actor))
193            (self-destruct actor))))))))
194
195
196 (define (droid-status-format shot-response)
197   (call-with-message
198    shot-response
199    (lambda* (_ #:key alive damage-taken hp-left)
200      (if alive
201          (format #f "Droid ~a shot; taken ~a damage. Still alive... ~a hp left."
202                  (address-actor-id (message-from shot-response))
203                  damage-taken hp-left)
204          (format #f "Droid ~a shot; taken ~a damage. Terminated."
205                  (address-actor-id (message-from shot-response))
206                  damage-taken)))))
207
208
209 ;;; Security robot... designed to seek out and destroy infected droids.
210 (define-simple-actor <security-robot>
211   (begin-mission security-robot-begin-mission))
212
213 (define* (security-robot-begin-mission actor message
214                                        #:key starting-room overseer)
215   ;; used to track the current room / if any rooms are remaining
216   (define room starting-room)
217
218   ;; Walk through all rooms, clearing out infected droids
219   ;; Continue this whil there's still another room to investigate.
220   (define response)
221   (while room
222     (<- actor overseer 'transmission
223         #:text (format #f "Entering room ~a..."
224                        (address-actor-id room)))
225
226     ;; Find all droids in this room and exterminate the infected ones.
227     (msg-receive (_ #:key list-droids droid-ids #:allow-other-keys)
228         (<-wait actor room 'list-droids)
229       (for-each
230        (lambda (droid-id)
231          (cond
232           ;; Looks like it's infected
233           ((msg-val (<-wait actor droid-id 'infection-expose))
234            ;; Inform that it's infected
235            (<- actor overseer 'transmission
236                #:text (format #f "~a found to be infected... taking out"
237                               (address-actor-id droid-id)))
238
239            ;; Keep firing till it's dead.
240            (let ((still-alive #t))
241              (while still-alive
242                (msg-receive (response #:key alive #:allow-other-keys)
243                    (<-wait actor droid-id 'get-shot)
244                  (<- actor overseer 'transmission
245                      #:text (droid-status-format response))
246                  (set! still-alive alive)))))
247
248           ;; Not infected... inform and go to the next one
249           (else
250            (<- actor overseer 'transmission
251                #:text
252                (format #f "~a is clean... moving on."
253                        (address-actor-id droid-id))))))
254        droid-ids))
255
256     ;; Switch to next room, if there is one.
257     (set! room (msg-val (<-wait actor room 'get-next-room))))
258
259   ;; Good job everyone!  Shut down the operation.
260   (<- actor overseer 'transmission
261       #:text "Mission accomplished."))
262
263 (define (main . args)
264   (define hive (make-hive))
265   (define overseer (hive-create-actor hive <overseer>))
266   (define initial-messages
267     (list (bootstrap-message hive overseer 'init-world)))
268   (ez-run-hive hive initial-messages))