-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissile_agent.py
36 lines (27 loc) · 1.25 KB
/
missile_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import asyncio
from spade.behaviour import CyclicBehaviour
from spade.template import Template
from stationary_agent import StationaryAgent
import util
class RecvBehav(CyclicBehaviour):
def __init__(self, name):
super().__init__()
self.name = name
self.destroyed_items = []
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)
class MissileAgent(StationaryAgent):
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)