Skip to content

Commit 713a513

Browse files
avoitishinXottab-DUTY
authored andcommitted
Script callbacks (examples in bind_stalker.script)
+ 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)
1 parent 923466a commit 713a513

File tree

7 files changed

+160
-8
lines changed

7 files changed

+160
-8
lines changed

res/gamedata/scripts/bind_stalker.script

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ function actor_binder:net_destroy()
8787

8888
----| aVo |--------------------------------------------------------------------
8989
self.object:set_callback(callback.hit, nil)
90-
--self.object:set_callback(123, nil)
90+
self.object:set_callback(callback.key_press, nil)
91+
self.object:set_callback(callback.key_release, nil)
92+
self.object:set_callback(callback.key_hold, nil)
93+
self.object:set_callback(callback.mouse_move, nil)
94+
self.object:set_callback(callback.mouse_wheel, nil)
95+
self.object:set_callback(callback.item_to_belt, nil)
96+
self.object:set_callback(callback.item_to_ruck, nil)
97+
self.object:set_callback(callback.item_to_slot, nil)
9198
----| end:aVo |----------------------------------------------------------------
9299

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

127134
----| aVo |--------------------------------------------------------------------
128135
self.object:set_callback(callback.hit, self.actor_hit, self)
129-
-- self.object:set_callback(123, self.key_press, self)
136+
self.object:set_callback(callback.key_press, self.key_press, self)
137+
self.object:set_callback(callback.key_release, self.key_release, self)
138+
self.object:set_callback(callback.key_hold, self.key_hold, self)
139+
self.object:set_callback(callback.mouse_move, self.mouse_move, self)
140+
self.object:set_callback(callback.mouse_wheel, self.mouse_wheel, self)
141+
self.object:set_callback(callback.item_to_ruck, self.item_to_ruck, self)
142+
self.object:set_callback(callback.item_to_belt, self.item_to_belt, self)
143+
self.object:set_callback(callback.item_to_slot, self.item_to_slot, self)
130144
----| end:aVo |----------------------------------------------------------------
131145
end
132146
---------------------------------------------------------------------------------------------------------------------
133147

134148

135149
----| aVo |--------------------------------------------------------------------
136-
-- Actor hit callback
150+
-- actor hit callback
137151
function actor_binder:actor_hit(obj, amount, local_direction, who, bone_index)
138152
-- local msg = string.format("actor_hit %f, %i", amount, bone_index)
139153
-- get_console():execute("load ~:"..msg)
140154
end
141-
-- x-ray extensions key press callback
142-
--function actor_binder:key_press(key)
143-
--sm:call("key_press", key)
144-
--end
155+
-- key press callback
156+
function actor_binder:key_press(key)
157+
-- sm:call("key_press", key)
158+
-- log1(string.format("key pressed %d", key))
159+
if key == DIK_keys.DIK_F7 then
160+
log1("F7 pressed")
161+
end
162+
end
163+
-- key release callback (turn on in build_config_defines.h only if absolutely needed)
164+
function actor_binder:key_release(key)
165+
-- sm:call("key_release", key)
166+
-- log1(string.format("key released %d", key))
167+
if key == DIK_keys.DIK_F7 then
168+
log1("F7 released")
169+
end
170+
end
171+
-- key hold callback (turn on in build_config_defines.h only if absolutely needed)
172+
function actor_binder:key_hold(key)
173+
-- sm:call("key_hold", key)
174+
-- log1(string.format("key hold %d", key))
175+
if key == DIK_keys.DIK_F7 then
176+
log1("F7 hold")
177+
end
178+
end
179+
-- mouse move callback (turn on in build_config_defines.h only if absolutely needed)
180+
function actor_binder:mouse_move(dx, dy)
181+
log1(string.format("mouse moved to: [%d]:[%d]", dx, dy))
182+
end
183+
-- mouse wheel callback (direction is 120 up and -120 down)
184+
function actor_binder:mouse_wheel(direction)
185+
log1(string.format("mouse wheel moved %s", tostring(direction)))
186+
end
187+
-- item to ruck callback
188+
-- note: this is called on new game and level change
189+
function actor_binder:item_to_ruck(obj)
190+
log1(string.format("item_to_ruck [%s]", obj:name()))
191+
end
192+
-- item to belt callback
193+
-- note: this is called on new game and level change
194+
function actor_binder:item_to_belt(obj)
195+
log1(string.format("item_to_belt [%s]", obj:name()))
196+
end
197+
-- item to slot callback
198+
-- note: this is called on new game and level change
199+
function actor_binder:item_to_slot(obj)
200+
log1(string.format("item_to_slot [%s]", obj:name()))
201+
end
145202
----------------------------------------------------------------------------------------------------------------------
146203
function actor_binder:take_item_from_box(box, item)
147204
local box_name = box:name()

