a418cf0c1566b38622dd61d74264f389605c5458
[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
56 ;;; *all* game components that talk to players should somehow
57 ;;; derive from this class.
58 ;;; And all of them need a GM!
59
60 (define-class <gameobj> (<actor>)
61   ;; location id
62   (loc #:init-value #f
63        #:accessor gameobj-loc)
64   
65   ;; Uses a hash table like a set (values ignored)
66   (occupants #:init-thunk make-hash-table
67              #:accessor gameobj-occupants)
68
69   ;; game master id
70   (gm #:init-keyword #:gm
71       #:getter gameobj-gm)
72   ;; a name to be known by
73   (name #:init-keyword #:name
74         #:accessor gameobj-name)
75
76   (desc #:init-value ""
77         #:init-keyword #:desc)
78
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))
83
84   ;; Name aliases
85   (aliases #:init-keyword #:aliases
86            #:init-value '())
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
98 ;;; gameobj message handlers
99 ;;; ========================
100
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))))
106
107
108 ;; @@: This could be kind of a messy way of doing gameobj-init
109 ;;   stuff.  If only we had generic methods :(
110 (define-mhandler (gameobj-init actor message)
111   "Your most basic game object init procedure.  Does nothing."
112   #f)
113
114 (define (val-or-run val-or-proc)
115   "Evaluate if a procedure, or just return otherwise"
116   (if (procedure? val-or-proc)
117       (val-or-proc)
118       val-or-proc))
119
120 (define (filter-commands commands verb)
121   (filter
122    (lambda (cmd)
123      (equal? (command-verbs cmd)
124              verb))
125    commands))
126
127 (define-mhandler (gameobj-get-commands actor message verb)
128   (define filtered-commands
129     (filter-commands (val-or-run (slot-ref actor 'commands))
130                      verb))
131   (<-reply actor message #:commands filtered-commands))
132
133 (define-mhandler (gameobj-get-container-commands actor message verb)
134   (define filtered-commands
135     (filter-commands (val-or-run (slot-ref actor 'container-commands))
136                      verb))
137   (<-reply actor message #:commands filtered-commands))
138
139 (define-mhandler (gameobj-add-occupant! actor message who)
140   (hash-set! (slot-ref actor 'occupants)
141              who #t))
142
143 (define-mhandler (gameobj-remove-occupant! actor message who)
144   (hash-remove! (slot-ref actor 'occupants) who))
145
146 (define-mhandler (gameobj-get-occupants actor message)
147   (define occupants
148     (hash-map->list (lambda (key val) key)
149                     (gameobj-occupants actor)))
150
151   (<-reply actor message
152            #:occupants occupants))
153
154 ;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
155 (define-mhandler (gameobj-set-loc! actor message loc)
156   (define old-loc (gameobj-loc actor))
157   (format #t "DEBUG: Location set to ~s for ~s\n"
158           loc (actor-id-actor actor))
159
160   (set! (gameobj-loc actor) loc)
161   ;; Change registation of where we currently are
162   (if loc
163       (<-wait actor loc 'add-occupant! #:who (actor-id actor)))
164   (if old-loc
165       (<-wait actor old-loc 'remove-occupant! #:who (actor-id actor))))
166
167 (define gameobj-get-name (simple-slot-getter 'name))
168 (define gameobj-get-desc (simple-slot-getter 'desc))
169
170 (define (gameobj-simple-name-f gameobj)
171   "Simplest version: return ourselves for our name."
172   (gameobj-name gameobj))
173
174