Skip to content

Commit

Permalink
Script callbacks (examples in bind_stalker.script)
Browse files Browse the repository at this point in the history
+ key_press
+ key_release (disabled in Common/Config.hpp)
+ key_hold (disabled in Common/Config.hpp)
+ mouse_move (disabled in Common/Config.hpp)
+ move_wheel
+ item_to_ruck
+ item_to_belt
+ item_to_slot

Misc:
+ first person death (disabled in Common/Config.hpp)
  • Loading branch information
avoitishin authored and Xottab-DUTY committed Aug 14, 2017
1 parent 923466a commit 713a513
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 8 deletions.
71 changes: 64 additions & 7 deletions res/gamedata/scripts/bind_stalker.script
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ function actor_binder:net_destroy()

----| aVo |--------------------------------------------------------------------
self.object:set_callback(callback.hit, nil)
--self.object:set_callback(123, nil)
self.object:set_callback(callback.key_press, nil)
self.object:set_callback(callback.key_release, nil)
self.object:set_callback(callback.key_hold, nil)
self.object:set_callback(callback.mouse_move, nil)
self.object:set_callback(callback.mouse_wheel, nil)
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)
----| end:aVo |----------------------------------------------------------------

log("--------->"..tostring(_G.amb_vol))
Expand Down Expand Up @@ -126,22 +133,72 @@ function actor_binder:reinit()

----| aVo |--------------------------------------------------------------------
self.object:set_callback(callback.hit, self.actor_hit, self)
-- self.object:set_callback(123, self.key_press, self)
self.object:set_callback(callback.key_press, self.key_press, self)
self.object:set_callback(callback.key_release, self.key_release, self)
self.object:set_callback(callback.key_hold, self.key_hold, self)
self.object:set_callback(callback.mouse_move, self.mouse_move, self)
self.object:set_callback(callback.mouse_wheel, self.mouse_wheel, self)
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)
----| end:aVo |----------------------------------------------------------------
end
---------------------------------------------------------------------------------------------------------------------


----| aVo |--------------------------------------------------------------------
-- Actor hit callback
-- actor hit callback
function actor_binder:actor_hit(obj, amount, local_direction, who, bone_index)
-- local msg = string.format("actor_hit %f, %i", amount, bone_index)
-- get_console():execute("load ~:"..msg)
end
-- x-ray extensions key press callback
--function actor_binder:key_press(key)
--sm:call("key_press", key)
--end
-- key press callback
function actor_binder:key_press(key)
-- sm:call("key_press", key)
-- log1(string.format("key pressed %d", key))
if key == DIK_keys.DIK_F7 then
log1("F7 pressed")
end
end
-- key release callback (turn on in build_config_defines.h only if absolutely needed)
function actor_binder:key_release(key)
-- sm:call("key_release", key)
-- log1(string.format("key released %d", key))
if key == DIK_keys.DIK_F7 then
log1("F7 released")
end
end
-- key hold callback (turn on in build_config_defines.h only if absolutely needed)
function actor_binder:key_hold(key)
-- sm:call("key_hold", key)
-- log1(string.format("key hold %d", key))
if key == DIK_keys.DIK_F7 then
log1("F7 hold")
end
end
-- mouse move callback (turn on in build_config_defines.h only if absolutely needed)
function actor_binder:mouse_move(dx, dy)
log1(string.format("mouse moved to: [%d]:[%d]", dx, dy))
end
-- mouse wheel callback (direction is 120 up and -120 down)
function actor_binder:mouse_wheel(direction)
log1(string.format("mouse wheel moved %s", tostring(direction)))
end
-- item to ruck callback
-- note: this is called on new game and level change
function actor_binder:item_to_ruck(obj)
log1(string.format("item_to_ruck [%s]", obj:name()))
end
-- item to belt callback
-- note: this is called on new game and level change
function actor_binder:item_to_belt(obj)
log1(string.format("item_to_belt [%s]", obj:name()))
end
-- item to slot callback
-- note: this is called on new game and level change
function actor_binder:item_to_slot(obj)
log1(string.format("item_to_slot [%s]", obj:name()))
end
----------------------------------------------------------------------------------------------------------------------
function actor_binder:take_item_from_box(box, item)
local box_name = box:name()
Expand Down
6 changes: 6 additions & 0 deletions src/Common/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
// XXX: convert to command line params
// CONFIG_SCRIPT_ENGINE_LOG_EXPORTS
// CONFIG_SCRIPT_ENGINE_LOG_SKIPPED_EXPORTS

/* Scripts */
//#define MOUSE_MOVE_CALLBACK // expose mouse move callback to scripts (configure in bind_stalker)
//#define KEY_RELEASE_CALLBACK // expose key release callback to scripts (configure in bind_stalker)
//#define KEY_HOLD_CALLBACK // expose key hold callback to scripts (configure in bind_stalker)
//#define FP_DEATH // first person death view
6 changes: 6 additions & 0 deletions src/xrGame/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
#include "ActorHelmet.h"
#include "UI/UIDragDropReferenceList.h"

#include "Common/Config.hpp"

const u32 patch_frames = 50;
const float respawn_delay = 1.f;
const float respawn_auto = 7.f;
Expand Down Expand Up @@ -808,7 +810,11 @@ void CActor::Die(IGameObject* who)

