- Code: Select all
from commands import add
from pyspades.contained import BlockAction, SetColor
from pyspades.constants import *
from pyspades.world import Grenade
from pyspades.common import Vertex3
from pyspades.server import orientation_data, grenade_packet
from twisted.internet.task import LoopingCall
from twisted.internet import reactor
from math import sin, cos
import random
def apply_script(protocol, connection, config):
class SantaConnection(connection):
def on_block_build_attempt(self, x, y, z):
c1, c2, c3 = self.color
if (c1 > 100 and c2 > 100 and c3 > 100) and ((c1 + c2 + c3) > 500):
return False
return connection.on_block_build_attempt(self, x, y, z)
def spew_santa_grenades(self, x, y, z):
santa_nade = False
for i in xrange(0, 6):
grenade_packet.value = 3 # fuse
grenade_packet.player_id = 32
grenade_packet.position = (x, y, z)
pi = 3.14159265
a = i * ((2 * pi) / 6)
vel_x = sin(a)
vel_y = cos(a)
vel_z = 1.0
grenade_packet.velocity = (vel_x, vel_y, vel_z)
self.protocol.send_contained(grenade_packet)
position = Vertex3(x, y, z)
velocity = Vertex3(vel_x, vel_y, vel_z)
santa_nade = self.protocol.world.create_object(Grenade, 3,
position, None, velocity, self.grenade_exploded)
santa_nade = 'Santa'
def spawn_santa_grenade_fountain(self, x, y, z):
santa_nade = False
grenade_packet.value = 3 # fuse
grenade_packet.player_id = 32
grenade_packet.position = (x, y, z)
vel_x = random.random() * random.randrange(-1, 2, 2)
vel_y = random.random() * random.randrange(-1, 2, 2)
vel_z = -0.5
grenade_packet.velocity = (vel_x, vel_y, vel_z)
self.protocol.send_contained(grenade_packet)
position = Vertex3(x, y, z)
velocity = Vertex3(vel_x, vel_y, vel_z)
santa_nade = self.protocol.world.create_object(Grenade, 3,
position, None, velocity, self.grenade_exploded)
santa_nade = 'Santa'
def grenade_fountain(self, x, y, z):
for i in xrange(16):
time = i * 0.5
reactor.callLater(time, self.spawn_santa_grenade_fountain,
x, y, z)
def on_block_destroy(self, x, y, z, mode):
if ((x, y, z) in self.protocol.santa_blocks) and self.name is not None:
gift = ""
val = random.randrange(1, 8)
if val == 1:
gift = "death"
self.kill()
elif val == 2:
gift = "an airstrike"
self.airstrike = True
self.refill()
if self.kills < self.protocol.airstrike_min_score_req:
self.kills = self.protocol.airstrike_min_score_req
self.send_chat('Airstrike support ready! Launch with e.g. '
'/airstrike B4')
elif val == 3 or val == 4:
gift = "a restock"
self.refill()
elif val == 5:
gift = "several grenades without the pin"
self.spew_santa_grenades(x, y, z)
elif val == 6:
gift = "a grenade fountain"
self.grenade_fountain(x, y, z)
else:
gift = "nothing"
self.protocol.send_chat('%s opened one of Santa\'s presents and '
'received %s!' % (self.name, gift))
self.protocol.irc_say('%s opened one of Santa\'s presents and '
'received %s!' % (self.name, gift))
return connection.on_block_destroy(self, x, y, z, mode)
def on_block_removed(self, x, y, z):
if ((x, y, z) in self.protocol.santa_blocks):
self.protocol.santa_blocks.discard((x, y, z))
return connection.on_block_removed(self, x, y, z)
class SantaProtocol(protocol):
santa_blocks = set()
def __init__(self, *arg, **kw):
protocol.__init__(self, *arg, **kw)
self.santa_gift_loop = LoopingCall(self.santa_drop)
self.santa_gift_loop.start(300) # 5 minutes
def santa_drop(self):
"""Spawns random blocks in the center of the map D4, D5, E4, E5"""
if len(self.santa_blocks) > 4: # don't add anymore if there
print "Too many gifts exist right now." # are more than 10.
return
block_num = random.randrange(1, 3)
print "Attempting to place " + str(block_num) + " gifts"
print str(len(self.santa_blocks)) + " gifts currently"
for i in xrange(1, (block_num + 1)):
x, y, z = self.get_random_location(True, (192, 192, 320, 320))
z -= 1
self.santa_drop_block(x, y, z)
self.santa_blocks.add((x, y, z))
self.send_chat('Santa just dropped off some goodies!')
def santa_drop_block(self, x, y, z):
"""Places a gift from Santa"""
block_action = BlockAction()
block_action.player_id = 32
set_color = SetColor()
set_color.value = 0xFFFFFF
set_color.player_id = block_action.player_id
self.send_contained(set_color, save = True)
self.map.set_point_unsafe_int(x, y, z, 0xFFFFFF)
block_action.value = BUILD_BLOCK
set_color.value = 0xFFFFFF
block_action.x = x
block_action.y = y
block_action.z = z
self.send_contained(block_action, save = True)
return SantaProtocol, SantaConnection