git @ Cat's Eye Technologies The-Swallows / master src / swallows / engine / objects.py
master

Tree @master (Download .tar.gz)

objects.py @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import random
import sys

from swallows.engine.events import Event

### TOPICS ###

# a "topic" is just what a character has recently had addressed to
# them.  It could be anything, not just words, by another character
# (for example, a gesture.)

class Topic(object):
    def __init__(self, originator, subject=None):
        self.originator = originator
        self.subject = subject


class GreetTopic(Topic):
    pass


class SpeechTopic(Topic):
    pass


class QuestionTopic(Topic):
    pass


### BELIEFS ###

#
# a belief is something an Animate believes.  they come in a few types:
#
# - a belief that an object is somewhere
#   - because they saw it there (memory)
#   - because some other character told them it was there
# - a belief that they should do something (a goal), which has subtypes:
#   - a belief that an object is desirable & they should try to acquire it
#   - a belief that something should be done about something (bland, general)
# - a belief that another Animate believes something
#
# of course, any particular belief may turn out not to be true
#

# abstract base class
class Belief(object):
    # constructor of all subclasses of this class should accept being
    # called with only one argument, as a convenience sort of thing
    # for BeliefSet.get and .remove, which don't really care about anything
    # about the Belief except for its class and its subject.
    # although, usually, you do want to pass more than one argument when
    # making a real Belief to pass to BeliefSet.add.  (clear as mud, right?)
    def __init__(self, subject):          # kind of silly for an ABC to have a
        assert isinstance(subject, Actor) # constructor, but it is to emphasize
        self.subject = subject            # that all beliefs have a subject,
                                          # which is the thing we believe
                                          # something about

    def __str__(self):
        raise NotImplementedError


class ItemLocation(Belief):   # formerly "Memory"
    def __init__(self, subject, location=None, informant=None, concealer=None):
        assert isinstance(subject, Actor)
        assert isinstance(location, Actor) or location is None
        self.subject = subject      # the thing we think is somewhere
        self.location = location    # the place we think it is
        self.informant = informant  # the actor who told us about it
        self.concealer = concealer  # the actor who we think hid it there

    def __str__(self):
        s = "%s is in %s" % (
            self.subject.render(),
            self.location.render()
        )
        if self.concealer:
            s += " (hidden there by %s)" % self.concealer.render()
        if self.informant:
            s += " (%s told me so)" % self.informant.render()
        return s


class Goal(Belief):
    def __init__(self, subject, phrase=None):
        assert isinstance(subject, Actor)
        self.subject = subject   # the thing we would like to do something about
        self.phrase = phrase     # human-readable description

    def __str__(self):
        return "I should %s %s" % (
            self.phrase,
            self.subject.render()
        )


class Desire(Goal):
    def __init__(self, subject):
        assert isinstance(subject, Actor)
        self.subject = subject   # the thing we would like to acquire

    def __str__(self):
        return "I want %s" % (
            self.subject.render()
        )


# oh dear
class BeliefsBelief(Belief):
    def __init__(self, subject, belief_set=None):
        assert isinstance(subject, Animate)
        self.subject = subject        # the animate we think holds the belief
        if belief_set is None:
            belief_set = BeliefSet()
        assert isinstance(belief_set, BeliefSet)
        self.belief_set = belief_set  # the beliefs we think they hold

    def __str__(self):
        return "%s believes { %s }" % (
            self.subject.render(),
            self.belief_set
        )


class BeliefSet(object):
    """A BeliefSet works something like a Python set(), but has the
    following constraints:
    
    There can be only one of each type of Belief about a particular
    item in the set.

    So it's really kind of a map from Actors to maps from Belief
    subclasses to Beliefs.

    But it behooves us (or at least, me) to think of it as a set.
    (Besides, it might change.)

    """
    def __init__(self):
        self.belief_map = {}

    def add(self, belief):
        assert isinstance(belief, Belief)
        subject = belief.subject
        self.belief_map.setdefault(subject, {})[belief.__class__] = belief

    def remove(self, belief):
        # the particular belief passed to us doesn't really matter.  we extract
        # the class and subject and return any existing belief we may have
        assert isinstance(belief, Belief)
        subject = belief.subject
        beliefs = self.belief_map.setdefault(subject, {})
        if belief.__class__ in beliefs:
            del beliefs[belief.__class__]

    def get(self, belief):
        # the particular belief passed to us doesn't really matter.  we extract
        # the class and subject and return any existing belief we may have
        assert isinstance(belief, Belief)
        subject = belief.subject
        return self.belief_map.setdefault(subject, {}).get(
            belief.__class__, None
        )

    def subjects(self):
        for subject in self.belief_map:
            yield subject

    def beliefs_for(self, subject):
        beliefs = self.belief_map.setdefault(subject, {})
        for class_ in beliefs:
            yield beliefs[class_]

    def beliefs_of_class(self, class_):
        for subject in self.subjects():
            for belief in self.beliefs_for(subject):
                if belief.__class__ == class_:
                    yield belief

    def __str__(self):
        l = []
        for subject in self.subjects():
            for belief in self.beliefs_for(subject):
                l.append(str(belief))
        return ', '.join(l)


