1d120072380ad17d49b276c016beba8a729ace47
[mudsync.git] / mudsync / gameobj.scm
1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of Mudsync.
5 ;;;
6 ;;; Mudsync is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or
9 ;;; (at your option) any later version.
10 ;;;
11 ;;; Mudsync is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;;; General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with Mudsync.  If not, see <http://www.gnu.org/licenses/>.
18
19 ;;; Game actor
20 ;;; ==========
21
22 (define-module (mudsync gameobj)
23   #:use-module (mudsync command)
24   #:use-module (8sync systems actors)
25   #:use-module (8sync agenda)
26   #:use-module (srfi srfi-1)
27   #:use-module (ice-9 match)
28   #:use-module (oop goops)
29   #:export (<gameobj>
30             gameobj-simple-name-f
31
32             gameobj-loc
33             gameobj-gm
34             gameobj-name
35             gameobj-name-f
36
37             gameobj-actions))
38
39 ;;; Gameobj
40 ;;; =======
41
42
43 ;;; Actions supported by all gameobj
44 (define gameobj-actions
45   (build-actions
46    (init (wrap-apply gameobj-init))
47    (get-commands (wrap-apply gameobj-get-commands))
48    (get-container-commands (wrap-apply gameobj-get-container-commands))
49    (get-occupants (wrap-apply gameobj-get-occupants))
50    (add-occupant! (wrap-apply gameobj-add-occupant!))
51    (remove-occupant! (wrap-apply gameobj-remove-occupant!))
52    (set-loc! (wrap-apply gameobj-set-loc!))
53    (get-name (wrap-apply gameobj-get-name))
54    (get-desc (wrap-apply gameobj-get-desc))
55    (goes-by (wrap-apply gameobj-goes-by))))
56
57 ;;; *all* game components that talk to players should somehow
58 ;;; derive from this class.
59 ;;; And all of them need a GM!
60
61 (define-class <gameobj> (<actor>)
62   ;; location id
63   (loc #:init-value #f
64        #:getter gameobj-loc)
65   
66   ;; Uses a hash table like a set (values ignored)
67   (occupants #:init-thunk make-hash-table
68              #:getter gameobj-occupants)
69
70   ;; game master id
71   (gm #:init-keyword #:gm
72       #:getter gameobj-gm)
73   ;; a name to be known by
74   (name #:init-keyword #:name
75         #:init-value #f)
76   (goes-by #:init-keyword #:goes-by
77            #:init-value #f)
78
79   (desc #:init-value ""
80         #:init-keyword #:desc)
81
82   ;; how to print our name
83   (name-f #:init-keyword #:name-f
84           #:getter gameobj-name-f
85           #:init-value (wrap gameobj-simple-name-f))
86
87   ;; Commands we can handle
88   (commands #:init-value '())
89
90   ;; Commands we can handle by being something's container
91   (container-commands #:init-value '())
92   (message-handler
93    #:init-value
94    (simple-dispatcher gameobj-actions)))
95
96
97 ;;; gameobj message handlers
98 ;;; ========================
99
100 ;; Kind of a useful utility, maybe?
101 (define (simple-slot-getter slot)
102   (lambda (actor message)
103     (reply-message actor message
104                    #:val (slot-ref actor slot))))
105
106
107 ;; @@: This could be kind of a messy way of doing gameobj-init
108 ;;   stuff.  If only we had generic methods :(
109 (define-mhandler (gameobj-init actor message)
110   "Your most basic game object init procedure.  Does nothing."
111   #f)
112
113 (define (gameobj-goes-by gameobj)
114   "Find the name we go by.  Defaults to #:name if nothing else provided."
115   (cond ((slot-ref gameobj 'goes-by) =>
116          identity)
117         ((slot-ref gameobj 'name) =>
118          (lambda (name)
119            (list name)))
120         (else '())))
121
122 (define (val-or-run val-or-proc)
123   "Evaluate if a procedure, or just return otherwise"
124   (if (procedure? val-or-proc)
125       (val-or-proc)
126       val-or-proc))
127
128 (define (filter-commands commands verb)
129   (filter
130    (lambda (cmd)
131      (equal? (command-verbs cmd)
132              verb))
133    commands))
134
135 (define-mhandler (gameobj-get-commands actor message verb)
136   "Get commands a co-occupant of the room might execute for VERB"
137   (define filtered-commands
138     (filter-commands (val-or-run (slot-ref actor 'commands))
139                      verb))
140   (<-reply actor message
141            #:commands filtered-commands
142            #:goes-by (gameobj-goes-by actor)))
143
144 (define-mhandler (gameobj-get-container-commands actor message verb)
145   "Get commands as the container / room of message's sender"
146   (define filtered-commands
147     (filter-commands (val-or-run (slot-ref actor 'container-commands))
148                      verb))
149   (<-reply actor message #:commands filtered-commands))
150
151 (define-mhandler (gameobj-add-occupant! actor message who)
152   "Add an actor to our list of present occupants"
153   (hash-set! (slot-ref actor 'occupants)
154              who #t))
155
156 (define-mhandler (gameobj-remove-occupant! actor message who)
157   "Remove an occupant from the room."
158   (hash-remove! (slot-ref actor 'occupants) who))
159
160 (define-mhandler (gameobj-get-occupants actor message)
161   "Get all present occupants of the room."
162   (define occupants
163     (hash-map->list (lambda (key val) key)
164                     (gameobj-occupants actor)))
165
166   (<-reply actor message
167            #:occupants occupants))
168
169 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
170 (define-mhandler (gameobj-set-loc! actor message loc)
171   "Set the location of this object."
172   (define old-loc (gameobj-loc actor))
173   (format #t "DEBUG: Location set to ~s for ~s\n"
174           loc (actor-id-actor actor))
175
176   (slot-set! actor 'loc loc)
177   ;; Change registation of where we currently are
178   (if loc
179       (<-wait actor loc 'add-occupant! #:who (actor-id actor)))
180   (if old-loc
181       (<-wait actor old-loc 'remove-occupant! #:who (actor-id actor))))
182
183 (define gameobj-get-name (simple-slot-getter 'name))
184 (define gameobj-get-desc (simple-slot-getter 'desc))
185
186 (define (gameobj-simple-name-f gameobj)
187   "Simplest version: return ourselves for our name."
188   (gameobj-name gameobj))