Permalink
Browse files

Thirst is working, part 1 before rooms and water.

  • Loading branch information...
thebracket committed Apr 14, 2018
1 parent f2ccba9 commit 27488d8241f75a3023cd86ee915e9165f4f070aa
@@ -261,6 +261,7 @@
<ClCompile Include="..\..\src\systems\ai\sentient_ai_system.cpp" />
<ClCompile Include="..\..\src\systems\ai\settler\ai_deconstruct.cpp" />
<ClCompile Include="..\..\src\systems\ai\settler\ai_idle_time.cpp" />
<ClCompile Include="..\..\src\systems\ai\settler\ai_leisure_drink.cpp" />
<ClCompile Include="..\..\src\systems\ai\settler\ai_leisure_eat.cpp" />
<ClCompile Include="..\..\src\systems\ai\settler\ai_leisure_time.cpp" />
<ClCompile Include="..\..\src\systems\ai\settler\ai_new_arrival.cpp" />
@@ -350,6 +351,7 @@
<ItemGroup>
<ClInclude Include="..\..\src\components\ai_tags\ai_mode_idle.hpp" />
<ClInclude Include="..\..\src\components\ai_tags\ai_settler_new_arrival.hpp" />
<ClInclude Include="..\..\src\components\ai_tags\ai_tag_leisure_drink.hpp" />
<ClInclude Include="..\..\src\components\ai_tags\ai_tag_leisure_eat.hpp" />
<ClInclude Include="..\..\src\components\ai_tags\ai_tag_leisure_shift.hpp" />
<ClInclude Include="..\..\src\components\ai_tags\ai_tag_my_turn.hpp" />
@@ -459,6 +461,7 @@
<ClInclude Include="..\..\src\components\species.hpp" />
<ClInclude Include="..\..\src\components\stockpile.hpp" />
<ClInclude Include="..\..\src\components\buildings\turret_t.hpp" />
<ClInclude Include="..\..\src\components\thirst_t.hpp" />
<ClInclude Include="..\..\src\components\viewshed.hpp" />
<ClInclude Include="..\..\src\components\water_spawner.hpp" />
<ClInclude Include="..\..\src\components\wildlife_group.hpp" />
@@ -500,6 +503,7 @@
<ClInclude Include="..\..\src\systems\ai\settler\ai_deconstruct.hpp" />
<ClInclude Include="..\..\src\systems\ai\settler\ai_idle.hpp" />
<ClInclude Include="..\..\src\systems\ai\settler\ai_idle_time.hpp" />
<ClInclude Include="..\..\src\systems\ai\settler\ai_leisure_drink.hpp" />
<ClInclude Include="..\..\src\systems\ai\settler\ai_leisure_eat.hpp" />
<ClInclude Include="..\..\src\systems\ai\settler\ai_leisure_time.hpp" />
<ClInclude Include="..\..\src\systems\ai\settler\ai_new_arrival.hpp" />
@@ -503,6 +503,9 @@
<ClCompile Include="..\..\src\systems\ai\settler\ai_leisure_eat.cpp">
<Filter>Source Files\systems\ai\settler</Filter>
</ClCompile>
<ClCompile Include="..\..\src\systems\ai\settler\ai_leisure_drink.cpp">
<Filter>Source Files\systems\ai\settler</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\components\calendar.hpp">
@@ -1237,5 +1240,14 @@
<ClInclude Include="..\..\src\components\ai_tags\ai_tag_leisure_eat.hpp">
<Filter>Header Files\components\ai_tags</Filter>
</ClInclude>
<ClInclude Include="..\..\src\components\thirst_t.hpp">
<Filter>Header Files\components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\components\ai_tags\ai_tag_leisure_drink.hpp">
<Filter>Header Files\components\ai_tags</Filter>
</ClInclude>
<ClInclude Include="..\..\src\systems\ai\settler\ai_leisure_drink.hpp">
<Filter>Header Files\systems\ai\settler</Filter>
</ClInclude>
</ItemGroup>
</Project>
@@ -0,0 +1,11 @@
#pragma once
struct ai_tag_leisure_drink_t {
enum drink_steps { FIND_DRINK, GOTO_DRINK, CONSUME_DRINK };
drink_steps step = FIND_DRINK;
std::size_t tool_id = 0;
std::shared_ptr<navigation_path_t> current_path; // Not serialized
};
@@ -112,6 +112,8 @@
#include "buildings/construct_provides_support.hpp"
#include "hunger_t.hpp"
#include "ai_tags/ai_tag_leisure_eat.hpp"
#include "ai_tags/ai_tag_leisure_drink.hpp"
#include "thirst_t.hpp"
namespace bengine {
template<class ... Components>
@@ -134,7 +136,7 @@ namespace bengine {
item_topsoil_t, item_t, item_carried_t, item_creator_t, item_quality_t, item_stored_t, item_wear_t, designated_miner_t,
mining_designations_t, ai_tag_work_stockpiles_t, item_food_prepared_t, flying_t, ai_tag_work_deconstruct, sends_signal_t,
signal_processor_t, float_gauge_t, pressure_plate_t, proximity_sensor_t, oscillator_t, node_graph_position_t, construct_support_t,
hunger_t, ai_tag_leisure_eat_t
hunger_t, ai_tag_leisure_eat_t, thirst_t, ai_tag_leisure_drink_t
>;
}
}
@@ -0,0 +1,7 @@
#pragma once
struct thirst_t {
int thirst_clock = 24 * 60; // 24 hours in minutes
bool is_thirsty = false;
bool is_dehydrating = false;
};
@@ -83,6 +83,13 @@ void serialize(Archive & archive, ai_tag_leisure_eat_t &tag)
archive(tag.tool_id, tag.step);
}
template<class Archive>
void serialize(Archive & archive, ai_tag_leisure_drink_t &tag)
{
// Nothing to save
archive(tag.tool_id, tag.step);
}
template<class Archive>
void serialize(Archive & archive, ai_tag_my_turn_t &tag)
{
@@ -413,6 +420,12 @@ void serialize(Archive & archive, hunger_t &h)
archive(h.hunger_clock); // serialize things by passing them to the archive
}
template<class Archive>
void serialize(Archive & archive, thirst_t &h)
{
archive(h.thirst_clock); // serialize things by passing them to the archive
}
template<class Archive>
void serialize(Archive & archive, initiative_t &i)
{
@@ -264,7 +264,8 @@ void create_settler(planet_t &planet, const int x, const int y, const int z, ben
->assign(initiative_t{})
->assign(ai_settler_new_arrival_t{})
->assign(sleep_clock_t{})
->assign(hunger_t{});
->assign(hunger_t{})
->assign(thirst_t{});
// Create clothing items
//std::cout << settler->id << "\n";
@@ -25,6 +25,12 @@ namespace systems {
{
damage_system::inflict_damage(damage_system::inflict_damage_message{e.id, 1, "Starvation"});
}
const auto thirst = e.component<thirst_t>();
if (thirst && thirst->is_dehydrating)
{
damage_system::inflict_damage(damage_system::inflict_damage_message{ e.id, 1, "Dehydration" });
}
});
}
}
@@ -0,0 +1,98 @@
#include "ai_leisure_drink.hpp"
#include "templated_idle_steps_t.hpp"
#include "../../gui/log_system.hpp"
namespace systems {
namespace ai_leisure_drink {
using namespace bengine;
using namespace jobs_board;
using namespace region;
namespace jobs_board {
void evaluate_thirst(job_board_t &board, entity_t &e, position_t &pos, job_evaluator_base_t *jt) {
const auto thirst = e.component<thirst_t>();
if (!thirst) return;
if (!thirst->is_thirsty) return;
if (!inventory::is_item_category_available<item_drink_t>()) return;
board.insert(std::make_pair(2, jt)); // Eating is a high priority!
}
}
static const char * job_tag = "Drinking";
static work::templated_idle_steps_t<ai_tag_leisure_drink_t> work;
inline void find_food(entity_t &e, ai_tag_leisure_drink_t &h, ai_tag_my_turn_t &t, position_t &pos) {
h.current_path.reset();
const auto food_id = inventory::find_closest_unclaimed_item_by_category_and_claim_it_immediately<item_drink_t>(e.id, pos);
if (food_id > 0) {
const auto food_pos = inventory::get_item_location(food_id);
if (!food_pos) {
inventory::unclaim_by_id(food_id);
work.cancel_work_tag(e);
}
else {
h.tool_id = food_id;
h.current_path = find_path(pos, *food_pos);
if (!h.current_path->success) {
inventory::unclaim_by_id(food_id);
work.cancel_work_tag(e);
}
else {
h.step = ai_tag_leisure_drink_t::drink_steps::GOTO_DRINK;
}
}
}
else {
work.cancel_work_tag(e);
}
}
inline void goto_food(entity_t &e, ai_tag_leisure_drink_t &h, ai_tag_my_turn_t &t, position_t &pos) {
work.follow_path(h, pos, e, [&h]() {
// Cancel
h.current_path.reset();
h.step = ai_tag_leisure_drink_t::drink_steps::FIND_DRINK;
inventory::unclaim_by_id(h.tool_id);
return;
}, [&h, &e]() {
// Success
h.current_path.reset();
h.step = ai_tag_leisure_drink_t::drink_steps::CONSUME_DRINK;
inventory_system::pickup_item(h.tool_id, e.id);
return;
});
}
inline void eat_food(entity_t &e, ai_tag_leisure_drink_t &h, ai_tag_my_turn_t &t, position_t &pos) {
work.cancel_work_tag(e);
const auto hunger = e.component<thirst_t>();
if (hunger) {
hunger->is_thirsty = false;
hunger->thirst_clock = 24 * 60;
}
const auto food_entity = entity(h.tool_id);
if (food_entity) {
const auto name = food_entity->component<item_t>();
if (name) {
logging::log_message lmsg{ LOG{}.settler_name(e.id)->text(std::string(" drinks "))->text(name->item_name)->chars };
logging::log(lmsg);
}
inventory::delete_item(h.tool_id);
}
}
void dispatch(entity_t &e, ai_tag_leisure_drink_t &h, ai_tag_my_turn_t &t, position_t &pos) {
switch (h.step) {
case ai_tag_leisure_eat_t::eat_steps::FIND_FOOD: find_food(e, h, t, pos); break;
case ai_tag_leisure_eat_t::eat_steps::GOTO_FOOD: goto_food(e, h, t, pos); break;
case ai_tag_leisure_eat_t::eat_steps::EAT_FOOD: eat_food(e, h, t, pos); break;
}
}
void run(const double &duration_ms)
{
work.do_idle(jobs_board::evaluate_thirst, dispatch, job_tag);
}
}
}
@@ -0,0 +1,7 @@
#pragma once
namespace systems {
namespace ai_leisure_drink {
void run(const double &duration_ms);
}
}
@@ -90,6 +90,7 @@
#include "gui/building_info.hpp"
#include "scheduler/hunger_system.hpp"
#include "ai/settler/ai_leisure_eat.hpp"
#include "ai/settler/ai_leisure_drink.hpp"
namespace systems {
constexpr int CAMERA_SYSTEM = 1;
@@ -175,6 +176,7 @@ namespace systems {
constexpr int BUILDING_INFO_SYS = 81;
constexpr int HUNGER_SYSTEM = 82;
constexpr int EAT_SYSTEM = 83;
constexpr int DRINK_SYSTEM = 84;
boost::container::flat_map<int, std::pair<int, std::vector<float>>> run_time;
boost::container::flat_map<int, std::string> system_names;
@@ -281,6 +283,7 @@ namespace systems {
system_names[BUILDING_INFO_SYS] = "Building Info";
system_names[HUNGER_SYSTEM] = "Hunger System";
system_names[EAT_SYSTEM] = "Eating System";
system_names[DRINK_SYSTEM] = "Drinking System";
game_master_mode = PLAY;
}
@@ -368,6 +371,7 @@ namespace systems {
run_system(ai_work_stockpiles::run, duration_ms, AI_STOCKPILE_SYSTEM);
run_system(ai_deconstruction::run, duration_ms, AI_DECONSTRUCT_SYSTEM);
run_system(ai_leisure_eat::run, duration_ms, EAT_SYSTEM);
run_system(ai_leisure_drink::run, duration_ms, DRINK_SYSTEM);
run_system(ai_idle_time::run, duration_ms, AI_IDLE);
run_system(movement::run, duration_ms, MOVEMENT_SYSTEM);
run_system(triggers::run, duration_ms, TRIGGER_SYSTEM);
@@ -12,6 +12,13 @@ namespace systems {
if (h.hunger_clock < 60) h.is_hungry = true;
if (h.hunger_clock < -(24 * 60 * 3)) h.is_starving = true;
});
bengine::each<thirst_t>([](bengine::entity_t &e, thirst_t &h)
{
--h.thirst_clock;
if (h.thirst_clock < 60) h.is_thirsty = true;
if (h.thirst_clock < -(24 * 60 * 3)) h.is_dehydrating = true;
});
}
}
}

0 comments on commit 27488d8

Please sign in to comment.