Skip to content

Commit

Permalink
Release 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
dronelektron committed Apr 28, 2022
2 parents b952ee8 + 0cd72d0 commit 98fa376
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 203 deletions.
224 changes: 25 additions & 199 deletions scripting/damage-info.sp
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,30 @@
#pragma semicolon 1
#pragma newdecls required

#define HIT_GROUP_RIGHT_LEG 7
#define VECTOR_SIZE 3
#define METERS_PER_UNIT (2.54 / 100)
#define COOKIE_VALUE_MAX_SIZE 4
#define ITEM_INFO_MAX_SIZE 32
#define TEXT_BUFFER_MAX_SIZE (256 * 4)
#define NULL_CHARACTER '\0'

#define SHOW_DAMAGE_IN_CHAT_DEFAULT_VALUE true
#define SHOW_DAMAGE_ON_SCREEN_DEFAULT_VALUE true

#define SHOW_DAMAGE_ENABLED "1"
#define SHOW_DAMAGE_DISABLED "0"

#define MENU_ITEM_IN_CHAT "in_chat"
#define MENU_ITEM_ON_SCREEN "on_screen"
#include "di/math"
#include "di/menu"
#include "di/preferences"
#include "di/use-case"

#include "modules/console-variable.sp"
#include "modules/math.sp"
#include "modules/menu.sp"
#include "modules/message.sp"
#include "modules/preferences.sp"
#include "modules/use-case.sp"

public Plugin myinfo = {
name = "Damage info",
author = "Dron-elektron",
description = "Shows damage information in chat and on screen",
version = "1.0.2",
version = "1.0.3",
url = ""
}

char g_hitGroups[][] = {
"Body",
"Head",
"Chest",
"Stomach",
"Left arm",
"Right arm",
"Left leg",
"Right leg"
};

ConVar g_pluginEnabled = null;

Handle g_cookieShowDamageInChat = null;
Handle g_cookieShowDamageOnScreen = null;

bool g_showDamageInChat[MAXPLAYERS + 1];
bool g_showDamageOnScreen[MAXPLAYERS + 1];

int g_lastDamage[MAXPLAYERS + 1];
int g_lastHitGroup[MAXPLAYERS + 1];

