Пример #1
0
    async def setup(self):
        util.mas_print_info("[MISSILE] ({}) starting...".format(self.aid))

        b = RecvBehav(self.aid)
        template = Template()
        template.set_metadata("performative", "inform")
        self.add_behaviour(b, template)
Пример #2
0
    async def setup(self):
        util.mas_print_info("[HQ] {} starting...".format(self.aid))

        b = RecvBehav(self.locations)
        template = Template()
        template.set_metadata("performative", "inform")
        self.add_behaviour(b, template)
        self.presence.set_available(show=aioxmpp.PresenceShow.CHAT)
Пример #3
0
    async def run(self):
        offline_prob = random.randint(0, 100) / 100
        if offline_prob < 0.2:
            self.presence.set_unavailable()
            util.mas_print_data('state|hq|offline')
            util.mas_print_info("HQ went offline")
        else:
            if not self.presence.is_available():
                self.presence.set_available(show=aioxmpp.PresenceShow.CHAT)
                util.mas_print_data('state|hq|online')
                util.mas_print_info("HQ back online")

        msg = await self.receive(timeout=1)  # wait for a message for 10 seconds
        if msg:
            util.mas_print_info("[HQ] received message with content: {}".format(msg.body))
            payload = msg.body.split('|')
            x = int(payload[3])
            y = int(payload[4])

            for agent, data in self.locations.items():
                if agent.startswith('missile') and util.circle_contains(data['x'], data['y'], data['range'], x, y):
                    to = agent + '@xmpp.test'
                    missile_msg = Message(to=to)  # Instantiate the message
                    missile_msg.set_metadata("performative", "inform")  # Set the "inform" FIPA performative
                    missile_msg.body = msg.body  # Set the message content

                    util.mas_print_data('msg|hq|{}|{}'.format(agent, msg.body))
                    util.mas_print_info("[HQ] forwarded message '{}' to [{}]".format(msg.body, agent))
                    await self.send(missile_msg)

        await asyncio.sleep(util.step_delay)
Пример #4
0
    async def run(self):
        msg = await self.receive(timeout=1
                                 )  # wait for a message for 10 seconds
        if msg:
            util.mas_print_info(
                "[MISSILE] ({}) received message with content: {}".format(
                    self.name, msg.body))
            parts = msg.body.split('|')

            if parts[2] == 'enemy' and not parts[1] in self.destroyed_items:
                self.destroyed_items.append(parts[1])
                util.mas_print_data('fire|{}|{}'.format(self.name, msg.body))
                util.mas_print_info(
                    "[MISSILE] ({}) Firing missile to enemy {} at ({}, {})".
                    format(self.name, parts[0], parts[3], parts[4]))

        await asyncio.sleep(util.step_delay)
Пример #5
0
    async def run(self):
        data = self.timeline.next()
        if not data:
            return

        if data['type'] == 'enemy':
            self.detected = True
            self.object = data['object']
            self.object_id = data['id']
            self.type = data['type']
            self.at_x = data['x']
            self.at_y = data['y']
        else:
            self.detected = False

        if self.detected:
            if not self.hq_agent.presence.is_available():
                await self.send_message('missile1')
                await self.send_message('missile2')
                util.mas_print_info(
                    "[RADAR] ({}) HQ not available, directly sent messages to missiles"
                    .format(self.name))
            elif util.circle_contains(
                    self.locations['hq']['x'], self.locations['hq']['y'], 10,
                    self.at_x,
                    self.at_y):  # To HQ directly order missile to fire
                await self.send_message('missile1')
                await self.send_message('missile2')
                util.mas_print_info(
                    "[RADAR] ({}) directly sent messages to missiles".format(
                        self.name))
            else:
                await self.send_message("hq")
                util.mas_print_info("[RADAR] ({}) sent message to HQ".format(
                    self.name))

        await asyncio.sleep(util.step_delay)
Пример #6
0
    util.start_web(m1)

    m2 = MissileAgent('missile2')
    m2_future = m2.start()
    util.start_web(m2)

    # Wait for hq & missiles to be prepared
    hq_future.result()
    m1_future.result()
    m2_future.result()

    r1 = RadarAgent('radar1', timeline, hq, settings.agents)
    r1.start()
    util.start_web(r1)

    r2 = RadarAgent('radar2', timeline, hq, settings.agents)
    r2.start()
    util.start_web(r2)

    util.mas_print_info("Wait until user interrupts with ctrl+C")
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break
    hq.stop()
    m1.stop()
    m2.stop()
    r1.stop()
    r2.stop()
Пример #7
0
 async def setup(self):
     util.mas_print_info("[RADAR] ({}) starting...".format(self.aid))
     b = ScanBehaviour(self.aid, self.timeline, self.hq_agent,
                       self.locations)
     self.add_behaviour(b)
Пример #8
0
 async def on_start(self):
     util.mas_print_info("[RADAR] ({}) start scanning...".format(self.name))