Package horizons :: Package scenario :: Module scenarioeventhandler :: Class ScenarioEventHandler
[hide private]
[frames] | no frames]

Class ScenarioEventHandler

source code

util.living.LivingObject --+
                           |
                          ScenarioEventHandler

Handles event, that make up a scenario. See wiki. An instance of this class is bound to a set of events. On a new scenario, you need a new instance.

Scenarios consist of condition-action events. When all conditions of an event become true, the action is executed and the event is removed from the scenario. All events only happen once.

Whenever the game state changes in a way, that can change the truth value of a condition, the event handler must be notified. It will then check all relevant events. It is imperative for this notification to always be triggered, else the scenario gets stuck. For conditions, where this approach doesn't make sense (e.g. too frequent changes), a periodic check can be used.

Save/load works by dumping all info into a yaml string in the savegame, which is loaded just like normal scenarios are loaded.

Instance Methods [hide private]
 
__init__(self, session, scenariofile=None) source code
 
start(self) source code
 
sleep(self, ticks)
Sleep the ScenarioEventHandler for number of ticks.
source code
 
_reduce_sleep(self) source code
 
end(self)
Put all the code the object needs to end safely here.
source code
 
save(self, db) source code
 
load(self, db) source code
 
schedule_check(self, condition)
Let check_events run in one tick for condition.
source code
 
schedule_action(self, action) source code
 
check_events(self, condition)
Checks whether an event happened.
source code
 
get_map_file(self) source code
 
drop_events(self)
Removes all events.
source code
 
_apply_data(self, data)
Apply data to self loaded via from yaml
source code
 
_scheduled_check(self)
Check conditions that can only be checked periodically
source code
 
_remove_event(self, event) source code
 
to_yaml(self)
Returns yaml representation of current state of self.
source code
Class Methods [hide private]
 
get_metadata_from_file(cls, filename)
Returns metadata dictionary from a yaml scenario file.
source code
 
_parse_yaml_file(cls, filename) source code
Static Methods [hide private]
 
_parse_yaml(string_or_stream) source code
Class Variables [hide private]
  CHECK_CONDITIONS_INTERVAL = 3
  PICKLE_PROTOCOL = 2
Method Details [hide private]

__init__(self, session, scenariofile=None)
(Constructor)

source code 
Parameters:
  • session - Session instance
  • scenariofile - yaml file that describes the scenario @throws InvalidScenarioFileFormat on yaml parse error

sleep(self, ticks)

source code 

Sleep the ScenarioEventHandler for number of ticks. This delays all callbacks by the specific amount

end(self)

source code 

Put all the code the object needs to end safely here. Make sure it always contains the super(YOUROBJECT, self).end() call, to ensure all parentobjects are deinited correctly.

Overrides: util.living.LivingObject.end
(inherited documentation)

schedule_check(self, condition)

source code 

Let check_events run in one tick for condition. Useful for lag prevetion if time is a critical factor, e.g. when the user has to wait for a function to return..

check_events(self, condition)

source code 

Checks whether an event happened.

Parameters:
  • condition - condition from enum conditions that changed

get_metadata_from_file(cls, filename)
Class Method

source code 

Returns metadata dictionary from a yaml scenario file.

Dictionary contains "unknown" for all of these fields if not specified in the scenario file:

  • difficulty
  • author
  • description

@throws InvalidScenarioFileFormat on yaml parse error

drop_events(self)

source code 

Removes all events. Useful when player lost.

_apply_data(self, data)

source code 

Apply data to self loaded via from yaml

Parameters:
  • data - return value of yaml.load or _parse_yaml resp.

to_yaml(self)

source code 

Returns yaml representation of current state of self. Another object of this type, constructed with the return value of this function, has to result in the very same object.