예제 #1
0
    def tick(self):
        procs = self.procs
        nrobots = self.nrobots
        timeouts = self.timeouts
        w = self.w
        rnd = self.rnd
        result = ''

        items = self.models.items()
        random.shuffle(items)
        for robotname, model in items:
            if robotname not in self.procs:
                continue

            health = model.health
            body = model.body
            pos = body.position
            possens = '%s;%s' % (int(pos.x*10), int(pos.y*10))
            tur = model.get_turretangle()
            ping = '%s;%s;%s' % (model._pingtype,
                                    model._pingangle,
                                    model._pingdist)
            gyro = model.gyro()
            heat = int(model._cannonheat)
            loading = int(model._cannonreload)
            pinged = int(model._pinged == rnd - 1)
            line = 'TICK:%s|HEALTH:%s|POS:%s|TUR:%s|PING:%s|GYRO:%s|HEAT:%s|LOADING:%s|PINGED:%s\n' % (rnd, health, possens, tur, ping, gyro, heat, loading, pinged)
            #print robotname, line

            proc = procs[robotname]

            if not model.alive:
                model._kills = nrobots - len(procs)
                del procs[robotname]
                print 'DEAD robot', robotname, 'health is 0'
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()
                continue

            proc.stdin.write(line)
            try:
                result = proc.stdout.readline().strip()
            except IOError:
                print 'ERROR with', robotname

            if result == 'TIMEOUT':
                timeouts[robotname] += 1
                if timeouts[robotname] > 5:
                    del procs[robotname]
                    print 'REMOVED robot', robotname, 'due to excessive timeouts'
                    proc.stdin.flush()
                    proc.stdin.close()
                    proc.stdout.close()
                    proc.kill()

            elif result == 'END':
                del procs[robotname]
                print 'FINISHED: robot', robotname
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()

            elif result == 'ERROR':
                del procs[robotname]
                print 'ERROR: robot', robotname
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()

            else:
                timeouts[robotname] = 0


            #print 'RR', result, 'RR'
            commands = {}
            try:
                props = result.split('|')
                for prop in props:
                    kind, val = prop.split(':')
                    try:
                        vconv = int(val)
                    except ValueError:
                        pass
                    else:
                        val = vconv
                    commands[kind] = val
            except ValueError:
                continue

            #print 'KV', kind, val
            #print 'R', robot, 'R', result, 'R'
            #print 'R', robotname, 'T', '%s -> %.3f' % (model._turretangletarget, model.turretjoint.GetJointAngle())

            for kind, val in commands.items():
                if kind == 'FORCE':
                    # Make sure force is not more than 100% or less than -100%
                    val = min(val, 100)
                    val = max(-100, val)
                    force = conf.maxforce * val/100.0
                    localforce = box2d.b2Vec2(val, 0)
                    worldforce = body.GetWorldVector(localforce)
                    body.ApplyForce(worldforce, pos)
                elif kind == 'TORQUE':
                    # Make sure torque is not more than 100% or less than -100%
                    val = min(val, 100)
                    val = max(-100, val)
                    torque = conf.maxtorque * val/100.0
                    body.ApplyTorque(torque)
                elif kind == 'FIRE':
                    if val == '_':
                        # no fire
                        pass
                    elif val == 'X':
                        # non-exploding shell
                        w.makebullet(robotname)
                    else:
                        # exploding shell
                        ticks = int(60 * val / conf.bulletspeed)
                        w.makebullet(robotname, ticks)
                elif kind == 'PING':
                    if val:
                        kind, angle, dist = w.makeping(robotname, rnd)
                        if kind is not None:
                            model._pingtype = kind[0]
                            model._pingangle = angle
                            model._pingdist = int(dist)
                elif kind == 'TURRET':
                    model.set_turretangle(val)


        w.step()
        #Send shit to nosql
        worldData = self.w.to_dict()
        worldData['time'] = conf.maxtime - rnd/60
        worldData['_id'] = 1
        self.mc[self.game_id].update({}, worldData, upsert=True)

        # Maybe turn this into a log later
        #if not rnd%60:
        #    print '%s seconds (%s real)' % (rnd/60, int(time.time())-self.t0)
        self.rnd += 1
