💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.4 KiB

  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. #ifndef ANTKEEPER_GAME_ANT_GENE_GENE_LOADER_HPP
  20. #define ANTKEEPER_GAME_ANT_GENE_GENE_LOADER_HPP
  21. #include "game/ant/gene/monophenic-gene.hpp"
  22. #include "game/ant/gene/polyphenic-gene.hpp"
  23. #include "resources/json.hpp"
  24. #include "resources/resource-manager.hpp"
  25. namespace game {
  26. namespace ant {
  27. namespace gene {
  28. /**
  29. * Deserializes a gene.
  30. *
  31. * @tparam T Phene type.
  32. *
  33. * @param gene Gene to deserialize.
  34. * @param deserialize_phene Phene deserialization function.
  35. * @param gene_element JSON element containing a gene definition.
  36. * @param resource_manager Resource manager pointer.
  37. */
  38. /// @{
  39. template <class T>
  40. void deserialize_gene(monophenic_gene<T>& gene, void (*deserialize_phene)(T&, const json&, resource_manager*), const json& gene_element, resource_manager* resource_manager)
  41. {
  42. // Read gene name
  43. if (auto element = gene_element.find("name"); element != gene_element.end())
  44. gene.name = element->get<std::string>();
  45. // Deserialize phene
  46. if (auto element = gene_element.find("phene"); element != gene_element.end())
  47. deserialize_phene(gene.phene, *element, resource_manager);
  48. }
  49. template <class T>
  50. void deserialize_gene(polyphenic_gene<T>& gene, void (*deserialize_phene)(T&, const json&, resource_manager*), const json& gene_element, resource_manager* resource_manager)
  51. {
  52. // Read gene name
  53. if (auto element = gene_element.find("name"); element != gene_element.end())
  54. gene.name = element->get<std::string>();
  55. // Deserialize phenes
  56. if (auto phenes_element = gene_element.find("phenes"); phenes_element != gene_element.end())
  57. {
  58. if (auto element = phenes_element->find("female"); element != phenes_element->end())
  59. {
  60. deserialize_phene(gene.phenes[caste::queen], *element, resource_manager);
  61. deserialize_phene(gene.phenes[caste::worker], *element, resource_manager);
  62. deserialize_phene(gene.phenes[caste::soldier], *element, resource_manager);
  63. }
  64. if (auto element = phenes_element->find("male"); element != phenes_element->end())
  65. deserialize_phene(gene.phenes[caste::male], *element, resource_manager);
  66. if (auto element = phenes_element->find("queen"); element != phenes_element->end())
  67. deserialize_phene(gene.phenes[caste::queen], *element, resource_manager);
  68. if (auto element = phenes_element->find("worker"); element != phenes_element->end())
  69. deserialize_phene(gene.phenes[caste::worker], *element, resource_manager);
  70. if (auto element = phenes_element->find("soldier"); element != phenes_element->end())
  71. deserialize_phene(gene.phenes[caste::soldier], *element, resource_manager);
  72. }
  73. }
  74. /// @}
  75. } // namespace gene
  76. } // namespace ant
  77. } // namespace game
  78. #endif // ANTKEEPER_GAME_ANT_GENE_GENE_LOADER_HPP