1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of Mudsync.
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.
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.
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/>.
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)
43 ;;; Actions supported by all gameobj
44 (define gameobj-actions
46 (get-commands (wrap-apply gameobj-get-commands))
47 (get-container-commands (wrap-apply gameobj-get-container-commands))
48 (get-occupants (wrap-apply gameobj-get-occupants))
49 (add-occupant! (wrap-apply gameobj-add-occupant!))
50 (remove-occupant! (wrap-apply gameobj-remove-occupant!))
51 (set-loc! (wrap-apply gameobj-set-loc!))
52 (get-name (wrap-apply gameobj-get-name))
53 (get-desc (wrap-apply gameobj-get-desc))
56 ;;; *all* game components that talk to players should somehow
57 ;;; derive from this class.
58 ;;; And all of them need a GM!
60 (define-class <gameobj> (<actor>)
63 #:accessor gameobj-loc)
65 ;; Uses a hash table like a set (values ignored)
66 (occupants #:init-thunk make-hash-table
67 #:accessor gameobj-occupants)
70 (gm #:init-keyword #:gm
72 ;; a name to be known by
73 (name #:init-keyword #:name
74 #:accessor gameobj-name)
77 #:init-keyword #:desc)
79 ;; how to print our name
80 (name-f #:init-keyword #:name-f
81 #:getter gameobj-name-f
82 #:init-value (wrap gameobj-simple-name-f))
85 (aliases #:init-keyword #:aliases
88 ;; Commands we can handle
89 (commands #:init-value '())
91 ;; Commands we can handle by being something's container
92 (container-commands #:init-value '())
95 (simple-dispatcher gameobj-actions)))
98 ;;; gameobj message handlers
99 ;;; ========================
101 ;; Kind of a useful utility, maybe?
102 (define (simple-slot-getter slot)
103 (lambda (actor message)
104 (reply-message actor message
105 #:val (slot-ref actor slot))))
107 (define (val-or-run val-or-proc)
108 "Evaluate if a procedure, or just return otherwise"
109 (if (procedure? val-or-proc)
113 (define (filter-commands commands verb)
116 (equal? (command-verbs cmd)
120 (define-mhandler (gameobj-get-commands actor message verb)
121 (define filtered-commands
122 (filter-commands (val-or-run (slot-ref actor 'commands))
124 (<-reply actor message #:commands filtered-commands))
126 (define-mhandler (gameobj-get-container-commands actor message verb)
127 (define filtered-commands
128 (filter-commands (val-or-run (slot-ref actor 'container-commands))
130 (<-reply actor message #:commands filtered-commands))
132 (define-mhandler (gameobj-add-occupant! actor message who)
133 (hash-set! (slot-ref actor 'occupants)
136 (define-mhandler (gameobj-remove-occupant! actor message who)
137 (hash-remove! (slot-ref actor 'occupants) who))
139 (define-mhandler (gameobj-get-occupants actor message)
141 (hash-map->list (lambda (key val) key)
142 (gameobj-occupants actor)))
144 (<-reply actor message
145 #:occupants occupants))
147 ;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc?
148 (define-mhandler (gameobj-set-loc! actor message loc)
149 (define old-loc (gameobj-loc actor))
150 (format #t "DEBUG: Location set to ~s for ~s\n"
151 loc (actor-id-actor actor))
153 (set! (gameobj-loc actor) loc)
154 ;; Change registation of where we currently are
156 (<-wait actor loc 'add-occupant! #:who (actor-id actor)))
158 (<-wait actor old-loc 'remove-occupant! #:who (actor-id actor))))
160 (define gameobj-get-name (simple-slot-getter 'name))
161 (define gameobj-get-desc (simple-slot-getter 'desc))
163 (define (gameobj-simple-name-f gameobj)
164 "Simplest version: return ourselves for our name."
165 (gameobj-name gameobj))