2014-10-14 13:11:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2021-09-11 13:52:34 +00:00
|
|
|
* Copyright (C) 2001-2021, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2015-08-22 14:40:02 +00:00
|
|
|
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
2014-10-14 13:11:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see http://gnu.org/licenses
|
|
|
|
*/
|
2013-03-31 08:21:22 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-20 18:02:40 +00:00
|
|
|
* \file object/object_manager.h
|
2014-12-20 18:09:53 +00:00
|
|
|
* \brief Object manager
|
2013-03-31 08:21:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/singleton.h"
|
|
|
|
|
2015-06-20 17:27:43 +00:00
|
|
|
#include "math/const.h"
|
|
|
|
#include "math/vector.h"
|
|
|
|
|
2015-07-22 10:45:50 +00:00
|
|
|
#include "object/object_create_params.h"
|
2015-08-07 18:48:55 +00:00
|
|
|
#include "object/object_interface_type.h"
|
2015-06-20 17:27:43 +00:00
|
|
|
#include "object/object_type.h"
|
2018-04-19 23:31:11 +00:00
|
|
|
|
2017-05-17 16:22:27 +00:00
|
|
|
#include "object/interface/destroyable_object.h"
|
2015-06-20 17:27:43 +00:00
|
|
|
|
2015-01-08 17:56:26 +00:00
|
|
|
#include <map>
|
2015-06-20 17:27:43 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
|
2015-08-02 09:40:47 +00:00
|
|
|
namespace Gfx
|
|
|
|
{
|
2015-06-20 17:27:43 +00:00
|
|
|
class CEngine;
|
2015-07-11 17:48:37 +00:00
|
|
|
class CModelManager;
|
2015-07-09 16:45:02 +00:00
|
|
|
class COldModelManager;
|
2015-06-20 17:27:43 +00:00
|
|
|
class CParticle;
|
|
|
|
class CTerrain;
|
|
|
|
} // namespace Gfx
|
|
|
|
|
|
|
|
class CObject;
|
|
|
|
class CObjectFactory;
|
|
|
|
|
|
|
|
enum RadarFilter
|
|
|
|
{
|
2015-07-08 17:05:11 +00:00
|
|
|
FILTER_NONE = 0,
|
2015-07-07 16:54:36 +00:00
|
|
|
|
2015-07-08 17:05:11 +00:00
|
|
|
FILTER_ONLYLANDING = 1 << (8+0),
|
|
|
|
FILTER_ONLYFLYING = 1 << (8+1),
|
2015-07-07 16:54:36 +00:00
|
|
|
|
2015-07-08 17:05:11 +00:00
|
|
|
FILTER_FRIENDLY = 1 << (8+2),
|
|
|
|
FILTER_ENEMY = 1 << (8+3),
|
|
|
|
FILTER_NEUTRAL = 1 << (8+4),
|
2015-06-20 17:27:43 +00:00
|
|
|
};
|
|
|
|
|
2015-06-21 09:16:09 +00:00
|
|
|
using CObjectMap = std::map<int, std::unique_ptr<CObject>>;
|
2015-06-21 14:22:09 +00:00
|
|
|
using CObjectMapCIt = std::map<int, std::unique_ptr<CObject>>::const_iterator;
|
|
|
|
|
|
|
|
class CObjectIteratorProxy
|
|
|
|
{
|
2015-09-06 11:23:20 +00:00
|
|
|
private:
|
|
|
|
friend class CObjectContainerProxy;
|
|
|
|
|
|
|
|
CObjectIteratorProxy(CObjectMapCIt currentIt, CObjectMapCIt endIt)
|
|
|
|
: m_currentIt(currentIt)
|
|
|
|
, m_endIt(endIt)
|
2015-09-06 14:29:21 +00:00
|
|
|
{
|
|
|
|
while (m_currentIt != m_endIt && m_currentIt->second == nullptr)
|
|
|
|
{
|
|
|
|
++m_currentIt;
|
|
|
|
}
|
|
|
|
}
|
2015-09-06 11:23:20 +00:00
|
|
|
|
2015-06-21 14:22:09 +00:00
|
|
|
public:
|
2015-09-06 11:23:20 +00:00
|
|
|
CObject* operator*()
|
2015-06-21 14:22:09 +00:00
|
|
|
{
|
2015-07-12 11:16:07 +00:00
|
|
|
return m_currentIt->second.get();
|
2015-06-21 14:22:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 11:23:20 +00:00
|
|
|
void operator++()
|
2015-06-21 14:22:09 +00:00
|
|
|
{
|
2015-07-12 11:16:07 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
++m_currentIt;
|
2015-08-02 09:40:47 +00:00
|
|
|
}
|
|
|
|
while (m_currentIt != m_endIt && m_currentIt->second == nullptr);
|
2015-06-21 14:22:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 18:38:25 +00:00
|
|
|
bool operator==(const CObjectIteratorProxy& other)
|
|
|
|
{
|
|
|
|
return m_currentIt == other.m_currentIt;
|
|
|
|
}
|
|
|
|
|
2015-09-06 11:23:20 +00:00
|
|
|
bool operator!=(const CObjectIteratorProxy& other)
|
2015-06-21 14:22:09 +00:00
|
|
|
{
|
2015-07-12 11:16:07 +00:00
|
|
|
return m_currentIt != other.m_currentIt;
|
2015-06-21 14:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-07-12 11:16:07 +00:00
|
|
|
CObjectMapCIt m_currentIt;
|
|
|
|
CObjectMapCIt m_endIt;
|
2015-06-21 14:22:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CObjectContainerProxy
|
|
|
|
{
|
2015-07-12 08:34:00 +00:00
|
|
|
private:
|
|
|
|
friend class CObjectManager;
|
|
|
|
|
2015-09-06 11:23:20 +00:00
|
|
|
CObjectContainerProxy(const CObjectMap& map, int& activeIteratorsCounter)
|
|
|
|
: m_map(map),
|
|
|
|
m_activeIteratorsCounter(activeIteratorsCounter)
|
|
|
|
{
|
|
|
|
++m_activeIteratorsCounter;
|
|
|
|
}
|
2015-07-12 08:34:00 +00:00
|
|
|
|
2015-06-21 14:22:09 +00:00
|
|
|
public:
|
2015-09-06 11:23:20 +00:00
|
|
|
~CObjectContainerProxy()
|
|
|
|
{
|
|
|
|
--m_activeIteratorsCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
CObjectIteratorProxy begin() const
|
2015-06-21 14:22:09 +00:00
|
|
|
{
|
2015-07-12 11:16:07 +00:00
|
|
|
return CObjectIteratorProxy(m_map.begin(), m_map.end());
|
2015-06-21 14:22:09 +00:00
|
|
|
}
|
2015-09-06 11:23:20 +00:00
|
|
|
CObjectIteratorProxy end() const
|
2015-06-21 14:22:09 +00:00
|
|
|
{
|
2015-07-12 11:16:07 +00:00
|
|
|
return CObjectIteratorProxy(m_map.end(), m_map.end());
|
2015-06-21 14:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CObjectMap& m_map;
|
2015-09-06 11:23:20 +00:00
|
|
|
int& m_activeIteratorsCounter;
|
2015-06-21 14:22:09 +00:00
|
|
|
};
|
2013-03-31 08:21:22 +00:00
|
|
|
|
|
|
|
/**
|
2015-07-07 14:17:19 +00:00
|
|
|
* \class CObjectManager
|
2014-12-20 18:09:53 +00:00
|
|
|
* \brief Manages CObject instances
|
2013-03-31 08:21:22 +00:00
|
|
|
*/
|
|
|
|
class CObjectManager : public CSingleton<CObjectManager>
|
|
|
|
{
|
|
|
|
public:
|
2015-06-20 17:27:43 +00:00
|
|
|
CObjectManager(Gfx::CEngine* engine,
|
|
|
|
Gfx::CTerrain* terrain,
|
2015-07-11 17:48:37 +00:00
|
|
|
Gfx::COldModelManager* oldModelManager,
|
|
|
|
Gfx::CModelManager* modelManager,
|
2015-07-10 17:53:32 +00:00
|
|
|
Gfx::CParticle* particle);
|
2013-03-31 08:21:22 +00:00
|
|
|
virtual ~CObjectManager();
|
|
|
|
|
2015-06-21 09:16:09 +00:00
|
|
|
//! Creates an object
|
2015-07-22 10:45:50 +00:00
|
|
|
//@{
|
|
|
|
CObject* CreateObject(ObjectCreateParams params);
|
2021-12-28 22:01:30 +00:00
|
|
|
CObject* CreateObject(glm::vec3 pos, float angle, ObjectType type, float power = -1.0f);
|
2015-07-22 10:45:50 +00:00
|
|
|
//@}
|
2015-06-21 09:16:09 +00:00
|
|
|
|
|
|
|
//! Deletes the object
|
2014-12-20 18:09:53 +00:00
|
|
|
bool DeleteObject(CObject* instance);
|
2015-06-21 09:16:09 +00:00
|
|
|
//! Deletes all objects
|
|
|
|
void DeleteAllObjects();
|
|
|
|
|
2014-12-21 16:06:55 +00:00
|
|
|
//! Finds object by id (CObject::GetID())
|
|
|
|
CObject* GetObjectById(unsigned int id);
|
2015-06-21 09:16:09 +00:00
|
|
|
|
2015-06-21 14:22:09 +00:00
|
|
|
//! Gets object by id in range <0; number of objects - 1>
|
2014-12-21 16:06:55 +00:00
|
|
|
CObject* GetObjectByRank(unsigned int id);
|
2015-06-21 09:16:09 +00:00
|
|
|
|
2015-06-26 20:07:55 +00:00
|
|
|
//! Gets all objects of given team
|
|
|
|
std::vector<CObject*> GetObjectsOfTeam(int team);
|
|
|
|
|
2015-07-07 15:48:49 +00:00
|
|
|
//! Checks if any of team's objects exist
|
|
|
|
bool TeamExists(int team);
|
|
|
|
|
|
|
|
//! Destroy all objects of team
|
|
|
|
// TODO: This should be probably moved to separate class
|
2017-05-17 16:22:27 +00:00
|
|
|
void DestroyTeam(int team, DestructionType destructionType = DestructionType::Explosion);
|
2015-07-07 15:48:49 +00:00
|
|
|
|
2015-08-07 18:48:55 +00:00
|
|
|
//! Counts all objects implementing given interface
|
|
|
|
int CountObjectsImplementing(ObjectInterfaceType interface);
|
|
|
|
|
2014-12-20 19:36:09 +00:00
|
|
|
//! Returns all objects
|
2015-09-06 11:23:20 +00:00
|
|
|
CObjectContainerProxy GetAllObjects()
|
2015-06-21 09:16:09 +00:00
|
|
|
{
|
2015-09-06 11:23:20 +00:00
|
|
|
CleanRemovedObjectsIfNeeded();
|
|
|
|
return CObjectContainerProxy(m_objects, m_activeObjectIterators);
|
2015-06-21 09:16:09 +00:00
|
|
|
}
|
2015-06-20 17:27:43 +00:00
|
|
|
|
2015-11-22 13:10:45 +00:00
|
|
|
//! Finds an object, like radar() in CBot
|
|
|
|
//@{
|
|
|
|
std::vector<CObject*> RadarAll(CObject* pThis,
|
|
|
|
ObjectType type = OBJECT_NULL,
|
|
|
|
float angle = 0.0f,
|
|
|
|
float focus = Math::PI*2.0f,
|
|
|
|
float minDist = 0.0f,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool furthest = false,
|
|
|
|
RadarFilter filter = FILTER_NONE,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
std::vector<CObject*> RadarAll(CObject* pThis,
|
|
|
|
std::vector<ObjectType> type = std::vector<ObjectType>(),
|
|
|
|
float angle = 0.0f,
|
|
|
|
float focus = Math::PI*2.0f,
|
|
|
|
float minDist = 0.0f,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool furthest = false,
|
|
|
|
RadarFilter filter = FILTER_NONE,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
std::vector<CObject*> RadarAll(CObject* pThis,
|
2021-12-28 22:01:30 +00:00
|
|
|
glm::vec3 thisPosition,
|
2015-11-22 13:10:45 +00:00
|
|
|
float thisAngle,
|
|
|
|
ObjectType type = OBJECT_NULL,
|
|
|
|
float angle = 0.0f,
|
|
|
|
float focus = Math::PI*2.0f,
|
|
|
|
float minDist = 0.0f,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool furthest = false,
|
|
|
|
RadarFilter filter = FILTER_NONE,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
std::vector<CObject*> RadarAll(CObject* pThis,
|
2021-12-28 22:01:30 +00:00
|
|
|
glm::vec3 thisPosition,
|
2015-11-22 13:10:45 +00:00
|
|
|
float thisAngle,
|
|
|
|
std::vector<ObjectType> type = std::vector<ObjectType>(),
|
|
|
|
float angle = 0.0f,
|
|
|
|
float focus = Math::PI*2.0f,
|
|
|
|
float minDist = 0.0f,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool furthest = false,
|
|
|
|
RadarFilter filter = FILTER_NONE,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
//@}
|
2014-12-20 19:36:09 +00:00
|
|
|
//! Finds an object, like radar() in CBot
|
|
|
|
//@{
|
2015-06-21 09:16:09 +00:00
|
|
|
CObject* Radar(CObject* pThis,
|
|
|
|
ObjectType type = OBJECT_NULL,
|
|
|
|
float angle = 0.0f,
|
|
|
|
float focus = Math::PI*2.0f,
|
|
|
|
float minDist = 0.0f,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool furthest = false,
|
|
|
|
RadarFilter filter = FILTER_NONE,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
CObject* Radar(CObject* pThis,
|
|
|
|
std::vector<ObjectType> type = std::vector<ObjectType>(),
|
|
|
|
float angle = 0.0f,
|
|
|
|
float focus = Math::PI*2.0f,
|
|
|
|
float minDist = 0.0f,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool furthest = false,
|
|
|
|
RadarFilter filter = FILTER_NONE,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
CObject* Radar(CObject* pThis,
|
2021-12-28 22:01:30 +00:00
|
|
|
glm::vec3 thisPosition,
|
2015-06-21 09:16:09 +00:00
|
|
|
float thisAngle,
|
|
|
|
ObjectType type = OBJECT_NULL,
|
|
|
|
float angle = 0.0f,
|
|
|
|
float focus = Math::PI*2.0f,
|
|
|
|
float minDist = 0.0f,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool furthest = false,
|
|
|
|
RadarFilter filter = FILTER_NONE,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
CObject* Radar(CObject* pThis,
|
2021-12-28 22:01:30 +00:00
|
|
|
glm::vec3 thisPosition,
|
2015-06-21 09:16:09 +00:00
|
|
|
float thisAngle,
|
|
|
|
std::vector<ObjectType> type = std::vector<ObjectType>(),
|
|
|
|
float angle = 0.0f,
|
|
|
|
float focus = Math::PI*2.0f,
|
|
|
|
float minDist = 0.0f,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool furthest = false,
|
|
|
|
RadarFilter filter = FILTER_NONE,
|
|
|
|
bool cbotTypes = false);
|
2014-12-20 19:36:09 +00:00
|
|
|
//@}
|
2014-12-21 16:06:55 +00:00
|
|
|
//! Returns nearest object that's closer than maxDist
|
|
|
|
//@{
|
2015-06-21 09:16:09 +00:00
|
|
|
CObject* FindNearest(CObject* pThis,
|
|
|
|
ObjectType type = OBJECT_NULL,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
CObject* FindNearest(CObject* pThis,
|
|
|
|
std::vector<ObjectType> type = std::vector<ObjectType>(),
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
CObject* FindNearest(CObject* pThis,
|
2021-12-28 22:01:30 +00:00
|
|
|
glm::vec3 thisPosition,
|
2015-06-21 09:16:09 +00:00
|
|
|
ObjectType type = OBJECT_NULL,
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool cbotTypes = false);
|
|
|
|
CObject* FindNearest(CObject* pThis,
|
2021-12-28 22:01:30 +00:00
|
|
|
glm::vec3 thisPosition,
|
2015-06-21 09:16:09 +00:00
|
|
|
std::vector<ObjectType> type = std::vector<ObjectType>(),
|
|
|
|
float maxDist = 1000.0f,
|
|
|
|
bool cbotTypes = false);
|
2014-12-21 16:06:55 +00:00
|
|
|
//@}
|
2013-03-31 08:21:22 +00:00
|
|
|
|
2015-07-12 11:16:07 +00:00
|
|
|
private:
|
2020-07-20 19:55:44 +00:00
|
|
|
//! Prevents creation of overcharged power cells
|
|
|
|
float ClampPower(ObjectType type, float power);
|
2015-09-06 11:23:20 +00:00
|
|
|
void CleanRemovedObjectsIfNeeded();
|
2015-07-12 11:16:07 +00:00
|
|
|
|
|
|
|
private:
|
2015-06-21 14:22:09 +00:00
|
|
|
CObjectMap m_objects;
|
2015-06-20 17:27:43 +00:00
|
|
|
std::unique_ptr<CObjectFactory> m_objectFactory;
|
2015-06-21 09:16:09 +00:00
|
|
|
int m_nextId;
|
2015-09-06 11:23:20 +00:00
|
|
|
int m_activeObjectIterators;
|
2015-07-12 11:16:07 +00:00
|
|
|
bool m_shouldCleanRemovedObjects;
|
2013-03-31 08:21:22 +00:00
|
|
|
};
|