### ACTORS (objects in the world) ###

class Actor(object):
    def __init__(self, name, location=None, owner=None, collector=None):
        self.name = name
        self.collector = collector
        self.contents = set()
        self.enter = ""
        self.owner = owner
        self.location = None
        if location is not None:
            self.move_to(location)

    def notable(self):
        return self.treasure() or self.weapon() or self.animate() or self.horror()

    def treasure(self):
        return False

    def weapon(self):
        return False

    def horror(self):
        return False

    def takeable(self):
        return False

    def animate(self):
        return False

    def container(self):
        return False

    def article(self):
        return 'the'

    def posessive(self):
        return "its"

    def accusative(self):
        return "it"

    def pronoun(self):
        return "it"

    def was(self):
        return "was"

    def is_(self):
        return "is"

    def emit(self, *args, **kwargs):
        if self.collector:
            self.collector.collect(Event(*args, **kwargs))

    def move_to(self, location):
        if self.location:
            self.location.contents.remove(self)
        self.location = location
        self.location.contents.add(self)

    def render(self, event=None):
        """Return a string containing what we call this object, in the context
        of the given event (which may be None, to get a 'generic' description.)

        """
        name = self.name
        repl = None
        if self.owner is not None:
            repl = self.owner.render() + "'s"
        if event:
            if event.speaker is self.owner:
                repl = 'my'
            elif event.addressed_to is self.owner:
                repl = 'your'
            elif event.initiator() is self.owner:
                repl = event.initiator().posessive()
        if repl is not None:
            name = name.replace('<*>', repl)
        article = self.article()
        if not article:
            return name
        return '%s %s' % (article, name)

    def indefinite(self):
        article = 'a'
        if self.name.startswith(('a', 'e', 'i', 'o', 'u')):
            article = 'an'
        return '%s %s' % (article, self.name)


### some mixins for Actors ###

class ProperMixin(object):
    def article(self):
        return ''


class PluralMixin(object):
    def posessive(self):
        return "their"

    def accusative(self):
        return "them"

    def pronoun(self):
        return "they"

    def indefinite(self):
        article = 'some'
        return '%s %s' % (article, self.name)

    def was(self):
        return "were"

    def is_(self):
        return "are"


class MasculineMixin(object):
    def posessive(self):
        return "his"

    def accusative(self):
        return "him"

    def pronoun(self):
        return "he"


class FeminineMixin(object):
    def posessive(self):
        return "her"

    def accusative(self):
        return "her"

    def pronoun(self):
        return "she"


### ANIMATE OBJECTS ###

