def __init__(self, position, radius, tag=False, scale=Vector2(1.0, 1.0)):
        import entity.EntityManager as EntityManager

        self.entity_id = CommonUtil.genID()
        EntityManager.register_entity(self)

        self.state_machine = StateMachine(self)

        self._position = position
        self._bounding_radius = radius
        self._tag = tag
        self.scale = scale
def dispatch_message(delay, sender_id, receiver_id, msg, extra):
    receiver = EntityManager.get_entity_from_id(receiver_id)
    message = Message(sender_id, receiver_id, msg, extra)

    if delay <= 0.0:
        discharge(receiver, message)
    else:
        message.setTime(time.time() + delay)
        priorityQ.add(message)
def update():
    while len(priorityQ) > 0:
        current_time = time.time()
        message = priorityQ[0]

        if message.get_time() < current_time:
            receiver_id = message.get_received_id()
            receiver = EntityManager.get_entity_from_id(receiver_id)
            discharge(receiver, message)
            priorityQ.pop(0)
        time.sleep(2)
Example #4
0
#!/usr/bin/env python
# encoding: utf-8

from miner import Miner, Wife
from entity import EntityManager

em = EntityManager()

m = Miner('Bob')
w = Wife('Elsa')

em.register_entity(m)
em.register_entity(w)

for i in xrange(200):
    m.update()
    w.update()

Example #5
0
    print("---------------------------------")
    return image, res_set

cnt = 0
fps = 10

from constants import labels
from constants import bg_color
from constants import screen_size
from util import drawMark,drawRectBox
from entity import EntityManager

if __name__ == "__main__":
    path = './samples/9.avi'
    cap = cv2.VideoCapture(path)
    emangers = EntityManager()
    fps = cap.get(cv2.CAP_PROP_FPS)
    ispaused=False
    if os.path.exists(path):
        print('processing...')
        while True:
            # check if space key pressed or not
            #if cv2.waitKey(33) == 32:
            #    ispaused =  not ispaused
            # check if paused or not
            if ispaused:
                continue
            # reads frames from a video
            ret, frame = cap.read()
            if ret is False:
                break
from PyQt5.QtGui import QPainter, QPen, QBrush#, QPainterPath
from PyQt5.QtCore import QLine, QPoint, QRect#,QSize, 

import numpy as np
#import random
#import time

from detector import VehicleDetector

detector = VehicleDetector()

#from alpr_hyper import recognize_plate

from entity import EntityManager

EM = EntityManager()

from constants import image_exts,video_exts
from constants import labels
#from constants import bg_color
from constants import screen_size

from util import drawMark,drawStatus

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# create a file handler
handler = logging.FileHandler('vehicle.log')
Example #7
0
    def read_map(self, config):
        try:
            with open(self.filename) as f:
                lines = f.readlines()
                related_data = ''.join(lines[0:3])
                map_data = lines[3:]

                # First line: rows \d+
                # Second line: cols \d+
                # Third line: players \d+

                related_data_re = "rows\s+(\d+)\s+cols\s+(\d+)\s+players\s+(\d)"
                m = re.match(related_data_re, related_data, re.DOTALL)
                if not m:
                    raise MapException("Map file '%s' is not valid - header not correct", self.filename)


                self.rows = int(m.group(1))
                self.cols = int(m.group(2))
                self.nb_players = int(m.group(3))
                self.entities = EntityManager(self.nb_players)

                view_radius = config.getint('initial', 'view_radius')
                view_radiusf = config.getfloat('initial', 'view_radius')
                self.relative_visible_cells = []

                # Compute relative visible cells
                for row in xrange(-view_radius, view_radius + 1):
                    for col in xrange(-view_radius, view_radius + 1):
                        if row == 0 and col == 0:
                            continue
                        if sqrt(row*row + col*col) <= view_radiusf:
                            self.relative_visible_cells.append((row, col))


                self.arena = [[Cell() for i in xrange(self.cols)] for j in xrange(self.rows)]

                try:
                    for row in xrange(self.rows):
                        for col in xrange(self.cols):
                            if map_data[row][col] == '%':
                                # Water cell
                                self.arena[row][col].surface = Surface.WATER

                            elif map_data[row][col] == '+':
                                # Human on this cell
                                entity = Entity(row, col)
                                entity.create_human()
                                self.entities.add_np_entity(entity)

                            elif map_data[row][col] == '*':
                                # Cop on this cell
                                entity = Entity(row, col)
                                entity.create_cop(config.getint('initial', 'bullet_amount'))
                                self.entities.add_np_entity(entity)

                            elif map_data[row][col] == '$':
                                # Berzerk on this cell
                                entity = Entity(row, col)
                                entity.create_berzerk(config.getint('initial', 'berzerk_delay'))
                                self.entities.add_np_entity(entity)

                            elif ord(map_data[row][col]) in range(ord('1'), ord('8')):
                                # Initial zombie for a player
                                team = int(map_data[row][col])
                                entity = Entity(row, col)
                                entity.create_zombie(team, config.getint('initial', 'contagion_amount'))
                                self.entities.add_zombie(entity)

                            else:
                                # Probability to spawn a human or a cop
                                if random.random() <= 0.09:
                                    if random.random() <= 0.05:
                                        # Cop on this cell
                                        entity = Entity(row, col)
                                        entity.create_cop(config.getint('initial', 'bullet_amount'))
                                        self.entities.add_np_entity(entity)
                                    else:
                                        # Human on this cell
                                        entity = Entity(row, col)
                                        entity.create_human()
                                        self.entities.add_np_entity(entity)



                except IndexError:
                    raise MapException("Map file '%s' not valid - col/row problem", self.filename)


        except IOError:
            raise MapException("Can't open map file '%s'", self.filename)
