Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Animals.py
blob: 3eb660dc68415bbce42b9134913b1a1fd444753b (plain)
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
### By Gonzalo Odiard, 2006 godiard at gmail.com
### GPL License - http://www.gnu.org/copyleft/gpl.html

import gobject, gtk , cairo,os
import random
import World

print "Init aimals"

def getRandomDirection():
    return 1+int(random.random()*7)        

#    Deltas 
#    1 2 3
#    8 X 4
#    7 6 5

def getNextDirection(direction):
    if (direction == 8):
        return 1
    else:
        return direction + 1

def getDeltaX(direction):
    #print "getDeltaX",direction
    if (direction == 1) or (direction == 8) or (direction == 7):
        return -1
    if (direction == 2) or (direction == 6):
        return 0
    if (direction == 3) or (direction == 4) or (direction == 5):
        return 1
    return 0
def getDeltaY(direction):
    #print "getDeltaY",direction
    if (direction == 1) or (direction == 2) or (direction == 3):
        return -1
    if (direction == 8) or (direction == 4):
        return 0
    if (direction == 7) or (direction == 6) or (direction == 5):
        return 1
    return 0


def getState(world,x,y):
    # verify limits
    if (x >= World.CANT_TILES-1):
        x = 0
    if (y >= World.CANT_TILES-1):
        y = 0
    if (x < 0):
        x = World.CANT_TILES-2
    if (y < 0):
        y = World.CANT_TILES-2
    #print "getState",x,y,world.state[x][y].STATE
    return world.state[x][y].STATE

def internalMove(world,x,y,direction):
    tentativeX = x + int(getDeltaX(direction))           
    tentativeY = y + int(getDeltaY(direction))
    if (tentativeX >= World.CANT_TILES-1):
        tentativeX = 0
    if (tentativeY >= World.CANT_TILES-1):
        tentativeY = 0
    if (tentativeX < 0):
        tentativeX = World.CANT_TILES-2
    if (tentativeY < 0):
        tentativeY = World.CANT_TILES-2
    return tentativeX,tentativeY



class AbstractAnimal:
    
    edadMaxima = 100
    madurezSexual = 50
    nivelCadenaAlimenticia = 1
    minFrecuenciaAlimentacion = 10
    maxFrecuenciaAlimentacion = 1
    especie = "Indefinida"
    maxNumeroCrias = 4
    frecuenciaSexual = 10

    def __init__(self,x,y):
        print "Iniciando AbstractAnimal"
        self.orden = 0
        self.edad = 30
        self.sexo = "F"
        self.ultimaActSexual = self.edad
        self.ultimaAlimentacion = self.edad
        self.posX = x
        self.posY = y
        self.ultimaDireccion = -1

    def draw(ctx):
        print "Draw"

    def move(self):
        print "Move"

    def checkLive(self):
        if (self.edad > self.edadMaxima):
            print "Muere por edad"
            return False    
        elif ((self.edad - self.ultimaAlimentacion)  > self.minFrecuenciaAlimentacion):
            print "Muere por falta de alimentacion"
            return False
        else:
            return True

# RABBIT        