src/Common/Config.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@
44
// XXX: convert to command line params
55
// CONFIG_SCRIPT_ENGINE_LOG_EXPORTS
66
// CONFIG_SCRIPT_ENGINE_LOG_SKIPPED_EXPORTS
7+
8+
/* Scripts */
9+
//#define MOUSE_MOVE_CALLBACK // expose mouse move callback to scripts (configure in bind_stalker)
10+
//#define KEY_RELEASE_CALLBACK // expose key release callback to scripts (configure in bind_stalker)
11+
//#define KEY_HOLD_CALLBACK // expose key hold callback to scripts (configure in bind_stalker)
12+
//#define FP_DEATH // first person death view

src/xrGame/Actor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
#include "ActorHelmet.h"
7474
#include "UI/UIDragDropReferenceList.h"
7575

76+
#include "Common/Config.hpp"
77+
7678
const u32 patch_frames = 50;
7779
const float respawn_delay = 1.f;
7880
const float respawn_auto = 7.f;
@@ -808,7 +810,11 @@ void CActor::Die(IGameObject* who)
808810

809811
if (IsGameTypeSingle())
810812
{
813+
#ifdef FP_DEATH
814+
cam_Set(eacFirstEye);
815+
#else
811816
cam_Set(eacFreeLook);
817+
#endif // FP_DEATH
812818
CurrentGameUI()->HideShownDialogs();
813819
start_tutorial("game_over");
814820
}

src/xrGame/InventoryOwner.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,35 @@ void CInventoryOwner::OnItemDrop(CInventoryItem* inventory_item, bool just_befor
444444
}
445445

446446
void CInventoryOwner::OnItemDropUpdate() {}
447-
void CInventoryOwner::OnItemBelt(CInventoryItem* inventory_item, const SInvItemPlace& previous_place) {}
447+
448+
void CInventoryOwner::OnItemBelt(CInventoryItem* inventory_item, const SInvItemPlace& previous_place)
449+
{
450+
/* avo: script callback */
451+
CGameObject *object = smart_cast<CGameObject*>(this);
452+
VERIFY(object);
453+
object->callback(GameObject::eItemToBelt)(inventory_item->object().lua_game_object());
454+
/* avo: end */
455+
}
456+
448457
void CInventoryOwner::OnItemRuck(CInventoryItem* inventory_item, const SInvItemPlace& previous_place)
449458
{
459+
/* avo: script callback */
460+
CGameObject *object = smart_cast<CGameObject*>(this);
461+
VERIFY(object);
462+
object->callback(GameObject::eItemToRuck)(inventory_item->object().lua_game_object());
463+
/* avo: end */
464+
450465
detach(inventory_item);
451466
}
467+
452468
void CInventoryOwner::OnItemSlot(CInventoryItem* inventory_item, const SInvItemPlace& previous_place)
453469
{
470+
/* avo: script callback */
471+
CGameObject *object = smart_cast<CGameObject*>(this);
472+
VERIFY(object);
473+
object->callback(GameObject::eItemToSlot)(inventory_item->object().lua_game_object());
474+
/* avo: end */
475+
454476
attach(inventory_item);
455477
}
456478

src/xrGame/Level_input.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#include "Include/xrRender/DebugRender.h"
2626

27+
#include "Common/Config.hpp"
28+
2729
#ifdef DEBUG
2830
#include "ai/monsters/BaseMonster/base_monster.h"
2931

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

