Skip to content

Commit c043291

Browse files
committed
add carry success callback
1 parent 9888254 commit c043291

8 files changed

Lines changed: 164 additions & 15 deletions

File tree

src/levels_test.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ void levels_test(Gamep g)
3939

4040
level_fixed_add(g, CHANCE_NORMAL, LEVEL_TYPE_TEST, "treasure", __FUNCTION__, __LINE__, overrides, 0,
4141
/* line */ (const char *) "xxxxxxxxxxx",
42-
/* line */ (const char *) "xxxxxxxxxxx",
43-
/* line */ (const char *) "xx.......xx",
44-
/* line */ (const char *) "xx.......xx",
45-
/* line */ (const char *) "xx.......xx",
46-
/* line */ (const char *) "xx.@.c...xx",
47-
/* line */ (const char *) "xx.......xx",
48-
/* line */ (const char *) "xx.......xx",
49-
/* line */ (const char *) "xx.......xx",
50-
/* line */ (const char *) "xxxxxxxxxxx",
42+
/* line */ (const char *) "xx...cccccx",
43+
/* line */ (const char *) "xx...cccccx",
44+
/* line */ (const char *) "xx...cccccx",
45+
/* line */ (const char *) "xx...cccccx",
46+
/* line */ (const char *) "xx.@.cccccx",
47+
/* line */ (const char *) "xx...cccccx",
48+
/* line */ (const char *) "xx...cccccx",
49+
/* line */ (const char *) "xx...cccccx",
50+
/* line */ (const char *) "xx...cccccx",
5151
/* line */ (const char *) "xxxxxxxxxxx",
5252
/* end */ nullptr);
5353
}

src/my_thing_callbacks.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ using thing_on_drop_request_t = bool (*)(Gamep, Levelsp, Levelp, Thingp me, Thin
6060
void thing_on_drop_request_set(Tpp tp, thing_on_drop_request_t callback);
6161
[[nodiscard]] auto thing_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp me, Thingp dropper) -> bool;
6262

63+
using thing_on_carry_success_t = bool (*)(Gamep, Levelsp, Levelp, Thingp me, Thingp carrier);
64+
void thing_on_carry_success_set(Tpp tp, thing_on_carry_success_t callback);
65+
[[nodiscard]] auto thing_on_carry_success(Gamep g, Levelsp v, Levelp l, Thingp me, Thingp carrier) -> bool;
66+
67+
using thing_on_drop_success_t = bool (*)(Gamep, Levelsp, Levelp, Thingp me, Thingp dropper);
68+
void thing_on_drop_success_set(Tpp tp, thing_on_drop_success_t callback);
69+
[[nodiscard]] auto thing_on_drop_success(Gamep g, Levelsp v, Levelp l, Thingp me, Thingp dropper) -> bool;
70+
6371
using thing_on_use_weapon_request_t = Tpp (*)(Gamep, Levelsp, Levelp, Thingp me, Thingp user);
6472
void thing_on_use_weapon_request_set(Tpp tp, thing_on_use_weapon_request_t callback);
6573
[[nodiscard]] auto thing_on_use_weapon_request(Gamep g, Levelsp v, Levelp l, Thingp me, Thingp user) -> Tpp;

src/my_tp_class.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ class Tp
253253
thing_on_open_request_t on_open_request = {};
254254
thing_on_carry_request_t on_carry_request = {};
255255
thing_on_drop_request_t on_drop_request = {};
256+
thing_on_carry_success_t on_carry_success = {};
257+
thing_on_drop_success_t on_drop_success = {};
256258
thing_on_use_weapon_request_t on_use_weapon_request = {};
257259
thing_on_wield_request_t on_wield_request = {};
258260
thing_on_unwield_request_t on_unwield_request = {};

