Skip to content

Commit

Permalink
+ actor_before_death script call back (set in bind_stalker). This wil…
Browse files Browse the repository at this point in the history
…l allow scripts to process actor condition and prevent actor's death or kill him if desired. IMPORTANT: if you wish to kill actor you need to call db.actor:kill(level:object_by_id(whoID), true) in actor_before_death callback, to ensure all objects are properly destroyed.

+ set_health_ex script method for game_object. This will directly set entity's health instead of going through health property which operates on delta
  • Loading branch information
avoitishin authored and Xottab-DUTY committed Aug 19, 2017
1 parent 664fda4 commit a91ff26
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 18 deletions.
9 changes: 9 additions & 0 deletions res/gamedata/scripts/bind_stalker.script
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function actor_binder:net_destroy()
self.object:set_callback(callback.item_to_belt, nil)
self.object:set_callback(callback.item_to_ruck, nil)
self.object:set_callback(callback.item_to_slot, nil)
self.object:set_callback(callback.actor_before_death, nil)
----| end:aVo |----------------------------------------------------------------

log("--------->"..tostring(_G.amb_vol))
Expand Down Expand Up @@ -141,6 +142,7 @@ function actor_binder:reinit()
self.object:set_callback(callback.item_to_ruck, self.item_to_ruck, self)
self.object:set_callback(callback.item_to_belt, self.item_to_belt, self)
self.object:set_callback(callback.item_to_slot, self.item_to_slot, self)
self.object:set_callback(callback.actor_before_death, self.on_actor_before_death, self)
----| end:aVo |----------------------------------------------------------------
end
---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -197,6 +199,13 @@ end
function actor_binder:item_to_slot(obj)
log(string.format("item_to_slot [%s]", obj:name()))
end
-- actor before death callback
-- IMPORTANT: if you wish to kill actor you need to call db.actor:kill(level:object_by_id(whoID), true) in actor_before_death callback, to ensure all objects are properly destroyed.
function actor_binder:on_actor_before_death(whoID)
-- log("[AVO] on_actor_before_death callback")
-- db.actor:set_health_ex(1)
db.actor:kill(level:object_by_id(whoID), true)
end
----| end:aVo |---------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
function actor_binder:take_item_from_box(box, item)
Expand Down
14 changes: 12 additions & 2 deletions src/xrGame/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,22 @@ void CEntity::net_Destroy()
set_ready_to_save();
}

void CEntity::KillEntity(u16 whoID)
void CEntity::KillEntity(u16 whoID, bool bypass_actor_check)
{
//AVO: allow scripts to process actor condition and prevent actor's death or kill him if desired.
//IMPORTANT: if you wish to kill actor you need to call db.actor:kill(level:object_by_id(whoID), true) in actor_before_death callback, to ensure all objects are properly destroyed
// this will bypass below if block and go to normal KillEntity routine.
if (IsGameTypeSingle() && this->ID() == Actor()->ID() && bypass_actor_check != true)
{
Actor()->callback(GameObject::eActorBeforeDeath)(whoID);
return;
}
//-AVO

if (this->ID() == Actor()->ID())
{
Actor()->detach_Vehicle();
Actor()->use_MountedWeapon(NULL);
Actor()->use_MountedWeapon(nullptr);
}
if (whoID != ID())
{
Expand Down
4 changes: 2 additions & 2 deletions src/xrGame/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class CEntity : public CPhysicsShellHolder, public CDamageManager
virtual void HitImpulse(float P, Fvector& vWorldDir, Fvector& vLocalDir) = 0;

virtual void Die(IGameObject* who);
// void KillEntity (IGameObject* who);
void KillEntity(u16 whoID);
//void KillEntity (IGameObject* who);
void KillEntity(u16 whoID, bool bypass_actor_check = false);

// Events
virtual void OnEvent(NET_Packet& P, u16 type);
Expand Down
10 changes: 6 additions & 4 deletions src/xrGame/game_object_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ enum ECallbackType
eInvBoxItemTake,
eWeaponNoAmmoAvailable,

/* avo: custom callbacks */
// input
//AVO: custom callbacks
// Input
eKeyPress,
eKeyRelease,
eKeyHold,
eMouseMove,
eMouseWheel,
// inventory
// Inventory
eItemToBelt,
eItemToSlot,
eItemToRuck,
/* avo: end */
// Actor
eActorBeforeDeath,
//-AVO

eDummy = u32(-1),
};
Expand Down
5 changes: 3 additions & 2 deletions src/xrGame/script_game_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class CScriptGameObject
_DECLARE_FUNCTION10(Squad, int);
_DECLARE_FUNCTION10(Group, int);

void Kill(CScriptGameObject* who);
void Kill(CScriptGameObject* who, bool bypass_actor_check = false /*AVO: added for actor before death callback*/);

// CEntityAlive
_DECLARE_FUNCTION10(GetFOV, float);
Expand Down Expand Up @@ -821,7 +821,8 @@ class CScriptGameObject
bool isWeaponGL() const;
bool isInventoryBox() const;
bool IsActorOutdoors() const;
//end AVO
void SetHealthEx(float hp);
//-AVO

doors::door* m_door;
};
Expand Down
10 changes: 10 additions & 0 deletions src/xrGame/script_game_object4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ void CScriptGameObject::stop_particles(LPCSTR pname, LPCSTR bone)
LuaMessageType::Error, "Cant stop particles, bone [%s] is not visible now", bone);
}

