Package horizons :: Package scenario :: Module actions
[hide private]
[frames] | no frames]

Source Code for Module horizons.scenario.actions

  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  import math 
 23   
 24  import horizons.globals 
 25  from horizons.command.game import PauseCommand, UnPauseCommand 
 26  from horizons.command.unit import CreateUnit 
 27  from horizons.constants import MESSAGES 
 28  from horizons.i18n import gettext as T 
 29  from horizons.messaging import SettlerUpdate 
 30  from horizons.scheduler import Scheduler 
 31  from horizons.util.python.callback import Callback 
 32  from horizons.util.python.registry import Registry 
 33  from horizons.util.shapes import Circle, Point 
 34  from horizons.util.worldobject import WorldObject 
 35   
 36  from .conditions import CONDITIONS 
37 38 39 -class ActionsRegistry(Registry):
40 """Class that holds all available action functions."""
41 - def register_function(self, func, name=None):
42 """Register action. 43 44 By default, the function's name is used as identifier of the action. You can supply 45 a `name` parameter to use instead. 46 """ 47 self.registry[name or func.__name__] = func
48 49 50 ACTIONS = ActionsRegistry() 51 register = ACTIONS.register 52 53 54 @register(name='message')
55 -def show_message(session, type=None, *messages):
56 """Shows a message with custom text in the messagewidget. 57 If you pass more than one message, they are shown simultaneously.""" 58 visible_ticks = Scheduler().get_ticks(MESSAGES.CUSTOM_MSG_VISIBLE_FOR) 59 60 return [session.ingame_gui.message_widget.add_custom(msg, msg_type=type, visible_for=visible_ticks) 61 for msg in messages]
62 63 64 @register(name='db_message')
65 -def show_db_message(session, database_message_id):
66 """Shows a message with predefined text in the messagewidget.""" 67 session.ingame_gui.message_widget.add(database_message_id)
68 69 70 @register(name='logbook')
71 -def show_logbook_entry_delayed(session, *parameters):
72 """Shows a logbook entry and opens the logbook after some seconds. 73 Displays a YAML-defined notification message on logbook close. 74 75 @param parameters: list of logbook parameters, including their values. 76 See widgets.logbook:add_captainslog_entry for parameter documentation. 77 """ 78 def write_logbook_entry(session, parameters): 79 """Adds an entry to the logbook and displays it. 80 On logbook close, displays a notification message defined in the YAML.""" 81 session.ingame_gui.logbook.add_captainslog_entry(parameters, show_logbook=True)
82 delay = MESSAGES.LOGBOOK_DEFAULT_DELAY 83 callback = Callback(write_logbook_entry, session, parameters) 84 Scheduler().add_new_object(callback, session.scenario_eventhandler, run_in=Scheduler().get_ticks(delay)) 85 86 87 @register(name='win')
88 -def do_win(session):
89 """The player wins the current scenario.""" 90 PauseCommand().execute(session) 91 show_db_message(session, 'YOU_HAVE_WON') 92 horizons.globals.fife.play_sound('effects', "content/audio/sounds/events/scenario/win.ogg") 93 94 continue_playing = session.ingame_gui.open_popup(T("You have won!"), 95 T("You have completed this scenario.") + " " + 96 T("Do you want to continue playing?"), 97 show_cancel_button=True) 98 if not continue_playing: 99 Scheduler().add_new_object(session.quit, session, run_in=0) 100 else: 101 UnPauseCommand().execute(session)
102 103 104 @register(name='goal_reached')
105 -def goal_reached(session, goal_number):
106 """The player reaches a certain goal in the current scenario.""" 107 # This method is kept to make some tests happy. 108 pass
109 110 111 @register(name='lose')
112 -def do_lose(session):
113 """The player fails the current scenario.""" 114 show_db_message(session, 'YOU_LOST') 115 horizons.globals.fife.play_sound('effects', 'content/audio/sounds/events/scenario/lose.ogg') 116 # drop events after this event 117 Scheduler().add_new_object(session.scenario_eventhandler.drop_events, session.scenario_eventhandler)
118
119 120 @register() 121 -def set_var(session, variable, value):
122 """Assigns values to scenario variables. Overwrites previous assignments to the same variable.""" 123 session.scenario_eventhandler._scenario_variables[variable] = value 124 check_callbacks = Callback.ChainedCallbacks( 125 Callback(session.scenario_eventhandler.check_events, CONDITIONS.var_eq), 126 Callback(session.scenario_eventhandler.check_events, CONDITIONS.var_lt), 127 Callback(session.scenario_eventhandler.check_events, CONDITIONS.var_gt) 128 ) 129 Scheduler().add_new_object(check_callbacks, session.scenario_eventhandler, run_in=0)
130
131 132 @register() 133 -def wait(session, seconds):
134 """Postpones any other scenario events for a certain amount of seconds.""" 135 delay = Scheduler().get_ticks(seconds) 136 session.scenario_eventhandler.sleep(delay)
137
138 139 @register() 140 -def alter_inventory(session, resource, amount):
141 """Alters the inventory of each settlement.""" 142 # NOTE avoid circular import 143 from horizons.component.storagecomponent import StorageComponent 144 for settlement in session.world.settlements: 145 if settlement.owner == session.world.player and settlement.warehouse: 146 settlement.warehouse.get_component(StorageComponent).inventory.alter( 147 resource, amount)
148
149 150 @register() 151 -def highlight_position(session, where, play_sound=False, color=(0, 0, 0)):
152 """Highlights a position on the minimap. 153 where: (x, y) coordinate tuple 154 color is a optional parameter that defines the color of the highlight. """ 155 session.ingame_gui.minimap.highlight(where, color=color) 156 if play_sound: 157 horizons.globals.fife.play_sound('effects', 'content/audio/sounds/ships_bell.ogg')
158 159 160 @register(name='change_increment')
161 -def change_tier(session, tier):
162 """ Changes the tier of the settlements. """ 163 for settlement in session.world.settlements: 164 if settlement.owner == session.world.player: 165 # Settler levels are zero-based! 166 SettlerUpdate.broadcast(settlement.warehouse, tier - 1, tier - 1)
167
168 169 @register() 170 -def spawn_ships(session, owner_id, ship_id, number, *position):
171 """ 172 Creates a number of ships controlled by a certain player around a position on the map. 173 @param owner_id: the owner worldid 174 @param ship_id: the ship id 175 @param number: number of ships to be spawned 176 @param position: position around the ships to be spawned 177 """ 178 center = Point(*position) 179 player = WorldObject.get_object_by_id(owner_id) 180 # calculate a radius that should fit all the ships 181 # if it doesn't fit them all increase the radius 182 radius = int(math.sqrt(number)) 183 while number != 0: 184 for point in Circle(center, radius): 185 if (point.x, point.y) in session.world.ship_map \ 186 or session.world.get_island(point) is not None: 187 continue 188 CreateUnit(owner_id, ship_id, point.x, point.y)(issuer=player) 189 number -= 1 190 if number == 0: 191 break 192 radius += 1
193