-
Notifications
You must be signed in to change notification settings - Fork 2
/
nuke.py
352 lines (318 loc) · 17.5 KB
/
nuke.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from piqueserver.config import config
from piqueserver.commands import command, admin, player_only, target_player
from piqueserver import map
from pyspades.common import Vertex3
from pyspades.world import Grenade
from pyspades import contained as loaders
from math import ceil, sqrt, pi, sin, cos, exp, floor
from random import random
from time import time
from pyspades.constants import GRENADE_KILL, DESTROY_BLOCK
from twisted.internet import reactor
#Things for overriding grenade_explode
from itertools import product
from pyspades.constants import GRENADE_DESTROY
nukeErrorMessage = "Invalid Input, provide coordinate (eg: C3) and region (eg: NE for northeast)"
sectionConfig = config.section('nuke')
radiusConfig = sectionConfig.option('explosionRadius', default = 10)
maximumRadiusConfig = sectionConfig.option('maximumExplosionRadius', default = 50)
flatnessConfig = sectionConfig.option('flatness', default = 0.0083)
shiftnessConfig = sectionConfig.option('shiftness', default = 131)
#You can paste this into https://www.desmos.com/calculator to see a graph
#
# f\left(x\right)\ =\ \left(\left(M-N\right)\right)/(1+1e^{-k\left(x-\left(S\right)\right)})
#N = minimum (explosionRadius)
#M = maximum (maximumExplosionRadius)
#S = shiftness (shiftness)
#K = flatness (flatness)
#To modify start with default values and go from there
#X axis = grenade, Y axis = distance from explosionRadius
#Your domains should be X:(0,grenadeAmount) Y:(0,maximum-minimum)
propogationTimeConfig = sectionConfig.option('propogationTime', default = 2.5)
upHeightConfig = sectionConfig.option('upHeight', default = 8)
downHeightConfig = sectionConfig.option('downHeight', default = 2)
grenadeAmountConfig = sectionConfig.option('grenadeAmount', default = 400)
teamSuccessCDConfig = sectionConfig.option('teamSuccessCooldown', default = 30)
playerSuccessCDConfig = sectionConfig.option('playerSuccessCooldown', default = 60)
teamCDConfig = sectionConfig.option('teamCooldown', default = 5)
playerCDConfig = sectionConfig.option('playerCooldown', default = 5)
nukeTKConfig = sectionConfig.option('friendlyFire', default = False)
nukeLaunchTimeConfig = sectionConfig.option('launchTime', default = 30)
warningGlobalInitialConfig = sectionConfig.option('warningGlobalInitial', default = True)
warningOnIntervalConfig = sectionConfig.option('warningOnInterval', default = True)
warningIntervalConfig = sectionConfig.option('interval', default = 10)
warningCountdownConfig = sectionConfig.option('warningOnCountdown', default = True)
warningCountdownStartConfig = sectionConfig.option('warningCountdownStart', default = 10)
@command('nukes')
@target_player
@player_only
def nukes(connection, player):
connection.send_chat("%s has %d nukes." %(player.name, player.nukesAvailable))
@admin
@command('givenuke', 'gn')
@target_player
def givenuke(connection, player):
"""
Gives a nuke to target player
/givenuke *<player>*
"""
player.nukesAvailable = player.nukesAvailable+1
try:
connection.send_chat_notice("You have given %s a nuke." %player.name)
except AttributeError as ex:
print("%s has been given a nuke, but there was a %s error" %(player.name, type(ex)))
player.send_chat_notice("%s has given you a nuke." %connection.name)
@command('nuke', 'n')
@player_only
def nuke(connection, *args):
"""
Drops a devastating nuke that destroys building and kills many people
/nuke <Coordinate> *<Region>*
"""
if(connection.nukesAvailable>0):
if(len(args)>=1):
if(len(args[0])==2):
horizontal = ord(args[0][0].lower())-97
aimPosX = (horizontal*64) + 32
vertical = ord(args[0][1]) - 49
aimPosY = (vertical*64) + 32
accuracy = 32
if(horizontal in range(0,9) and vertical in range(0,9)):
if(len(args)==2):
if(len(args[1])>=1):
input = args[1].lower()
if(args[1][0].lower() in "neswc"):
accuracy=16
input = args[1][0].lower()
if(input=="n"):
aimPosY-=16
elif(input=="s"):
aimPosY+=16
elif(input=="e") and len(args[1])==1:
aimPosX+=16
elif(input=="w" and len(args[1])==1):
aimPosX-=16
if(len(args[1])==2):
input = args[1][1].lower()
if(input in "ew"):
if(input=="e"):
aimPosX+=16
elif(input=="w"):
aimPosX-=16
verticalDirection = ["north", "south"]["ns".find(args[1][0].lower())]
horizontalDirection = ["east", "west"]["ew".find(args[1][1].lower())]
connection.send_chat_notice("Nuking %s%s of %s%s" %(verticalDirection, horizontalDirection, chr(horizontal + 97).upper(), str(vertical+1)))
if(warningGlobalInitialConfig.get()):
connection.protocol.broadcast_chat("NUKE INBOUND: %s%s of %s%s" %(verticalDirection, horizontalDirection, chr(horizontal + 97).upper(), str(vertical+1)), global_message = True)
else:
#error
connection.send_chat_error(nukeErrorMessage)
return
else:
direction = ["north", "east", "south", "west", "center"]["neswc".find(args[1][0].lower())]
connection.send_chat_notice("Nuking %s of %s%s" %(direction, chr(horizontal + 97).upper(), str(vertical+1)))
if(warningGlobalInitialConfig.get()):
connection.protocol.broadcast_chat("NUKE INBOUND: %s of %s%s" %(direction, chr(horizontal + 97).upper(), str(vertical+1)), global_message = True)
else:
#error
connection.send_chat_error(nukeErrorMessage)
return
else:
#error
connection.send_chat_error(nukeErrorMessage)
return
else:
connection.send_chat_notice("Nuking coordinate %s%s" %(chr(horizontal + 97).upper(), str(vertical+1)))
if(warningGlobalInitialConfig.get()):
connection.protocol.broadcast_chat("NUKE INBOUND: %s%s" %(chr(horizontal + 97).upper(), str(vertical+1)), global_message = True)
else:
#error
connection.send_chat_error(nukeErrorMessage)
return
else:
#error
connection.send_chat_error(nukeErrorMessage)
return
else:
#error
connection.send_chat_error(nukeErrorMessage)
return
else:
connection.send_chat_error("You do not have a nuke available")
return
personalSuccessTimeLeft = playerSuccessCDConfig.get()-(time()-connection.personalSuccessNukeTime)
teamSuccessTimeLeft = teamSuccessCDConfig.get()-(time()-(connection.protocol.team1SuccessNukeTime if connection.team.id==0 else connection.protocol.team2SuccessNukeTime))
personalTimeLeft = playerCDConfig.get()-(time()-connection.personalNukeTime)
teamTimeLeft = teamCDConfig.get()-(time()-(connection.protocol.team1NukeTime if connection.team.id==0 else connection.protocol.team2NukeTime))
if(personalSuccessTimeLeft>0):
connection.send_chat_error("Your nuke is on cooldown for another %s seconds" %(round(personalSuccessTimeLeft,1)))
return
elif(teamSuccessTimeLeft>0):
connection.send_chat_error("Your nuke is on a team cooldown for another %s seconds" %(round(teamSuccessTimeLeft,1)))
return
elif(personalTimeLeft>0):
connection.send_chat_error("Your nuke is on cooldown for another %s seconds" %(round(personalTimeLeft,1)))
return
elif(teamTimeLeft>0):
connection.send_chat_error("Your nuke is on a team cooldown for another %s seconds" %(round(teamTimeLeft,1)))
return
randomDegrees = random() * 360
randomRadius = accuracy * random()
centerX = aimPosX + (randomRadius * cos(randomDegrees))
centerY = aimPosY + (randomRadius * sin(randomDegrees))
nukeLaunchTime = nukeLaunchTimeConfig.get()
#dropNuke(centerX, centerY, accuracy, connection)
reactor.callLater(nukeLaunchTime, dropNuke, centerX, centerY, accuracy, connection.team, connection)
#warningOnInterval, warningInterval
warningInterval = warningIntervalConfig.get()
if(warningOnIntervalConfig.get() and nukeLaunchTime>warningInterval):
print("WarningOnInterval true and launch time is greater than interval")
reactor.callLater(nukeLaunchTime%warningInterval, intervalWarning, connection, warningInterval * floor(nukeLaunchTime/warningInterval))
warningCountdownStart = warningCountdownStartConfig.get()
if(warningCountdownConfig.get()):
if(nukeLaunchTime>=warningCountdownStart):
reactor.callLater(nukeLaunchTime-warningCountdownStart, countdownWarning, connection, warningCountdownStart)
else:
print("Cannot print nuke launch time because config option \"nukeLaunchTime\" is not greater than 5")
connection.personalNukeTime = time()
connection.nukesAvailable-=1
def intervalWarning(connection, timeLeft):
warningInterval = warningIntervalConfig.get()
warningCountdown = warningCountdownConfig.get()
if(timeLeft < warningInterval):
if(not warningCountdown):
connection.protocol.broadcast_chat("NUKE IMMINENT", global_message=True)
else:
connection.protocol.broadcast_chat("NUKE IN: %s" %(round(timeLeft)), global_message = True)
if(not(round(timeLeft) <= warningCountdownStartConfig.get()+warningInterval and warningCountdown)):
reactor.callLater(warningInterval, intervalWarning, connection, min(timeLeft,timeLeft-warningInterval))
def countdownWarning(connection, timeLeft):
if(round(timeLeft)==0):
connection.protocol.broadcast_chat("NUKE IMMINENT", global_message=True)
else:
connection.protocol.broadcast_chat("NUKE IN: %s" %(round(timeLeft)), global_message = True)
reactor.callLater(1, countdownWarning, connection, timeLeft-1)
def dropNuke(x, y, accuracy, connectionTeam, connection=None):
mapData = connection.protocol.map
z = mapData.get_z(x, y)
centerPosition = [x,y,z]
block_action = loaders.BlockAction()
block_action.value = DESTROY_BLOCK
block_action.player_id = 32
radius = radiusConfig.get()
upHeight = upHeightConfig.get()
downHeight = downHeightConfig.get()
for x in range(-1*radius,radius):
block_action.x = centerPosition[0]+x
for y in range(-1*radius,radius):
block_action.y = centerPosition[1]+y
for z in range(-1*downHeight,upHeight):
block_action.z = centerPosition[2] + z
if(block_action.z >= 63): continue
if(sqrt((block_action.x-centerPosition[0])**2 + (block_action.y-centerPosition[1])**2) <= radius):
if(mapData.get_color(block_action.x, block_action.y, block_action.z) is not None):
connection.protocol.broadcast_contained(block_action)
mapData.remove_point(block_action.x, block_action.y, block_action.z)
def killPlayers(players):
for player in players:
ploc = player.get_location()
if(sqrt((ploc[0]-centerPosition[0])**2 + (ploc[1]-centerPosition[1])**2)<=radius and (centerPosition[2]-upHeight+4) <= (ploc[2]) <= (centerPosition[2]+downHeight+2)):
#player.kill(kill_type=GRENADE_KILL)
killGrenade = connection.protocol.world.create_object(Grenade, 0.0, Vertex3(-5, -5, -5), None, Vertex3(0,0,2), None)
killGrenade.name = "NoStreak"
player.kill(kill_type=GRENADE_KILL, by = connection, grenade = killGrenade)
connection.personalSuccessNukeTime = time()
if(connectionTeam.id==0):
connection.protocol.team1SuccessNukeTime = time()
else:
connection.protocol.team2SuccessNukeTime = time()
killPlayers(connectionTeam.other.get_players())
#killPlayers((connection.protocol.team_1 if connectionTeam.id==1 else connection.protocol.team_2).get_players())
if(nukeTKConfig.get()):
killPlayers(connection.team.get_players())
grenadeAmount = grenadeAmountConfig.get()
for i in range(0,grenadeAmount):
randomDistance = (maximumRadiusConfig.get() - radius) / (1 + exp(-flatnessConfig.get() * (i-shiftnessConfig.get())))
fuse = (propogationTimeConfig.get())/(grenadeAmount-radius)*i
randomDegrees = random() * 360
grenadeX = centerPosition[0] + (randomDistance * cos(randomDegrees))
grenadeY = centerPosition[1] + (randomDistance * sin(randomDegrees))
grenade = connection.protocol.world.create_object(Grenade, fuse, Vertex3(grenadeX, grenadeY, min(62,mapData.get_z(grenadeX, grenadeY)+ceil((random()-0.25)*1.5))), None, Vertex3(0,0,2), connection.nuke_grenade_exploded)
grenade.team = connectionTeam
grenade.name = "nadeNoStreak" + ("NukeTeamID"+str(connectionTeam.id))
if(connectionTeam.id==0):
connection.protocol.team1NukeTime = time()
else:
connection.protocol.team2NukeTime = time()
def apply_script(protocol, connection, config):
class nukeConnection(connection):
personalSuccessNukeTime = 0
personalNukeTime = 0
nukesAvailable = 0
def on_hit(self, hit_amount, hit_player, kill_type, grenade):
connection.on_hit(self, hit_amount, hit_player, kill_type, grenade)
#def nuke_hit(self, hit_amount, hit_player, kill_type, grenade):
def nuke_grenade_exploded(self, grenade):
if(self.name is None):
self.name = "AAA"
self.team = self.protocol.team_2 if "NukeTeamID1" in grenade.name else self.protocol.team_1
grenade.team = self.team
import math
if self.name is None or self.team.spectator:
return
if grenade.team is not None and grenade.team is not self.team:
# could happen if the player changed team
return
position = grenade.position
x = position.x
y = position.y
z = position.z
if x < 0 or x > 512 or y < 0 or y > 512 or z < 0 or z > 63:
return
x = int(math.floor(x))
y = int(math.floor(y))
z = int(math.floor(z))
for player_list in (self.team.other.get_players(), (self,), self.team.get_players() if ("Nuke" in grenade.name and nukeTKConfig.get()) else []):
for player in player_list:
if not player.hp:
continue
damage = grenade.get_damage(player.world_object.position)
if damage == 0:
continue
returned = self.on_hit(damage, player, GRENADE_KILL, grenade)
if returned == False:
continue
elif returned is not None:
damage = returned
player.set_hp(player.hp - damage, self, hit_indicator=position.get(), kill_type=GRENADE_KILL,grenade=grenade)
if self.on_block_destroy(x, y, z, GRENADE_DESTROY) == False:
return
map = self.protocol.map
for n_x, n_y, n_z in product(range(x - 1, x + 2), range(y - 1, y + 2), range(z - 1, z + 2)):
count = map.destroy_point(n_x, n_y, n_z)
if count:
self.total_blocks_removed += count
self.on_block_removed(n_x, n_y, n_z)
block_action = loaders.BlockAction()
block_action.x = x
block_action.y = y
block_action.z = z
block_action.value = GRENADE_DESTROY
block_action.player_id = self.player_id
self.protocol.broadcast_contained(block_action, save=True)
self.protocol.update_entities()
def on_kill(self, killer, kill_type, grenade):
if(grenade is not None):
if("Nuke" in grenade.name):
killer.personalSuccessNukeTime = time()
if(killer.team.id==0):
killer.protocol.team1SuccessNukeTime = time()
else:
killer.protocol.team2SuccessNukeTime = time()
connection.on_kill(self, killer, kill_type, grenade);
class nukeProtocol(protocol):
team1SuccessNukeTime = 0
team2SuccessNukeTime = 0
team1NukeTime = 0
team2NukeTime = 0
return nukeProtocol, nukeConnection