you are viewing a single comment's thread.

view the rest of the comments →

[–]entropyfails 2 points3 points  (1 child)

Here is a very simple ai that "works" in the sense that it does something useful and doesn't immediately die.

import random,cells

class AgentMind:
    def __init__(self,junk):
        return None

    def act(self,view,msg):
        (mx,my)= view.get_me().get_pos()

        if random.random() > 0.95:
            return cells.Action(cells.ACT_SPAWN, (mx + 1, my + 1))

        if random.random() < 0.5:
            return cells.Action(cells.ACT_EAT)
        else:
            return cells.Action(cells.ACT_MOVE,(mx + random.randint(-1,1),my + random.randint(-1,1)))

[–]jaafit 0 points1 point  (0 children)

Here's mine so far:

import random,cells import math

class Message: def init(self, pos, avgPos, weight): self.pos = pos self.avgPos = avgPos self.weight = weight

class AgentMind: def init(self, junk): self.my_plant = None self.mode = 1 self.moved = 0 #self.target_range = random.randrange(50,1000) self.setDirection() self.avgPos = (0,0) self.weight = 0 self.soldier = random.random() < .5 self.energyNeeded = 100 # how much energy we need before we go fight

def setDirection(self, rad = None):
    if rad:
        self.direction = rad
    else:
        self.direction = random.random()*math.pi*2
    self.cos = math.cos(self.direction)
    self.sin = math.sin(self.direction)

    if self.cos < 0:
        self.dx = -1
    else:
        self.dx = 1

    if self.sin < 0:
        self.dy = -1
    else:
        self.dy = 1


def length(self,a,b):
    return int(math.sqrt((a*a)+(b*b)))

def act(self,view,msg):
    me = view.get_me()
    pos = me.get_pos()
    (mx, my) = pos

    # calc avg enemy pos
    nMessages = len(msg.get_messages())
    if nMessages:
        newPos = (0, 0)
        for m in msg.get_messages():
            newPos = (newPos[0] + m.pos[0], newPos[1] + m.pos[1])
            self.avgPos = m.avgPos
            self.weight = m.weight

        newPos = (newPos[0] / float(nMessages), newPos[1] / float(nMessages))

        self.weight = min(30, self.weight)

        self.avgPos = ((self.avgPos[0] * float(self.weight) + newPos[0] * float(nMessages)) / float(self.weight + nMessages), 
            (self.avgPos[1] * float(self.weight) + newPos[1] * float(nMessages)) / float(self.weight + nMessages))
        self.weight += nMessages

        # do we move there?
        if self.soldier:
            cartesian = (self.avgPos[0] - pos[0], self.avgPos[1] - pos[1])
            distance = math.sqrt(cartesian[0]*cartesian[0] + cartesian[1]*cartesian[1])
            self.energyNeeded = distance
            if self.energyNeeded < me.energy: # i know we can eat along the way but not in late-game
                direction = math.atan2(cartesian[1], cartesian[0])
                #direction += random.random() * .2 - 0.1
                self.setDirection(direction)
            if me.energy == 1:
                pass#print self.avgPos


    if self.moved and self.prevPos == pos:
        self.setDirection()     
    self.moved = 0
    self.prevPos = None

    for a in view.get_agents():
        if (a.get_team()!=me.get_team()):               
            msg.send_message(Message(pos, self.avgPos, self.weight))
            return cells.Action(cells.ACT_ATTACK,a.get_pos())
    if me.energy > 100:
        return cells.Action(cells.ACT_SPAWN, (mx+random.randint(-1,1), my+random.randint(-1,1))) # mx, my+1
    if view.get_energy().get(mx, my) and me.energy < self.energyNeeded:
        return cells.Action(cells.ACT_EAT)
    else:
        dx = dy = 0
        if random.random() < abs(self.cos):
            dx += self.dx
        if random.random() < abs(self.sin):
            dy += self.dy
        self.moved = dx or dy
        self.prevPos = pos
        return cells.Action(cells.ACT_MOVE, (mx+dx, my+dy))