💿🐜 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.

106 lines
2.8 KiB

  1. /*
  2. * Copyright (C) 2017 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. #include "colony.hpp"
  20. #include "ant.hpp"
  21. #include "pheromone-matrix.hpp"
  22. #include "../configuration.hpp"
  23. Colony::Colony():
  24. antModel(nullptr),
  25. tripodGaitAnimation(nullptr)
  26. {
  27. Vector3 octreeMin = Vector3(-ANTKEEPER_TERRAIN_WIDTH, -ANTKEEPER_TERRAIN_BASE_HEIGHT, -ANTKEEPER_TERRAIN_DEPTH) * 0.5f - Vector3(ANTKEEPER_OCTREE_PADDING);
  28. Vector3 octreeMax = Vector3( ANTKEEPER_TERRAIN_WIDTH, ANTKEEPER_TERRAIN_BASE_HEIGHT, ANTKEEPER_TERRAIN_DEPTH) * 0.5f + Vector3(ANTKEEPER_OCTREE_PADDING);
  29. AABB octreeBounds(octreeMin, octreeMax);
  30. antOctree = new Octree<Agent*>(5, octreeBounds);
  31. // Create pheromone matrices
  32. homingMatrix = new PheromoneMatrix(PHEROMONE_MATRIX_COLUMNS, PHEROMONE_MATRIX_ROWS, WORLD_BOUNDS_MIN, WORLD_BOUNDS_MAX);
  33. recruitmentMatrix = new PheromoneMatrix(PHEROMONE_MATRIX_COLUMNS, PHEROMONE_MATRIX_ROWS, WORLD_BOUNDS_MIN, WORLD_BOUNDS_MAX);
  34. }
  35. Colony::~Colony()
  36. {
  37. killAll();
  38. delete antOctree;
  39. delete homingMatrix;
  40. delete recruitmentMatrix;
  41. }
  42. Ant* Colony::spawn(Navmesh* navmesh, Navmesh::Triangle* triangle, const Vector3& position)
  43. {
  44. // Allocate ant
  45. Ant* ant = new Ant(this);
  46. // Position it on the navmesh
  47. ant->setPosition(triangle, position);
  48. // Add ant to the colony
  49. ants.push_back(ant);
  50. return ant;
  51. }
  52. void Colony::update(float dt)
  53. {
  54. // Rebuild octree
  55. antOctree->clear();
  56. for (Ant* ant: ants)
  57. {
  58. antOctree->insert(ant->getModelInstance()->getBounds(), ant);
  59. }
  60. // Update ants
  61. for (Ant* ant: ants)
  62. {
  63. ant->update(dt);
  64. }
  65. }
  66. void Colony::setAntModel(Model* model)
  67. {
  68. this->antModel = model;
  69. // Find tripod gait animation
  70. tripodGaitAnimation = model->getSkeleton()->getAnimation("tripod-gait");
  71. if (!tripodGaitAnimation)
  72. {
  73. std::cerr << "Ant tripod gait animation not found" << std::endl;
  74. }
  75. }
  76. void Colony::queryAnts(const BoundingVolume& volume, std::list<Agent*>* results) const
  77. {
  78. antOctree->query(volume, results);
  79. }
  80. void Colony::killAll()
  81. {
  82. antOctree->clear();
  83. homingMatrix->clear();
  84. recruitmentMatrix->clear();
  85. for (Ant* ant: ants)
  86. {
  87. delete ant;
  88. }
  89. ants.clear();
  90. }