from pyspades.collision import distance_3d_vector
from pyspades.server import position_data
from commands import name, get_player, add, admin, alias
import commands

@admin
@name('fogline')
@alias('fl')

def fogline(self, *args): # lol why did i choose to make it with *args?

    if len(args) == 1:
        self.protocol.distancealert = int(args[0])
        return "Alerts will show up when someone delivers a hit over a distance of %d blocks." % (self.protocol.distancealert) # sounds so booooring

    else: return "Come on, don't be stupid and enter some correct values!" # yeah you gotta punish people for their mistakes

commands.add(fogline)


def apply_script(protocol, connection, config):

    class fogprotocol(protocol):

        distancealert = 127 # default distance for showing alerts, i prefer setting it to about 123, 126 is about the max range you can see (+10 if you include height)


    class fogconnection(connection): # just some random note

        class helppoint(): # hmm too lazy
            x = 0          # to check if
            y = 0          # its better to
            z = 0          # store it here, nah whatever

        def on_hit(self, hit_amount, hit_player, type, grenade):

            hit_player.helppoint.x = hit_player.world_object.position.x
            hit_player.helppoint.y = hit_player.world_object.position.y
            hit_player.helppoint.z = self.world_object.position.z

            distance = int(distance_3d_vector(self.world_object.position, hit_player.helppoint))               # phew and there i thought i had to calculate it manualy
            meow = "WOW! Foglinehit detected: %s horizontal distance: %d blocks " % (self.name, distance)

            if distance >= self.protocol.distancealert and distance <= 139 and grenade is None:           # i wonder if i should 
                for players in self.protocol.players.values():    # add something to make
                    if players.admin:                             # fun of the alerted
                       players.send_chat(meow)                    # admin

                if hit_player.protocol.irc_relay.factory.bot and hit_player.protocol.irc_relay.factory.bot.colors: # hehe the message should
                    alert = '\x0313' + meow + '\x0f'                                                               # be sent to everyone in
                    self.protocol.irc_relay.send(alert)                                                            # in the game

            return connection.on_hit(self, hit_amount, hit_player, type, grenade) # tried it with 'return protocol.on_hit(...)', did not turn out to be a smart idea though


    return fogprotocol, fogconnection