Line data Source code
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"
22 : #include "simulation2/Simulation2.h"
23 : #include "simulation2/helpers/Player.h"
24 : #include "simulation2/system/Component.h"
25 : #include "simulation2/system/Entity.h"
26 :
27 : #include <string>
28 : #include <utility>
29 : #include <vector>
30 :
31 : class CCamera;
32 :
33 : bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
34 :
35 : namespace EntitySelection
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 : */
51 : entity_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 : */
66 : 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);
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 : */
72 : std::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 : */
77 : struct DefaultComponentFilter
78 : {
79 : bool operator()(IComponent*)
80 : {
81 : return true;
82 : }
83 : };
84 : template<typename Filter = DefaultComponentFilter>
85 0 : std::vector<entity_id_t> GetEntitiesWithComponentInRect(CSimulation2& simulation, int cid, const CCamera& camera, int sx0, int sy0, int sx1, int sy1)
86 : {
87 0 : PROFILE2("GetEntitiesWithObstructionInRect");
88 :
89 : // Make sure sx0 <= sx1, and sy0 <= sy1
90 0 : if (sx0 > sx1)
91 0 : std::swap(sx0, sx1);
92 0 : if (sy0 > sy1)
93 0 : std::swap(sy0, sy1);
94 :
95 0 : std::vector<entity_id_t> hitEnts;
96 :
97 : Filter filter;
98 0 : const CSimulation2::InterfaceListUnordered& entities = simulation.GetEntitiesWithInterfaceUnordered(cid);
99 0 : for (const std::pair<const entity_id_t, IComponent*>& item : entities)
100 : {
101 0 : if (!filter(item.second))
102 0 : continue;
103 0 : if (CheckEntityInRect(item.second->GetEntityHandle(), camera, sx0, sy0, sx1, sy1, false))
104 0 : hitEnts.push_back(item.first);
105 : }
106 :
107 0 : return hitEnts;
108 0 : }
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 : */
130 : std::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
|