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

101 lines
2.5 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.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. pheromoneOctree = new Octree<Pheromone*>(5, octreeBounds);
  32. }
  33. Colony::~Colony()
  34. {
  35. killAll();
  36. delete antOctree;
  37. delete pheromoneOctree;
  38. }
  39. Ant* Colony::spawn(Navmesh* navmesh, Navmesh::Triangle* triangle, const Vector3& position)
  40. {
  41. // Allocate ant
  42. Ant* ant = new Ant(this);
  43. // Position it on the navmesh
  44. ant->setPosition(triangle, position);
  45. // Add ant to the colony
  46. ants.push_back(ant);
  47. return ant;
  48. }
  49. void Colony::update(float dt)
  50. {
  51. // Rebuild octree
  52. antOctree->clear();
  53. for (Ant* ant: ants)
  54. {
  55. antOctree->insert(ant->getModelInstance()->getBounds(), ant);
  56. }
  57. // Update ants
  58. for (Ant* ant: ants)
  59. {
  60. ant->update(dt);
  61. }
  62. }
  63. void Colony::setAntModel(Model* model)
  64. {
  65. this->antModel = model;
  66. // Find tripod gait animation
  67. tripodGaitAnimation = model->getSkeleton()->getAnimation("tripod-gait");
  68. if (!tripodGaitAnimation)
  69. {
  70. std::cerr << "Ant tripod gait animation not found" << std::endl;
  71. }
  72. }
  73. void Colony::queryAnts(const BoundingVolume& volume, std::list<Agent*>* results) const
  74. {
  75. antOctree->query(volume, results);
  76. }
  77. void Colony::killAll()
  78. {
  79. antOctree->clear();
  80. pheromoneOctree->clear();
  81. for (Ant* ant: ants)
  82. {
  83. delete ant;
  84. }
  85. ants.clear();
  86. }