51+
/* avo: script callback */
52+
if (g_actor)
53+
g_actor->callback(GameObject::eMouseWheel)(direction);
54+
/* avo: end */
55+
4956
if (CurrentGameUI()->IR_UIOnMouseWheel(direction))
5057
return;
5158
if (Device.Paused()
@@ -72,6 +79,14 @@ void CLevel::IR_OnMouseMove(int dx, int dy)
7279
{
7380
if (g_bDisableAllInput)
7481
return;
82+
83+
#ifdef MOUSE_MOVE_CALLBACK
84+
/* avo: script callback */
85+
if (g_actor)
86+
g_actor->callback(GameObject::eMouseMove)(dx, dy);
87+
/* avo: end */
88+
#endif // MOUSE_MOVE_CALLBACK
89+
7590
if (CurrentGameUI()->IR_UIOnMouseMove(dx, dy))
7691
return;
7792
if (Device.Paused() && !IsDemoPlay()
@@ -112,6 +127,11 @@ void CLevel::IR_OnKeyboardPress(int key)
112127

113128
EGameActions _curr = get_binded_action(key);
114129

130+
/* avo: script callback */
131+
if (!g_bDisableAllInput && g_actor)
132+
g_actor->callback(GameObject::eKeyPress)(key);
133+
/* avo: end */
134+
115135
if (_curr == kPAUSE)
116136
{
117137
#ifdef INGAME_EDITOR
@@ -483,6 +503,14 @@ void CLevel::IR_OnKeyboardRelease(int key)
483503
{
484504
if (!bReady || g_bDisableAllInput)
485505
return;
506+
507+
#ifdef KEY_RELEASE_CALLBACK
508+
/* avo: script callback */
509+
if (g_actor)
510+
g_actor->callback(GameObject::eKeyRelease)(key);
511+
/* avo: end */
512+
#endif // KEY_RELEASE_CALLBACK
513+
486514
if (CurrentGameUI() && CurrentGameUI()->IR_UIOnKeyboardRelease(key))
487515
return;
488516
if (game && game->OnKeyboardRelease(get_binded_action(key)))
@@ -507,6 +535,13 @@ void CLevel::IR_OnKeyboardHold(int key)
507535
if (g_bDisableAllInput)
508536
return;
509537

538+
#ifdef KEY_HOLD_CALLBACK
539+
/* avo: script callback */
540+
if (g_actor)
541+
g_actor->callback(GameObject::eKeyHold)(key);
542+
/* avo: end */
543+
#endif // KEY_HOLD_CALLBACK
544+
510545
#ifdef DEBUG
511546
// Lain: added
512547
if (key == DIK_UP)

src/xrGame/game_object_space.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ enum ECallbackType
5454
eInvBoxItemTake,
5555
eWeaponNoAmmoAvailable,
5656

57+
/* avo: custom callbacks */
58+
// input
59+
eKeyPress,
60+
eKeyRelease,
61+
eKeyHold,
62+
eMouseMove,
63+
eMouseWheel,
64+
// inventory
65+
eItemToBelt,
66+
eItemToSlot,
67+
eItemToRuck,
68+
/* avo: end */
69+
5770
eDummy = u32(-1),
5871
};
5972
};

src/xrGame/script_game_object_script.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ SCRIPT_EXPORT(CScriptGameObject, (), {
8383
value("take_item_from_box", int(GameObject::eInvBoxItemTake)),
8484
value("weapon_no_ammo", int(GameObject::eWeaponNoAmmoAvailable)),
8585

86+
/* avo: custom callbacks */
87+
// input
88+
value("key_press", int(GameObject::eKeyPress)),
89+
value("key_release", int(GameObject::eKeyRelease)),
90+
value("key_hold", int(GameObject::eKeyHold)),
91+
value("mouse_move", int(GameObject::eMouseMove)),
92+
value("mouse_wheel", int(GameObject::eMouseWheel)),
93+
// inventory
94+
value("item_to_belt", int(GameObject::eItemToBelt)),
95+
value("item_to_slot", int(GameObject::eItemToSlot)),
96+
value("item_to_ruck", int(GameObject::eItemToRuck)),
97+
/* avo: end */
98+
8699
value("map_location_added", int(GameObject::eMapLocationAdded))],
87100

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

0 commit comments

Comments
 (0)