more tweaks
[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-act-goes-by))
56    (visible-name (wrap-apply gameobj-visible-name))))
57
58 ;;; *all* game components that talk to players should somehow
59 ;;; derive from this class.
60 ;;; And all of them need a GM!
61
62 (define-class <gameobj> (<actor>)
63   ;; location id
64   (loc #:init-value #f
65        #:getter gameobj-loc)
66   
67   ;; Uses a hash table like a set (values ignored)
68   (occupants #:init-thunk make-hash-table
69              #:getter gameobj-occupants)
70
71   ;; game master id
72   (gm #:init-keyword #:gm
73       #:getter gameobj-gm)
74   ;; a name to be known by
75   (name #:init-keyword #:name
76         #:init-value #f)
77   (goes-by #:init-keyword #:goes-by
78            #:init-value #f)
79
80   (desc #:init-value #f
81         #:init-keyword #:desc)
82
83   ;; how to print our name
84   (name-f #:init-keyword #:name-f
85           #:getter gameobj-name-f
86           #:init-value (wrap gameobj-simple-name-f))
87
88   ;; Commands we can handle
89   (commands #:init-value '())
90
91   ;; Commands we can handle by being something's container
92   (container-commands #:init-value '())
93   (message-handler
94    #:init-value
95    (simple-dispatcher gameobj-actions))
96
97   ;; Most objects are generally visible by default
98   (generally-visible #:init-value #t
99                      #:init-keyword #:generally-visible)
100   ;; @@: Would be preferable to be using generic methods for this...
101   ;;   Hopefully we can port this to Guile 2.2 soon...
102   (visible-to-player?
103    #:init-value (wrap-apply gameobj-visible-to-player?)))
104
105
106 ;;; gameobj message handlers
107 ;;; ========================
108
109 ;; Kind of a useful utility, maybe?
110 (define (simple-slot-getter slot)
111   (lambda (actor message)
112     (reply-message actor message
113                    #:val (slot-ref actor slot))))
114
115
116 ;; @@: This could be kind of a messy way of doing gameobj-init
117 ;;   stuff.  If only we had generic methods :(
118 (define-mhandler (gameobj-init actor message)
119   "Your most basic game object init procedure.  Does nothing."
120   #f)
121
122 (define (gameobj-goes-by gameobj)
123   "Find the name we go by.  Defaults to #:name if nothing else provided."
124   (cond ((slot-ref gameobj 'goes-by) =>
125          identity)
126         ((slot-ref gameobj 'name) =>
127          (lambda (name)
128            (list name)))
129         (else '())))
130
131 (define (gameobj-act-goes-by actor message)
132   "Reply to a message requesting what we go by."
133   (<-reply actor message
134            #:goes-by (gameobj-goes-by actor)))
135
136 (define (val-or-run val-or-proc)
137   "Evaluate if a procedure, or just return otherwise"
138   (if (procedure? val-or-proc)
139       (val-or-proc)
140       val-or-proc))
141
142 (define (filter-commands commands verb)
143   (filter
144    (lambda (cmd)
145      (equal? (command-verbs cmd)
146              verb))
147    commands))
148
149 (define-mhandler (gameobj-get-commands actor message verb)
150   "Get commands a co-occupant of the room might execute for VERB"
151   (define filtered-commands
152     (filter-commands (val-or-run (slot-ref actor 'commands))
153                      verb))
154   (<-reply actor message
155            #:commands filtered-commands
156            #:goes-by (gameobj-goes-by actor)))
157
158 (define-mhandler (gameobj-get-container-commands actor message verb)
159   "Get commands as the container / room of message's sender"
160   (define filtered-commands
161     (filter-commands (val-or-run (slot-ref actor 'container-commands))
162                      verb))
163   (<-reply actor message #:commands filtered-commands))
164
165 (define-mhandler (gameobj-add-occupant! actor message who)
166   "Add an actor to our list of present occupants"
167   (hash-set! (slot-ref actor 'occupants)
168              who #t))
169
170 (define-mhandler (gameobj-remove-occupant! actor message who)
171   "Remove an occupant from the room."
172   (hash-remove! (slot-ref actor 'occupants) who))
173
174 (define-mhandler (gameobj-get-occupants actor message)
175   "Get all present occupants of the room."
176   (define occupants
177     (hash-map->list (lambda (key val) key)
178                     (gameobj-occupants actor)))
179
180   (<-reply actor message
181            #:occupants occupants))
182
183 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
184 (define-mhandler (gameobj-set-loc! actor message loc)
185   "Set the location of this object."
186   (define old-loc (gameobj-loc actor))
187   (format #t "DEBUG: Location set to ~s for ~s\n"
188           loc (actor-id-actor actor))
189
190   (slot-set! actor 'loc loc)
191   ;; Change registation of where we currently are
192   (if loc
193       (<-wait actor loc 'add-occupant! #:who (actor-id actor)))
194   (if old-loc
195       (<-wait actor old-loc 'remove-occupant! #:who (actor-id actor))))
196
197 (define gameobj-get-name (simple-slot-getter 'name))
198
199 (define-mhandler (gameobj-get-desc actor message whos-looking)
200   (define desc-text
201     (match (slot-ref actor 'desc)
202       ((? procedure? desc-proc)
203        (desc-proc actor whos-looking))
204       (desc desc)))
205   (<-reply actor message #:val desc-text))
206
207 (define (gameobj-simple-name-f gameobj)
208   "Simplest version: return ourselves for our name."
209   (gameobj-name gameobj))
210
211 (define (gameobj-visible-to-player? gameobj whos-looking)
212   "Check to see whether we're visible to the player or not.
213 By default, this is whether or not the generally-visible flag is set."
214   (slot-ref gameobj 'generally-visible))
215
216 (define-mhandler (gameobj-visible-name actor message whos-looking)
217   ;; Are we visible?
218   (define we-are-visible
219     ((slot-ref actor 'visible-to-player?) actor whos-looking))
220
221   (define name-to-return
222     (if we-are-visible
223         ;; Return our name
224         (match (slot-ref actor 'name)
225           ((? procedure? name-proc)
226            (name-proc actor whos-looking))
227           ((? string? name)
228            name)
229           (#f #f))
230         #f))
231   (<-reply actor message #:text name-to-return))