src/thing_carry_drop.cpp

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ static auto thing_drop_item(Gamep g, Levelsp v, Levelp l, Thingp item, Thingp dr
181181
//
182182
// Add to the inventory.
183183
//
184-
if (! thing_inventory_add(g, v, l, item, carrier)) {
184+
if (thing_inventory_add(g, v, l, item, carrier)) {
185+
//
186+
// Success
187+
//
188+
(void) thing_on_carry_success(g, v, l, item, carrier);
189+
} else {
185190
//
186191
// Possibly out of slots
187192
//
@@ -210,7 +215,15 @@ static auto thing_drop_item(Gamep g, Levelsp v, Levelp l, Thingp item, Thingp dr
210215
//
211216
// Remove from the inventory.
212217
//
213-
if (! thing_inventory_remove(g, v, l, item, carrier)) {
218+
if (thing_inventory_remove(g, v, l, item, carrier)) {
219+
//
220+
// Success
221+
//
222+
(void) thing_on_carry_success(g, v, l, item, carrier);
223+
} else {
224+
//
225+
// Remove failed
226+
//
214227
item->_is_carried = old_value;
215228

216229
auto s = to_string(g, v, l, item);
@@ -296,6 +309,68 @@ void thing_on_drop_request_set(Tpp tp, thing_on_drop_request_t callback)
296309
return tp->on_drop_request(g, v, l, me, dropper);
297310
}
298311

312+
void thing_on_carry_success_set(Tpp tp, thing_on_carry_success_t callback)
313+
{
314+
TRACE();
315+
if (tp == nullptr) [[unlikely]] {
316+
ERR("no thing template pointer");
317+
return;
318+
}
319+
tp->on_carry_success = callback;
320+
}
321+
322+
[[nodiscard]] auto thing_on_carry_success(Gamep g, Levelsp v, Levelp l, Thingp me, Thingp carrier) -> bool
323+
{
324+
TRACE();
325+
auto *tp = thing_tp(me);
326+
if (tp == nullptr) [[unlikely]] {
327+
ERR("no thing template pointer");
328+
return false;
329+
}
330+
if (tp->on_carry_success == nullptr) {
331+
//
332+
// Assume success
333+
//
334+
return true;
335+
}
336+
if (! thing_is_player(carrier) && ! thing_is_monst(carrier)) {
337+
thing_err(carrier, "unexpected thing for %s", __FUNCTION__);
338+
return false;
339+
}
340+
return tp->on_carry_success(g, v, l, me, carrier);
341+
}
342+
343+
void thing_on_drop_success_set(Tpp tp, thing_on_drop_success_t callback)
344+
{
345+
TRACE();
346+
if (tp == nullptr) [[unlikely]] {
347+
ERR("no thing template pointer");
348+
return;
349+
}
350+
tp->on_drop_success = callback;
351+
}
352+
353+
[[nodiscard]] auto thing_on_drop_success(Gamep g, Levelsp v, Levelp l, Thingp me, Thingp dropper) -> bool
354+
{
355+
TRACE();
356+
auto *tp = thing_tp(me);
357+
if (tp == nullptr) [[unlikely]] {
358+
ERR("no thing template pointer");
359+
return false;
360+
}
361+
if (tp->on_drop_success == nullptr) {
362+
//
363+
// Assume success
364+
//
365+
return true;
366+
}
367+
if (! thing_is_player(dropper) && ! thing_is_monst(dropper)) {
368+
thing_err(dropper, "unexpected thing for %s", __FUNCTION__);
369+
return false;
370+
}
371+
return tp->on_drop_success(g, v, l, me, dropper);
372+
}
373+
299374
[[nodiscard]] auto thing_carry(Gamep g, Levelsp v, Levelp l, Thingp me, Thingp item, ThingEvent &e) -> bool
300375
{
301376
TRACE();

src/things/staff/staff_fire.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,28 @@ static auto tp_staff_fire_detail_get(Gamep g, Levelsp v, Levelp l, Thingp t) ->
3434
{
3535
TRACE();
3636

37+
return true;
38+
}
39+
40+
[[nodiscard]] static auto tp_staff_fire_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
41+
{
42+
TRACE();
43+
44+
return true;
45+
}
46+
47+
[[nodiscard]] static auto tp_staff_fire_on_carry_success(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp collector) -> bool
48+
{
49+
TRACE();
50+
3751
if (thing_is_player(collector)) {
3852
thing_sound_play(g, v, l, collector, "item_collect");
3953
}
4054

4155
return true;
4256
}
4357

44-
[[nodiscard]] static auto tp_staff_fire_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
58+
[[nodiscard]] static auto tp_staff_fire_on_drop_success(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
4559
{
4660
TRACE();
4761

@@ -83,6 +97,8 @@ static auto tp_staff_fire_detail_get(Gamep g, Levelsp v, Levelp l, Thingp t) ->
8397
thing_detail_set(tp, tp_staff_fire_detail_get);
8498
thing_on_carry_request_set(tp, tp_staff_fire_on_carry_request);
8599
thing_on_drop_request_set(tp, tp_staff_fire_on_drop_request);
100+
thing_on_carry_success_set(tp, tp_staff_fire_on_carry_success);
101+
thing_on_drop_success_set(tp, tp_staff_fire_on_drop_success);
86102
thing_on_use_weapon_request_set(tp, tp_staff_fire_on_use_weapon_request);
87103
thing_on_wield_request_set(tp, tp_staff_fire_on_wield_request);
88104
tp_charge_count_set(tp, 5000);

src/things/treasure/potion.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,28 @@ static auto tp_potion_detail_get(Gamep g, Levelsp v, Levelp l, Thingp t) -> std:
3030
{
3131
TRACE();
3232

33+
return true;
34+
}
35+
36+
[[nodiscard]] static auto tp_potion_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
37+
{
38+
TRACE();
39+
40+
return true;
41+
}
42+
43+
[[nodiscard]] static auto tp_potion_on_carry_success(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp collector) -> bool
44+
{
45+
TRACE();
46+
3347
if (thing_is_player(collector)) {
3448
thing_sound_play(g, v, l, collector, "item_collect");
3549
}
3650

3751
return true;
3852
}
3953

40-
[[nodiscard]] static auto tp_potion_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
54+
[[nodiscard]] static auto tp_potion_on_drop_success(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
4155
{
4256
TRACE();
4357

@@ -60,6 +74,8 @@ static auto tp_potion_detail_get(Gamep g, Levelsp v, Levelp l, Thingp t) -> std:
6074
thing_detail_set(tp, tp_potion_detail_get);
6175
thing_on_carry_request_set(tp, tp_potion_on_carry_request);
6276
thing_on_drop_request_set(tp, tp_potion_on_drop_request);
77+
thing_on_carry_success_set(tp, tp_potion_on_carry_success);
78+
thing_on_drop_success_set(tp, tp_potion_on_drop_success);
6379
tp_chance_set(tp, THING_CHANCE_CONTINUE_TO_BURN, "1d2"); // roll max to continue burning
6480
tp_flag_set(tp, is_able_to_fall_sound);
6581
tp_flag_set(tp, is_able_to_fall);

src/things/wand/wand_fire.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,28 @@ static auto tp_wand_fire_detail_get(Gamep g, Levelsp v, Levelp l, Thingp t) -> s
3434
{
3535
TRACE();
3636

37+
return true;
38+
}
39+
40+
[[nodiscard]] static auto tp_wand_fire_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
41+
{
42+
TRACE();
43+
44+
return true;
45+
}
46+
47+
[[nodiscard]] static auto tp_wand_fire_on_carry_success(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp collector) -> bool
48+
{
49+
TRACE();
50+
3751
if (thing_is_player(collector)) {
3852
thing_sound_play(g, v, l, collector, "item_collect");
3953
}
4054

4155
return true;
4256
}
4357

44-
[[nodiscard]] static auto tp_wand_fire_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
58+
[[nodiscard]] static auto tp_wand_fire_on_drop_success(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
4559
{
4660
TRACE();
4761

@@ -83,6 +97,8 @@ static auto tp_wand_fire_detail_get(Gamep g, Levelsp v, Levelp l, Thingp t) -> s
8397
thing_detail_set(tp, tp_wand_fire_detail_get);
8498
thing_on_carry_request_set(tp, tp_wand_fire_on_carry_request);
8599
thing_on_drop_request_set(tp, tp_wand_fire_on_drop_request);
100+
thing_on_carry_success_set(tp, tp_wand_fire_on_carry_success);
101+
thing_on_drop_success_set(tp, tp_wand_fire_on_drop_success);
86102
thing_on_use_weapon_request_set(tp, tp_wand_fire_on_use_weapon_request);
87103
thing_on_wield_request_set(tp, tp_wand_fire_on_wield_request);
88104
tp_charge_count_set(tp, 500);

src/things/wand/wand_light.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,28 @@ static auto tp_wand_light_detail_get(Gamep g, Levelsp v, Levelp l, Thingp t) ->
3535
{
3636
TRACE();
3737

38+
return true;
39+
}
40+
41+
[[nodiscard]] static auto tp_wand_light_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
42+
{
43+
TRACE();
44+
45+
return true;
46+
}
47+
48+
[[nodiscard]] static auto tp_wand_light_on_carry_success(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp collector) -> bool
49+
{
50+
TRACE();
51+
3852
if (thing_is_player(collector)) {
3953
thing_sound_play(g, v, l, collector, "item_collect");
4054
}
4155

4256
return true;
4357
}
4458

45-
[[nodiscard]] static auto tp_wand_light_on_drop_request(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
59+
[[nodiscard]] static auto tp_wand_light_on_drop_success(Gamep g, Levelsp v, Levelp l, Thingp t, Thingp dropper) -> bool
4660
{
4761
TRACE();
4862

@@ -84,6 +98,8 @@ static auto tp_wand_light_detail_get(Gamep g, Levelsp v, Levelp l, Thingp t) ->
8498
thing_detail_set(tp, tp_wand_light_detail_get);
8599
thing_on_carry_request_set(tp, tp_wand_light_on_carry_request);
86100
thing_on_drop_request_set(tp, tp_wand_light_on_drop_request);
101+
thing_on_carry_success_set(tp, tp_wand_light_on_carry_success);
102+
thing_on_drop_success_set(tp, tp_wand_light_on_drop_success);
87103
thing_on_use_weapon_request_set(tp, tp_wand_light_on_light_weapon_request);
88104
thing_on_wield_request_set(tp, tp_wand_light_on_wield_request);
89105
tp_charge_count_set(tp, 500);

0 commit comments

Comments
 (0)