diff options
Diffstat (limited to 'graphs/cpp/caste_or_cast/provider.cc')
| -rw-r--r-- | graphs/cpp/caste_or_cast/provider.cc | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/graphs/cpp/caste_or_cast/provider.cc b/graphs/cpp/caste_or_cast/provider.cc new file mode 100644 index 0000000..5767743 --- /dev/null +++ b/graphs/cpp/caste_or_cast/provider.cc @@ -0,0 +1,47 @@ +// +// Created by martial.simon on 2/26/25. +// +#include "provider.hh" + +#include <iostream> + +#include "nurturer.hh" +#include "queen.hh" +void Provider::work() +{ + if (stage_ != DevelopmentStage::ADULT) + return; + food_level_ += food_stock_ - static_cast<int>(food_stock_); + food_stock_ -= food_stock_ - static_cast<int>(food_stock_); + harvestFood(); + Worker::work(); +} +void Provider::transferFood(Nurturer& nurturer) +{ + if (food_stock_ < 1) + return; + nurturer.increment_food_stock_by(static_cast<int>(food_stock_)); + food_stock_ -= static_cast<int>(food_stock_); + std::cout << "Transferring food.\n"; +} +void Provider::harvestFood() +{ + float harvest = luck_; + std::cout << "Harvested " << harvest << " food.\n"; + food_stock_ += harvest; + food_level_ -= (harvest - static_cast<int>(harvest)) * 0.5; +} +bool Provider::communicate(std::weak_ptr<Ant> wk_receiver) +{ + if (wk_receiver.lock() == nullptr) + return false; + if (!Ant::communicate(wk_receiver)) + return false; + std::cout << "Provider initiates communication.\n"; + auto p = dynamic_cast<Nurturer*>(wk_receiver.lock().get()); + if (p) + { + transferFood(*p); + } + return true; +}
\ No newline at end of file |
