"""an attempt to study what happens when being (requires energy).""" import random PLACE_START_ENERGY = 10 STARVELIMIT = 0.33 WEAKNESS = 0.12 class Being: #to be able to have many def __init__(self, name="x", place=None): self.name = name self.energy = 1.0 self.health = 1.0 self.place = place #these are now 'platial' (embodied?) if self.place is not None: self.place.add(self) self.died = False self.was_asked_on_turn = 0 def request_energy(self, other): self.was_asked_on_turn = self.place.turn if self.health < 0: print self.name, "is dead, being requested for energy .. is there some in the body, that could not use self - should that go to the requester", other.name, "now?" return 0 if self.energy > STARVELIMIT: #not starving marginal = 0 #0.1 extra = self.energy - (STARVELIMIT + marginal) if extra > 0: self.energy -= extra print self.name, "is giving", extra, "to", other.name return extra else: return 0 else: print self.name, "is starving and not giving anything to", other.name, ".. but is trying to get help" my_helpers = self.place.get_pot_helpers(self) others_helpers = self.place.get_pot_helpers(other) new_helpers = set(my_helpers) - set(others_helpers) if new_helpers: for h in new_helpers: if h.was_asked_on_turn != self.place.turn: print ".. from", h e = h.request_energy(other) #not asking for self! #but relaying.. #makes sense with proximal thing only, #like soyabeing if e > 0: print self.name, "relays help from", h.name, "to", other.name return e return 0 def be(self): """ does something motivate having energy consumption here? perhaps this is what the body uses .. so this is an embodied being? atm. not positioned tho, and without inputs / outputs. kinda makes sense that the world appears as a global variable, wrapped in a getter tho """ if self.health == 1.0: #no worries or problems, self.energy -= 0.01 #can act fully else: self.energy -= self.health / 100 #can not live fully if self.energy < WEAKNESS: #got weak, unable to operate well #needs to be helped by others print self.name, "is sick - requesting care taking:" for o in self.place.get_pot_helpers(self): energy = o.request_energy(self) if energy > 0: self.energy += energy if self.energy > STARVELIMIT: return True elif self.energy < STARVELIMIT: #getting weak self.health -= STARVELIMIT - self.energy self.energy += self.place.get_energy(STARVELIMIT) #needs some to recover elif self.energy < 0.35: #healthy hunger self.energy += self.place.get_energy(0.7) elif self.energy > 0.5: if self.health < 1: self.health += 0.05 if self.energy < 0 or self.health < 0: #asks from others as a last resort for o in self.place.get_pot_helpers(self): self.energy += o.request_energy(self) if self.energy > 0: self.health += self.energy #err self.energy = 0 if self.health > 0: return True #was saved, for a moment at least print self.name, "died on turn %d." % self.place.turn self.die() def die(self): #def no(): # return False #self.be = no #lambda _: False self.be = None def __repr__(self): return "[Being %s %f %f]" % (self.name, self.energy, self.health) class Saviour(Being): def request_energy(self, other): print self.name, "blesses", other.name return 1.0 def be(self): pass class Place: START_ENERGY = PLACE_START_ENERGY def __init__(self, beings=None): self.energy = Place.START_ENERGY self.beings = set() if beings is not None: self.beings.extend(beings) #may crash, now that it is a set #.. but i like the idea of this not having ordering ;p def add(self, being): self.beings.add(being) being.place = self #no being can be in several places. ok? def others(self, being): return set(self.beings) - set([being]) def others_alive(self, being): return [o for o in self.others(being) if o.be] def get_pot_helpers(self, being): return self.others_alive(being) def get_energy(self, requester, request=1.0): #can modules have properties? if not request > self.energy: self.energy -= request return request else: rest = self.energy self.energy = 0 return rest if __name__ == '__main__': p = Place() for i in range(10): p.add(Being(str(i))) #beings.append(Saviour('s')) turn = 0 hope = True while hope: hope = False #please bare with how procedural programming is #-this is not a declaration, just a way to make a dependency #between the following code, that goes thru the beings, and # .. what happens to them? turn += 1 p.turn = turn p.energy += random.random() / 30 #what if this was a sine wave, or something? for b in p.beings: if b.be: #is a function/method (instance callable), and not None (dead) b.be() print "[%d] name:%s energy:%f, health:%f, env: %f" % (turn, b.name, b.energy, b.health, p.energy)#, beings hope = True else: print b.name, "is no more." #if b.health < 0: print "stopped at turn", turn #let yourself reflect me, #don't let yourself forget me.