1
0
forked from 0ad/0ad
0ad/source/simulation2/helpers/Selection.h

135 lines
5.3 KiB
C++

/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. 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 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_SELECTION
#define INCLUDED_SELECTION
#include "ps/Profiler2.h"
#include "simulation2/helpers/Player.h"
#include "simulation2/Simulation2.h"
#include "simulation2/system/Entity.h"
#include "simulation2/system/IComponent.h"
#include <vector>
class CSimulation2;
class CCamera;
bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
namespace EntitySelection
{
/**
* Finds all selectable entities under the given screen coordinates.
*
* @param camera use this view to convert screen to world coordinates.
* @param screenX,screenY 2D screen coordinates.
* @param player player whose LOS will be used when selecting entities. In Atlas
* this value is ignored as the whole map is revealed.
* @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
* will be selected (including decorative actors), else only those selectable ingame.
* @param range Approximate range to check for entity in.
*
* @return ordered list of selected entities with the closest first.
*/
entity_id_t PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables);
/**
* Finds all selectable entities within the given screen coordinate rectangle,
* belonging to the given player. Used for bandboxing.
*
* @param camera use this view to convert screen to world coordinates.
* @param sx0,sy0,sx1,sy1 diagonally opposite corners of the rectangle in 2D screen coordinates.
* @param owner player whose entities we are selecting. Ownership is ignored if
* INVALID_PLAYER is used.
* @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
* will be selected (including decorative actors), else only those selectable ingame.
*
* @return unordered list of selected entities.
*/
std::vector<entity_id_t> PickEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, player_id_t owner, bool allowEditorSelectables);
/**
* Finds all selectable entities within the given screen coordinate rectangle,
* belonging to any given player (excluding Gaia). Used for status bars.
*/
std::vector<entity_id_t> PickNonGaiaEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
/**
* Finds all entities with a given component belonging to any given player.
*/
struct DefaultComponentFilter
{
bool operator()(IComponent* UNUSED(cmp))
{
return true;
}
};
template<typename Filter = DefaultComponentFilter>
std::vector<entity_id_t> GetEntitiesWithComponentInRect(CSimulation2& simulation, int cid, const CCamera& camera, int sx0, int sy0, int sx1, int sy1)
{
PROFILE2("GetEntitiesWithObstructionInRect");
// Make sure sx0 <= sx1, and sy0 <= sy1
if (sx0 > sx1)
std::swap(sx0, sx1);
if (sy0 > sy1)
std::swap(sy0, sy1);
std::vector<entity_id_t> hitEnts;
Filter filter;
const CSimulation2::InterfaceListUnordered& entities = simulation.GetEntitiesWithInterfaceUnordered(cid);
for (const std::pair<const entity_id_t, IComponent*>& item : entities)
{
if (!filter(item.second))
continue;
if (CheckEntityInRect(item.second->GetEntityHandle(), camera, sx0, sy0, sx1, sy1, false))
hitEnts.push_back(item.first);
}
return hitEnts;
}
/**
* Finds all entities with the given entity template name, belonging to the given player.
*
* @param camera use this view to convert screen to world coordinates.
* @param templateName the name of the template to match, or the selection group name
* for similar matching.
* @param owner player whose entities we are selecting. Ownership is ignored if
* INVALID_PLAYER is used.
* @param includeOffScreen if true, then all entities visible in the world will be selected,
* else only entities visible to the camera will be selected.
* @param matchRank if true, only entities that exactly match templateName will be selected,
* else entities with matching SelectionGroupName will be selected.
* @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
* will be selected (including decorative actors), else only those selectable in-game.
* @param allowFoundations if true, foundations are also included in the results. Only takes
* effect when matchRank = true.
*
* @return unordered list of selected entities.
* @see ICmpIdentity
*/
std::vector<entity_id_t> PickSimilarEntities(CSimulation2& simulation, const CCamera& camera, const std::string& templateName,
player_id_t owner, bool includeOffScreen, bool matchRank, bool allowEditorSelectables, bool allowFoundations);
} // namespace
#endif // INCLUDED_SELECTION