예제 #2
0
    def tick(self):
        procs = self.procs
        ntanks = self.ntanks
        timeouts = self.timeouts
        w = self.w
        rnd = self.rnd
        result = ''

        items = self.models.items()
        random.shuffle(items)
        for tankname, model in items:
            if tankname not in self.procs:
                continue

            health = model.health
            body = model.body
            pos = body.position
            possens = '%s;%s' % (int(pos.x*10), int(pos.y*10))
            tur = model.get_turretangle()
            ping = '%s;%s;%s' % (model._pingtype,
                                    model._pingangle,
                                    model._pingdist)
            gyro = model.gyro()
            heat = int(model._cannonheat)
            loading = int(model._cannonreload)
            pinged = int(model._pinged == rnd - 1)
            line = 'TICK:%s|HEALTH:%s|POS:%s|TUR:%s|PING:%s|GYRO:%s|HEAT:%s|LOADING:%s|PINGED:%s\n' % (rnd, health, possens, tur, ping, gyro, heat, loading, pinged)
            

            proc = procs[tankname]

            if not model.alive:
                model._kills = ntanks - len(procs)
                del procs[tankname]
                print 'DEAD tank', tankname, 'health is 0'
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()
                continue

            proc.stdin.write(line)
            try:
                result = proc.stdout.readline().strip()
            except IOError:
                print 'ERROR with', tankname

            if result == 'TIMEOUT':
                timeouts[tankname] += 1
                if timeouts[tankname] > 5:
                    del procs[tankname]
                    print 'REMOVED tank', tankname, 'due to excessive timeouts'
                    proc.stdin.flush()
                    proc.stdin.close()
                    proc.stdout.close()
                    proc.kill()

            elif result == 'END':
                del procs[tankname]
                print 'FINISHED: tank', tankname
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()

            elif result == 'ERROR':
                del procs[tankname]
                print 'ERROR: tank', tankname
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()

            else:
                timeouts[tankname] = 0


            
            commands = {}
            try:
                props = result.split('|')
                for prop in props:
                    kind, val = prop.split(':')
                    try:
                        vconv = int(val)
                    except ValueError:
                        pass
                    else:
                        val = vconv
                    commands[kind] = val
            except ValueError:
                continue

            
            
            

            for kind, val in commands.items():
                if kind == 'FORCE':
                    
                    val = min(val, 100)
                    val = max(-100, val)
                    
                    localforce = box2d.b2Vec2(val, 0)
                    worldforce = body.GetWorldVector(localforce)
                    body.ApplyForce(worldforce, pos)
                elif kind == 'TORQUE':
                    
                    val = min(val, 100)
                    val = max(-100, val)
                    torque = conf.maxtorque * val/100.0
                    body.ApplyTorque(torque)
                elif kind == 'FIRE':
                    if val == '_':
                        
                        pass
                    elif val == 'X':
                        
                        w.makebullet(tankname)
                    else:
                        
                        ticks = int(60 * val / conf.bulletspeed)
                        w.makebullet(tankname, ticks)
                elif kind == 'PING':
                    if val:
                        kind, angle, dist = w.makeping(tankname, rnd)
                        if kind is not None:
                            model._pingtype = kind[0]
                            model._pingangle = angle
                            model._pingdist = int(dist)
                elif kind == 'TURRET':
                    model.set_turretangle(val)


        w.step()



        self.rnd += 1
