Pyrogenesis  trunk
Selection.h
Go to the documentation of this file.
1 /* Copyright (C) 2019 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 <vector>
28 
29 class CSimulation2;
30 class CCamera;
31 
32 bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
33 
34 namespace EntitySelection
35 {
36 
37 /**
38  * Finds all selectable entities under the given screen coordinates.
39  *
40  * @param camera use this view to convert screen to world coordinates.
41  * @param screenX,screenY 2D screen coordinates.
42  * @param player player whose LOS will be used when selecting entities. In Atlas
43  * this value is ignored as the whole map is revealed.
44  * @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
45  * will be selected (including decorative actors), else only those selectable ingame.
46  * @param range Approximate range to check for entity in.
47  *
48  * @return ordered list of selected entities with the closest first.
49  */
50 entity_id_t PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables);
51 
52 /**
53  * Finds all selectable entities within the given screen coordinate rectangle,
54  * belonging to the given player. Used for bandboxing.
55  *
56  * @param camera use this view to convert screen to world coordinates.
57  * @param sx0,sy0,sx1,sy1 diagonally opposite corners of the rectangle in 2D screen coordinates.
58  * @param owner player whose entities we are selecting. Ownership is ignored if
59  * INVALID_PLAYER is used.
60  * @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
61  * will be selected (including decorative actors), else only those selectable ingame.
62  *
63  * @return unordered list of selected entities.
64  */
65 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);
66 
67 /**
68  * Finds all selectable entities within the given screen coordinate rectangle,
69  * belonging to any given player (excluding Gaia). Used for status bars.
70  */
71 std::vector<entity_id_t> PickNonGaiaEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
72 
73 /**
74  * Finds all entities with a given component belonging to any given player.
75  */
77 {
79  {
80  return true;
81  }
82 };
83 template<typename Filter = DefaultComponentFilter>
84 std::vector<entity_id_t> GetEntitiesWithComponentInRect(CSimulation2& simulation, int cid, const CCamera& camera, int sx0, int sy0, int sx1, int sy1)
85 {
86  PROFILE2("GetEntitiesWithObstructionInRect");
87 
88  // Make sure sx0 <= sx1, and sy0 <= sy1
89  if (sx0 > sx1)
90  std::swap(sx0, sx1);
91  if (sy0 > sy1)
92  std::swap(sy0, sy1);
93 
94  std::vector<entity_id_t> hitEnts;
95 
96  Filter filter;
98  for (const std::pair<const entity_id_t, IComponent*>& item : entities)
99  {
100  if (!filter(item.second))
101  continue;
102  if (CheckEntityInRect(item.second->GetEntityHandle(), camera, sx0, sy0, sx1, sy1, false))
103  hitEnts.push_back(item.first);
104  }
105 
106  return hitEnts;
107 }
108 
109 /**
110  * Finds all entities with the given entity template name, belonging to the given player.
111  *
112  * @param camera use this view to convert screen to world coordinates.
113  * @param templateName the name of the template to match, or the selection group name
114  * for similar matching.
115  * @param owner player whose entities we are selecting. Ownership is ignored if
116  * INVALID_PLAYER is used.
117  * @param includeOffScreen if true, then all entities visible in the world will be selected,
118  * else only entities visible to the camera will be selected.
119  * @param matchRank if true, only entities that exactly match templateName will be selected,
120  * else entities with matching SelectionGroupName will be selected.
121  * @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
122  * will be selected (including decorative actors), else only those selectable in-game.
123  * @param allowFoundations if true, foundations are also included in the results. Only takes
124  * effect when matchRank = true.
125  *
126  * @return unordered list of selected entities.
127  * @see ICmpIdentity
128  */
129 std::vector<entity_id_t> PickSimilarEntities(CSimulation2& simulation, const CCamera& camera, const std::string& templateName,
130  player_id_t owner, bool includeOffScreen, bool matchRank, bool allowEditorSelectables, bool allowFoundations);
131 
132 } // namespace
133 
134 #endif // INCLUDED_SELECTION
Definition: IComponent.h:32
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
std::unordered_map< entity_id_t, IComponent * > InterfaceListUnordered
Definition: Simulation2.h:212
Object wrapping an entity_id_t, with a SEntityComponentCache to support fast QueryInterface() / CmpPt...
Definition: Entity.h:79
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:197
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:36
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:174
const InterfaceListUnordered & GetEntitiesWithInterfaceUnordered(int iid)
Returns a list of components implementing the given interface, and their associated entities...
Definition: Simulation2.cpp:716
Public API for simulation system.
Definition: Simulation2.h:46
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
bool operator()(IComponent *cmp)
Definition: Selection.h:78
#define PROFILE2(region)
Starts timing from now until the end of the current scope.
Definition: Profiler2.h:536
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:96
New profiler (complementing the older CProfileManager)
Definition: Camera.h:41
std::vector< entity_id_t > GetEntitiesWithComponentInRect(CSimulation2 &simulation, int cid, const CCamera &camera, int sx0, int sy0, int sx1, int sy1)
Definition: Selection.h:84
Finds all entities with a given component belonging to any given player.
Definition: Selection.h:76
Filter
Definition: Sampler.h:35
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23
#define swap(a, i, j)
Definition: Selection.h:34
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:137