async def put(entity: Entity, keyword: str, target: str): await load_components(entity, PositionComponent, InventoryComponent) inventory = entity.get_component(InventoryComponent) items = await search_entities_in_container_by_keyword(inventory, keyword) target_entity = await search_entity_in_sight_by_keyword( entity, target, filter_by=InventoryComponent, include_self=False ) if not target_entity: await emit_msg(entity, messages.target_not_found()) return msgs_stack = get_stacker() items_to_drop = [] for item in items: items_to_drop.append( move_entity_from_container( item, target=target_entity.get_component(InventoryComponent), current_owner=entity ) ) if not items_to_drop: await emit_msg(entity, messages.target_not_found()) return msgs_stack.add( emit_sys_msg(entity, 'remove_items', messages.items_to_message(items_to_drop)), #emit_room_sys_msg(entity, 'add_items', messages.items_to_message(items_to_drop)) # todo event for container ) if len(items_to_drop) == 1: msgs_stack.add( emit_msg(entity, messages.on_put_item(items[0], target_entity)), emit_room_msg(origin=entity, message_template=messages.on_entity_put_item(items[0], target_entity)) ) else: msgs_stack.add( emit_msg(entity, messages.on_put_multiple_items(target_entity)), emit_room_msg(origin=entity, message_template=messages.on_entity_put_multiple_items(target_entity)) ) if not await update_entities(entity, target_entity, *items_to_drop): await emit_msg(entity, messages.target_not_found()) msgs_stack.cancel() else: await msgs_stack.execute()
async def do_move_entity(entity, room, direction, reason, emit_message=True, self_emit_message=True): init_room = entity.get_room() if await cast_entity(entity, room.position, reason=reason): self_emit_message and emit_message and await emit_msg( entity, messages.movement_success(direction)) emit_message and await asyncio.gather( emit_room_msg( room=init_room, # Emit this message in the previous room. origin=entity, message_template=messages.entity_movement_success_template( direction)), emit_room_msg(origin=entity, message_template=messages. entity_movement_success_arrive_template(direction))) await getmap(entity) await look(entity)
async def drop(entity: Entity, keyword: str): await load_components(entity, PositionComponent, InventoryComponent) inventory = entity.get_component(InventoryComponent) items = await search_entities_in_container_by_keyword(inventory, keyword) msgs_stack = get_stacker() items_to_drop = [] for item in items: items_to_drop.append( move_entity_from_container( item, target=entity.get_component(PositionComponent), current_owner=entity)) if not items_to_drop: await emit_msg(entity, messages.target_not_found()) return entity.set_for_update(inventory) msgs_stack.add( emit_sys_msg(entity, 'remove_items', messages.items_to_message(items_to_drop)), emit_room_sys_msg(entity, 'add_items', messages.items_to_message(items_to_drop))) if len(items_to_drop) == 1: msgs_stack.add( emit_msg(entity, messages.on_drop_item(items[0])), emit_room_msg(origin=entity, message_template=messages.on_entity_drop_item( items[0]))) else: msgs_stack.add( emit_msg(entity, messages.on_drop_multiple_items()), emit_room_msg( origin=entity, message_template=messages.on_entity_drops_multiple_items())) if not await update_entities(entity, *items_to_drop): await emit_msg(entity, messages.target_not_found()) msgs_stack.cancel() else: await msgs_stack.execute()
async def move_entity(entity: Entity, *arguments): await load_components(entity, AttributesComponent) assert len(arguments) == 1 direction = get_direction(arguments[0]) target_room = await get_room_at_direction(entity, direction, False) if not target_room: await emit_msg(entity, messages.invalid_direction()) return if not await target_room.walkable_by(entity): await emit_msg(entity, messages.invalid_direction()) return await asyncio.gather( emit_msg(entity, messages.movement_begins(direction)), emit_room_msg(origin=entity, message_template=messages.entity_movement_begin_template( direction))) action = cancellable_scheduled_action_factory( entity, ScheduledMovement(entity, direction, target_room), wait_for=speed_component_to_movement_waiting_time(0.05)) await move_entity.schedule(action)
async def unfollow(entity): from core.src.world.builder import follow_system_manager target_entity = follow_system_manager.get_follow_target(entity.entity_id) if not target_entity: await emit_msg(entity, messages.not_following_anyone()) else: follow_system_manager.stop_following(entity.entity_id) await batch_load_components(AttributesComponent, (SystemComponent, 'receive_events'), PositionComponent, entities=(target_entity, entity)) await asyncio.gather( emit_msg( target_entity, messages.entity_stop_following_you( entity.get_component(AttributesComponent).keyword)), emit_msg( entity, messages.do_unfollow( target_entity.get_component(AttributesComponent).keyword)), emit_room_msg( origin=entity, target=target_entity, message_template=messages.entity_follows_entity_template()))
async def pick(entity: Entity, *arguments): keyword = arguments[0] if len(arguments) == 1: room = await get_current_room(entity) items_to_pick = await search_entities_in_room_by_keyword( room, keyword, filter_by=(AttributesComponent, 'collectible', True)) container_entity = None elif len(arguments) == 2: container_keyword = arguments[1] container_entity = await search_entity_in_sight_by_keyword( entity, container_keyword, filter_by=InventoryComponent, include_self=False) await load_components(container_entity, InventoryComponent) if not container_entity: await emit_msg(entity, messages.target_not_in_room()) return container_inventory = container_entity.get_component( InventoryComponent) items_to_pick = await search_entities_in_container_by_keyword( container_inventory, keyword) else: raise ValueError('max 2 arguments') if not items_to_pick: await emit_msg(entity, messages.target_not_in_room()) return await load_components(entity, InventoryComponent) inventory = entity.get_component(InventoryComponent) for item in items_to_pick: move_entity_from_container(item, target=inventory, current_owner=container_entity) msgs_stack = get_stacker() if len(items_to_pick) == 1: if container_entity: # SINGLE ITEM FOUND IN CONTAINER msgs_stack.add( emit_msg( entity, messages.item_picked_from_container( items_to_pick[0], container_entity)), emit_room_msg(origin=entity, message_template=messages. entity_picked_item_from_container( items_to_pick[0], container_entity)), ) else: # SINGLE ITEM FOUND IN ROOM msgs_stack.add( emit_msg(entity, messages.item_picked(items_to_pick[0])), emit_room_msg(origin=entity, message_template=messages.entity_picked_item( items_to_pick[0])), ) else: if container_entity: # MULTIPLE ITEMS FOUND IN CONTAINER msgs_stack.add( emit_msg( entity, messages.picked_multiple_items_from_container( container_entity)), emit_room_msg(origin=entity, message_template=messages. entity_picked_multiple_items_from_container( container_entity)), ) else: # MULTIPLE ITEMS FOUND IN ROOM msgs_stack.add( emit_msg(entity, messages.picked_multiple_items()), emit_room_msg( origin=entity, message_template=messages.entity_picked_multiple_items()), ) if items_to_pick: msgs_stack.add( emit_sys_msg(entity, 'add_items', messages.items_to_message(items_to_pick)), ) if not container_entity: msgs_stack.add( emit_room_sys_msg(entity, 'remove_items', messages.items_to_message(items_to_pick))) if not await update_entities( entity, *items_to_pick, *(container_entity and (container_entity, ) or ())): await emit_msg(entity, messages.cannot_pick_item()) msgs_stack.cancel() else: await msgs_stack.execute()