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;
return; if ((bExisting = RegisterExistingItem(entity, REGISTERTYPE_TRIGGER)))
}
}
}
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock bool PerformRegisterItem(CConfig config, int entity, int type)
{
bool bSuccessful;
if (Entity_IsValid(entity))
{
if (g_hArray_Items.Length)
{
for (int index; index < g_hArray_Items.Length; index++)
{
CItem item = g_hArray_Items.Get(index);
if (AttemptRegisterItem(item, entity, type))
{ {
g_hArray_Items.Set(index, item); if (!bExisting)
bSuccessful = true; {
break; CItem item = new CItem(config);
if (RegisterItemEntity(item, entity, REGISTERTYPE_TRIGGER))
g_hArray_Items.Push(item);
}
return;
} }
} }
} }
if (!bSuccessful)
{
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 RegisterExistingItem(int entity, int type)
{
if (Entity_IsValid(entity) && g_hArray_Items.Length)
{
for (int index; index < g_hArray_Items.Length; index++)
{
CItem item = g_hArray_Items.Get(index);
if (RegisterItemEntity(item, entity, type))
return true;
}
}
return false;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
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,63 +517,69 @@ 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):
{ {
case(1): switch(item.dConfig.iMode)
{ {
if (item.iTimeReady < RoundToCeil(GetEngineTime())) case(1):
{ {
item.iTimeReady = RoundToCeil(GetEngineTime()) + config.iCooldown; if (item.iTimeReady < RoundToCeil(GetEngineTime()))
}
else return Plugin_Handled;
}
case(2):
{
if (item.iTimesUsed < config.iMaxUses)
{
item.iTimesUsed++;
}
else return Plugin_Handled;
}
case(3):
{
if (item.iTimeReady < RoundToCeil(GetEngineTime()) && item.iTimesUsed < config.iMaxUses)
{
item.iTimeReady = RoundToCeil(GetEngineTime()) + config.iCooldown;
item.iTimesUsed++;
}
else return Plugin_Handled;
}
case(4):
{
if (item.iTimeReady < RoundToCeil(GetEngineTime()))
{
item.iTimesUsed++;
if (item.iTimesUsed >= config.iMaxUses)
{ {
item.iTimeReady = RoundToCeil(GetEngineTime()) + config.iCooldown; item.iTimeReady = RoundToCeil(GetEngineTime()) + item.dConfig.iCooldown;
item.iTimesUsed = 0;
} }
else return Plugin_Handled;
}
case(2):
{
if (item.iTimesUsed < item.dConfig.iMaxUses)
{
item.iTimesUsed++;
}
else return Plugin_Handled;
}
case(3):
{
if (item.iTimeReady < RoundToCeil(GetEngineTime()) && item.iTimesUsed < item.dConfig.iMaxUses)
{
item.iTimeReady = RoundToCeil(GetEngineTime()) + item.dConfig.iCooldown;
item.iTimesUsed++;
}
else return Plugin_Handled;
}
case(4):
{
if (item.iTimeReady < RoundToCeil(GetEngineTime()))
{
item.iTimesUsed++;
if (item.iTimesUsed >= item.dConfig.iMaxUses)
{
item.iTimeReady = RoundToCeil(GetEngineTime()) + item.dConfig.iCooldown;
item.iTimesUsed = 0;
}
}
else return Plugin_Handled;
} }
else return Plugin_Handled;
} }
char sFilter[64];
if (item.dConfig.GetFilter(sFilter, sizeof(sFilter)))
Entity_SetName(client, sFilter);
Call_StartForward(g_hFwd_OnClientItemActivate);
Call_PushCell(client);
Call_PushCell(index);
Call_Finish();
return Plugin_Continue;
}
default:
{
return Plugin_Handled;
} }
char sFilter[64];
if (config.GetFilter(sFilter, sizeof(sFilter)))
Entity_SetName(client, sFilter);
Call_StartForward(g_hFwd_OnClientItemActivate);
Call_PushCell(client);
Call_PushCell(index);
Call_Finish();
} }
g_hArray_Items.Set(index, item);
return aResult;
} }
} }
} }
@ -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;
} }
} }