//AVO: directly set entity health instead of going throuhg normal health property which operates on delta
void CScriptGameObject::SetHealthEx(float hp)
{
CEntity* obj = smart_cast<CEntity*>(&object());
if (!obj) return;
clamp(hp, -0.01f, 1.0f);
obj->SetfHealth(hp);
}
//-AVO

// AVO: functions for testing object class
// Credits: KD
//#include "Car.h"
Expand Down
10 changes: 6 additions & 4 deletions src/xrGame/script_game_object_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ SCRIPT_EXPORT(CScriptGameObject, (), {
value("take_item_from_box", int(GameObject::eInvBoxItemTake)),
value("weapon_no_ammo", int(GameObject::eWeaponNoAmmoAvailable)),

/* avo: custom callbacks */
// input
//AVO: custom callbacks
// Input
value("key_press", int(GameObject::eKeyPress)),
value("key_release", int(GameObject::eKeyRelease)),
value("key_hold", int(GameObject::eKeyHold)),
value("mouse_move", int(GameObject::eMouseMove)),
value("mouse_wheel", int(GameObject::eMouseWheel)),
// inventory
// Inventory
value("item_to_belt", int(GameObject::eItemToBelt)),
value("item_to_slot", int(GameObject::eItemToSlot)),
value("item_to_ruck", int(GameObject::eItemToRuck)),
/* avo: end */
// Actor
value("actor_before_death", int(GameObject::eActorBeforeDeath)),
//-AVO

value("map_location_added", int(GameObject::eMapLocationAdded))],

Expand Down
5 changes: 3 additions & 2 deletions src/xrGame/script_game_object_script3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,9 @@ class_<CScriptGameObject>& script_register_game_object2(class_<CScriptGameObject
.def("is_torch", &CScriptGameObject::isTorch)
.def("is_weapon_gl", &CScriptGameObject::isWeaponGL)
.def("is_inventory_box", &CScriptGameObject::isInventoryBox)
//end AVO
.def("set_health_ex", &CScriptGameObject::SetHealthEx)
//-AVO

;
return (instance);
return instance;
}
4 changes: 2 additions & 2 deletions src/xrGame/script_game_object_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int CScriptGameObject::clsid() const { return (object().clsid()); }
LPCSTR CScriptGameObject::Name() const { return (*object().cName()); }
shared_str CScriptGameObject::cName() const { return (object().cName()); }
LPCSTR CScriptGameObject::Section() const { return (*object().cNameSect()); }
void CScriptGameObject::Kill(CScriptGameObject* who)
void CScriptGameObject::Kill(CScriptGameObject* who, bool bypass_actor_check /*AVO: added for actor before death callback*/)
{
CEntity* l_tpEntity = smart_cast<CEntity*>(&object());
if (!l_tpEntity)
Expand All @@ -69,7 +69,7 @@ void CScriptGameObject::Kill(CScriptGameObject* who)
return;
}
if (!l_tpEntity->AlreadyDie())
l_tpEntity->KillEntity(who ? who->object().ID() : object().ID());
l_tpEntity->KillEntity(who ? who->object().ID() : object().ID(), bypass_actor_check);
else
ai().script_engine().script_log(LuaMessageType::Error, "attempt to kill dead object %s", *object().cName());
}
Expand Down

0 comments on commit a91ff26

Please sign in to comment.