public void OnPluginStart() {
g_pluginEnabled = CreateConVar("sm_damageinfo_show", "1", "Enable (1) or disable (0) damage information");
g_cookieShowDamageInChat = RegClientCookie("damageinfo_show_in_chat", "Show damage info in chat", CookieAccess_Private);
g_cookieShowDamageOnScreen = RegClientCookie("damageinfo_show_on_screen", "Show damage info on screen", CookieAccess_Private);

SetCookieMenuItem(MenuHandler_ShowDamage, 0, "Show damage info");
Variable_Create();
Preferences_Create();
Menu_AddToPreferences();
CookiesLateLoad();
HookEvent("player_hurt", Event_PlayerHurt);
HookEvent("player_death", Event_PlayerDeath);
Expand All @@ -67,127 +38,22 @@ public void OnPluginStart() {
}

public void OnClientConnected(int client) {
g_showDamageInChat[client] = SHOW_DAMAGE_IN_CHAT_DEFAULT_VALUE;
g_showDamageOnScreen[client] = SHOW_DAMAGE_ON_SCREEN_DEFAULT_VALUE;
Preferences_Reset(client);
}

public void OnClientCookiesCached(int client) {
char cookieValue[COOKIE_VALUE_MAX_SIZE];

GetClientCookie(client, g_cookieShowDamageInChat, cookieValue, sizeof(cookieValue));

if (cookieValue[0] != NULL_CHARACTER) {
g_showDamageInChat[client] = view_as<bool>(StringToInt(cookieValue));
} else {
g_showDamageInChat[client] = SHOW_DAMAGE_IN_CHAT_DEFAULT_VALUE;
}

GetClientCookie(client, g_cookieShowDamageOnScreen, cookieValue, sizeof(cookieValue));

if (cookieValue[0] != NULL_CHARACTER) {
g_showDamageOnScreen[client] = view_as<bool>(StringToInt(cookieValue));
} else {
g_showDamageOnScreen[client] = SHOW_DAMAGE_ON_SCREEN_DEFAULT_VALUE;
}
}

void CookiesLateLoad() {
for (int i = 1; i <= MaxClients; i++) {
if (AreClientCookiesCached(i)) {
OnClientCookiesCached(i);
}
}
}

public void MenuHandler_ShowDamage(int client, CookieMenuAction action, any info, char[] buffer, int maxlen) {
if (action == CookieMenuAction_SelectOption) {
CreateSettingsMenu(client);
} else {
Format(buffer, maxlen, "%T", "Show damage", client);
}
}

public void CreateSettingsMenu(int client) {
Menu menu = new Menu(MenuHandler_Settings);

menu.SetTitle("%T", "Show damage", client);

AddBoolMenuItem(menu, MENU_ITEM_IN_CHAT, "Show damage in chat", client, g_showDamageInChat[client]);
AddBoolMenuItem(menu, MENU_ITEM_ON_SCREEN, "Show damage on screen", client, g_showDamageOnScreen[client]);

menu.ExitBackButton = true;
menu.Display(client, MENU_TIME_FOREVER);
}

public int MenuHandler_Settings(Menu menu, MenuAction action, int param1, int param2) {
if (action == MenuAction_Select) {
char info[ITEM_INFO_MAX_SIZE];

menu.GetItem(param2, info, sizeof(info));

if (StrEqual(info, MENU_ITEM_IN_CHAT)) {
SetShowDamageInChat(param1, !g_showDamageInChat[param1]);
} else if (StrEqual(info, MENU_ITEM_ON_SCREEN)) {
SetShowDamageOnScreen(param1, !g_showDamageOnScreen[param1]);
}

CreateSettingsMenu(param1);
} else if (action == MenuAction_Cancel) {
if (param2 == MenuCancel_ExitBack) {
ShowCookieMenu(param1);
}
} else if (action == MenuAction_End) {
delete menu;
}

return 0;
}

void AddBoolMenuItem(Menu menu, char[] info, char[] phrase, int client, bool enabled) {
char buffer[TEXT_BUFFER_MAX_SIZE];

Format(buffer, sizeof(buffer), "%T", phrase, client, enabled ? "Enabled" : "Disabled", client);

menu.AddItem(info, buffer);
}

void SetShowDamageInChat(int client, bool show) {
SetClientCookie(client, g_cookieShowDamageInChat, show ? SHOW_DAMAGE_ENABLED : SHOW_DAMAGE_DISABLED);

g_showDamageInChat[client] = show;
}

void SetShowDamageOnScreen(int client, bool show) {
SetClientCookie(client, g_cookieShowDamageOnScreen, show ? SHOW_DAMAGE_ENABLED : SHOW_DAMAGE_DISABLED);

g_showDamageOnScreen[client] = show;
Preferences_Refresh(client);
}

public void Event_PlayerHurt(Event event, const char[] name, bool dontBroadcast) {
if (!IsPluginEnabled()) {
return;
}

int hitGroup = event.GetInt("hitgroup");

if (hitGroup > HIT_GROUP_RIGHT_LEG) {
return;
}

int victimId = event.GetInt("userid");
int victim = GetClientOfUserId(victimId);
int attackerId = event.GetInt("attacker");
int attacker = GetClientOfUserId(attackerId);
int hitGroup = event.GetInt("hitgroup");
int damage = event.GetInt("damage");

g_lastDamage[victim] = damage;
g_lastHitGroup[victim] = hitGroup;

if (victim == 0 || attacker == 0 || damage == 0) {
return;
}

ShowDamageInfo(victim, attacker);
UseCase_PlayerHurt(victim, attacker, hitGroup, damage);
}

public void Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast) {
Expand All @@ -196,53 +62,13 @@ public void Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast
int attackerId = event.GetInt("attacker");
int attacker = GetClientOfUserId(attackerId);

if (victim == 0 || attacker == 0 || g_lastDamage[victim] > 0) {
return;
}

ShowDamageInfo(victim, attacker);
UseCase_PlayerDeath(victim, attacker);
}

