From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/cpp/caste_or_cast/nurturer.cc | 98 ++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 graphs/cpp/caste_or_cast/nurturer.cc (limited to 'graphs/cpp/caste_or_cast/nurturer.cc') diff --git a/graphs/cpp/caste_or_cast/nurturer.cc b/graphs/cpp/caste_or_cast/nurturer.cc new file mode 100644 index 0000000..09faa79 --- /dev/null +++ b/graphs/cpp/caste_or_cast/nurturer.cc @@ -0,0 +1,98 @@ +#include "nurturer.hh" + +#include + +// The Colony class was forward declared in Ant header +// We need to include its header here so we know Colony implementation +#include "colony.hh" +#include "provider.hh" + +void Nurturer::feedLarvae() +{ + if (food_stock_ < 0.5) + return; + size_t count = 0; + std::shared_ptr colony = check_colony_access(); + for (auto i = colony->workers_.begin(); i < colony->workers_.end(); i++) + { + // if it doesn't have food anymore, can't feed more larvae + if (food_stock_ < 0.5) + break; + std::shared_ptr worker = *i; + // only feed if it is a larvae and if it is not already full (its + // food_level_ => 4) + if (DevelopmentStage::LARVA == worker->get_stage() + && worker->get_food_level() < 4) + { + worker->increment_food_level_by(0.5); + food_stock_ -= 0.5; + colony->cleanliness -= 0.3; + food_level_ -= 0.03; + count++; + } + } + std::cout << count << " larvae were fed by nurturer.\n"; +} + +void Nurturer::work() +{ + if (stage_ != DevelopmentStage::ADULT) + return; + if (food_stock_ > 0.5) + { + // eat before working + food_level_ += 0.5; + food_stock_ -= 0.5; + // complete its tasks + feedLarvae(); + feedQueen(); + cleanNest(); + } + else + // make the ant more hungry + food_level_ -= 0.042; + //! call base class work() method + Worker::work(); +} +bool Nurturer::communicate(std::weak_ptr wk_receiver) +{ + if (wk_receiver.lock() == nullptr) + return false; + if (!Ant::communicate(wk_receiver)) + return false; + std::cout << "Nurturer initiates communication.\n"; + auto p = dynamic_cast(wk_receiver.lock().get()); + if (p) + { + p->transferFood(*this); + } + return true; +} +void Nurturer::feedQueen() +{ + if (food_stock_ < 1) + return; + std::cout << "Feeding Queen.\n"; + auto c = check_colony_access(); + c->queen_->increment_food_level_by(1); + food_stock_--; + if (static_cast(c->queen_->get_food_level()) > 0 + && static_cast(c->queen_->get_food_level()) % 6 == 0) + c->queen_->layEgg(); + c->cleanliness -= .2; +} + +void Nurturer::cleanNest() +{ + std::shared_ptr colony = check_colony_access(); + if (colony->cleanliness < 100) + { + // clean the nest according to the luck of the Nurturer + std::cout << "Cleaning nest: gained " << luck_ << " of cleanliness.\n"; + auto cleanliness = colony->cleanliness + luck_; + colony->cleanliness = (cleanliness > 100) ? 100 : cleanliness; + } +} + +// FIXME : Implements communicate(std::weak_ptr) virtual overridden method +// and feedQueen() -- cgit v1.2.3