class Rabbit(AbstractAnimal):
    #print "Inicio conejo"

    path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"images")

    imageSmallMale = cairo.ImageSurface.create_from_png (os.path.join(path,"rabbit_small_m.png"))
    imageSmallFem = cairo.ImageSurface.create_from_png (os.path.join(path,"rabbit_small_f.png"))
    imageMale = cairo.ImageSurface.create_from_png (os.path.join(path,"rabbit_m.png"))
    imageFem = cairo.ImageSurface.create_from_png (os.path.join(path,"rabbit_f.png"))

    especie = "Rabbit"


    def __init__(self,x,y,world):
        #print "Iniciando Rabbit"
        self._world = world
        
        self.orden = 0
        self.edad = world.rabbit_data.madurezSexual
        self.sexo = "F"
        if (random.random() > 0.5):
            self.sexo = "M"
        self.ultimaActSexual = self.edad
        self.ultimaAlimentacion = self.edad
        self.posX = x
        self.posY = y
        self.ultimaDireccion = -1
        self.edadMaxima = world.rabbit_data.edadMaxima
        self.madurezSexual = world.rabbit_data.madurezSexual
        self.frecuenciaSexual = world.rabbit_data.frecuenciaSexual
        self.nivelCadenaAlimenticia = world.rabbit_data.nivelCadenaAlimenticia
        self.minFrecuenciaAlimentacion = world.rabbit_data.minFrecuenciaAlimentacion
        self.maxFrecuenciaAlimentacion = world.rabbit_data.maxFrecuenciaAlimentacion
        self.maxNumeroCrias = world.rabbit_data.maxNumeroCrias

    def draw(self,ctx):
        #print "Draw"
        x1 = World.MARGEN+(self.posX*World.SIZE_TILE)
        y1 = World.MARGEN +(self.posY*World.SIZE_TILE)
        ctx.move_to(x1,y1)
        ctx.translate(x1,y1)
        ctx.rectangle(0,0,World.SIZE_TILE,World.SIZE_TILE);
        ctx.set_source_rgb(146.0/256.0,98.0/256.0,46.0/256.0)    
        if (self.sexo == "M"):
            if (self.edad < self.madurezSexual):
                ctx.set_source_surface(Rabbit.imageSmallMale,1,1)
            else:
                ctx.set_source_surface(Rabbit.imageMale,1,1)
        if (self.sexo == "F"):
            if (self.edad < self.madurezSexual):
                ctx.set_source_surface(Rabbit.imageSmallFem,1,1)
            else:
                ctx.set_source_surface(Rabbit.imageFem,1,1)

        ctx.fill()


    def move(self,world):
        self.edad = self.edad + 1
        
        # eat
        if (world.state[self.posX][self.posY].STATE >=1):
            world.state[self.posX][self.posY].STATE = world.state[self.posX][self.posY].STATE - 3
            if (world.state[self.posX][self.posY].STATE <0):
                world.state[self.posX][self.posY].STATE = 0
            self.ultimaAlimentacion = self.edad

        # move
        if (self.ultimaDireccion == -1) :
            self.ultimaDireccion = getRandomDirection()
        # verify green in the next position
        self.tentativeX,self.tentativeY = internalMove(world,self.posX,self.posY,self.ultimaDireccion)
        self.tentativeDirection = getNextDirection(self.ultimaDireccion)
         
        while ((getState(world,self.tentativeX,self.tentativeY) == 0) and 
            (self.tentativeDirection != self.ultimaDireccion)):
            #print self.tentativeX, self.tentativeY, self.tentativeDirection, self.ultimaDireccion            
            self.tentativeX,self.tentativeY = internalMove(world,self.posX,self.posY,self.tentativeDirection)
            self.tentativeDirection = getNextDirection(self.tentativeDirection)

        self.posX = self.tentativeX
        self.posY = self.tentativeY

        # if female verify for male rabbit
        if ((self.sexo == "F") and 
            (self.edad > self.madurezSexual) and
            ((self.edad - self.ultimaActSexual) > self.frecuenciaSexual)):
            
            animalsNearArray = world.animalsNear(self.posX,self.posY)    
            if (len(animalsNearArray) > 0):
                maleOk = False
                n = 0
                while (n < len(animalsNearArray) and not (maleOk)):
                    nearAnimal = animalsNearArray[n]
                    if ((nearAnimal.sexo == "M") and 
                        (nearAnimal.especie == self.especie) and
                        (nearAnimal.edad > self.madurezSexual) and
                        ((nearAnimal.edad - nearAnimal.ultimaActSexual) > self.frecuenciaSexual)):
                        # born rabbits
                        self.ultimaActSexual = self.edad
                        cantCrias = int(random.random()*self.maxNumeroCrias)
                        for n in range(cantCrias):
                            child = Rabbit(self.posX,self.posY,self._world)
                            world.animals.append(child)
                            print "Nace conejo"
                        # salgo del while
                        maleOk = True

                    n = n + 1


# FOX

