File: add_unique_goal.py

package info (click to toggle)
cyphesis-cpp 0.5.16-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,084 kB
  • ctags: 3,627
  • sloc: cpp: 30,418; python: 4,812; xml: 4,674; sh: 4,118; makefile: 902; ansic: 617
file content (48 lines) | stat: -rw-r--r-- 1,919 bytes parent folder | download | duplicates (3)
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
#This file is distributed under the terms of the GNU General Public license.
#Copyright (C) 1999 Aloril (See the file COPYING for details).

from mind.Goal import Goal
from mind.goals.dynamic.DynamicGoal import DynamicGoal

############################ ADD UNIQUE ####################################


class add_unique_goal(DynamicGoal):
    def __init__(self, goal_to_add, desc="add_unique_goal", **kw):
        kw['desc'] = desc
        apply(DynamicGoal.__init__,(self,),kw)
        self.goal_class = goal_to_add
        self.vars.append("goal_class")
    def event(self, me, original_op, op):
        """adds goal using given operation and execute that goal:
           first let see if we already have added this goal
           then we generate and add goal if it doesn't exist
           and finally we execute goal"""
        if not self.check_event(me, original_op, op):
            return
        goal = self.find_goal(me, self.goal_class)
        if not goal:
            goal = self.make_goal_instance(me, self.goal_class, original_op, op)
            if goal:
                self.add_goal(me, goal)
        if goal:
            return goal.check_goal(me, me.time)
    def find_goal(self, me, goal_class):
        for goal in me.goals:
            if goal.__class__==goal_class:
                return goal
    def add_goal(self, me, goal):
        """add goal into mind"""
        me.goals.insert(0,goal)
    def make_goal_instance(self, me, goal_class, original_op, op):
        #override this!
        return goal_class(me, original_op, op)
    def check_event(self, me, original_op, op):
        """override this to check to see whether we really want to add a goal"""
        return 1

class add_unique_goal_by_perception(add_unique_goal):
    def make_goal_instance(self, me, goal_class, original_op, op):
        object=me.map.get(op[0].id)
        if object:
            return goal_class(object)