entWatch4: Tweak registering, and formatting.

This commit is contained in:
zaCade 2019-03-16 23:29:55 +01:00
parent 46beb23bc3
commit aa2f02b141

View File

@ -18,6 +18,11 @@
#include "CConfig.inc" #include "CConfig.inc"
#include "CItem.inc" #include "CItem.inc"
/* REGISTERTYPES */
#define REGISTERTYPE_WEAPON 1
#define REGISTERTYPE_BUTTON 2
#define REGISTERTYPE_TRIGGER 3
/* BOOLS */ /* BOOLS */
bool g_bLate; bool g_bLate;
bool g_bIntermission; bool g_bIntermission;
@ -216,72 +221,79 @@ public void OnEntitySpawned(int entity)
if (config.iWeaponID && config.iWeaponID == Entity_GetHammerId(entity)) if (config.iWeaponID && config.iWeaponID == Entity_GetHammerId(entity))
{ {
if (PerformRegisterItem(config, entity, 1)) bool bExisting;
if ((bExisting = RegisterExistingItem(entity, REGISTERTYPE_WEAPON)))
{
if (!bExisting)
{
CItem item = new CItem(config);
if (RegisterItemEntity(item, entity, REGISTERTYPE_WEAPON))
g_hArray_Items.Push(item);
}
return; return;
} }
}
else if (config.iButtonID && config.iButtonID == Entity_GetHammerId(entity)) else if (config.iButtonID && config.iButtonID == Entity_GetHammerId(entity))
{ {
if (PerformRegisterItem(config, entity, 2)) bool bExisting;
if ((bExisting = RegisterExistingItem(entity, REGISTERTYPE_BUTTON)))
{
if (!bExisting)
{
CItem item = new CItem(config);
if (RegisterItemEntity(item, entity, REGISTERTYPE_BUTTON))
g_hArray_Items.Push(item);
}
return; return;
} }
}
else if (config.iTriggerID && config.iTriggerID == Entity_GetHammerId(entity)) else if (config.iTriggerID && config.iTriggerID == Entity_GetHammerId(entity))
{ {
if (PerformRegisterItem(config, entity, 3)) bool bExisting;
if ((bExisting = RegisterExistingItem(entity, REGISTERTYPE_TRIGGER)))
{
if (!bExisting)
{
CItem item = new CItem(config);
if (RegisterItemEntity(item, entity, REGISTERTYPE_TRIGGER))
g_hArray_Items.Push(item);
}
return; return;
} }
} }
} }
}
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
// Purpose: // Purpose:
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
stock bool PerformRegisterItem(CConfig config, int entity, int type) stock bool RegisterExistingItem(int entity, int type)
{ {
bool bSuccessful; if (Entity_IsValid(entity) && g_hArray_Items.Length)
if (Entity_IsValid(entity))
{
if (g_hArray_Items.Length)
{ {
for (int index; index < g_hArray_Items.Length; index++) for (int index; index < g_hArray_Items.Length; index++)
{ {
CItem item = g_hArray_Items.Get(index); CItem item = g_hArray_Items.Get(index);
if (AttemptRegisterItem(item, entity, type)) if (RegisterItemEntity(item, entity, type))
{ return true;
g_hArray_Items.Set(index, item);
bSuccessful = true;
break;
}
} }
} }
if (!bSuccessful) return false;
{
CItem item = new CItem(config);
if (AttemptRegisterItem(item, entity, type))
{
g_hArray_Items.Push(item);
bSuccessful = true;
}
else
{
LogError("Attempted to register new item, but failed! This should never happen.");
delete item;
}
}
}
return bSuccessful;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
// Purpose: // Purpose:
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
stock bool AttemptRegisterItem(CItem item, int entity, int type) stock bool RegisterItemEntity(CItem item, int entity, int type)
{ {
if (Entity_IsValid(entity)) if (Entity_IsValid(entity))
{ {
@ -292,6 +304,7 @@ stock bool AttemptRegisterItem(CItem item, int entity, int type)
if (!item.bWeapon && (Entity_GetOwner(entity) == INVALID_ENT_REFERENCE)) if (!item.bWeapon && (Entity_GetOwner(entity) == INVALID_ENT_REFERENCE))
{ {
item.iWeapon = entity; item.iWeapon = entity;
return true; return true;
} }
} }
@ -302,6 +315,7 @@ stock bool AttemptRegisterItem(CItem item, int entity, int type)
SDKHook(entity, SDKHook_Use, OnButtonPress); SDKHook(entity, SDKHook_Use, OnButtonPress);
item.iButton = entity; item.iButton = entity;
return true; return true;
} }
} }
@ -314,12 +328,14 @@ stock bool AttemptRegisterItem(CItem item, int entity, int type)
SDKHook(entity, SDKHook_Touch, OnTriggerTouch); SDKHook(entity, SDKHook_Touch, OnTriggerTouch);
item.iTrigger = entity; item.iTrigger = entity;
return true; return true;
} }
} }
default: default:
{ {
LogError("Attempted to register item with invalid type: %d! This should never happen.", type); LogError("Attempted to register item with invalid type: %d! This should never happen.", type);
return false; return false;
} }
} }
@ -350,14 +366,12 @@ public void OnEntityDestroyed(int entity)
{ {
item.iButton = INVALID_ENT_REFERENCE; item.iButton = INVALID_ENT_REFERENCE;
g_hArray_Items.Set(index, item);
return; return;
} }
else if (item.bTrigger && item.iTrigger == entity) else if (item.bTrigger && item.iTrigger == entity)
{ {
item.iTrigger = INVALID_ENT_REFERENCE; item.iTrigger = INVALID_ENT_REFERENCE;
g_hArray_Items.Set(index, item);
return; return;
} }
} }
@ -396,8 +410,6 @@ public void OnClientDisconnect(int client)
Call_PushCell(client); Call_PushCell(client);
Call_PushCell(index); Call_PushCell(index);
Call_Finish(); Call_Finish();
g_hArray_Items.Set(index, item);
} }
} }
} }
@ -424,8 +436,6 @@ public void OnClientDeath(Event hEvent, const char[] sEvent, bool bDontBroadcast
Call_PushCell(client); Call_PushCell(client);
Call_PushCell(index); Call_PushCell(index);
Call_Finish(); Call_Finish();
g_hArray_Items.Set(index, item);
} }
} }
} }
@ -451,7 +461,6 @@ public Action OnWeaponPickup(int client, int weapon)
Call_PushCell(index); Call_PushCell(index);
Call_Finish(); Call_Finish();
g_hArray_Items.Set(index, item);
return; return;
} }
} }
@ -478,7 +487,6 @@ public Action OnWeaponDrop(int client, int weapon)
Call_PushCell(index); Call_PushCell(index);
Call_Finish(); Call_Finish();
g_hArray_Items.Set(index, item);
return; return;
} }
} }
@ -500,9 +508,8 @@ public Action OnButtonPress(int button, int client)
{ {
CItem item = g_hArray_Items.Get(index); CItem item = g_hArray_Items.Get(index);
CConfig config = item.dConfig; if ((item.bButton && item.iButton == button) &&
(item.bOwner && item.iOwner == client))
if (item.bButton && item.iButton == button && item.bOwner && item.iOwner == client)
{ {
Action aResult; Action aResult;
Call_StartForward(g_hFwd_OnClientItemCanActivate); Call_StartForward(g_hFwd_OnClientItemCanActivate);
@ -510,21 +517,23 @@ public Action OnButtonPress(int button, int client)
Call_PushCell(index); Call_PushCell(index);
Call_Finish(aResult); Call_Finish(aResult);
if ((aResult == Plugin_Continue) || (aResult == Plugin_Changed)) switch(aResult)
{ {
switch(config.iMode) case(Plugin_Continue, Plugin_Changed):
{
switch(item.dConfig.iMode)
{ {
case(1): case(1):
{ {
if (item.iTimeReady < RoundToCeil(GetEngineTime())) if (item.iTimeReady < RoundToCeil(GetEngineTime()))
{ {
item.iTimeReady = RoundToCeil(GetEngineTime()) + config.iCooldown; item.iTimeReady = RoundToCeil(GetEngineTime()) + item.dConfig.iCooldown;
} }
else return Plugin_Handled; else return Plugin_Handled;
} }
case(2): case(2):
{ {
if (item.iTimesUsed < config.iMaxUses) if (item.iTimesUsed < item.dConfig.iMaxUses)
{ {
item.iTimesUsed++; item.iTimesUsed++;
} }
@ -532,9 +541,9 @@ public Action OnButtonPress(int button, int client)
} }
case(3): case(3):
{ {
if (item.iTimeReady < RoundToCeil(GetEngineTime()) && item.iTimesUsed < config.iMaxUses) if (item.iTimeReady < RoundToCeil(GetEngineTime()) && item.iTimesUsed < item.dConfig.iMaxUses)
{ {
item.iTimeReady = RoundToCeil(GetEngineTime()) + config.iCooldown; item.iTimeReady = RoundToCeil(GetEngineTime()) + item.dConfig.iCooldown;
item.iTimesUsed++; item.iTimesUsed++;
} }
else return Plugin_Handled; else return Plugin_Handled;
@ -545,9 +554,9 @@ public Action OnButtonPress(int button, int client)
{ {
item.iTimesUsed++; item.iTimesUsed++;
if (item.iTimesUsed >= config.iMaxUses) if (item.iTimesUsed >= item.dConfig.iMaxUses)
{ {
item.iTimeReady = RoundToCeil(GetEngineTime()) + config.iCooldown; item.iTimeReady = RoundToCeil(GetEngineTime()) + item.dConfig.iCooldown;
item.iTimesUsed = 0; item.iTimesUsed = 0;
} }
} }
@ -556,17 +565,21 @@ public Action OnButtonPress(int button, int client)
} }
char sFilter[64]; char sFilter[64];
if (config.GetFilter(sFilter, sizeof(sFilter))) if (item.dConfig.GetFilter(sFilter, sizeof(sFilter)))
Entity_SetName(client, sFilter); Entity_SetName(client, sFilter);
Call_StartForward(g_hFwd_OnClientItemActivate); Call_StartForward(g_hFwd_OnClientItemActivate);
Call_PushCell(client); Call_PushCell(client);
Call_PushCell(index); Call_PushCell(index);
Call_Finish(); Call_Finish();
}
g_hArray_Items.Set(index, item); return Plugin_Continue;
return aResult; }
default:
{
return Plugin_Handled;
}
}
} }
} }
} }
@ -595,7 +608,6 @@ public Action OnTriggerTouch(int trigger, int client)
Call_PushCell(index); Call_PushCell(index);
Call_Finish(aResult); Call_Finish(aResult);
g_hArray_Items.Set(index, item);
return aResult; return aResult;
} }
} }
@ -625,7 +637,6 @@ public Action OnWeaponTouch(int client, int weapon)
Call_PushCell(index); Call_PushCell(index);
Call_Finish(aResult); Call_Finish(aResult);
g_hArray_Items.Set(index, item);
return aResult; return aResult;
} }
} }