class Fox(AbstractAnimal):
    #print "Inicio zorro"

    path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"images")

    imageSmallMale = cairo.ImageSurface.create_from_png (os.path.join(path,"fox_small_m.png"))
    imageSmallFem = cairo.ImageSurface.create_from_png (os.path.join(path,"fox_small_f.png"))
    imageMale = cairo.ImageSurface.create_from_png (os.path.join(path,"fox_m.png"))
    imageFem = cairo.ImageSurface.create_from_png (os.path.join(path,"fox_f.png"))

    especie = "Fox"

    def __init__(self,x,y,world):
        #print "Iniciando Fox"
        self._world = world
        
        self.orden = 0
        self.edad = 1
        self.sexo = "F"
        if (random.random() > 0.5):
            self.sexo = "M"
        self.ultimaActSexual = self.edad
        self.minFrecuenciaAlimentacion = 30
        self.ultimaAlimentacion = self.edad
        self.posX = x
        self.posY = y
        self.ultimaDireccion = -1
        self.maxNumeroCrias = 2

        self.edadMaxima = world.fox_data.edadMaxima
        self.madurezSexual = world.fox_data.madurezSexual
        self.frecuenciaSexual = world.fox_data.frecuenciaSexual
        self.nivelCadenaAlimenticia = world.fox_data.nivelCadenaAlimenticia
        self.minFrecuenciaAlimentacion = world.fox_data.minFrecuenciaAlimentacion
        self.maxFrecuenciaAlimentacion = world.fox_data.maxFrecuenciaAlimentacion
        self.maxNumeroCrias = world.fox_data.maxNumeroCrias


    def draw(self,ctx):
        #print "Draw"
        x1 = World.MARGEN+(self.posX*World.SIZE_TILE)
        y1 = World.MARGEN +(self.posY*World.SIZE_TILE)
        ctx.move_to(x1,y1)
        ctx.translate(x1,y1)
        ctx.rectangle(0,0,World.SIZE_TILE,World.SIZE_TILE);
        ctx.set_source_rgb(146.0/256.0,98.0/256.0,46.0/256.0)    
        if (self.sexo == "M"):
            if (self.edad < self.madurezSexual):
                ctx.set_source_surface(Fox.imageSmallMale,1,1)
            else:
                ctx.set_source_surface(Fox.imageMale,1,1)
        if (self.sexo == "F"):
            if (self.edad < self.madurezSexual):
                ctx.set_source_surface(Fox.imageSmallFem,1,1)
            else:
                ctx.set_source_surface(Fox.imageFem,1,1)

        ctx.fill()


    def move(self,world):
        self.edad = self.edad + 1
        
        # move
        if (self.ultimaDireccion == -1) :
            self.ultimaDireccion = getRandomDirection()
        # verify green in the next position
        self.tentativeX,self.tentativeY = internalMove(world,self.posX,self.posY,self.ultimaDireccion)
        self.tentativeDirection = getNextDirection(self.ultimaDireccion)
         
        while ((getState(world,self.tentativeX,self.tentativeY) == 0) and 
            (self.tentativeDirection != self.ultimaDireccion)):
            #print self.tentativeX, self.tentativeY, self.tentativeDirection, self.ultimaDireccion            
            self.tentativeX,self.tentativeY = internalMove(world,self.posX,self.posY,self.tentativeDirection)
            self.tentativeDirection = getNextDirection(self.tentativeDirection)

        self.posX = self.tentativeX
        self.posY = self.tentativeY

        # eat
        animalsNearArray = world.animalsNear(self.posX,self.posY)    
        if ((self.edad - self.ultimaAlimentacion) > self.maxFrecuenciaAlimentacion ):
            if (len(animalsNearArray) > 0):
                n = 0
                eat = False
                while ((n < len(animalsNearArray)) and not eat):
                    nearAnimal = animalsNearArray[n]
                    if (nearAnimal.nivelCadenaAlimenticia < self.nivelCadenaAlimenticia):
                        print "Comer conejo!!"                
                        n = n+1
                        world.animals.remove(nearAnimal)
                        eat = True
                        self.ultimaAlimentacion = self.edad
                    n = n+1

        # if female verify for male fox
        if ((self.sexo == "F") and 
            (self.edad > self.madurezSexual) and
            ((self.edad - self.ultimaActSexual) > self.frecuenciaSexual)):
            
            if (len(animalsNearArray) > 0):
                maleOk = False
                n = 0
                while (n < len(animalsNearArray) and not (maleOk)):
                    nearAnimal = animalsNearArray[n]
                    if ((nearAnimal.sexo == "M") and 
                        (nearAnimal.especie == self.especie) and
                        (nearAnimal.edad > self.madurezSexual) and
                        ((nearAnimal.edad - nearAnimal.ultimaActSexual) > self.frecuenciaSexual) ):
                        #print "IUPI!!!!"
                        # born foxs
                        self.ultimaActSexual = self.edad
                        cantCrias = int(random.random()*self.maxNumeroCrias)
                        for n in range(cantCrias):
                            child = Fox(self.posX,self.posY,self._world)
                            world.animals.append(child)
                            print "Nace zorro"
                        # salgo del while
                        maleOk = True

                    n = n + 1