Package horizons :: Package command :: Module unit
[hide private]
[frames] | no frames]

Source Code for Module horizons.command.unit

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from horizons.command import Command, GenericCommand 
 23  from horizons.entities import Entities 
 24  from horizons.util.worldobject import WorldObject, WorldObjectNotFound 
 25   
 26   
27 -class GenericUnitCommand(GenericCommand):
28 """Same as GenericCommand, but checks if issuer == owner in __call__"""
29 - def __call__(self, issuer):
30 try: 31 unit = self._get_object() 32 except WorldObjectNotFound as e: 33 self.log.warning("Tried to call a unit command on an inexistent unit. It could have been killed: %s", e) 34 return 35 if unit.owner.worldid != issuer.worldid: 36 return # don't move enemy units 37 else: 38 return super().__call__(issuer)
39 40
41 -class Act(GenericUnitCommand):
42 """Command class that moves a unit. 43 @param unit: Instance of Unit 44 @param x, y: float coordinates where the unit is to be moved. 45 """
46 - def __init__(self, unit, x, y):
47 super().__init__(unit, "go", x, y)
48 49 50 GenericCommand.allow_network(Act) 51 52
53 -class Attack(GenericUnitCommand):
54 """Command class that triggers attack 55 @param unit: Instance of Unit 56 @param target: Instance of Target 57 """
58 - def __init__(self, unit, target):
59 super().__init__(unit, "user_attack", target.worldid)
60 61 62 GenericCommand.allow_network(Attack) 63 64
65 -class RemoveUnit(GenericUnitCommand):
66 """ 67 Command class that removes the unit. Not to be used if .remove() is going to be called through an indirect command anyway. 68 @param unit: Instance of Unit 69 """
70 - def __init__(self, unit):
71 super().__init__(unit, "remove")
72 73 74 GenericCommand.allow_network(RemoveUnit) 75 76
77 -class CreateRoute(GenericUnitCommand):
78 """Command class that moves a unit. 79 @param unit: Instance of Unit 80 @param x, y: float coordinates where the unit is to be moved. 81 """
82 - def __init__(self, unit):
83 super().__init__(unit, "create_route")
84 85 86 GenericCommand.allow_network(CreateRoute) 87 88
89 -class CreateUnit(Command):
90 """Command class that creates a unit. 91 TODO: remove this command and put the code in a method in e.g. world. 92 Commands are there for user interactions, and there is no user interaction, that creates a unit 93 You always only add a production that creates then units, but that is simulated on every machine 94 95 """
96 - def __init__(self, owner_id, unit_id, x, y, **kwargs):
97 """ 98 @param session: Session instance 99 @param id: Unit id that is to be created. 100 @param x, y: Unit's initial position 101 @param kwargs: Additional parameters for unit creation 102 """ 103 self.owner_id = owner_id 104 self.unit_id = unit_id 105 self.x = x 106 self.y = y 107 self.kwargs = kwargs
108
109 - def __call__(self, issuer):
110 """__call__() gets called by the manager. 111 @param issuer: the issuer of the command 112 """ 113 owner = WorldObject.get_object_by_id(self.owner_id) 114 unit = Entities.units[self.unit_id](session=owner.session, owner=owner, 115 x=self.x, y=self.y, **self.kwargs) 116 unit.initialize() 117 return unit
118 119 120 GenericCommand.allow_network(CreateUnit) 121 122
123 -class SetStance(GenericUnitCommand):
124 """Command class that moves a unit. 125 @param unit: Instance of Unit 126 @param stance: stance as string representation 127 """
128 - def __init__(self, unit, stance):
129 super().__init__(unit, "set_stance", stance.NAME)
130
131 - def __call__(self, issuer):
132 # we need the stance component, so resolve the name 133 self.args = (self._get_object().get_component_by_name(self.args[0]), ) 134 return super().__call__(issuer)
135 136 137 GenericCommand.allow_network(SetStance) 138