Skip to content

Commit

Permalink
Use CreateUnit for creation and reproduction of wild animals
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.unknown-horizons.org/trunk@2748 d3468d7a-13f2-4ea4-bf00-366cb6352a40
  • Loading branch information
totycro committed Sep 7, 2009
1 parent 8e15c74 commit 9897ddd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
9 changes: 6 additions & 3 deletions horizons/command/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ def __init__(self, unit, x, y):
class CreateUnit(Command):
"""Command class that creates a unit.
@param id: Unit id that is to be created.
@param x, y: Units initial position
@param x, y: Unit's initial position
@param kwargs: Additional parameters for unit creation
"""
def __init__(self, owner_id, unit_id, x, y):
def __init__(self, owner_id, unit_id, x, y, **kwargs):
self.owner_id = owner_id
self.unit_id = unit_id
self.x = x
self.y = y
self.kwargs = kwargs

def __call__(self, issuer):
"""__call__() gets called by the manager.
@param issuer: the issuer of the command
"""
owner = WorldObject.get_object_by_id(self.owner_id)
horizons.main.session.entities.units[self.unit_id](owner=owner, x=self.x, y=self.y)
horizons.main.session.entities.units[self.unit_id](owner=owner, x=self.x, y=self.y, \
**self.kwargs)


from horizons.util.encoder import register_classes
Expand Down
3 changes: 3 additions & 0 deletions horizons/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ def __init__(self):
super(Entities, self).__init__()
self.grounds = {}
for (ground_id,) in horizons.main.db("SELECT rowid FROM data.ground"):
assert ground_id not in self.grounds
self.grounds[ground_id] = GroundClass(ground_id)

self.buildings = {}
for (building_id,) in horizons.main.db("SELECT id FROM data.building"):
assert building_id not in self.buildings
self.buildings[building_id] = BuildingClass(building_id)

self.units = {}
for (unit_id,) in horizons.main.db("SELECT id FROM data.unit"):
assert unit_id not in self.units
self.units[unit_id] = UnitClass(unit_id)

def end(self):
Expand Down
3 changes: 1 addition & 2 deletions horizons/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class Session(LivingObject):
"""
timer = livingProperty()
manager = livingProperty()
scheduler = livingProperty()
view = livingProperty()
entities = livingProperty()
ingame_gui = livingProperty()
Expand Down Expand Up @@ -124,9 +123,9 @@ def end(self):
self.ingame_gui = None
self.entities = None
self.view = None
self.scheduler = None
self.manager = None
self.timer = None
Scheduler.destroy_instance()

self.selected_instances = None
self.selection_groups = None
Expand Down
3 changes: 3 additions & 0 deletions horizons/util/python/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def __call__(self, *args, **kwargs):
self.instance = super(Singleton, self).__call__(*args, **kwargs)
return self.instance

def destroy_instance(self):
self.instance = None

class ManualConstructionSingleton(Singleton):
"""Same as Singleton, but Class() never creates an instance. Only create_instances() does."""
def __call__(self):
Expand Down
6 changes: 3 additions & 3 deletions horizons/world/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,17 @@ def init_new_world(self):
if int(self.properties.get('RandomTrees', 1)) == 1:
#print "Adding trees and animals to the world..."
from horizons.command.building import Build
from horizons.command.unit import CreateUnit
tree = horizons.main.session.entities.buildings[BUILDINGS.TREE_CLASS]
wild_animal = horizons.main.session.entities.units[UNITS.WILD_ANIMAL_CLASS]
for island in self.islands:
for tile in island.ground_map.iterkeys():
# add tree to about every third tile
if random.randint(0, 10) < 3 and "constructible" in island.ground_map[tile]().classes:
building = horizons.main.session.manager.execute( \
Build(tree,tile[0],tile[1], ownerless=True, island=island))
building = Build(tree,tile[0],tile[1], ownerless=True, island=island).execute()
building.finish_production_now() # make trees big and fill their inventory
if random.randint(0, 40) < 1: # add animal to every nth tree
wild_animal(island, x=tile[0], y=tile[1])
CreateUnit(island.getId(), UNITS.WILD_ANIMAL_CLASS, *tile).execute()

# add free trader
self.trader = Trader(99999, "Free Trader", Color())
Expand Down
19 changes: 7 additions & 12 deletions horizons/world/units/animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import random
import logging

import horizons.main
from horizons.scheduler import Scheduler

from horizons.world.production.producer import Producer
from horizons.util import Point, Circle, WorldObject
from horizons.world.pathfinding.pather import SoldierPather, BuildingCollectorPather
from horizons.command.unit import CreateUnit
from collectors import Collector, BuildingCollector, JobList, Job
from horizons.constants import WILD_ANIMAL

Expand All @@ -36,11 +36,6 @@ class Animal(Producer):
and usually produce something (e.g. wool)."""
log = logging.getLogger('world.units.animal')

def __init__(self, **kwargs):
super(Animal, self).__init__(**kwargs)

def get_collectable_res(self):
return self.get_needed_resources()

class CollectorAnimal(Animal):
"""Animals that will inherit from collector"""
Expand Down Expand Up @@ -85,6 +80,8 @@ def search_job(self):
def get_home_inventory(self):
return self.inventory

def get_collectable_res(self):
return self.get_needed_resources()

class WildAnimal(CollectorAnimal, Collector):
"""Animals, that live in the nature and feed on natural resources.
Expand All @@ -100,10 +97,9 @@ class WildAnimal(CollectorAnimal, Collector):
work_duration = 96
pather_class = SoldierPather

# see documentation of self.health
def __init__(self, island, start_hidden=False, can_reproduce = True, **kwargs):
def __init__(self, owner, start_hidden=False, can_reproduce = True, **kwargs):
super(WildAnimal, self).__init__(start_hidden=start_hidden, **kwargs)
self.__init(island, can_reproduce)
self.__init(owner, can_reproduce)
self.log.debug("Wild animal %s created at "+str(self.position)+\
"; can_reproduce: %s; population now: %s", \
self.getId(), can_reproduce, len(self.home_island.wild_animals))
Expand Down Expand Up @@ -221,9 +217,8 @@ def reproduce(self):

self.log.debug("Wild animal %s REPRODUCING", self.getId())
# create offspring
horizons.main.session.entities.units[self.id](self.home_island, \
x=self.position.x, y=self.position.y, \
can_reproduce = self.next_clone_can_reproduce())
CreateUnit(self.owner.getId(), self.id, self.position.x, self.position.y, \
can_reproduce = self.next_clone_can_reproduce())
# reset own resources
for res in self.get_consumed_resources():
self.inventory.reset(res)
Expand Down

0 comments on commit 9897ddd

Please sign in to comment.