Esempio n. 1
0
def drop_object(obj_name, me):
    pos = _WORLD.component_for_entity(me, Position)
    inventory = _WORLD.component_for_entity(me, Inventory)
    skeleton = inventory.objects.get(obj_name, None)
    success: bool = False
    msg: str = "Something went wrong"
    if skeleton is None:
        msg = f'Not holding object {obj_name}'
    else:
        reply_channel = Store(_ENV)
        drop_offset = (pos.center[0], pos.center[1] + pos.h)
        drop_payload = ObjectManager.DropPayload(
            obj_name, ObjectManager.ObjectManagerOps.RECREATE, skeleton,
            drop_offset, reply_channel)
        _EVENT_STORE.put(EVENT(ObjectManager.ManagerTag, drop_payload))
        # Wait for reply
        response = yield reply_channel.get()
        if response.get('success', False):
            success = True
            msg = ''
    response = RESPONSE_ClawPayload(op=ClawOps.DROP,
                                    ent=me,
                                    success=success,
                                    msg=msg)
    event_to_put = EVENT(ClawDoneTag, response)
    _EVENT_STORE.put(event_to_put)
    return success, msg
Esempio n. 2
0
    def process(kwargs):
        # Init
        __event_store = kwargs.get('EVENT_STORE', None)
        __world: World = kwargs.get('WORLD', None)
        env = kwargs.get('ENV', None)
        if __event_store is None:
            raise Exception("Can't find eventStore")
        # On the first run, we put all the scripts in the world in the event queue
        for ent, Script in __world.get_component(scriptComponent.Script):
            payload = ExecutePayload(ent=ent)
            new_event = EVENT(ExecuteInstructionTag, payload)
            __event_store.put(new_event)

        def unblockEntity(
                ent: int,
                script: scriptComponent.Script) -> scriptComponent.States:
            script.curr_instruction += 1
            if script.curr_instruction == len(script.instructions):
                script.state = scriptComponent.States.DONE
            else:
                script.state = scriptComponent.States.READY
            return script.state

        # Now we keep checking for pending events and executing them
        while True:
            ev = yield __event_store.get(lambda e: e.type in watchlist)
            payload = ev.payload
            script = __world.component_for_entity(payload.ent,
                                                  scriptComponent.Script)
            if ev.type == ExecuteInstructionTag:
                if script.state != scriptComponent.States.READY:
                    print(f'WARN - Request to execute script not ready')
                i_type, *args = script.instructions[
                    script.curr_instruction].split(' ')
                next_state: scriptComponent.States
                if i_type in instruction_set:
                    next_state = instruction_set[i_type](payload.ent, args,
                                                         script, __event_store)
                else:
                    print(f'Unkown instrution {i_type}')
                    next_state = scriptComponent.States.READY
                if next_state == scriptComponent.States.READY:
                    payload = ExecutePayload(payload.ent)
                    new_event = EVENT(ExecuteInstructionTag, payload)
                    __event_store.put(new_event)
            else:
                ent = payload.ent
                print(f'[ScriptSystem] Got event {ev.type} for ent {ent}')
                if ev.type not in script.expecting:
                    print(f'WARN - Was not expecting {ev.type}')
                else:
                    script.expecting.remove(ev.type)
                    if not script.expecting:
                        r = unblockEntity(ent, script)
                        if r == scriptComponent.States.READY:
                            payload = ExecutePayload(ent=ent)
                            new_event = EVENT(ExecuteInstructionTag, payload)
                            __event_store.put(new_event)
Esempio n. 3
0
def pick_object(obj_name: str, me: int):
    pos = _WORLD.component_for_entity(me, Position)
    claw = _WORLD.component_for_entity(me, Claw)
    success: bool = False
    msg: str = f'Object {obj_name} not found.'
    # Create boundaries, if necessary
    if claw.boundaries is None:
        span = Ellipse(pos.center, claw.max_range, claw.max_range)
        col = Collidable(shape=helpers.collision_from_points(span, pos.center))
        claw.boundaries = col
    # For every pickable component, see if it's within range
    for _, (pick, col) in _WORLD.get_components(Pickable, Collidable):
        if pick.name == obj_name:
            # This is the object we want. Let's see if it's in range and under limit weight
            for s1 in col.shapes:
                if collide(claw.boundaries.shapes[0], s1):
                    if pick.weight <= claw.max_weight:
                        # Take the object
                        reply_channel = Store(_ENV)
                        payload = ObjectManager.GrabPayload(
                            obj_name, ObjectManager.ObjectManagerOps.REMOVE,
                            reply_channel)
                        event = EVENT(ObjectManager.ManagerTag, payload)
                        _EVENT_STORE.put(event)
                        # Wait for reply
                        response = yield reply_channel.get()
                        if response.get('success', False):
                            success = True
                            # Add removed component to my inventory
                            if not _WORLD.has_component(me, Inventory):
                                _WORLD.add_component(me, Inventory())
                            inventory = _WORLD.component_for_entity(
                                me, Inventory)
                            inventory.objects[obj_name] = pick.skeleton
                            msg = f'Picked {obj_name}. My inventory: {inventory.objects}'
                        else:
                            success = False
                            msg = response.get('msg', '')
                    else:
                        msg = f'Pickable {obj_name} too heavy. Max weight:{claw.max_weight}. Object weight: {pick.weight}'
                        success = False
                else:
                    msg = f'Pickable {obj_name} not within claw range!'
                    success = False
    response = RESPONSE_ClawPayload(op=ClawOps.GRAB,
                                    ent=me,
                                    success=success,
                                    msg=msg)
    event_to_put = EVENT(ClawDoneTag, response)
    _EVENT_STORE.put(event_to_put)
    return success, msg
