Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Selection.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_SELECTION
19#define INCLUDED_SELECTION
20
21#include "ps/Profiler2.h"
26
27#include <string>
28#include <utility>
29#include <vector>
30
31class CCamera;
32
33bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
34
36{
37
38/**
39 * Finds all selectable entities under the given screen coordinates.
40 *
41 * @param camera use this view to convert screen to world coordinates.
42 * @param screenX,screenY 2D screen coordinates.
43 * @param player player whose LOS will be used when selecting entities. In Atlas
44 * this value is ignored as the whole map is revealed.
45 * @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
46 * will be selected (including decorative actors), else only those selectable ingame.
47 * @param range Approximate range to check for entity in.
48 *
49 * @return ordered list of selected entities with the closest first.
50 */
51entity_id_t PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables);
52
53/**
54 * Finds all selectable entities within the given screen coordinate rectangle,
55 * belonging to the given player. Used for bandboxing.
56 *
57 * @param camera use this view to convert screen to world coordinates.
58 * @param sx0,sy0,sx1,sy1 diagonally opposite corners of the rectangle in 2D screen coordinates.
59 * @param owner player whose entities we are selecting. Ownership is ignored if
60 * INVALID_PLAYER is used.
61 * @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
62 * will be selected (including decorative actors), else only those selectable ingame.
63 *
64 * @return unordered list of selected entities.
65 */
66std::vector<entity_id_t> PickEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, player_id_t owner, bool allowEditorSelectables);
67
68/**
69 * Finds all selectable entities within the given screen coordinate rectangle,
70 * belonging to any given player (excluding Gaia). Used for status bars.
71 */
72std::vector<entity_id_t> PickNonGaiaEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
73
74/**
75 * Finds all entities with a given component belonging to any given player.
76 */
78{
80 {
81 return true;
82 }
83};
84template<typename Filter = DefaultComponentFilter>
85std::vector<entity_id_t> GetEntitiesWithComponentInRect(CSimulation2& simulation, int cid, const CCamera& camera, int sx0, int sy0, int sx1, int sy1)
86{
87 PROFILE2("GetEntitiesWithObstructionInRect");
88
89 // Make sure sx0 <= sx1, and sy0 <= sy1
90 if (sx0 > sx1)
91 std::swap(sx0, sx1);
92 if (sy0 > sy1)
93 std::swap(sy0, sy1);
94
95 std::vector<entity_id_t> hitEnts;
96
97 Filter filter;
99 for (const std::pair<const entity_id_t, IComponent*>& item : entities)
100 {
101 if (!filter(item.second))
102 continue;
103 if (CheckEntityInRect(item.second->GetEntityHandle(), camera, sx0, sy0, sx1, sy1, false))
104 hitEnts.push_back(item.first);
105 }
106
107 return hitEnts;
108}
109
110/**
111 * Finds all entities with the given entity template name, belonging to the given player.
112 *
113 * @param camera use this view to convert screen to world coordinates.
114 * @param templateName the name of the template to match, or the selection group name
115 * for similar matching.
116 * @param owner player whose entities we are selecting. Ownership is ignored if
117 * INVALID_PLAYER is used.
118 * @param includeOffScreen if true, then all entities visible in the world will be selected,
119 * else only entities visible to the camera will be selected.
120 * @param matchRank if true, only entities that exactly match templateName will be selected,
121 * else entities with matching SelectionGroupName will be selected.
122 * @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
123 * will be selected (including decorative actors), else only those selectable in-game.
124 * @param allowFoundations if true, foundations are also included in the results. Only takes
125 * effect when matchRank = true.
126 *
127 * @return unordered list of selected entities.
128 * @see ICmpIdentity
129 */
130std::vector<entity_id_t> PickSimilarEntities(CSimulation2& simulation, const CCamera& camera, const std::string& templateName,
131 player_id_t owner, bool includeOffScreen, bool matchRank, bool allowEditorSelectables, bool allowFoundations);
132
133} // namespace
134
135#endif // INCLUDED_SELECTION
#define swap(a, i, j)
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:26
New profiler (complementing the older CProfileManager)
#define PROFILE2(region)
Starts timing from now until the end of the current scope.
Definition: Profiler2.h:452
bool CheckEntityInRect(CEntityHandle handle, const CCamera &camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables)
Returns true if the given entity is visible in the given screen area.
Definition: Selection.cpp:101
Definition: Camera.h:46
Object wrapping an entity_id_t, with a SEntityComponentCache to support fast QueryInterface() / CmpPt...
Definition: Entity.h:80
Public API for simulation system.
Definition: Simulation2.h:57
std::unordered_map< entity_id_t, IComponent * > InterfaceListUnordered
Definition: Simulation2.h:219
const InterfaceListUnordered & GetEntitiesWithInterfaceUnordered(int iid)
Returns a list of components implementing the given interface, and their associated entities,...
Definition: Simulation2.cpp:724
Definition: IComponent.h:33
Definition: Selection.h:36
std::vector< entity_id_t > GetEntitiesWithComponentInRect(CSimulation2 &simulation, int cid, const CCamera &camera, int sx0, int sy0, int sx1, int sy1)
Definition: Selection.h:85
entity_id_t PickEntityAtPoint(CSimulation2 &simulation, const CCamera &camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables)
Finds all selectable entities under the given screen coordinates.
Definition: Selection.cpp:42
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 the given pl...
Definition: Selection.cpp:141
std::vector< entity_id_t > PickNonGaiaEntitiesInRect(CSimulation2 &simulation, const CCamera &camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables)
Finds all selectable entities within the given screen coordinate rectangle, belonging to any given pl...
Definition: Selection.cpp:178
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)
Finds all entities with the given entity template name, belonging to the given player.
Definition: Selection.cpp:201
Filter
Definition: Sampler.h:33
std::uint32_t entity_id_t
Entity ID type.
Definition: Entity.h:29
Finds all entities with a given component belonging to any given player.
Definition: Selection.h:78
bool operator()(IComponent *)
Definition: Selection.h:79