Antkeeper  0.0.1
gene-loader.hpp
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 
20 #ifndef ANTKEEPER_GAME_ANT_GENE_GENE_LOADER_HPP
21 #define ANTKEEPER_GAME_ANT_GENE_GENE_LOADER_HPP
22 
27 
28 namespace ant {
29 namespace gene {
30 
42 template <class T>
43 void deserialize_gene(monophenic_gene<T>& gene, void (*deserialize_phene)(T&, const json&, resource_manager*), const json& gene_element, resource_manager* resource_manager)
44 {
45  // Read gene name
46  if (auto element = gene_element.find("name"); element != gene_element.end())
47  gene.name = element->get<std::string>();
48 
49  // Deserialize phene
50  if (auto element = gene_element.find("phene"); element != gene_element.end())
51  deserialize_phene(gene.phene, *element, resource_manager);
52 }
53 
54 template <class T>
55 void deserialize_gene(polyphenic_gene<T>& gene, void (*deserialize_phene)(T&, const json&, resource_manager*), const json& gene_element, resource_manager* resource_manager)
56 {
57  // Read gene name
58  if (auto element = gene_element.find("name"); element != gene_element.end())
59  gene.name = element->get<std::string>();
60 
61  // Deserialize phenes
62  if (auto phenes_element = gene_element.find("phenes"); phenes_element != gene_element.end())
63  {
64  if (auto element = phenes_element->find("female"); element != phenes_element->end())
65  {
66  deserialize_phene(gene.phenes[caste::queen], *element, resource_manager);
67  deserialize_phene(gene.phenes[caste::worker], *element, resource_manager);
68  deserialize_phene(gene.phenes[caste::soldier], *element, resource_manager);
69  }
70  if (auto element = phenes_element->find("male"); element != phenes_element->end())
71  deserialize_phene(gene.phenes[caste::male], *element, resource_manager);
72  if (auto element = phenes_element->find("queen"); element != phenes_element->end())
73  deserialize_phene(gene.phenes[caste::queen], *element, resource_manager);
74  if (auto element = phenes_element->find("worker"); element != phenes_element->end())
75  deserialize_phene(gene.phenes[caste::worker], *element, resource_manager);
76  if (auto element = phenes_element->find("soldier"); element != phenes_element->end())
77  deserialize_phene(gene.phenes[caste::soldier], *element, resource_manager);
78  }
79 }
81 
82 } // namespace gene
83 } // namespace ant
84 
85 #endif // ANTKEEPER_GAME_ANT_GENE_GENE_LOADER_HPP
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
Definition: caste.hpp:25
@ male
Male caste type.
@ queen
Queen caste type.
@ soldier
Soldier caste type.
@ worker
Worker caste type.
Gene with a single phene.
std::string name
Gene name.
Gene with caste-specific phenes.
std::unordered_map< caste, T > phenes
Caste-specific phene definitions.
std::string name
Gene name.