Esempio n. 4
0
def on_key_press(key, mod):
    if key == KEYS.P:
        print('Removing')
        payload = ClawProcessor.GRAB_ClawPayload(ClawProcessor.ClawOps.GRAB,
                                                 'medicine', 2)
        event = EVENT(ClawProcessor.ClawTag, payload)
        eventStore.put(event)
    if key == KEYS.D:
        print('Re-creating')
        payload = ClawProcessor.GRAB_ClawPayload(ClawProcessor.ClawOps.DROP,
                                                 'medicine', 2)
        event = EVENT(ClawProcessor.ClawTag, payload)

        eventStore.put(event)
Esempio n. 5
0
def mapInstruction(ent: int, args: List[str], script: scriptComponent.Script,
                   event_store: FilterStore) -> scriptComponent.States:
    payload = MapPayload(ent, args[0])
    new_event = EVENT(MapEventTag, payload)
    event_store.put(new_event)
    script.state = scriptComponent.States.BLOQUED
    script.expecting.append(EndOfPathTag)
    return script.state
Esempio n. 6
0
def process(kwargs):
    event_store = kwargs.get('EVENT_STORE', None)
    world: esper.World = kwargs.get('WORLD', None)
    env: simpy.Environment = kwargs.get('ENV', None)
    if event_store is None:
        raise Exception("Can't find eventStore")

    while True:
        # Gets next collision event
        event = yield event_store.get(lambda ev: ev.type == StopEventTag or ev.
                                      type == GenericCollisionTag)
        if event.type == GenericCollisionTag:
            continue
        (ent, otherEnt) = event.payload
        pos = world.component_for_entity(ent, Position)
        other_pos = world.component_for_entity(otherEnt, Position)
        (mx, my) = pos.center
        (ox, oy) = other_pos.center

        if mx < ox:
            # I'm on the left of the other entity
            pos.x = other_pos.x - 1
        elif mx > ox:
            # I'm on the right
            pos.x = other_pos.x + other_pos.w + 1
        # TODO: Handle case when they are in the same X coordinate?...
        # TODO: Handle diagonal contact with round shapes
        if my < oy:
            pos.y = other_pos.y - 1
        elif my > oy:
            pos.y = other_pos.y + other_pos.h + 1
        # TODO: Handle case when they are in the same Y coordinate?...

        # If following path, remove it
        if world.has_component(ent, Path):
            end_of_path = EVENT(EndOfPathTag, EndOfPathPayload(ent, env.now))
            event_store.put(end_of_path)
            world.remove_component(ent, Path)
Esempio n. 7
0
    def process(self, kwargs):
        event_store: FilterStore = kwargs.get('EVENT_STORE', None)
        env = kwargs.get('ENV', None)
        for ent, (pos, path, vel) in self.world.get_components(Position, Path, Velocity):
            # print(f"Processing {ent}")
            point = path.points[path.curr_point]
            pos_center = pos.center
            # print(f"[Path] Point {point} is {path.curr_point}th point")
            if pos_center[0] == point[0] and pos_center[1] == point[1]:
                # print("Going to next point")
                path.curr_point += 1
                if path.curr_point == len(path.points):
                    # end of path
                    vel.x = 0
                    vel.y = 0
                    # print("Removing Path component from", ent)
                    pos.changed = False or pos.changed
                    self.world.remove_component(ent, Path)
                    # Adds an EndOfPath event, in case anyone is listening
                    end_of_path = EVENT(EndOfPathTag, EndOfPathPayload(ent, env.now))
                    # print(f'[{env.now}] PathProcessor adding EndOfPath event {end_of_path}')
                    event_store.put(end_of_path)
                    return
                point = path.points[path.curr_point]
                # print(f"Point {point} is {path.curr_point}th point")

            dx = point[0] - pos_center[0]
            if dx > 0:
                vel.x = min(path.speed, dx)
            else:
                vel.x = max(- path.speed, dx)
            dy = point[1] - pos_center[1]
            if dy > 0:
                vel.y = min(path.speed, dy)
            else:
                vel.y = max(- path.speed, dy)