Example #8
0
class Arena:
    def __init__(self, filename, config):
        self.filename = filename
        self.entities = None
        self.read_map(config)

    def read_map(self, config):
        try:
            with open(self.filename) as f:
                lines = f.readlines()
                related_data = ''.join(lines[0:3])
                map_data = lines[3:]

                # First line: rows \d+
                # Second line: cols \d+
                # Third line: players \d+

                related_data_re = "rows\s+(\d+)\s+cols\s+(\d+)\s+players\s+(\d)"
                m = re.match(related_data_re, related_data, re.DOTALL)
                if not m:
                    raise MapException("Map file '%s' is not valid - header not correct", self.filename)


                self.rows = int(m.group(1))
                self.cols = int(m.group(2))
                self.nb_players = int(m.group(3))
                self.entities = EntityManager(self.nb_players)

                view_radius = config.getint('initial', 'view_radius')
                view_radiusf = config.getfloat('initial', 'view_radius')
                self.relative_visible_cells = []

                # Compute relative visible cells
                for row in xrange(-view_radius, view_radius + 1):
                    for col in xrange(-view_radius, view_radius + 1):
                        if row == 0 and col == 0:
                            continue
                        if sqrt(row*row + col*col) <= view_radiusf:
                            self.relative_visible_cells.append((row, col))


                self.arena = [[Cell() for i in xrange(self.cols)] for j in xrange(self.rows)]

                try:
                    for row in xrange(self.rows):
                        for col in xrange(self.cols):
                            if map_data[row][col] == '%':
                                # Water cell
                                self.arena[row][col].surface = Surface.WATER

                            elif map_data[row][col] == '+':
                                # Human on this cell
                                entity = Entity(row, col)
                                entity.create_human()
                                self.entities.add_np_entity(entity)

                            elif map_data[row][col] == '*':
                                # Cop on this cell
                                entity = Entity(row, col)
                                entity.create_cop(config.getint('initial', 'bullet_amount'))
                                self.entities.add_np_entity(entity)

                            elif map_data[row][col] == '$':
                                # Berzerk on this cell
                                entity = Entity(row, col)
                                entity.create_berzerk(config.getint('initial', 'berzerk_delay'))
                                self.entities.add_np_entity(entity)

                            elif ord(map_data[row][col]) in range(ord('1'), ord('8')):
                                # Initial zombie for a player
                                team = int(map_data[row][col])
                                entity = Entity(row, col)
                                entity.create_zombie(team, config.getint('initial', 'contagion_amount'))
                                self.entities.add_zombie(entity)

                            else:
                                # Probability to spawn a human or a cop
                                if random.random() <= 0.09:
                                    if random.random() <= 0.05:
                                        # Cop on this cell
                                        entity = Entity(row, col)
                                        entity.create_cop(config.getint('initial', 'bullet_amount'))
                                        self.entities.add_np_entity(entity)
                                    else:
                                        # Human on this cell
                                        entity = Entity(row, col)
                                        entity.create_human()
                                        self.entities.add_np_entity(entity)



                except IndexError:
                    raise MapException("Map file '%s' not valid - col/row problem", self.filename)


        except IOError:
            raise MapException("Can't open map file '%s'", self.filename)


    def get_targeted_cell(self, entity, direction):
        # Compute the targeted cell
        if direction == Direction.NORTH:
            t_row, t_col = entity._row - 1, entity._col
        elif direction == Direction.SOUTH:
            t_row, t_col = entity._row + 1, entity._col
        elif direction == Direction.EAST:
            t_row, t_col = entity._row, entity._col + 1
        elif direction == Direction.WEST:
            t_row, t_col = entity._row, entity._col - 1

        return t_row, t_col


    def is_valid_move(self, entity, direction):
        """ Return whether or not the move is valid for this entity """
        t_row, t_col = self.get_targeted_cell(entity, direction)

        # Valid move = target inside the map and the cell is not water
        if t_row < 0 or t_row >= self.rows or \
           t_col < 0 or t_col >= self.cols or \
           self.arena[t_row][t_col].surface == Surface.WATER:
            return False
        return True

    def get_opposite_dirs(self, entity, avoid):
        """ Return directions that get away entity from avoid entity """
        possible_dir = ['N', 'S', 'W', 'E']
        ret = []

        for dir in possible_dir:
            target = self.get_targeted_cell(entity, dir)
            if entity.distance_to(avoid) < avoid.distance_to_location(*target):
                ret.append(dir)

        return ret

    def get_towards_dirs(self, entity, avoid):
        possible_dir = ['N', 'S', 'W', 'E']
        ret = []

        for dir in possible_dir:
            target = self.get_targeted_cell(entity, dir)
            if entity.distance_to(avoid) < avoid.distance_to_location(*target):
                ret.append(dir)

        return ret
    def destroy(self):
        import entity.EntityManager as EntityManager

        EntityManager.un_register_entity(self)