model.world module

Module that holds classes that represent the world.

class model.world.World

Bases: events.EventListener

Class that represents the world.

add_complex_enact_logic(callback, action=None)

Add a complex enact logic callback. The callback will be called when all agents have prepared their interaction. The callback receives an instance of the world and a mapping of all agents to their prepared interactions.

The callback can handle on zero, one, or more agents present in the mapping. Not all agents have to be handled by the callback. The callback should manipulate the world state (e.g., move one or more agents) and it should return a mapping of agents to primitive interactions the agents have actually enacted for each agent the callback handled.

Multiple complex callbacks can be registered. The callbacks are processed in a first-come first-out basis (i.e., callbacks registered first will process interactions first).

Parameters:
  • callback – The callable to add
  • action – Optional, if set the callback will only receive agents trying to enact the interaction with the given action
add_enact_logic(agent, callback_dict)

Set the enact logic for a given agent. callback_dict is a dictionary (map) of primitive interactions to callbacks. The callbacks will be called when the agent attempts to enact a certain (primitive) interaction. The callback receives as parameters an intance of the world, the agent and the primitive interaction that was enacted.

The callback should manipulate the world state (e.g., move the agent) and return the primitive interaction that was interacted (potentially different from the interaction that was intended to be interacted).

add_entity(entity)
add_mutate_callback(callback)

Add a world mutate callback method, to be called before enaction in a tick.

build_position_entity_map()

Builds the position-entity map. This is used for fast lookup of entity positions.

can_step(agent)
collidable_entity_at(position)

Test whether there is a collidable entity at the given position.

Parameters:position – The position to check for entities at
Returns:A boolean indicating whether there is a collidable entitity at the given position
enact(agents_data)

Let all agents enact their prepared interaction.

Parameters:agents_data – The agent and data mapping as generated in self.prepare.
entity_rect_collision(rect)

Test whether an entitity collides with the given rectangle.

Parameters:rect – The rectangle to check for whether there are entities colliding with it
Returns:A boolean indicating whether there is a collidable entitity within the given rectangle
get_agents()

Get all agent entities in the world :return: All agent entities in the world

get_entities()

Get all entities (structures and agents) in the world :return: All entities in the world

get_entities_at(position)

Get the entities that are at a given position

Parameters:position – The position for which to get all entities
Returns:All entities at the given position
get_entities_in_front(entity)

Get the entities that are in front of the given entity.

Parameters:entity – The entity for which we should get the entities that are in front of it
get_entities_of_type(type)

Get all entities of a specific type in the world :param type: The type of entities to get :return: All agent entities of a specific type in the world

get_free_positions()

Get all positions without any entities :return: A list of positions without entities.

get_height()
get_width()
notify(event)
prepare(agents)

Let all agents prepare their next interaction. Store the interaction they wish to enact and any potential (optional) data the agents return.

Parameters:agents – The agents to have prepare their interactions.
Returns:A dictionary of agents mapping to a tuple with the interaction they wish to enact and the data returned by their preparation (this data is to be delivered back to the agents (unmutated) when they are told which interaction was enacted).
remove_complex_enact_logic(callback)
Parameters:callback – The callable to remove
remove_entity(entity)
set_height(height)
set_width(width)