#This file is distributed under the terms of the GNU General Public license.
#Copyright (C) 2005 Al Riddoch (See the file COPYING for details).

from atlas import *

from world.objects.Thing import Thing

class Combat(Thing):
    """The mason combat system."""
    def __init__(self, cppthing):
        self.cinit(cppthing)
        self.attackers=[]
        self.defenders=[]
        self.attack=True
        print "Task Created"
    def attack_operation(self, op):
        print "Got attack"
        defender = op.to
        if defender != self.character.id:
            print "Attach operation is wierd"
        if defender not in self.defenders:
            print "New defender"
            self.defenders.append(defender)
        print "Attacker", op[0].id
        attacker = op.from_
        if attacker not in self.attackers:
            print "New attacker"
            self.attackers.append(attacker)
        attack_ent = self.character.world.get_object(attacker)
        if not attack_ent:
            print "No attacker"
            return
        print attack_ent, attack_ent.statistics
        res=Operation("tick", Entity(name="task"), to=self.character.id)
        return res
    def tick_operation(self, op):
        print "Got tick"
        res=Message()
        if self.attack:
            print "Attacking"
            attackers=self.attackers
            defenders=self.defenders
        else:
            print "Defending"
            defenders=self.attackers
            attackers=self.defenders
        for i in attackers:
            attacker = self.character.world.get_object(i)
            if not attacker:
                print "Dud attacker"
                continue
            print attacker.id, " attacking"
            if attacker.stamina <= 0.0:
                print "attacker is out"
                continue
            attacker.send_world(Operation("imaginary", Entity(description="hits for massive damage."), to=attacker))
        for i in defenders:
            defender = self.character.world.get_object(i)
            if not defender:
                print "Dud defender"
                continue
            print defender.id, " defending"
            if attacker.stamina <= 0.0:
                print "defender is out"
                continue
            defender.send_world(Operation("imaginary", Entity(description="defends skillfully."), to=defender))
        self.attack=not self.attack
        res=Operation("tick", Entity(name="task"), to=self.character.id)
        res.setFutureSeconds(1.75)
        return res
