-
Notifications
You must be signed in to change notification settings - Fork 1
/
health.inc
576 lines (454 loc) · 14.5 KB
/
health.inc
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// built-in include guard removal
// just in case the user has a local dependency with the same file name
#if defined _inc_health
#undef _inc_health
#endif
// custom include-guard to ensure we don't duplicate
#if defined _health_included
#endinput
#endif
#define _health_included
#include "health_version"
#include <a_samp>
#include <logger>
#include <mathutil>
#include <energy>
#include <language>
#include <YSI\y_iterate>
#include "knockout.inc"
#include <YSI\y_hooks>
// MAX_WOUNDS controls the maximum amount of wounds that can be applied to a player. If a player has
// more wounds inflicted than this value, they will die instantly.
#if !defined MAX_WOUNDS
#define MAX_WOUNDS (32)
#endif
// MAX_WOUND_SOURCE_LEN is the maximum string length for a wound source.
#if !defined MAX_WOUND_SOURCE_LEN
#define MAX_WOUND_SOURCE_LEN (10)
#endif
// BLOOD_ATTACH_INDEX controls which attachment index is used for the blood particle effect.
#if !defined BLOOD_ATTACH_INDEX
#define BLOOD_ATTACH_INDEX (9)
#endif
enum {
BODY_PART_TORSO = 3,
BODY_PART_GROIN,
BODY_PART_LEFT_ARM,
BODY_PART_RIGHT_ARM,
BODY_PART_LEFT_LEG,
BODY_PART_RIGHT_LEG,
BODY_PART_HEAD,
}
enum E_WOUND_TYPE {
E_WOUND_FIREARM, // All guns
E_WOUND_LACERATION, // Bladed melee weapons
E_WOUND_BRUISE, // Blunt melee weapons
E_WOUND_BURN // Fire/acid/etc
}
enum E_WOUND_DATA {
E_WOUND_TYPE:wound_type, // Type of wound from the E_WOUND_TYPE enumerator
Float:wound_bleedrate, // Bleed rate of the wound
wound_timestamp, // Time of wound infliction
wound_bodypart, // Body part the wound is on
wound_source[MAX_WOUND_SOURCE_LEN] // Name of the source of the wound infliction
}
static
bool:health_Active[MAX_PLAYERS], // health processing is active
Float:health_Blood[MAX_PLAYERS], // total blood ("health")
Float:health_BleedRate[MAX_PLAYERS], // total bleed rate
health_WoundData[MAX_PLAYERS][MAX_WOUNDS][E_WOUND_DATA], // all wound data for player
Iterator:health_WoundIndex[MAX_PLAYERS]<MAX_WOUNDS>, // index for wound data
health_DeltDamageTo[MAX_PLAYERS][MAX_PLAYER_NAME], // last player to deal damage to
health_TookDamageFrom[MAX_PLAYERS][MAX_PLAYER_NAME], // last player took damage from
health_DeltDamageTick[MAX_PLAYERS], // last recorded damage delt
health_TookDamageTick[MAX_PLAYERS]; // last recorded damage taken
forward OnPlayerWounded(playerid, targetid);
// -
// Init
// -
hook OnScriptInit() {
new languageid = InitLanguage("English");
AddLanguageEntry(languageid, "WOUNDEDMSSG", "Wounded: %s~n~Severity: %s");
Iter_Init(health_WoundIndex);
}
hook OnPlayerConnect(playerid) {
health_Active[playerid] = true;
health_DeltDamageTo[playerid][0] = EOS;
health_TookDamageFrom[playerid][0] = EOS;
health_DeltDamageTick[playerid] = 0;
health_TookDamageTick[playerid] = 0;
Iter_Clear(health_WoundIndex[playerid]);
return 1;
}
// -
// API
// -
// ToggleHealthProcessingForPlayer when set to false disables all health effects and processing for
// the given player. This is useful for when players go on admin duty or change minigame.
stock ToggleHealthProcessingForPlayer(playerid, bool:toggle) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
health_Active[playerid] = toggle;
return 0;
}
stock IsHealthProcessingActive(playerid) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
return health_Active[playerid];
}
// SetPlayerBlood updates the player's blood
stock SetPlayerBlood(playerid, Float:blood) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(blood > 100.0) {
blood = 100.0;
} else if(blood < 0.0) {
blood = 0.0;
}
health_Blood[playerid] = blood;
SetPlayerHealth(playerid, blood);
return 0;
}
// GivePlayerBlood adds `blood` to the player's blood level
stock GivePlayerBlood(playerid, Float:blood) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
return SetPlayerBlood(playerid, health_Blood[playerid] + blood);
}
// GetPlayerBlood returns the players blood into `blood`
stock GetPlayerBlood(playerid, &Float:blood) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
blood = health_Blood[playerid];
return 0;
}
// SetPlayerBleedRate updates the player's bleed rate
stock SetPlayerBleedRate(playerid, Float:rate) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
health_BleedRate[playerid] = rate;
return 0;
}
// GetPlayerBleedRate returns the players bleed rate into `rate`
stock GetPlayerBleedRate(playerid, &Float:rate) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
rate = health_BleedRate[playerid];
return 0;
}
stock PlayerInflictWound(playerid, targetid, E_WOUND_TYPE:type, Float:bleedrate, Float:knockmult, bodypart, source[]) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(!IsPlayerConnected(targetid)) {
return 2;
}
if(!health_Active[targetid]) {
return 3;
}
new
woundid = Iter_Free(health_WoundIndex[targetid]),
woundcount,
Float:totalbleedrate = health_BleedRate[targetid];
// target has maximum amount of wounds, kill instantly
if(woundid == ITER_NONE) {
SetPlayerBlood(targetid, 0.0);
return 4;
}
Iter_Add(health_WoundIndex[targetid], woundid);
woundcount = Iter_Count(health_WoundIndex[targetid]);
health_WoundData[targetid][woundid][wound_type] = type;
health_WoundData[targetid][woundid][wound_bleedrate] = bleedrate;
health_WoundData[targetid][woundid][wound_timestamp] = gettime();
health_WoundData[targetid][woundid][wound_bodypart] = bodypart;
strcpy(health_WoundData[targetid][woundid][wound_source], source, MAX_WOUND_SOURCE_LEN);
totalbleedrate += bleedrate;
// Truncate result to 1.0
totalbleedrate = totalbleedrate > 1.0 ? 1.0 : totalbleedrate;
health_BleedRate[targetid] = totalbleedrate;
// Remove a chunk of blood
GivePlayerBlood(targetid, -(bleedrate * 100.0));
switch(bodypart) {
case BODY_PART_TORSO: knockmult *= 1.0;
case BODY_PART_GROIN: knockmult *= 1.2;
case BODY_PART_LEFT_ARM: knockmult *= 0.9;
case BODY_PART_RIGHT_ARM: knockmult *= 0.9;
case BODY_PART_LEFT_LEG: knockmult *= 0.8;
case BODY_PART_RIGHT_LEG: knockmult *= 0.8;
case BODY_PART_HEAD: knockmult *= 9.9;
}
new Float:knockoutchance = knockmult * ((woundcount + 1) * 0.2) * ((totalbleedrate * 50) + 1);
if(frandom(100.0) < knockoutchance) {
new
Float:hp = health_Blood[targetid],
knockouttime;
knockouttime = floatround((knockmult * 0.2) * ((woundcount + 1) * ((totalbleedrate * 10) + 1) * (110.0 - hp) + (200 * (110.0 - hp))));
if(knockouttime > 1500) {
dbg("health", "knocked out player",
_i("targetid", targetid),
_i("knockouttime", knockouttime),
_i("woundcount", woundcount),
_f("hp", hp),
_f("totalbleedrate", totalbleedrate));
KnockOutPlayer(targetid, knockouttime);
}
}
health_TookDamageTick[targetid] = GetTickCount();
if(IsPlayerConnected(playerid)) {
health_DeltDamageTick[playerid] = GetTickCount();
GetPlayerName(targetid, health_DeltDamageTo[playerid], MAX_PLAYER_NAME);
GetPlayerName(playerid, health_TookDamageFrom[targetid], MAX_PLAYER_NAME);
dbg("health", "player wounded another player",
_i("playerid", playerid),
_i("targetid", targetid),
_f("bleedrate", bleedrate),
_f("knockmult", knockmult),
_i("bodypart", bodypart),
_s("source", source));
} else {
dbg("health", "player wounded",
_i("targetid", targetid),
_f("bleedrate", bleedrate),
_f("knockmult", knockmult),
_i("bodypart", bodypart),
_s("source", source));
}
CallLocalFunction("OnPlayerWounded", "dd", playerid, targetid);
new message[128];
format(
message,
sizeof(message),
@L(targetid, "WOUNDEDMSSG", true),
source,
knockoutchance < 50.0 ? ("Minor") : ("Severe")
);
ShowActionText(targetid, message, 5000);
return 1;
}
stock GetPlayerKnockoutChance(playerid, Float:knockmult, &Float:chance) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
chance = knockmult * (((Iter_Count(health_WoundIndex[playerid]) + 1) * 0.2) * ((health_BleedRate[playerid] * 50) + 1));
return 0;
}
stock GetLastDeltDamageTo(playerid, name[MAX_PLAYER_NAME])
{
if(!IsPlayerConnected(playerid))
return 0;
name[0] = EOS;
strcat(name, health_DeltDamageTo[playerid]);
return 1;
}
stock GetLastTookDamageFrom(playerid, name[MAX_PLAYER_NAME])
{
if(!IsPlayerConnected(playerid))
return 0;
name[0] = EOS;
strcat(name, health_TookDamageFrom[playerid]);
return 1;
}
stock GetPlayerDeltDamageTick(playerid, &tick)
{
if(!IsPlayerConnected(playerid))
return 0;
tick = health_DeltDamageTick[playerid];
return 0;
}
stock GetPlayerTookDamageTick(playerid, &tick) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
tick = health_TookDamageTick[playerid];
return 0;
}
stock RemovePlayerWounds(playerid, amount = 1) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
new idx;
foreach(new i : health_WoundIndex[playerid]) {
if(idx == amount) {
break;
}
new next;
Iter_SafeRemove(health_WoundIndex[playerid], i, next);
i = next;
idx++;
}
return 0;
}
stock GetPlayerWounds(playerid, &wounds) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
wounds = Iter_Count(health_WoundIndex[playerid]);
return 0;
}
stock Float:GetBleedSlowRate(Float:blood, Float:bleedrate, wounds) {
return (((((100.0 - blood) / 360.0) * bleedrate) / (1 + wounds)) / 100.0);
}
stock GetPlayerWoundsPerBodypart(playerid, output[7]) {
if(!IsPlayerConnected(playerid)) {
return 0;
}
foreach(new i : health_WoundIndex[playerid]) {
switch(health_WoundData[playerid][i][wound_bodypart]) {
case BODY_PART_TORSO: output[0]++;
case BODY_PART_GROIN: output[1]++;
case BODY_PART_LEFT_ARM: output[2]++;
case BODY_PART_RIGHT_ARM: output[3]++;
case BODY_PART_LEFT_LEG: output[4]++;
case BODY_PART_RIGHT_LEG: output[5]++;
case BODY_PART_HEAD: output[6]++;
default: output[0]++;
}
}
return 1;
}
// SerialiseWoundData turns player's wound data into an array of cells for storage.
stock SerialiseWoundData(playerid, output[], len = sizeof(output)) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
new
idx = 1,
sourcelen;
output[0] = Iter_Count(health_WoundIndex[playerid]);
if(output[0] == 0) {
return 2;
}
foreach(new i : health_WoundIndex[playerid]) {
if(idx > len) {
err("array index out of bounds");
return 3;
}
output[idx++] = _:health_WoundData[playerid][i][wound_type];
output[idx++] = _:health_WoundData[playerid][i][wound_bleedrate];
output[idx++] = health_WoundData[playerid][i][wound_timestamp];
output[idx++] = health_WoundData[playerid][i][wound_bodypart];
sourcelen = strlen(health_WoundData[playerid][i][wound_source]) + 1; // + \0
output[idx++] = sourcelen;
//memcpy(output[idx++], health_WoundData[playerid][i][wound_source], 0, 32 * 4, 32);
// alternative version, memcpy seems to be causing stack issues:
for(new j; j < sourcelen; j++) {
output[idx++] = health_WoundData[playerid][i][wound_source][j];
}
}
return 0;
}
// DeSerialiseWoundData unpacks an array of cells from SerialiseWoundData and applies it.
stock DeSerialiseWoundData(playerid, input[]) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(input[0] == 0) {
// no wounds in stream
return 2;
}
if(!(0 < input[0] < MAX_WOUNDS)) {
err("wound count invalid",
_i("count", input[0]));
return 3;
}
new
idx = 1,
woundid,
sourcelen;
for(new i; i < input[0]; i++) {
woundid = Iter_Free(health_WoundIndex[playerid]);
if(woundid == ITER_NONE) {
err("out of wound slots",
_i("cell", (idx - 1) / _:E_WOUND_DATA),
_i("idx", idx));
break;
}
health_WoundData[playerid][woundid][wound_type] = E_WOUND_TYPE:input[idx++];
health_WoundData[playerid][woundid][wound_bleedrate] = Float:input[idx++];
health_WoundData[playerid][woundid][wound_timestamp] = input[idx++];
health_WoundData[playerid][woundid][wound_bodypart] = input[idx++];
sourcelen = input[idx++]; // source string length
if(sourcelen > MAX_WOUND_SOURCE_LEN) {
err("sourcelen out of bounds", _i("sourcelen", sourcelen));
sourcelen = MAX_WOUND_SOURCE_LEN;
} else if(sourcelen == 0) {
err("sourcelen below zero", _i("sourcelen", sourcelen));
sourcelen = 0;
}
// memcpy(health_WoundData[playerid][woundid][wound_source], input[idx], 0, 32 * 4); // no idx++
// idx += sourcelen; // jump over the string
for(new k; k < sourcelen; k++) {
health_WoundData[playerid][woundid][wound_source][k] = input[idx++];
}
Iter_Add(health_WoundIndex[playerid], woundid);
}
return 0;
}
// -
// Internal
// -
hook OnPlayerUpdate(playerid) {
if(health_Active[playerid]) {
SetPlayerHealth(playerid, health_Blood[playerid]);
}
return Y_HOOKS_CONTINUE_RETURN_1;
}
ptask _health_timerUpdate[1000](playerid) {
if(!health_Active[playerid]) {
RemovePlayerAttachedObject(playerid, BLOOD_ATTACH_INDEX);
return;
}
if(IsNaN(health_BleedRate[playerid]) || health_BleedRate[playerid] < 0.0) {
health_BleedRate[playerid] = 0.0;
}
if(health_BleedRate[playerid] > 0.0) {
SetPlayerBlood(playerid, health_Blood[playerid] - health_BleedRate[playerid]);
if(health_Blood[playerid] < 0.1) {
SetPlayerBlood(playerid, 0.0);
}
/*
Slow bleeding based on health and wound count. Less wounds means
faster degradation of bleed rate. As blood rate drops, the bleed
rate will slow down faster (pseudo blood pressure). Results in a
bleed-out that slows down faster over time (only subtly). No wounds
will automatically stop the bleed rate due to the nature of the
formula (however this is still intentional).
*/
health_BleedRate[playerid] -= GetBleedSlowRate(health_Blood[playerid], health_BleedRate[playerid], Iter_Count(health_WoundIndex[playerid]));
if(!IsPlayerInAnyVehicle(playerid)) {
if(IsPlayerAttachedObjectSlotUsed(playerid, BLOOD_ATTACH_INDEX)) {
if(frandom(0.1) < 0.1 - health_BleedRate[playerid]) {
RemovePlayerAttachedObject(playerid, BLOOD_ATTACH_INDEX);
}
} else {
if(frandom(0.1) < health_BleedRate[playerid]) {
SetPlayerAttachedObject(playerid, BLOOD_ATTACH_INDEX, 18706, 1, 0.088999, 0.020000, 0.044999, 0.088999, 0.020000, 0.044999, 1.179000, 1.510999, 0.005000);
}
}
} else {
RemovePlayerAttachedObject(playerid, BLOOD_ATTACH_INDEX);
}
} else {
if(IsPlayerAttachedObjectSlotUsed(playerid, BLOOD_ATTACH_INDEX)) {
RemovePlayerAttachedObject(playerid, BLOOD_ATTACH_INDEX);
}
new Float:energy;
GetPlayerEnergy(playerid, energy);
GivePlayerBlood(playerid, 0.001925925 * energy);
if(health_BleedRate[playerid] < 0.0) {
health_BleedRate[playerid] = 0.0;
}
}
return;
}
hook OnDeath(playerid, killerid, reason) {
Iter_Clear(health_WoundIndex[playerid]);
return Y_HOOKS_CONTINUE_RETURN_0;
}