class Animate(Actor):
    def __init__(self, name, location=None, owner=None, collector=None):
        Actor.__init__(
            self, name, location=location, owner=owner, collector=None
        )
        self.topic = None
        self.beliefs = BeliefSet()

    def animate(self):
        return True

    # for debugging
    def dump_beliefs(self):
        for subject in self.beliefs.subjects():
            for belief in self.beliefs.beliefs_for(subject):
                print ".oO{ %s }" % belief

    ###--- belief accessors/manipulators ---###
    
    # these are mostly just aliases for accessing the BeliefSet.

    def remember_location(self, thing, location, concealer=None):
        """Update this Animate's beliefs to include a belief that the
        given thing is located at the given location.

        Really just a readable alias for believe_location.

        """
        self.believe_location(thing, location, informant=None, concealer=concealer)

    def believe_location(self, thing, location, informant=None, concealer=None):
        """Update this Animate's beliefs to include a belief that the
        given thing is located at the given location.  They may have
        been told this by someone.

        """
        self.beliefs.add(ItemLocation(
            thing, location, informant=informant, concealer=concealer
        ))

    def recall_location(self, thing):
        """Return an ItemLocation (belief) about this thing, or None."""
        return self.beliefs.get(ItemLocation(thing))

    def forget_location(self, thing):
        self.beliefs.remove(ItemLocation(thing))

    def desire(self, thing):
        self.beliefs.add(Desire(thing))

    def quench_desire(self, thing):
        # usually called when it has been acquired
        self.beliefs.remove(Desire(thing))

    def does_desire(self, thing):
        if thing.treasure():
            return True  # omg YES
        if thing.weapon():
            return True  # could come in handy.  (TODO, sophisticate this?)
        return self.beliefs.get(Desire(thing)) is not None

    def believed_beliefs_of(self, other):
        """Returns a BeliefSet (not a Belief) that this Animate
        believes the other Animate holds.

        Typically you would manipulate this BeliefSet directly
        with add, remove, get, etc.

        """
        assert isinstance(other, Animate)
        # for extra fun, try reading the code of this method out loud!
        beliefs_belief = self.beliefs.get(BeliefsBelief(other))
        if beliefs_belief is None:
            beliefs_belief = BeliefsBelief(other, BeliefSet())
            self.beliefs.add(beliefs_belief)
        return beliefs_belief.belief_set

    ###--- topic stuff ---###

    def address(self, other, topic, phrase, participants=None):
        if participants is None:
            participants = [self, other]
        other.topic = topic
        self.emit(phrase, participants, speaker=self, addressed_to=other)

    def greet(self, other, phrase, participants=None):
        self.address(other, GreetTopic(self), phrase, participants)

    def speak_to(self, other, phrase, participants=None, subject=None):
        self.address(other, SpeechTopic(self, subject=subject), phrase, participants)

    def question(self, other, phrase, participants=None, subject=None):
        self.address(other, QuestionTopic(self, subject=subject), phrase, participants)

    ###--- generic actions ---###

    def place_in(self, location):
        """Like move_to but quieter.  For setting up scenes, etc.

        """
        if self.location is not None:
            self.location.contents.remove(self)
        self.location = location
        self.location.contents.add(self)
        # this is needed so that the Editor knows where the character starts.
        # the Editor should (does?) strip out all instances of these that
        # aren't informative to the reader.
        self.emit("<1> <was-1> in <2>", [self, self.location])
        # a side-effect of the following code is, if they start in a location
        # with a horror,they don't react to it.  They probably should.
        for x in self.location.contents:
            if x == self:
                continue
            if x.notable():
                self.emit("<1> saw <2>", [self, x])
                self.remember_location(x, self.location)

    def move_to(self, location):
        assert(location != self.location)
        assert(location is not None)
        for x in self.location.contents:
            # otherwise we get "Bob saw Bob leave the room", eh?
            if x is self:
                continue
            if x.animate():
                x.emit("<1> saw <2> leave the %s" % x.location.noun(), [x, self])
        if self.location is not None:
            self.location.contents.remove(self)
        previous_location = self.location
        self.location = location
        assert self not in self.location.contents
        self.location.contents.add(self)
        self.emit("<1> went to <2>", [self, self.location],
                  previous_location=previous_location)

    def point_at(self, other, item):
        # it would be nice if there was some way to
        # indicate the revolver as part of the Topic which will follow,
        # or otherwise indicate the context as "at gunpoint"

        assert self.location == other.location
        assert item.location == self
        self.emit("<1> pointed <3> at <2>",
            [self, other, item])
        for actor in self.location.contents:
            if actor.animate():
                actor.remember_location(item, self)

    def put_down(self, item):
        assert(item.location == self)
        self.emit("<1> put down <2>", [self, item])
        item.move_to(self.location)
        for actor in self.location.contents:
            if actor.animate():
                actor.remember_location(item, self.location)

    def pick_up(self, item):
        assert(item.location == self.location)
        self.emit("<1> picked up <2>", [self, item])
        item.move_to(self)
        for actor in self.location.contents:
            if actor.animate():
                actor.remember_location(item, self)

    def give_to(self, other, item):
        assert(item.location == self)
        assert(self.location == other.location)
        self.emit("<1> gave <3> to <2>", [self, other, item])
        item.move_to(other)
        for actor in self.location.contents:
            if actor.animate():
                actor.remember_location(item, other)

    def wander(self):
        self.move_to(
            self.location.exits[
                random.randint(0, len(self.location.exits)-1)
            ]
        )

    def live(self):
        """This gets called on each turn an animate moves.
        
        You need to implement this for particular animates.
        
        """
        raise NotImplementedError(
            'Please implement %s.live()' % self.__class__.__name__
        )


class Male(MasculineMixin, ProperMixin, Animate):
    pass


class Female(FeminineMixin, ProperMixin, Animate):
    pass


### LOCATIONS ###

class Location(Actor):
    def __init__(self, name, enter="went to", noun="room", owner=None):
        self.name = name
        self.enter = enter
        self.contents = set()
        self.exits = []
        self.noun_ = noun
        self.owner = owner

    def noun(self):
        return self.noun_

    def set_exits(self, *exits):
        for exit in exits:
            assert isinstance(exit, Location)
        self.exits = exits


class ProperLocation(ProperMixin, Location):
    pass


### OTHER INANIMATE OBJECTS ###

class Item(Actor):
    def takeable(self):
        return True


class Weapon(Item):
    def weapon(self):
        return True


class Container(Actor):
    def container(self):
        return True


class ProperContainer(ProperMixin, Container):
    pass


class Treasure(Item):
    def treasure(self):
        return True


class PluralTreasure(PluralMixin, Treasure):
    pass


class Horror(Actor):
    def horror(self):
        return True