Skip to content

Commit

Permalink
Merge pull request #77791 from GuardianDll/better_f_consume_item_sum
Browse files Browse the repository at this point in the history
Improve f_consume_item_sum and f_has_items_sum, use it more
  • Loading branch information
Anton Burmistrov authored Nov 18, 2024
2 parents a377385 + 01a9455 commit 1302eac
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 155 deletions.
96 changes: 0 additions & 96 deletions data/json/effects_on_condition/npc_eocs/generic_npc_eocs.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,102 +132,6 @@
"effect": [ { "u_sell_item": "drivebelt", "count": 3 } ],
"false_effect": [ { "u_sell_item": "drivebelt_makeshift", "count": 3 } ]
},
{
"type": "effect_on_condition",
"id": "EOC_MISSION_LUMBERMILL_GET_6_BLANKETS_done",
"global": true,
"condition": {
"or": [
{ "u_has_items": { "item": "blanket", "count": 6 } },
{ "u_has_items": { "item": "fur_blanket", "count": 6 } },
{ "u_has_items": { "item": "electric_blanket", "count": 6 } },
{ "u_has_items": { "item": "quilt", "count": 6 } },
{ "u_has_items": { "item": "quilt_patchwork", "count": 6 } }
]
},
"effect": [
{
"run_eocs": [
{
"id": "sell_blanket",
"condition": { "u_has_items": { "item": "blanket", "count": 6 } },
"effect": { "u_sell_item": "blanket", "count": 6 }
},
{
"id": "sell_fur_blanket",
"condition": { "u_has_items": { "item": "fur_blanket", "count": 6 } },
"effect": { "u_sell_item": "fur_blanket", "count": 6 }
},
{
"id": "sell_electric_blanket",
"condition": { "u_has_items": { "item": "electric_blanket", "count": 6 } },
"effect": { "u_sell_item": "electric_blanket", "count": 6 }
},
{
"id": "sell_quilt",
"condition": { "u_has_items": { "item": "quilt", "count": 6 } },
"effect": { "u_sell_item": "quilt", "count": 6 }
},
{
"id": "sell_quilt_patchwork",
"condition": { "u_has_items": { "item": "quilt_patchwork", "count": 6 } },
"effect": { "u_sell_item": "quilt_patchwork", "count": 6 }
}
]
}
]
},
{
"type": "effect_on_condition",
"id": "EOC_MISSION_GODCO_TROPHY2_done",
"global": true,
"condition": {
"or": [
{ "u_has_items": { "item": "egg_spider_cellar", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_jumping", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_trapdoor", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_web", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_widow", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_wolf", "count": 1 } }
]
},
"effect": [
{
"run_eocs": [
{
"id": "sell_egg_spider_cellar",
"condition": { "u_has_items": { "item": "egg_spider_cellar", "count": 1 } },
"effect": { "u_sell_item": "egg_spider_cellar", "count": 1 }
},
{
"id": "sell_egg_spider_jumping",
"condition": { "u_has_items": { "item": "egg_spider_jumping", "count": 1 } },
"effect": { "u_sell_item": "egg_spider_jumping", "count": 1 }
},
{
"id": "sell_egg_spider_trapdoor",
"condition": { "u_has_items": { "item": "egg_spider_trapdoor", "count": 1 } },
"effect": { "u_sell_item": "egg_spider_trapdoor", "count": 1 }
},
{
"id": "sell_egg_spider_web",
"condition": { "u_has_items": { "item": "egg_spider_web", "count": 1 } },
"effect": { "u_sell_item": "egg_spider_web", "count": 1 }
},
{
"id": "sell_egg_spider_widow",
"condition": { "u_has_items": { "item": "egg_spider_widow", "count": 1 } },
"effect": { "u_sell_item": "egg_spider_widow", "count": 1 }
},
{
"id": "sell_egg_spider_wolf",
"condition": { "u_has_items": { "item": "egg_spider_wolf", "count": 1 } },
"effect": { "u_sell_item": "egg_spider_wolf", "count": 1 }
}
]
}
]
},
{
"type": "effect_on_condition",
"id": "EOC_BANDIT_ASSASSIN",
Expand Down
25 changes: 17 additions & 8 deletions data/json/npcs/godco/godco_missions.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@
"value": 0,
"count": 1,
"goal_condition": {
"or": [
{ "u_has_items": { "item": "egg_spider_cellar", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_jumping", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_trapdoor", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_web", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_widow", "count": 1 } },
{ "u_has_items": { "item": "egg_spider_wolf", "count": 1 } }
"u_has_items_sum": [
{ "item": "egg_spider_cellar", "amount": 1 },
{ "item": "egg_spider_jumping", "amount": 1 },
{ "item": "egg_spider_trapdoor", "amount": 1 },
{ "item": "egg_spider_web", "amount": 1 },
{ "item": "egg_spider_widow", "amount": 1 },
{ "item": "egg_spider_wolf", "amount": 1 }
]
},
"origins": [ "ORIGIN_SECONDARY" ],
Expand All @@ -397,7 +397,16 @@
"effect": [
{ "u_spawn_item": "270win_jsp", "count": 4 },
{ "u_spawn_item": "icon", "count": 5 },
{ "run_eocs": "EOC_MISSION_GODCO_TROPHY2_done" }
{
"u_consume_item_sum": [
{ "item": "egg_spider_cellar", "amount": 1 },
{ "item": "egg_spider_jumping", "amount": 1 },
{ "item": "egg_spider_trapdoor", "amount": 1 },
{ "item": "egg_spider_web", "amount": 1 },
{ "item": "egg_spider_widow", "amount": 1 },
{ "item": "egg_spider_wolf", "amount": 1 }
]
}
],
"opinion": { "trust": 2, "value": 2 }
},
Expand Down
26 changes: 19 additions & 7 deletions data/json/npcs/lumbermill_employees/lumbermill_missions.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,30 @@
"value": 4000,
"//": "Dynamic response dialogs would be more natural to accept so many alternate items here.",
"goal_condition": {
"or": [
{ "u_has_items": { "item": "blanket", "count": 6 } },
{ "u_has_items": { "item": "fur_blanket", "count": 6 } },
{ "u_has_items": { "item": "electric_blanket", "count": 6 } },
{ "u_has_items": { "item": "quilt", "count": 6 } },
{ "u_has_items": { "item": "quilt_patchwork", "count": 6 } }
"u_has_items_sum": [
{ "item": "blanket", "amount": 6 },
{ "item": "fur_blanket", "amount": 6 },
{ "item": "down_blanket", "amount": 6 },
{ "item": "blanket_weighted", "amount": 6 },
{ "item": "quilt", "amount": 6 },
{ "item": "quilt_patchwork", "amount": 6 },
{ "item": "electric_blanket", "amount": 6 }
]
},
"origins": [ "ORIGIN_SECONDARY" ],
"end": {
"update_mapgen": { "om_terrain": "lumbermill_1_1_ocu", "place_nested": [ { "chunks": [ "occupied_lum_hotel_1_1" ], "x": 0, "y": 0 } ] },
"effect": { "run_eocs": "EOC_MISSION_LUMBERMILL_GET_6_BLANKETS_done" }
"effect": {
"u_consume_item_sum": [
{ "item": "blanket", "amount": 6 },
{ "item": "fur_blanket", "amount": 6 },
{ "item": "down_blanket", "amount": 6 },
{ "item": "blanket_weighted", "amount": 6 },
{ "item": "quilt", "amount": 6 },
{ "item": "quilt_patchwork", "amount": 6 },
{ "item": "electric_blanket", "amount": 6 }
]
}
},
"followup": "MISSION_LUMBERMILL_SET_TRADE_ROUTE",
"has_generic_rewards": true,
Expand Down
7 changes: 5 additions & 2 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,11 +931,14 @@ conditional_t::func f_has_items_sum( const JsonObject &jo, const std::string_vie
double count_present;
double charges_present;
double total_present;
const Character *you = d.const_actor( is_npc )->get_const_character();
inventory inventory_and_around = you->crafting_inventory( you->pos(), PICKUP_RANGE );

for( const auto &pair : item_and_amount ) {
item_to_find = itype_id( pair.first.evaluate( d ) );
count_desired = pair.second.evaluate( d );
count_present = d.const_actor( is_npc )->get_amount( item_to_find );
charges_present = d.const_actor( is_npc )->charges_of( item_to_find );
count_present = inventory_and_around.amount_of( item_to_find );
charges_present = inventory_and_around.charges_of( item_to_find );
total_present = std::max( count_present, charges_present );
percent += total_present / count_desired;

Expand Down
72 changes: 30 additions & 42 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3867,53 +3867,41 @@ talk_effect_fun_t::func f_consume_item_sum( const JsonObject &jo, std::string_vi

itype_id item_to_remove;
double percent = 0.0f;
double count_desired;
double count_present;
double charges_present;
double ratio = 0.0f;
double amount_desired = 0.0f;
int count_present = 0;
Character *you = d.actor( is_npc )->get_character();
inventory inventory_and_around = you->crafting_inventory( you->pos(), PICKUP_RANGE );
std::vector<item_comp> items_to_remove_vector;

for( const auto &pair : item_and_amount ) {
int amount_to_remove = 0;
item_to_remove = itype_id( pair.first.evaluate( d ) );
count_desired = pair.second.evaluate( d );
count_present = d.actor( is_npc )->get_amount( item_to_remove );
charges_present = d.actor( is_npc )->charges_of( item_to_remove );
if( charges_present > count_present ) {
percent += charges_present / count_desired;
// if percent is equal or less than 1, it is safe to remove all charges_present
// otherwise loop to remove charges one by one
if( percent <= 1 ) {
d.actor( is_npc )->use_charges( item_to_remove, charges_present, true );

add_msg_debug( debugmode::DF_TALKER,
"removing item: %s, count_desired: %f, charges_present: %f, percent: %f, removing all",
item_to_remove.c_str(), count_desired, charges_present, percent );
} else {
percent -= charges_present / count_desired;
while( percent < 1.0f ) {
percent += 1 / count_desired;
d.actor( is_npc )->use_charges( item_to_remove, 1, true );
add_msg_debug( debugmode::DF_TALKER,
"removing item: %s, count_desired: %f, charges_present: %f, percent: %f, removing one by one",
item_to_remove.c_str(), count_desired, charges_present, percent );
}
}
amount_desired = pair.second.evaluate( d );
count_present = inventory_and_around.count_item( item_to_remove );

if( count_present == 0 ) {
continue;
}

percent += count_present / amount_desired;

if( percent <= 1.0 ) {
// either lack or just right amount of items to consume
items_to_remove_vector = { { item_to_remove, static_cast<int>( count_present ) } };
you->consume_items( items_to_remove_vector );

} else {
percent += count_present / count_desired;
if( percent <= 1 ) {
d.actor( is_npc )->use_amount( item_to_remove, count_present );
add_msg_debug( debugmode::DF_TALKER,
"removing item: %s, count_desired: %f, count_present: %f, percent: %f, removing all",
item_to_remove.c_str(), count_desired, count_present, percent );
} else {
percent -= count_present / count_desired;
while( percent < 1.0f ) {
percent += 1 / count_desired;
d.actor( is_npc )->use_amount( item_to_remove, 1 );

add_msg_debug( debugmode::DF_TALKER,
"removing item: %s, count_desired: %f, count_present: %f, percent: %f, removing one by one",
item_to_remove.c_str(), count_desired, count_present, percent );
}
// too much items to consume, consuming only to hit 1.00 percent
percent -= count_present / amount_desired;
ratio = count_present / amount_desired;

while( percent < 1.0 ) {
percent += ratio / count_present;
++amount_to_remove;
}
items_to_remove_vector = { { item_to_remove, amount_to_remove } };
you->consume_items( items_to_remove_vector );
}
}
};
Expand Down

0 comments on commit 1302eac

Please sign in to comment.