예제 #3
0
    def tick(self):
        procs = self.procs
        nrobots = self.nrobots
        timeouts = self.timeouts
        w = self.w
        rnd = self.rnd
        result = ''

        items = self.models.items()
        random.shuffle(items)
        for robotname, model in items:
            if robotname not in self.procs:
                continue

            proc = procs[robotname]

            if model._enable_debug is None:
                pass
            elif model._enable_debug:
                line = 'DEBUG\n'
                proc.stdin.write(line)
                model._enable_debug = None
                continue
            else:
                line = 'NODEBUG\n'
                proc.stdin.write(line)
                model._enable_debug = None
                continue

            health = model.health
            body = model.body
            pos = body.position
            possens = '%s;%s' % (int(pos.x), int(pos.y))
            tur = model.get_turretangle()
            ping = '%s;%s;%s' % (model._pingtype,
                                    model._pingangle,
                                    model._pingdist)
            gyro = model.gyro()
            heat = int(model._cannonheat)
            loading = int(model._cannonreload)
            pinged = int(model._pinged == rnd - 1)
            line = 'TICK:%s|HEALTH:%s|POS:%s|TUR:%s|PING:%s|GYRO:%s|HEAT:%s|LOADING:%s|PINGED:%s\n' % (rnd, health, possens, tur, ping, gyro, heat, loading, pinged)
            #print robotname, line

            if not model.alive:
                model._outlasted = nrobots - len(procs)
                del procs[robotname]
                print 'DEAD robot', robotname, 'health is 0'
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()
                time.sleep(0.1)
                model._commands = {'INACTIVE':'DEAD'}
                continue

            proc.stdin.write(line)
            try:
                result = proc.stdout.readline().strip()
            except IOError:
                print 'ERROR with', robotname
                continue

            if result == 'TIMEOUT':
                timeouts[robotname] += 1
                if timeouts[robotname] > 5:
                    del procs[robotname]
                    print 'REMOVED robot', robotname, 'due to excessive timeouts'
                    proc.stdin.flush()
                    proc.stdin.close()
                    proc.stdout.close()
                    proc.kill()

            elif result == 'END':
                del procs[robotname]
                print 'FINISHED: robot', robotname
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()

            elif result == 'ERROR':
                del procs[robotname]
                print 'ERROR: robot', robotname
                proc.stdin.flush()
                proc.stdin.close()
                proc.stdout.close()
                proc.kill()

            else:
                timeouts[robotname] = 0


            #print 'RR', result, 'RR'
            commands = {}
            try:
                props = result.split('|')
                for prop in props:
                    kind, val = prop.split(':')
                    try:
                        vconv = int(val)
                    except ValueError:
                        pass
                    else:
                        val = vconv
                    commands[kind] = val
            except ValueError:
                continue

            #print 'KV', kind, val
            #print 'R', model, 'R', result, 'R'
            #print 'R', robotname, 'T', '%s -> %.3f' % (model._turretangletarget, model.turretjoint.angle)

            model._commands = commands or {'INACTIVE':result}

            for kind, val in commands.items():
                if kind == 'FORCE':
                    # Make sure force is not more than 100% or less than -100%
                    val = min(val, 100)
                    val = max(-100, val)
                    force = conf.maxforce * val/100.0
                    localforce = box2d.b2Vec2(val, 0)
                    worldforce = body.GetWorldVector(localforce)
                    body.ApplyForce(worldforce, pos, True)
                elif kind == 'TORQUE':
                    # Make sure torque is not more than 100% or less than -100%
                    val = min(val, 100)
                    val = max(-100, val)
                    torque = conf.maxtorque * val/100.0
                    body.ApplyTorque(torque, True)
                elif kind == 'FIRE':
                    if val == '_':
                        # no fire
                        pass
                    elif val == 'X':
                        # non-exploding shell
                        w.makebullet(robotname)
                    else:
                        # exploding shell
                        ticks = int(60 * val / conf.bulletspeed)
                        w.makebullet(robotname, ticks)
                elif kind == 'PING':
                    if val:
                        kind, angle, dist = w.makeping(robotname, rnd)
                        if kind is not None:
                            model._pingtype = kind[0]
                            model._pingangle = angle
                            model._pingdist = int(dist)
                elif kind == 'TURRET':
                    val = min(val, 100)
                    val = max(-100, val)
                    torque = conf.turret_maxMotorSpeed * val/100.0
                    model.turretcontrol(torque)

        w.step()

        if not rnd%60:
            print '%s seconds (%s real)' % (rnd/60, int(time.time())-self.t0)
        self.rnd += 1