-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra_firstflag.py
More file actions
170 lines (122 loc) · 4.8 KB
/
extra_firstflag.py
File metadata and controls
170 lines (122 loc) · 4.8 KB
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
"""
extra_firstflag - Announce first-flag incursions and kills during the protection window.
In AAS game mode, each team's "first flag" is the outermost capturable control
point on the opponent's side (determined by sgid ordering). For a configurable
protection window after round start (default 600 s), this module:
- Broadcasts an admin message when a player enters the opposing team's first
flag while it is still uncapturable (FIRSTFLAG event).
- Broadcasts an admin message when a vehicle kill occurs at the victim's own
first flag while it is still uncapturable (FIRSTFLAGKILL event).
Both handlers are automatically unregistered once the protection window expires.
Usage (standalone):
import extra_firstflag
extra_firstflag.setup(protection_time=300) # optional: override default (600 s)
extra_firstflag.init() # start listening
extra_firstflag.deinit() # stop listening and cancel timer
Usage (via toggler):
# enabled=True means the module starts active (init already called by you);
# enabled=False means it starts inactive (deinit already called / never init'd).
g_toggler.register("firstflag", extra_firstflag.init, extra_firstflag.deinit, enabled=True)
"""
import bf2
import host
import realityadmin as radmin
import realitygamemode as rgamemode
import realitycore as rcore
import realitytimer as rtimer
import realityserver
PROTECTION_TIME = 600
g_first_flags = {1: None, 2: None}
g_enabled = False
g_fire_once_timer = None
g_protection_time = PROTECTION_TIME
def setup(protection_time=PROTECTION_TIME):
global g_protection_time
g_protection_time = protection_time
def init():
global g_enabled
g_enabled = True
host.registerGameStatusHandler(onGameStatusChanged)
if bf2.gameLogic.getGameStatus() == bf2.GameStatus.Playing:
_tryStart()
def deinit():
global g_enabled, g_fire_once_timer
g_enabled = False
unregisterHandlers()
if g_fire_once_timer:
g_fire_once_timer.destroy()
g_fire_once_timer = None
host.unregisterHandler(onGameStatusChanged)
def onGameStatusChanged(status):
if status == bf2.GameStatus.Playing:
if not g_enabled:
return
_tryStart()
def _tryStart():
global g_fire_once_timer
if rgamemode.getCurrentGameModeType() != "aas":
return
unregisterHandlers()
buildFirstFlags()
elapsed = rcore.getTimeSinceRoundStart()
remaining = realityserver.C('STARTDELAY') + g_protection_time - elapsed
if remaining <= 0:
return
host.registerHandler('EnterControlPoint', onEnterControlPoint)
host.registerHandler('PlayerEnemyKilled', onPlayerEnemyKilled)
g_fire_once_timer = rtimer.fireOnce(unregisterHandlers, remaining)
def unregisterHandlers(data=None):
host.unregisterHandler(onEnterControlPoint)
host.unregisterHandler(onPlayerEnemyKilled)
def buildFirstFlags():
global g_first_flags
g_first_flags = {1: None, 2: None}
sgids = []
for cp in rcore.getControlPoints():
if cp.cp_getParam('unableToChangeTeam') == 1:
continue
sgids.append(cp.sgid)
if not sgids:
return
sgids = sorted(sgids)
# Team 1 advances from low sgid toward high sgid:
# their first flag to capture is the lowest sgid group
# Team 2 advances from high sgid toward low sgid:
# their first flag to capture is the highest sgid group
g_first_flags[1] = sgids[0]
g_first_flags[2] = sgids[-1]
def onEnterControlPoint(player, cp):
team = player.getTeam()
gpm = rgamemode.getCurrentGameMode()
if cp.cp_getParam('team') == team:
return
enemyTeam = 1 if team == 2 else 2
if cp.sgid != g_first_flags[enemyTeam]:
return
if gpm.isCPCapturableByTeam(cp, team):
return
# Player entered the first flag of the opposing team's main base side
radmin.adminPM("FIRSTFLAG: %s (%s, %s) entered first flag %s" % (
player.getName(), rcore.getTeamName(team), player.getSquadId(), cp.text
), None, history=False)
def onPlayerEnemyKilled(victim, attacker, weapon, assists, obj):
victimTeam = victim.getTeam()
attackerTeam = attacker.getTeam()
gpm = rgamemode.getCurrentGameMode()
victimCP = gpm.getOccupyingCP(victim)
if not victimCP:
return
if gpm.isCPCapturableByTeam(victimCP, attackerTeam):
return
if victimCP.sgid != g_first_flags[victimTeam]:
return
if attacker.getVehicle() and victim.getVehicle():
distance = int(
rcore.getVectorDistance(
attacker.getVehicle().getPosition(),
victim.getVehicle().getPosition()
)
)
radmin.adminPM("FIRSTFLAGKILL: %s [%s : %s m] %s" % (
attacker.getName(), weapon, distance, victim.getName()
), None, history=False)