git @ Cat's Eye Technologies The-Swallows / f2dcad4
Desires are now beliefs too. Cat's Eye Technologies 11 years ago
2 changed file(s) with 80 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
3434 # - a belief that an object is somewhere
3535 # - because they saw it there (memory)
3636 # - because some other character told them it was there
37 # - a belief that they should do something (a goal)
37 # - a belief that they should do something (a goal), which has subtypes:
38 # - a belief that an object is desirable & they should try to acquire it
39 # - a belief that something should be done about something (bland, general)
3840 # - a belief that another Animate believes something
3941 #
4042 # of course, any particular belief may turn out not to be true
4244
4345 # abstract base class
4446 class Belief(object):
45 def __init__(self, subject): # kind of silly for an ABC to have a constructor, but ok
47 def __init__(self, subject): # kind of silly for an ABC to have a
48 # constructor, but it is to emphasize that
49 # all beliefs have a subject, which is
4650 self.subject = subject # the thing we believe something about
4751
4852 def __str__(self):
5761 self.concealer = concealer # the actor who we think hid it ther
5862
5963 def __str__(self):
60 return "%s is in %s" % (
64 s = "%s is in %s" % (
6165 self.subject.render([]),
6266 self.location.render([])
6367 )
64 #if .
65 # print ".oO{ I hid it there }"
68 if self.concealer:
69 s += " (hidden there by %s)" % self.concealer.render([])
70 if self.informant:
71 s += " (%s told me so)" % self.informant.render([])
72 return s
6673
6774
6875 # this could itself have several subclasses
69 class GoalBelief(Belief):
76 class Goal(Belief):
7077 def __init__(self, subject, phrase):
7178 self.subject = subject # the thing we would like to do something about
7279 self.phrase = phrase # human-readable description
80
81 def __str__(self):
82 return "I should %s %s" % (
83 self.phrase,
84 self.subject.render([])
85 )
86
87
88 class Desire(Belief):
89 def __init__(self, subject):
90 self.subject = subject # the thing we would like to acquire
91
92 def __str__(self):
93 return "I want %s" % (
94 self.subject.render([])
95 )
7396
7497
7598 # oh dear
79102 assert isinstance(belief, Belief)
80103 self.subject = subject # the animate we think holds the belief
81104 self.belief = belief # the belief we think they hold
105
106 def __str__(self):
107 return "%s believes %s" % (
108 self.subject.render([]),
109 self.belief
110 )
82111
83112
84113 ### ACTORS (objects in the world) ###
218247 self.topic = None
219248 # map of actors to sets of Beliefs about them
220249 self.beliefs = {}
221 self.desired_items = set()
222250 # this should really be *derived* from having a recent memory
223251 # of seeing a dead body in the bathroom. but for now,
224252 self.nerves = 'calm'
235263 # for debugging
236264 def dump_beliefs(self):
237265 for subject in self.beliefs:
238 print ".oO{ %s }" % self.beliefs[subject]
239 print "desired items:", repr(self.desired_items)
266 for belief in self.beliefs[subject]:
267 print ".oO{ %s }" % belief
240268 print "decisions:", repr(self.what_to_do_about)
241269 print "knowledge of others' decisions:", repr(self.other_decision_about)
270
271 ###--- belief accessors/manipulators ---###
242272
243273 def remember_location(self, thing, location, concealer=None):
244274 """Update this Animate's beliefs to include a belief that the
281311 return None
282312
283313 def forget_location(self, thing):
314 # this code is unusually roundabout because Python does
315 # not like you to modify a set while iterating it and because
316 # how I've chosen to implement these sets of beliefs
284317 assert isinstance(thing, Actor)
285318 belief_set = self.beliefs.get(thing, set())
286319 target_beliefs = set()
287320 for belief in belief_set:
288321 if isinstance(belief, ItemLocationBelief):
289322 target_beliefs.add(belief)
290 assert len(target_beliefs) in (0, 1)
323 assert len(target_beliefs) in (0, 1), len(target_beliefs)
291324 for belief in target_beliefs:
292325 belief_set.remove(belief)
326
327 def desire(self, thing):
328 assert isinstance(thing, Actor)
329 belief_set = self.beliefs.get(thing, set())
330 for belief in belief_set:
331 if isinstance(belief, Desire):
332 return
333 belief_set.add(Desire(thing))
334 self.beliefs[thing] = belief_set
335
336 def quench_desire(self, thing):
337 # usually called when it has been acquired
338 assert isinstance(thing, Actor)
339 belief_set = self.beliefs.get(thing, set())
340 target_beliefs = set()
341 for belief in belief_set:
342 if isinstance(belief, Desire):
343 target_beliefs.add(belief)
344 assert len(target_beliefs) in (0, 1), len(target_beliefs)
345 for belief in target_beliefs:
346 belief_set.remove(belief)
347
348 def does_desire(self, thing):
349 assert isinstance(thing, Actor)
350 if thing.treasure():
351 return True # omg YES
352 if thing.weapon():
353 return True # well it could come in useful. (this may change)
354 belief_set = self.beliefs.get(thing, set())
355 for belief in belief_set:
356 if isinstance(belief, Desire):
357 return True
293358
294359 def address(self, other, topic, phrase, participants=None):
295360 if participants is None:
0 #!/usr/bin/env python
1
20 import random
31 import sys
42
146144 # otherwise, if there are items here that you desire, you *must* pick
147145 # them up.
148146 for x in self.location.contents:
149 if x.treasure() or x.weapon() or x in self.desired_items:
147 if self.does_desire(x):
150148 self.pick_up(x)
151149 return
152150 people_about = False
257255 for thing in container.contents:
258256 # remember what you saw whilst searching this container
259257 self.remember_location(thing, container)
260 if thing.treasure() or thing.weapon() or thing in self.desired_items:
258 if self.does_desire(thing):
261259 desired_things.append(thing)
262260 if desired_things:
263261 thing = random.choice(desired_things)
404402 if self.brandy.location == self:
405403 self.emit("<1> poured <him-1>self a glass of brandy",
406404 [self, other, self_memory.subject])
407 if self.brandy in self.desired_items:
408 self.desired_items.remove(self.brandy)
405 self.quench_desire(self.brandy)
409406 self.nerves = 'calm'
410407 self.put_down(self.brandy)
411408 elif self.recall_location(self.brandy):
413410 "'I really must pour myself a drink,' moaned <1>",
414411 [self, other, self_memory.subject],
415412 subject=self.brandy)
416 self.desired_items.add(self.brandy)
413 self.desire(self.brandy)
417414 if random.randint(0, 1) == 0:
418415 self.address(other, WhereQuestionTopic(self, subject=self.brandy),
419416 "'Where did you say <3> was?'",
422419 self.address(other, WhereQuestionTopic(self, subject=self.brandy),
423420 "'Where is the brandy? I need a drink,' managed <1>",
424421 [self, other, self_memory.subject])
425 self.desired_items.add(self.brandy)
422 self.desire(self.brandy)
426423
427424 # this is its own method for indentation reasons
428425 def decide_what_to_do_about(self, other, thing):