diff --git a/modules/engine/amxxapi.cpp b/modules/engine/amxxapi.cpp index 52f3ab4c..3202f063 100644 --- a/modules/engine/amxxapi.cpp +++ b/modules/engine/amxxapi.cpp @@ -164,7 +164,7 @@ qboolean Voice_SetClientListening(int iReceiver, int iSender, qboolean bListen) int AddToFullPack_Post(struct entity_state_s *state, int e, edict_t *ent, edict_t *host, int hostflags, int player, unsigned char *pSet) { - if( player && ent == host && plinfo[ENTINDEX(ent)].iViewType != CAMERA_NONE ) + if( player && ent && ent == host && plinfo[TypeConversion.edict_to_id(ent)].iViewType != CAMERA_NONE ) { state->rendermode = kRenderTransTexture; state->renderamt = 100; @@ -175,7 +175,7 @@ int AddToFullPack_Post(struct entity_state_s *state, int e, edict_t *ent, edict_ void ClientDisconnect(edict_t *pEntity) { - int id = ENTINDEX(pEntity); + auto id = TypeConversion.edict_to_id(pEntity); if (plinfo[id].iViewType != CAMERA_NONE) // Verify that they were originally in a modified view { @@ -194,7 +194,7 @@ void ClientDisconnect(edict_t *pEntity) BOOL ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char szRejectReason[128]) { - int id = ENTINDEX(pEntity); + auto id = TypeConversion.edict_to_id(pEntity); plinfo[id].iSpeakFlags = SPEAK_NORMAL; plinfo[id].iViewType = CAMERA_NONE; diff --git a/modules/engine/engine.cpp b/modules/engine/engine.cpp index 5219e9af..b7aae920 100644 --- a/modules/engine/engine.cpp +++ b/modules/engine/engine.cpp @@ -334,7 +334,7 @@ static cell AMX_NATIVE_CALL trace_line(AMX *amx, cell *params) if (FNullEnt(pHit)) return 0; - return ENTINDEX(pHit); + return TypeConversion.edict_to_id(pHit); } static cell AMX_NATIVE_CALL set_speak(AMX *amx, cell *params) { @@ -429,10 +429,10 @@ static cell AMX_NATIVE_CALL set_view(AMX *amx, cell *params) { { case CAMERA_NONE: SET_VIEW(pPlayer, pPlayer); - if(plinfo[ENTINDEX(pPlayer)].pViewEnt) { - REMOVE_ENTITY(plinfo[ENTINDEX(pPlayer)].pViewEnt); + if(plinfo[iIndex].pViewEnt) { + REMOVE_ENTITY(plinfo[iIndex].pViewEnt); } - if (plinfo[ENTINDEX(pPlayer)].iViewType != CAMERA_NONE) // Verify that they were originally in a modified view + if (plinfo[iIndex].iViewType != CAMERA_NONE) // Verify that they were originally in a modified view { g_CameraCount--; if (g_CameraCount < 0) @@ -441,20 +441,20 @@ static cell AMX_NATIVE_CALL set_view(AMX *amx, cell *params) { g_pFunctionTable->pfnAddToFullPack=NULL; } - plinfo[ENTINDEX(pPlayer)].iViewType = CAMERA_NONE; - plinfo[ENTINDEX(pPlayer)].pViewEnt = NULL; + plinfo[iIndex].iViewType = CAMERA_NONE; + plinfo[iIndex].pViewEnt = NULL; return 1; break; case CAMERA_3RDPERSON: - if(plinfo[ENTINDEX(pPlayer)].iViewType != CAMERA_NONE) { - plinfo[ENTINDEX(pPlayer)].iViewType = CAMERA_3RDPERSON; + if(plinfo[iIndex].iViewType != CAMERA_NONE) { + plinfo[iIndex].iViewType = CAMERA_3RDPERSON; return 1; } g_CameraCount++; g_pFunctionTable_Post->pfnAddToFullPack=AddToFullPack_Post; g_pFunctionTable_Post->pfnPlayerPostThink=PlayerPostThink_Post; - plinfo[ENTINDEX(pPlayer)].iViewType = CAMERA_3RDPERSON; + plinfo[iIndex].iViewType = CAMERA_3RDPERSON; pNewCamera = CREATE_NAMED_ENTITY(MAKE_STRING("info_target")); pNewCamera->v.classname = MAKE_STRING("VexdCam"); @@ -472,11 +472,11 @@ static cell AMX_NATIVE_CALL set_view(AMX *amx, cell *params) { SET_VIEW(pPlayer, pNewCamera); - plinfo[ENTINDEX(pPlayer)].pViewEnt = pNewCamera; + plinfo[iIndex].pViewEnt = pNewCamera; break; case CAMERA_UPLEFT: - if(plinfo[ENTINDEX(pPlayer)].iViewType != CAMERA_NONE) { - plinfo[ENTINDEX(pPlayer)].iViewType = CAMERA_UPLEFT; + if(plinfo[iIndex].iViewType != CAMERA_NONE) { + plinfo[iIndex].iViewType = CAMERA_UPLEFT; return 1; } @@ -484,7 +484,7 @@ static cell AMX_NATIVE_CALL set_view(AMX *amx, cell *params) { g_pFunctionTable_Post->pfnAddToFullPack=AddToFullPack_Post; g_pFunctionTable_Post->pfnPlayerPostThink=PlayerPostThink_Post; - plinfo[ENTINDEX(pPlayer)].iViewType = CAMERA_UPLEFT; + plinfo[iIndex].iViewType = CAMERA_UPLEFT; pNewCamera = CREATE_NAMED_ENTITY(MAKE_STRING("info_target")); pNewCamera->v.classname = MAKE_STRING("VexdCam"); @@ -502,11 +502,11 @@ static cell AMX_NATIVE_CALL set_view(AMX *amx, cell *params) { SET_VIEW(pPlayer, pNewCamera); - plinfo[ENTINDEX(pPlayer)].pViewEnt = pNewCamera; + plinfo[iIndex].pViewEnt = pNewCamera; break; case CAMERA_TOPDOWN: - if(plinfo[ENTINDEX(pPlayer)].iViewType != CAMERA_NONE) { - plinfo[ENTINDEX(pPlayer)].iViewType = CAMERA_TOPDOWN; + if(plinfo[iIndex].iViewType != CAMERA_NONE) { + plinfo[iIndex].iViewType = CAMERA_TOPDOWN; return 1; } @@ -514,7 +514,7 @@ static cell AMX_NATIVE_CALL set_view(AMX *amx, cell *params) { g_pFunctionTable_Post->pfnAddToFullPack=AddToFullPack_Post; g_pFunctionTable_Post->pfnPlayerPostThink=PlayerPostThink_Post; - plinfo[ENTINDEX(pPlayer)].iViewType = CAMERA_TOPDOWN; + plinfo[iIndex].iViewType = CAMERA_TOPDOWN; pNewCamera = CREATE_NAMED_ENTITY(MAKE_STRING("info_target")); pNewCamera->v.classname = MAKE_STRING("VexdCam"); @@ -532,7 +532,7 @@ static cell AMX_NATIVE_CALL set_view(AMX *amx, cell *params) { SET_VIEW(pPlayer, pNewCamera); - plinfo[ENTINDEX(pPlayer)].pViewEnt = pNewCamera; + plinfo[iIndex].pViewEnt = pNewCamera; break; default: break; @@ -915,7 +915,7 @@ static cell AMX_NATIVE_CALL traceresult(AMX *amx, cell *params) return g_tr.iHitgroup; case TR_Hit: if (!FNullEnt(g_tr.pHit)) - return ENTINDEX(g_tr.pHit); + return TypeConversion.edict_to_id(g_tr.pHit); else return -1; case TR_Fraction: diff --git a/modules/engine/entity.cpp b/modules/engine/entity.cpp index 545c3896..1d056945 100644 --- a/modules/engine/entity.cpp +++ b/modules/engine/entity.cpp @@ -112,9 +112,11 @@ static cell AMX_NATIVE_CALL create_entity(AMX *amx, cell *params) edict_t *pEnt = CREATE_NAMED_ENTITY(iszClass); if (FNullEnt(pEnt)) + { return 0; + } - return ENTINDEX(pEnt); + return TypeConversion.edict_to_id(pEnt); } static cell AMX_NATIVE_CALL remove_entity(AMX *amx, cell *params) @@ -125,11 +127,13 @@ static cell AMX_NATIVE_CALL remove_entity(AMX *amx, cell *params) MF_LogError(amx, AMX_ERR_NATIVE, "Entity %d can not be removed", id); return 0; } - + edict_t *pEnt = TypeConversion.id_to_edict(id); if (FNullEnt(pEnt)) + { return 0; + } REMOVE_ENTITY(pEnt); @@ -1118,7 +1122,7 @@ static cell AMX_NATIVE_CALL entity_get_edict2(AMX *amx, cell *params) if (FNullEnt(pRet)) return -1; - return ENTINDEX(pRet); + return TypeConversion.edict_to_id(pRet); } static cell AMX_NATIVE_CALL entity_get_edict(AMX *amx, cell *params) @@ -1348,12 +1352,12 @@ static cell AMX_NATIVE_CALL find_ent_in_sphere(AMX *amx, cell *params) }; REAL radius = amx_ctof(params[3]); - int returnEnt = ENTINDEX(FIND_ENTITY_IN_SPHERE(pEnt, origin, radius)); + if (!FNullEnt(pEnt = FIND_ENTITY_IN_SPHERE(pEnt, origin, radius))) + { + return TypeConversion.cbase_to_id(pEnt); + } - if (FNullEnt(returnEnt)) - return 0; - - return returnEnt; + return 0; } static cell AMX_NATIVE_CALL find_ent_by_class(AMX *amx, cell *params) /* 3 param */ @@ -1363,12 +1367,12 @@ static cell AMX_NATIVE_CALL find_ent_by_class(AMX *amx, cell *params) /* 3 param int len; char* sValue = MF_GetAmxString(amx, params[2], 0, &len); - pEnt = FIND_ENTITY_BY_STRING(pEnt, "classname", sValue); + if (!FNullEnt(pEnt = FIND_ENTITY_BY_CLASSNAME(pEnt, sValue))) + { + return TypeConversion.edict_to_id(pEnt); + } - if (FNullEnt(pEnt)) - return 0; - - return ENTINDEX(pEnt); + return 0; } static cell AMX_NATIVE_CALL find_sphere_class(AMX *amx, cell *params) // find_sphere_class(aroundent, _lookforclassname[], Float:radius, entlist[], maxents, Float:origin[3] = {0.0, 0.0, 0.0}); // 6 params @@ -1405,7 +1409,7 @@ static cell AMX_NATIVE_CALL find_sphere_class(AMX *amx, cell *params) // find_sp else { if (strcmp(STRING(pSearchEnt->v.classname), classToFind) == 0) { // Add to entlist (params[4]) - entList[entsFound++] = ENTINDEX(pSearchEnt); // raise entsFound + entList[entsFound++] = TypeConversion.edict_to_id(pSearchEnt); // raise entsFound } } } @@ -1419,20 +1423,14 @@ static cell AMX_NATIVE_CALL find_ent_by_target(AMX *amx, cell *params) int iLength; char *szValue = MF_GetAmxString(amx, params[2], 0, &iLength); - edict_t *pStart; + auto pStart = TypeConversion.id_to_edict(iStart); - if (iStart == -1) { - pStart = NULL; - } else { - if (!is_ent_valid(iStart)) - pStart = NULL; - else - pStart = TypeConversion.id_to_edict(iStart); + if (!FNullEnt(pStart = FIND_ENTITY_BY_TARGET(pStart, szValue))) + { + return TypeConversion.edict_to_id(pStart); } - int iReturnEnt = ENTINDEX(FIND_ENTITY_BY_TARGET(pStart, szValue)); - - return iReturnEnt; + return 0; } static cell AMX_NATIVE_CALL find_ent_by_model(AMX *amx, cell *params) { @@ -1441,29 +1439,14 @@ static cell AMX_NATIVE_CALL find_ent_by_model(AMX *amx, cell *params) { char *szClass = MF_GetAmxString(amx, params[2], 0, &iLength); char *szModel = MF_GetAmxString(amx, params[3], 1, &iLength2); - edict_t *pStart; + auto pEdict = TypeConversion.id_to_edict(iStart); - if (iStart == -1) + while (!FNullEnt(pEdict = FIND_ENTITY_BY_CLASSNAME(pEdict, szClass))) { - pStart = NULL; - } else { - if (!is_ent_valid(iStart)) - pStart = NULL; - else - pStart = TypeConversion.id_to_edict(iStart); - } - - edict_t *pEdict = FIND_ENTITY_BY_STRING(pStart, "classname", szClass); - - const char *check; - - while (pEdict && !FNullEnt(pEdict)) - { - check = STRING(pEdict->v.model); - if (!check || strcmp(check, szModel)) - pEdict = FIND_ENTITY_BY_STRING(pEdict, "classname", szClass); - else - return ENTINDEX(pEdict); + if (pEdict->v.model > 0 && !strcmp(STRING(pEdict->v.model), szModel)) + { + return TypeConversion.edict_to_id(pEdict); + } } return 0; @@ -1474,20 +1457,14 @@ static cell AMX_NATIVE_CALL find_ent_by_tname(AMX *amx, cell *params) { int iLength; char *szValue = MF_GetAmxString(amx, params[2], 0, &iLength); - edict_t *pStart; + auto pStart = TypeConversion.id_to_edict(iStart); - if (iStart == -1) { - pStart = NULL; - } else { - if (!is_ent_valid(iStart)) - pStart = NULL; - else - pStart = TypeConversion.id_to_edict(iStart); + if (!FNullEnt(pStart = FIND_ENTITY_BY_TARGETNAME(pStart, szValue))) + { + return TypeConversion.edict_to_id(pStart); } - int iReturnEnt = ENTINDEX(FIND_ENTITY_BY_TARGETNAME(pStart, szValue)); - - return iReturnEnt; + return 0; } static cell AMX_NATIVE_CALL find_ent_by_owner(AMX *amx, cell *params) // native find_ent_by_owner(start_from_ent, classname[], owner_index); = 3 params @@ -1512,15 +1489,14 @@ static cell AMX_NATIVE_CALL find_ent_by_owner(AMX *amx, cell *params) // native int len; char* classname = MF_GetAmxString(amx, params[2], 0, &len); - while (true) { - pEnt = FIND_ENTITY_BY_STRING(pEnt, sCategory, classname); - if (FNullEnt(pEnt)) // break and return 0 if bad - break; - else if (pEnt->v.owner == entOwner) // compare pointers - return ENTINDEX(pEnt); + while (!FNullEnt(pEnt = FIND_ENTITY_BY_STRING(pEnt, sCategory, classname))) + { + if (pEnt->v.owner == entOwner) + { + return TypeConversion.edict_to_id(pEnt); + } } - // If it comes here, the while loop ended because an ent failed (FNullEnt() == true) return 0; } @@ -1544,7 +1520,7 @@ static cell AMX_NATIVE_CALL get_grenade_id(AMX *amx, cell *params) /* 4 param * if (params[3]>0) { szModel = (char*)STRING(pentFind->v.model); MF_SetAmxString(amx, params[2], szModel, params[3]); - return ENTINDEX(pentFind); + return TypeConversion.edict_to_id(pentFind); } } pentFind = FIND_ENTITY_BY_CLASSNAME( pentFind, "grenade" ); diff --git a/modules/engine/forwards.cpp b/modules/engine/forwards.cpp index 5c1f3c64..56ef5ab1 100644 --- a/modules/engine/forwards.cpp +++ b/modules/engine/forwards.cpp @@ -57,12 +57,14 @@ int Spawn(edict_t *pEntity) PRECACHE_MODEL("models/rpgrocket.mdl"); g_precachedStuff = true; } - if (SpawnForward != -1) { - int retVal = 0; - int id = ENTINDEX(pEntity); - retVal = MF_ExecuteForward(SpawnForward, (cell)id); - if (retVal) + if (SpawnForward != -1) + { + int id = TypeConversion.edict_to_id(pEntity); + + if (MF_ExecuteForward(SpawnForward, (cell)id)) + { RETURN_META_VALUE(MRES_SUPERCEDE, -1); + } } RETURN_META_VALUE(MRES_IGNORED, 0); } @@ -71,6 +73,7 @@ void PlaybackEvent(int flags, const edict_t *pInvoker, unsigned short eventindex { if (PlaybackForward != -1) { edict_t *e = (edict_t *)pInvoker; + int invoker = 0; int retVal = 0; static cell cOrigin[3]; static cell cAngles[3]; @@ -84,7 +87,9 @@ void PlaybackEvent(int flags, const edict_t *pInvoker, unsigned short eventindex cAngles[2] = amx_ftoc(vAngles.z); cell CellOrigin = MF_PrepareCellArray(cOrigin, 3); cell CellAngles = MF_PrepareCellArray(cAngles, 3); - retVal = MF_ExecuteForward(PlaybackForward, (cell)flags, (cell)ENTINDEX(e), (cell)eventindex, delay, CellOrigin, CellAngles, fparam1, fparam2, (cell)iparam1, (cell)iparam2, (cell)bparam1, (cell)bparam2); + if (!FNullEnt(e)) + invoker = TypeConversion.edict_to_id(e); + retVal = MF_ExecuteForward(PlaybackForward, (cell)flags, (cell)invoker, (cell)eventindex, delay, CellOrigin, CellAngles, fparam1, fparam2, (cell)iparam1, (cell)iparam2, (cell)bparam1, (cell)bparam2); if (retVal) RETURN_META(MRES_SUPERCEDE); } @@ -97,9 +102,8 @@ void KeyValue(edict_t *pEntity, KeyValueData *pkvd) int retVal = 0; g_inKeyValue=true; g_pkvd=pkvd; - int index = ENTINDEX(pEntity); if (DispatchKeyForward != -1) { - retVal = MF_ExecuteForward(DispatchKeyForward, (cell)index); + retVal = MF_ExecuteForward(DispatchKeyForward, (cell)TypeConversion.edict_to_id(pEntity)); g_inKeyValue=false; if (retVal) RETURN_META(MRES_SUPERCEDE); @@ -125,11 +129,14 @@ void CmdStart(const edict_t *player, const struct usercmd_s *_cmd, unsigned int edict_t *pEntity = (edict_t *)player; g_cmd = (struct usercmd_s *)_cmd; int origImpulse = g_cmd->impulse; // incase a plugin alters it + + auto index = TypeConversion.edict_to_id(pEntity); + for (i=0; iCheck == g_cmd->impulse) { - retVal = MF_ExecuteForward(Impulses[i]->Forward, (cell)ENTINDEX(pEntity), (cell)origImpulse); + retVal = MF_ExecuteForward(Impulses[i]->Forward, (cell)index, (cell)origImpulse); // don't return SUPERCEDE in any way here, // we don't want to break client_impulse forward and access to cmd with [g/s]et_usercmd @@ -141,7 +148,7 @@ void CmdStart(const edict_t *player, const struct usercmd_s *_cmd, unsigned int // client_impulse if (ClientImpulseForward != -1 && origImpulse != 0) { - retVal = MF_ExecuteForward(ClientImpulseForward, (cell)ENTINDEX(pEntity), (cell)origImpulse); + retVal = MF_ExecuteForward(ClientImpulseForward, (cell)index, (cell)origImpulse); if (retVal) g_cmd->impulse = 0; @@ -151,7 +158,7 @@ void CmdStart(const edict_t *player, const struct usercmd_s *_cmd, unsigned int if (CmdStartForward != -1) { incmd = true; - retVal = MF_ExecuteForward(CmdStartForward, (cell)ENTINDEX(pEntity)); + retVal = MF_ExecuteForward(CmdStartForward, (cell)index); incmd = false; if (retVal) @@ -163,12 +170,12 @@ void CmdStart(const edict_t *player, const struct usercmd_s *_cmd, unsigned int void ClientKill(edict_t *pEntity) { - int retVal = 0; - - if (ClientKillForward != -1) { - retVal = MF_ExecuteForward(ClientKillForward, (cell)ENTINDEX(pEntity)); - if (retVal) + if (ClientKillForward != -1) + { + if (MF_ExecuteForward(ClientKillForward, (cell)TypeConversion.edict_to_id(pEntity))) + { RETURN_META(MRES_SUPERCEDE); + } } RETURN_META(MRES_IGNORED); @@ -176,21 +183,27 @@ void ClientKill(edict_t *pEntity) void PlayerPreThink(edict_t *pEntity) { - MF_ExecuteForward(PlayerPreThinkForward, (cell)ENTINDEX(pEntity)); + if (PlayerPreThinkForward != -1) + { + MF_ExecuteForward(PlayerPreThinkForward, (cell)TypeConversion.edict_to_id(pEntity)); + } + RETURN_META(MRES_IGNORED); } void PlayerPostThink_Post(edict_t *pEntity) { - if(plinfo[ENTINDEX(pEntity)].pViewEnt) { - edict_t *pCamEnt = plinfo[ENTINDEX(pEntity)].pViewEnt; + auto index = TypeConversion.edict_to_id(pEntity); + + if(plinfo[index].pViewEnt) { + edict_t *pCamEnt = plinfo[index].pViewEnt; MAKE_VECTORS(pEntity->v.v_angle + pEntity->v.punchangle); Vector vecSrc = pEntity->v.origin + pEntity->v.view_ofs; Vector vecAiming = gpGlobals->v_forward; TraceResult tr; - switch(plinfo[ENTINDEX(pEntity)].iViewType) { + switch(plinfo[index].iViewType) { case CAMERA_3RDPERSON: TRACE_LINE(vecSrc, vecSrc - (vecAiming * 128), ignore_monsters, ENT(pEntity), &tr); SET_VIEW(pEntity, pCamEnt); @@ -212,16 +225,16 @@ void PlayerPostThink_Post(edict_t *pEntity) break; default: SET_VIEW(pEntity, pEntity); - REMOVE_ENTITY(plinfo[ENTINDEX(pEntity)].pViewEnt); - plinfo[ENTINDEX(pEntity)].iViewType = CAMERA_NONE; - plinfo[ENTINDEX(pEntity)].pViewEnt = NULL; + REMOVE_ENTITY(plinfo[index].pViewEnt); + plinfo[index].iViewType = CAMERA_NONE; + plinfo[index].pViewEnt = NULL; break; } } if (PlayerPostThinkForward != -1) { - if (MF_ExecuteForward(PlayerPostThinkForward, (cell)ENTINDEX(pEntity))) + if (MF_ExecuteForward(PlayerPostThinkForward, (cell)index)) RETURN_META(MRES_SUPERCEDE); } @@ -234,8 +247,8 @@ void pfnTouch(edict_t *pToucher, edict_t *pTouched) int retVal = 0; const char *ptrClass = STRING(pToucher->v.classname); const char *ptdClass = STRING(pTouched->v.classname); - int ptrIndex = ENTINDEX(pToucher); - int ptdIndex = ENTINDEX(pTouched); + int ptrIndex = TypeConversion.edict_to_id(pToucher); + int ptdIndex = TypeConversion.edict_to_id(pTouched); META_RES res=MRES_IGNORED; for (i=0; iClass.compare(cls)==0) { - retVal=MF_ExecuteForward(Thinks[i]->Forward, (cell)ENTINDEX(pent)); + retVal=MF_ExecuteForward(Thinks[i]->Forward, (cell)TypeConversion.edict_to_id(pent)); if (retVal & 2/*PLUGIN_HANDLED_MAIN*/) RETURN_META(MRES_SUPERCEDE); else if (retVal) res=MRES_SUPERCEDE; } } - retVal=MF_ExecuteForward(pfnThinkForward, (cell)ENTINDEX(pent)); + retVal=MF_ExecuteForward(pfnThinkForward, (cell)TypeConversion.edict_to_id(pent)); if (retVal) res=MRES_SUPERCEDE; diff --git a/modules/engine/globals.cpp b/modules/engine/globals.cpp index 4b859c12..d914a029 100644 --- a/modules/engine/globals.cpp +++ b/modules/engine/globals.cpp @@ -170,9 +170,10 @@ static cell AMX_NATIVE_CALL get_global_edict2(AMX *amx, cell *params) return -1; } - // Will crash if ENTINDEX() is called on bad pointer? - if(!FNullEnt(pReturnEntity)) - return ENTINDEX(pReturnEntity); + if (!FNullEnt(pReturnEntity)) + { + return TypeConversion.edict_to_id(pReturnEntity); + } return -1; }