void ShowDamageInfo(int victim, int attacker) {
if (victim == attacker) {
return;
}

float victimPos[VECTOR_SIZE];
float attackerPos[VECTOR_SIZE];

GetClientAbsOrigin(victim, victimPos);
GetClientAbsOrigin(attacker, attackerPos);

float distance = GetVectorDistance(victimPos, attackerPos, false) * METERS_PER_UNIT;
int damage = max(g_lastDamage[victim], 1);
int hitGroup = g_lastHitGroup[victim];

if (g_showDamageInChat[victim]) {
PrintDamageInfoInChat(victim, attacker, "Attacker", g_hitGroups[hitGroup], damage, distance);
}

if (g_showDamageInChat[attacker]) {
PrintDamageInfoInChat(attacker, victim, "Target", g_hitGroups[hitGroup], damage, distance);
}

if (g_showDamageOnScreen[attacker]) {
PrintDamageInfoOnScreen(attacker, damage);
void CookiesLateLoad() {
for (int i = 1; i <= MaxClients; i++) {
if (AreClientCookiesCached(i)) {
OnClientCookiesCached(i);
}
}
}

int max(int a, int b) {
return a > b ? a : b;
}

void PrintDamageInfoInChat(int victim, int attacker, char[] prefix, char[] hitGroup, int damage, float distance) {
CPrintToChat(victim, "%t", "Damage info in chat", prefix, attacker, hitGroup, damage, distance);
}

void PrintDamageInfoOnScreen(int attacker, int damage) {
PrintCenterText(attacker, "%t", "Damage info on screen", damage);
}

bool IsPluginEnabled() {
return g_pluginEnabled.IntValue == 1;
}
8 changes: 8 additions & 0 deletions scripting/include/di/math.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if defined _di_math_included
#endinput
#endif
#define _di_math_included

#define VECTOR_SIZE 3
#define METERS_PER_UNIT (2.54 / 100)
#define SQUARED_NO false
11 changes: 11 additions & 0 deletions scripting/include/di/menu.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if defined _di_menu_included
#endinput
#endif
#define _di_menu_included

#define ITEM_INFO_MAX_SIZE 32
#define TEXT_BUFFER_MAX_SIZE (256 * 4)

#define SHOW_DAMAGE "Show damage"
#define ITEM_SHOW_DAMAGE_IN_CHAT "Show damage in chat"
#define ITEM_SHOW_DAMAGE_ON_SCREEN "Show damage on screen"
13 changes: 13 additions & 0 deletions scripting/include/di/preferences.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#if defined _di_preferences_included
#endinput
#endif
#define _di_preferences_included

#define COOKIE_VALUE_MAX_SIZE 4
#define NULL_CHARACTER '\0'

#define SHOW_DAMAGE_ENABLED "1"
#define SHOW_DAMAGE_DISABLED "0"

#define SHOW_DAMAGE_IN_CHAT_DEFAULT_VALUE true
#define SHOW_DAMAGE_ON_SCREEN_DEFAULT_VALUE true
6 changes: 6 additions & 0 deletions scripting/include/di/use-case.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if defined _di_use_case_included
#endinput
#endif
#define _di_use_case_included

#define HIT_GROUP_RIGHT_LEG 7
9 changes: 9 additions & 0 deletions scripting/modules/console-variable.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
static ConVar g_pluginEnabled = null;

void Variable_Create() {
g_pluginEnabled = CreateConVar("sm_damageinfo_show", "1", "Enable (1) or disable (0) damage information");
}

bool Variable_IsPluginEnabled() {
return g_pluginEnabled.IntValue == 1;
}
13 changes: 13 additions & 0 deletions scripting/modules/math.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
float Math_CalculateDistance(int victim, int attacker) {
float victimPos[VECTOR_SIZE];
float attackerPos[VECTOR_SIZE];

GetClientAbsOrigin(victim, victimPos);
GetClientAbsOrigin(attacker, attackerPos);

return GetVectorDistance(victimPos, attackerPos, SQUARED_NO) * METERS_PER_UNIT;
}

int Math_Max(int a, int b) {
return a > b ? a : b;
}
55 changes: 55 additions & 0 deletions scripting/modules/menu.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
void Menu_AddToPreferences() {
SetCookieMenuItem(MenuHandler_ShowDamage, 0, SHOW_DAMAGE);
}

public void MenuHandler_ShowDamage(int client, CookieMenuAction action, any info, char[] buffer, int maxlen) {
if (action == CookieMenuAction_SelectOption) {
Menu_Settings(client);
} else {
Format(buffer, maxlen, "%T", SHOW_DAMAGE, client);
}
}

public void Menu_Settings(int client) {
Menu menu = new Menu(MenuHandler_Settings);

menu.SetTitle("%T", SHOW_DAMAGE, client);

Menu_AddBoolItem(menu, ITEM_SHOW_DAMAGE_IN_CHAT, client, Preferences_IsShowDamageInChat(client));
Menu_AddBoolItem(menu, ITEM_SHOW_DAMAGE_ON_SCREEN, client, Preferences_IsShowDamageOnScreen(client));

menu.ExitBackButton = true;
menu.Display(client, MENU_TIME_FOREVER);
}

public int MenuHandler_Settings(Menu menu, MenuAction action, int param1, int param2) {
if (action == MenuAction_Select) {
char info[ITEM_INFO_MAX_SIZE];

menu.GetItem(param2, info, sizeof(info));

if (StrEqual(info, ITEM_SHOW_DAMAGE_IN_CHAT)) {
Preferences_ToggleShowDamageInChat(param1);
} else if (StrEqual(info, ITEM_SHOW_DAMAGE_ON_SCREEN)) {
Preferences_ToggleShowDamageOnScreen(param1);
}

Menu_Settings(param1);
} else if (action == MenuAction_Cancel) {
if (param2 == MenuCancel_ExitBack) {
ShowCookieMenu(param1);
}
} else if (action == MenuAction_End) {
delete menu;
}

return 0;
}

void Menu_AddBoolItem(Menu menu, char[] phrase, int client, bool enabled) {
char buffer[TEXT_BUFFER_MAX_SIZE];

Format(buffer, sizeof(buffer), "%T", phrase, client, enabled ? "Enabled" : "Disabled", client);

menu.AddItem(phrase, buffer);
}
Loading

0 comments on commit 98fa376

Please sign in to comment.