Antkeeper  0.0.1
foraging-time-loader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
25 #include <engine/math/angles.hpp>
26 #include <engine/math/numbers.hpp>
27 #include <stdexcept>
28 
29 using namespace ::ant;
30 
31 static void deserialize_foraging_time_phene(phene::foraging_time& phene, const json& phene_element, resource_manager* resource_manager)
32 {
33  phene.min_solar_altitude = -math::half_pi<float>;
34  phene.max_solar_altitude = math::half_pi<float>;
35 
36  // Parse min solar altitude
37  if (auto element = phene_element.find("min_solar_altitude"); element != phene_element.end())
38  phene.min_solar_altitude = math::radians(element->get<float>());
39 
40  // Parse max solar altitude
41  if (auto element = phene_element.find("max_solar_altitude"); element != phene_element.end())
42  phene.max_solar_altitude = math::radians(element->get<float>());
43 }
44 
45 template <>
47 {
48  // Load JSON data
50 
51  // Validate gene file
52  auto foraging_time_element = data->find("foraging_time");
53  if (foraging_time_element == data->end())
54  throw std::runtime_error("Invalid foraging time gene.");
55 
56  // Allocate gene
58 
59  // Deserialize gene
60  gene::deserialize_gene(*foraging_time, &deserialize_foraging_time_phene, *foraging_time_element, resource_manager);
61 
62  // Free JSON data
63  delete data;
64 
65  return foraging_time;
66 }
static T * load(resource_manager *resourceManager, PHYSFS_File *file, const std::filesystem::path &path)
Loads resource data.
Loads resources.
nlohmann::json json
JSON data.
Definition: json.hpp:26
void deserialize_gene(monophenic_gene< T > &gene, void(*deserialize_phene)(T &, const json &, resource_manager *), const json &gene_element, resource_manager *resource_manager)
Deserializes a gene.
Definition: gene-loader.hpp:43
monophenic_gene< phene::foraging_time > foraging_time
Monophenic foraging time gene.
Definition: caste.hpp:25
constexpr T radians(T degrees) noexcept
Converts an angle given in degrees to radians.
Definition: angles.hpp:48