clofresh / emergent

A humble attempt at creating emergent behavior

Clone this repository (size: 55.2 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/clofresh/emergent/
commit 15: ab44a74590aa
parent 14: e26ed541e0bf
branch: default
replaced lists with sets where applicable
Carlo Cabanilla
20 months ago

Changed (Δ131 bytes):

raw changeset »

emergent/behaviors.py (6 lines added, 6 lines removed)

emergent/definition.py (4 lines added, 4 lines removed)

emergent/entities.py (4 lines added, 1 lines removed)

Up to file-list emergent/behaviors.py:

@@ -82,7 +82,7 @@ class RandomMove(Move):
82
82
        Move.__init__(self, chanceOfAction)
83
83
84
84
    def do(self, doer, world):
85
        stride = round(doer.getDimensions()[0] / 2)
85
        stride = doer.getStride()
86
86
    
87
87
        dx = randint(-1 * stride, stride)
88
88
        dy = randint(-1 * stride, stride)
@@ -106,9 +106,8 @@ class Sense(Behavior):
106
106
107
107
    def filterTargets(self, possibleTargets, targetEntities):
108
108
        if possibleTargets and targetEntities:    
109
            def filterEntities(e):
110
                return e.__class__ in targetEntities
111
            possibleTargets = filter(filterEntities, possibleTargets)
109
            possibleTargets = [e for e in possibleTargets
110
                              if e.__class__ in targetEntities]
112
111
        return possibleTargets
113
112
        
114
113
    def doToWithinRange(self, doer, world):
@@ -168,7 +167,8 @@ class Approach(Response):
168
167
        self.displacement = normalize(getDisplacement(doer.boundingBox, toApproach.boundingBox))
169
168
170
169
        if any(self.displacement):
171
            self.displacement = [round(num * 2) for num in self.displacement]
170
            self.displacement = [round(num * 2) 
171
                                for num in self.displacement]
172
172
            Move.do(self, doer, world)
173
173
        else:
174
174
            self.randomMove.do(doer, world)
@@ -334,7 +334,7 @@ class Explode(Decay):
334
334
        origin = doer.getPosition()
335
335
        dimensions = doer.getDimensions()
336
336
        area = dimensions[0] * dimensions[1]
337
        offspringCount = int(round(float(area) / 128.0))
337
        offspringCount = int(round(float(area)/10.))
338
338
339
339
        if offspringCount > 1:
340
340
            for i in xrange(offspringCount):

Up to file-list emergent/definition.py:

@@ -15,7 +15,7 @@ def apply():
15
15
                                    chanceOfAction=0.75,
16
16
                                    senseDistance=100,
17
17
                                    senseFamily=False,
18
                                    toApproach=[Queen]
18
                                    toApproach=set([Queen])
19
19
                                ),
20
20
                                
21
21
                                Mutate(
@@ -40,7 +40,7 @@ def apply():
40
40
                                Gather(
41
41
                                    chanceOfAction=0.50,
42
42
                                    senseDistance=100,
43
                                    toGather=[Food]
43
                                    toGather=set([Food])
44
44
                                )
45
45
                            ])
46
46
        
@@ -62,7 +62,7 @@ def apply():
62
62
                                  chanceOfAction=0.75,
63
63
                                  senseDistance=200,
64
64
                                  senseFamily=False,
65
                                  toApproach=[Drone],
65
                                  toApproach=set([Drone]),
66
66
                                ),
67
67
                                
68
68
                                Mate(
@@ -76,7 +76,7 @@ def apply():
76
76
                                
77
77
                                Absorb(
78
78
                                  chanceOfAction=1.0,
79
                                  toAbsorb=[Queen],
79
                                  toAbsorb=set([Queen]),
80
80
                                ),
81
81
                                
82
82
                                StartNewFamily(

Up to file-list emergent/entities.py:

@@ -45,7 +45,7 @@ class Genotype(object):
45
45
46
46
class Personality(object):
47
47
    def __init__(self, behavior_list=[]):
48
        self.behaviors = behavior_list
48
        self.behaviors = set(behavior_list)
49
49
    
50
50
    def __iter__(self):
51
51
        for behavior in self.behaviors:
@@ -94,6 +94,9 @@ class Entity(Paintable, Updateable):
94
94
95
95
    def getPosition(self):
96
96
        return self.boundingBox.center
97
    
98
    def getStride(self):
99
        return round(self.getDimensions()[0] / 2.)
97
100
        
98
101
    def isCarryingEntity(self, type = None):
99
102
        if type: