-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathEventHandler.zs
283 lines (258 loc) · 11.2 KB
/
EventHandler.zs
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
// Event handler for Gun Bonsai.
// Handles giving players a stat tracking item when they spawn in, and assigning
// XP to their currently wielded weapon when they damage something.
#namespace TFLV;
#debug off;
class ::EventHandler : StaticEventHandler {
::PerPlayerStats playerstats[MAXPLAYERS];
::Upgrade::Registry UPGRADE_REGISTRY;
::RC rc;
::GunBonsaiService service;
ui ::HUD hud;
override void OnRegister() {
console.printf("Initializing Gun Bonsai v%s...", MOD_VERSION());
UPGRADE_REGISTRY = new("::Upgrade::Registry");
if (::Settings.have_legendoom()) {
console.printf("%s", StringTable.Localize("$TFLV_MSG_LD_YES"));
} else {
console.printf("%s", StringTable.Localize("$TFLV_MSG_LD_NO"));
}
rc = ::RC.LoadAll("BONSAIRC");
rc.Finalize(self);
let service = ::GunBonsaiService(ServiceIterator.Find("::GunBonsaiService").Next());
service.Init(self);
self.service = service;
}
override void WorldLoaded(WorldEvent evt) {
if (level.totaltime == 0) {
// Starting a new game? Clear all info.
for (uint i = 0; i < MAXPLAYERS; ++i) playerstats[i] = null;
}
for (uint i = 0; i < MAXPLAYERS; ++i) {
if (playeringame[i]) InitPlayer(i, true);
}
}
void InitPlayer(uint p, bool new_map=false) {
PlayerPawn pawn = players[p].mo;
if (!pawn) return;
let proxy = ::PerPlayerStatsProxy(pawn.FindInventory("::PerPlayerStatsProxy"));
if (proxy) {
// Spawned in player already has stats, those take precedence over whatever
// we remember.
DEBUG("Restoring old stats already held by player %d", p);
playerstats[p] = proxy.stats;
playerstats[p].Initialize(proxy);
DEBUG("proxy ring ok: %d", proxy.stats.proxy == proxy);
DEBUG("stats ring ok: %d", playerstats[p].proxy.stats == playerstats[p]);
DEBUG("pawn ring ok: %d %d",
pawn == proxy.owner,
pawn == proxy.stats.owner);
} else {
// Spawned in player doesn't have stats, give them a proxy holding whatever
// stats we have for them.
DEBUG("Player %d doesn't have stats, reassigning", p);
if (!playerstats[p] || (new_map && !bonsai_ignore_death_exits)) {
// Either we don't have stats for this player, or we do but we're meant
// to respect death exits; in either case create new stats for them ex
// nihilo.
// Note that in the latter case we run this branch only when starting a
// new map; if the player's inventory has vanished mid-map we make sure
// to restore the upgrades they already have, for compatibility with mods
// like Blade of Agony that are constantly taking away your inventory for
// cutscenes and stuff and then giving it back later.
DEBUG("No stats recorded in playerstats[%d], starting from scratch", p);
playerstats[p] = new("::PerPlayerStats");
}
proxy = ::PerPlayerStatsProxy(pawn.GiveInventoryType("::PerPlayerStatsProxy"));
proxy.Initialize(playerstats[p]);
}
}
// Return the stats struct for the consoleplayer, without modifying the playsim.
// If the stats are missing, null will be returned, and if the playerpawn doesn't
// have a proxy object or disagrees with the contents of playerstats, too bad.
// This is used by menu code to get the stats for the player-at-keyboard.
ui static ::PerPlayerStats GetConsolePlayerStats() {
return ::EventHandler(StaticEventHandler.Find("::EventHandler")).playerstats[consoleplayer];
}
// Get the stats struct for the consoleplayer, resolving any mismatches between
// the EventHandler's view of things and the PlayerPawn. In particular, if only
// one of them has the info, that is taken as authoritative and copied to the other;
// if they both have info but disagree on it, PlayerPawn wins.
// TODO: some planned upgrades probably need the proxy to be moved to the head
// of the player's inventory when this happens.
::PerPlayerStats GetStatsFor(PlayerPawn pawn) {
// DEBUG("GetStatsFor: %s", TAG(pawn));
if (!pawn) return null;
let stats = playerstats[pawn.PlayerNumber()];
// If the stats are still attached to the proxy object and the proxy object
// is still attached to the player, we don't need to rummage through the
// player's inventory.
if (stats && stats.proxy && stats.proxy.owner == stats.owner && stats.owner == pawn)
return stats;
// Otherwise, something has gone wrong -- either we don't have playerstats
// recorded here, or they don't match the stats in the player's inventory.
DEBUG("GetStats: eventhandler/playsim mismatch for player %d", pawn.PlayerNumber());
InitPlayer(pawn.PlayerNumber());
return playerstats[pawn.PlayerNumber()];
}
ui bool ShouldDrawHUD(uint p) const {
return playeringame[p]
&& playerstats[p]
&& players[p].ReadyWeapon
&& screenblocks <= 11
&& !automapactive;
}
/* ui */ override void RenderOverlay(RenderEvent evt) {
if (!ShouldDrawHUD(consoleplayer)) return;
if (!hud) hud = new("::HUD");
::CurrentStats stats;
if (playerstats[consoleplayer].GetCurrentStats(stats))
hud.Draw(stats);
}
void ShowInfo(uint p) {
// Force info creation
let stats = playerstats[p];
if (!stats) {
console.printf(StringTable.Localize("$TFLV_MSG_PLAYERSTATS_MISSING"), p);
return;
}
// Check for pending level ups and apply those if present.
if (stats.GetInfoForCurrentWeapon() && stats.GetInfoForCurrentWeapon().StartLevelUp()) return;
if (stats.StartLevelUp()) return;
if (p == consoleplayer) Menu.SetMenu("GunBonsaiStatusDisplay");
return;
}
void ChooseLevelUpOption(uint p, int index) {
if (!playerstats[p]) return;
let giver = playerstats[p].currentEffectGiver;
if (!giver) {
// They chose a level up but their effectgiver is missing! This probably means
// that something ate their inventory between them opening the menu and choosing
// an option.
DEBUG("Stats for player %d have id %d and no effect giver", p, playerstats[p].id);
return;
}
giver.Choose(index);
}
void CycleLDEffect(uint p) {
if (!playerstats[p]) return;
let info = playerstats[p].GetInfoForCurrentWeapon();
if (info) info.ld_info.CycleEffect();
}
void SelectLDEffect(uint p, int index) {
if (!playerstats[p]) return;
let info = playerstats[p].GetInfoForCurrentWeapon();
if (info) info.ld_info.SelectEffect(index);
}
override void NetworkProcess(ConsoleEvent evt) {
if (evt.name == "bonsai-show-info") {
ShowInfo(evt.player);
} else if (evt.name == "bonsai-cycle-ld-effect") {
if (::Settings.have_legendoom()) {
CycleLDEffect(evt.player);
} else {
players[evt.player].mo.A_Log(StringTable.Localize("$TFLV_MSG_LD_REQUIRED"));
}
} else if (evt.name == "bonsai-select-effect") {
if (::Settings.have_legendoom()) {
SelectLDEffect(evt.player, evt.args[0]);
} else {
players[evt.player].mo.A_Log(StringTable.Localize("$TFLV_MSG_LD_REQUIRED"));
}
} else if (evt.name == "bonsai-choose-level-up-option") {
ChooseLevelUpOption(evt.player, evt.args[0]);
} else if (evt.name == "bonsai-toggle-upgrade") {
// args[0] holds the bag index, args[1] the index of the upgrade within that bag.
// In the future this might be extended to allow viewing and toggling of upgrades
// on weapons not presently equipped, but for now [0] is always either 0 (player
// upgrades) or 1 (currently equipped weapon upgrades).
ToggleUpgrade(evt.player, evt.args[0] != 0, evt.args[1]);
} else if (evt.name == "bonsai-tune-upgrade") {
// Same as above, except that args[2] holds the amount to tune by, which
// may be negative.
TuneUpgrade(evt.player, evt.args[0] != 0, evt.args[1], evt.args[2]);
} else if (evt.name.IndexOf("bonsai-debug") == 0) {
::Debug.DebugCommand(service, evt.player, evt.name, evt.args[0]);
}
}
void ToggleUpgrade(int player, bool on_weapon, int index) {
let stats = playerstats[player];
DEBUG("toggle: %d %d", on_weapon, index);
// ::Upgrade::BaseUpgrade upgrade;
if (on_weapon) {
DEBUG("weapon has upgrades: %d", stats.GetInfoForCurrentWeapon().upgrades != null);
stats.GetInfoForCurrentWeapon().ToggleUpgrade(index);
} else {
DEBUG("player has upgrades: %d", stats.upgrades != null);
stats.ToggleUpgrade(index);
}
}
void TuneUpgrade(int player, bool on_weapon, int index, int amount) {
let stats = playerstats[player];
DEBUG("tune: %d %d %d", on_weapon, index, amount);
// ::Upgrade::BaseUpgrade upgrade;
if (on_weapon) {
stats.GetInfoForCurrentWeapon().TuneUpgrade(index, amount);
} else {
stats.TuneUpgrade(index, amount);
}
}
static PlayerPawn GetPlayerDamageSource(WorldEvent evt) {
if (evt.damage <= 0
|| !evt.thing.bISMONSTER
|| !evt.damagesource
|| evt.thing.bFRIENDLY
|| evt.thing == evt.damagesource) {
// Don't trigger on non-damaging "attacks", attacks against things that
// aren't monsters, attacks on friendlies, or self-damage.
return null;
}
// If the damage source is a player, we're good to go.
if (evt.damagesource is "PlayerPawn") return PlayerPawn(evt.damagesource);
// If the damage source has a FriendPlayer defined, attribute the damage to
// them instead of the real source. Subtract 1 because FriendPlayer is 1-indexed.
if (evt.damagesource.FriendPlayer && playeringame[evt.damagesource.FriendPlayer-1])
return players[evt.damagesource.FriendPlayer-1].mo;
//
return null;
}
override void WorldThingDamaged(WorldEvent evt) {
DEBUG("WTD: %s inflictor=%s source=%s damage=%d type=%s flags=%X, hp=%d",
TAG(evt.thing), TAG(evt.inflictor), TAG(evt.damagesource),
evt.damage, evt.damagetype, evt.damageflags, evt.thing.health);
// Player taking damage from something.
// Note that this triggers even on self-damage, which is what allows upgrades
// like Blast Shaping to work.
if (PlayerPawn(evt.thing)) {
::PerPlayerStats.GetStatsFor(PlayerPawn(evt.thing)).OnDamageReceived(
evt.inflictor, evt.damagesource, evt.damage);
return;
}
// Player damaging something. This does not trigger on self-damage (c.f.
// GetPlayerDamageSource()) so you don't end up applying dots to yourself
// or exploding or something.
PlayerPawn source = GetPlayerDamageSource(evt);
if (source) {
let stats = ::PerPlayerStats.GetStatsFor(PlayerPawn(source));
stats.OnDamageDealt(evt.inflictor, evt.thing, evt.damage);
if (evt.thing.health <= 0) {
stats.OnKill(evt.inflictor, evt.thing);
}
return;
}
}
override void WorldThingSpawned(WorldEvent evt) {
Actor thing = evt.thing;
if (!thing) return;
if (thing.bMISSILE && PlayerPawn(thing.target)) {
// If it's a projectile (MISSILE flag is set) and target=player, the player
// just fired a shot. This is our chance to fiddle with its flags and whatnot.
::PerPlayerStats.GetStatsFor(thing.target).OnProjectileCreated(thing);
}
// DEBUG("WTS: %s (owner=NONE) (master=%s) (target=%s) (tracer=%s)",
// thing.GetClassName(),
// TAG(thing.master),
// TAG(thing.target),
// TAG(thing.tracer));
}
}