if (IsGameTypeSingle())
{
#ifdef FP_DEATH
cam_Set(eacFirstEye);
#else
cam_Set(eacFreeLook);
#endif // FP_DEATH
CurrentGameUI()->HideShownDialogs();
start_tutorial("game_over");
}
Expand Down
24 changes: 23 additions & 1 deletion src/xrGame/InventoryOwner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,35 @@ void CInventoryOwner::OnItemDrop(CInventoryItem* inventory_item, bool just_befor
}

void CInventoryOwner::OnItemDropUpdate() {}
void CInventoryOwner::OnItemBelt(CInventoryItem* inventory_item, const SInvItemPlace& previous_place) {}

void CInventoryOwner::OnItemBelt(CInventoryItem* inventory_item, const SInvItemPlace& previous_place)
{
/* avo: script callback */
CGameObject *object = smart_cast<CGameObject*>(this);
VERIFY(object);
object->callback(GameObject::eItemToBelt)(inventory_item->object().lua_game_object());
/* avo: end */
}

void CInventoryOwner::OnItemRuck(CInventoryItem* inventory_item, const SInvItemPlace& previous_place)
{
/* avo: script callback */
CGameObject *object = smart_cast<CGameObject*>(this);
VERIFY(object);
object->callback(GameObject::eItemToRuck)(inventory_item->object().lua_game_object());
/* avo: end */

detach(inventory_item);
}

void CInventoryOwner::OnItemSlot(CInventoryItem* inventory_item, const SInvItemPlace& previous_place)
{
/* avo: script callback */
CGameObject *object = smart_cast<CGameObject*>(this);
VERIFY(object);
object->callback(GameObject::eItemToSlot)(inventory_item->object().lua_game_object());
/* avo: end */

attach(inventory_item);
}

Expand Down
35 changes: 35 additions & 0 deletions src/xrGame/Level_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "Include/xrRender/DebugRender.h"

#include "Common/Config.hpp"

#ifdef DEBUG
#include "ai/monsters/BaseMonster/base_monster.h"

Expand All @@ -46,6 +48,11 @@ void CLevel::IR_OnMouseWheel(int direction)
if (g_bDisableAllInput)
return;

/* avo: script callback */
if (g_actor)
g_actor->callback(GameObject::eMouseWheel)(direction);
/* avo: end */

if (CurrentGameUI()->IR_UIOnMouseWheel(direction))
return;
if (Device.Paused()
Expand All @@ -72,6 +79,14 @@ void CLevel::IR_OnMouseMove(int dx, int dy)
{
if (g_bDisableAllInput)
return;

#ifdef MOUSE_MOVE_CALLBACK
/* avo: script callback */
if (g_actor)
g_actor->callback(GameObject::eMouseMove)(dx, dy);
/* avo: end */
#endif // MOUSE_MOVE_CALLBACK

if (CurrentGameUI()->IR_UIOnMouseMove(dx, dy))
return;
if (Device.Paused() && !IsDemoPlay()
Expand Down Expand Up @@ -112,6 +127,11 @@ void CLevel::IR_OnKeyboardPress(int key)

EGameActions _curr = get_binded_action(key);

/* avo: script callback */
if (!g_bDisableAllInput && g_actor)
g_actor->callback(GameObject::eKeyPress)(key);
/* avo: end */

if (_curr == kPAUSE)
{
#ifdef INGAME_EDITOR
Expand Down Expand Up @@ -483,6 +503,14 @@ void CLevel::IR_OnKeyboardRelease(int key)
{
if (!bReady || g_bDisableAllInput)
return;

#ifdef KEY_RELEASE_CALLBACK
/* avo: script callback */
if (g_actor)
g_actor->callback(GameObject::eKeyRelease)(key);
/* avo: end */
#endif // KEY_RELEASE_CALLBACK

if (CurrentGameUI() && CurrentGameUI()->IR_UIOnKeyboardRelease(key))
return;
if (game && game->OnKeyboardRelease(get_binded_action(key)))
Expand All @@ -507,6 +535,13 @@ void CLevel::IR_OnKeyboardHold(int key)
if (g_bDisableAllInput)
return;

#ifdef KEY_HOLD_CALLBACK
/* avo: script callback */
if (g_actor)
g_actor->callback(GameObject::eKeyHold)(key);
/* avo: end */
#endif // KEY_HOLD_CALLBACK

#ifdef DEBUG
// Lain: added
if (key == DIK_UP)
Expand Down
13 changes: 13 additions & 0 deletions src/xrGame/game_object_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ enum ECallbackType
eInvBoxItemTake,
eWeaponNoAmmoAvailable,

/* avo: custom callbacks */
// input
eKeyPress,
eKeyRelease,
eKeyHold,
eMouseMove,
eMouseWheel,
// inventory
eItemToBelt,
eItemToSlot,
eItemToRuck,
/* avo: end */

eDummy = u32(-1),
};
};
13 changes: 13 additions & 0 deletions src/xrGame/script_game_object_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ SCRIPT_EXPORT(CScriptGameObject, (), {
value("take_item_from_box", int(GameObject::eInvBoxItemTake)),
value("weapon_no_ammo", int(GameObject::eWeaponNoAmmoAvailable)),

/* 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
value("item_to_belt", int(GameObject::eItemToBelt)),
value("item_to_slot", int(GameObject::eItemToSlot)),
value("item_to_ruck", int(GameObject::eItemToRuck)),
/* avo: end */

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

def("buy_condition", (void (*)(CScriptIniFile*, LPCSTR))(&::buy_condition)),
Expand Down

0 comments on commit 713a513

Please sign in to comment.