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 : #include "precompiled.h"
19 :
20 : #include "Selection.h"
21 :
22 : #include "graphics/Camera.h"
23 : #include "lib/debug.h"
24 : #include "maths/Frustum.h"
25 : #include "maths/Vector2D.h"
26 : #include "maths/Vector3D.h"
27 : #include "ps/Profiler2.h"
28 : #include "simulation2/components/ICmpIdentity.h"
29 : #include "simulation2/components/ICmpOwnership.h"
30 : #include "simulation2/components/ICmpRangeManager.h"
31 : #include "simulation2/components/ICmpSelectable.h"
32 : #include "simulation2/components/ICmpTemplateManager.h"
33 : #include "simulation2/components/ICmpUnitRenderer.h"
34 : #include "simulation2/components/ICmpVisual.h"
35 : #include "simulation2/system/Component.h"
36 :
37 : #include <algorithm>
38 : #include <cstddef>
39 : #include <string_view>
40 : #include <unordered_map>
41 :
42 0 : entity_id_t EntitySelection::PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables)
43 : {
44 0 : PROFILE2("PickEntityAtPoint");
45 0 : const auto [origin, dir] = camera.BuildCameraRay(screenX, screenY);
46 :
47 0 : CmpPtr<ICmpUnitRenderer> cmpUnitRenderer(simulation.GetSimContext().GetSystemEntity());
48 0 : ENSURE(cmpUnitRenderer);
49 :
50 0 : std::vector<std::pair<CEntityHandle, CVector3D> > entities;
51 0 : cmpUnitRenderer->PickAllEntitiesAtPoint(entities, origin, dir, allowEditorSelectables);
52 0 : if (entities.empty())
53 : return INVALID_ENTITY;
54 :
55 : // Filter for relevent entities in the list of candidates (all entities below the mouse)
56 0 : std::vector<std::pair<float, CEntityHandle> > hits; // (dist^2, entity) pairs
57 0 : for (size_t i = 0; i < entities.size(); ++i)
58 : {
59 : // Find the perpendicular distance from the object's centre to the picker ray
60 0 : float dist2;
61 0 : const CVector3D center = entities[i].second;
62 0 : CVector3D closest = origin + dir * (center - origin).Dot(dir);
63 0 : dist2 = (closest - center).LengthSquared();
64 0 : hits.emplace_back(dist2, entities[i].first);
65 : }
66 :
67 : // Sort hits by distance
68 0 : std::sort(hits.begin(), hits.end(),
69 0 : [](const std::pair<float, CEntityHandle>& a, const std::pair<float, CEntityHandle>& b) {
70 0 : return a.first < b.first;
71 : });
72 :
73 0 : CmpPtr<ICmpRangeManager> cmpRangeManager(simulation, SYSTEM_ENTITY);
74 0 : ENSURE(cmpRangeManager);
75 :
76 0 : for (size_t i = 0; i < hits.size(); ++i)
77 : {
78 0 : const CEntityHandle& handle = hits[i].second;
79 :
80 0 : CmpPtr<ICmpSelectable> cmpSelectable(handle);
81 0 : if (!cmpSelectable)
82 0 : continue;
83 :
84 : // Check if this entity is only selectable in Atlas
85 0 : if (!allowEditorSelectables && cmpSelectable->IsEditorOnly())
86 0 : continue;
87 :
88 : // Ignore entities hidden by LOS (or otherwise hidden, e.g. when not IsInWorld)
89 0 : if (cmpRangeManager->GetLosVisibility(handle, player) == LosVisibility::HIDDEN)
90 0 : continue;
91 :
92 0 : return handle.GetId();
93 : }
94 : return INVALID_ENTITY;
95 0 : }
96 :
97 : /**
98 : * Returns true if the given entity is visible in the given screen area.
99 : * If the entity is a decorative, the function will only return true if allowEditorSelectables.
100 : */
101 0 : bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables)
102 : {
103 : // Check if this entity is only selectable in Atlas
104 0 : CmpPtr<ICmpSelectable> cmpSelectable(handle);
105 0 : if (!cmpSelectable || (!allowEditorSelectables && cmpSelectable->IsEditorOnly()))
106 0 : return false;
107 :
108 : // Find the current interpolated model position.
109 : // (We just use the centre position and not the whole bounding box, because maybe
110 : // that's better for users trying to select objects in busy areas)
111 :
112 0 : CmpPtr<ICmpVisual> cmpVisual(handle);
113 0 : if (!cmpVisual)
114 : return false;
115 :
116 0 : CVector3D position = cmpVisual->GetPosition();
117 :
118 : // Reject if it's not on-screen (e.g. it's behind the camera)
119 0 : if (!camera.GetFrustum().IsPointVisible(position))
120 : return false;
121 :
122 : // Compare screen-space coordinates
123 0 : const CVector2D screenPos{camera.GetScreenCoordinates(position)};
124 0 : int ix = static_cast<int>(screenPos.X);
125 0 : int iy = static_cast<int>(screenPos.Y);
126 0 : return sx0 <= ix && ix <= sx1 && sy0 <= iy && iy <= sy1;
127 : }
128 :
129 : /**
130 : * Returns true if the given entity is visible to the given player and visible in the given screen area.
131 : */
132 0 : static bool CheckEntityVisibleAndInRect(CEntityHandle handle, CmpPtr<ICmpRangeManager> cmpRangeManager, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, player_id_t player, bool allowEditorSelectables)
133 : {
134 : // Ignore entities hidden by LOS (or otherwise hidden, e.g. when not IsInWorld)
135 0 : if (cmpRangeManager->GetLosVisibility(handle, player) == LosVisibility::HIDDEN)
136 : return false;
137 :
138 0 : return CheckEntityInRect(handle, camera, sx0, sy0, sx1, sy1, allowEditorSelectables);
139 : }
140 :
141 0 : std::vector<entity_id_t> EntitySelection::PickEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, player_id_t owner, bool allowEditorSelectables)
142 : {
143 0 : PROFILE2("PickEntitiesInRect");
144 : // Make sure sx0 <= sx1, and sy0 <= sy1
145 0 : if (sx0 > sx1)
146 0 : std::swap(sx0, sx1);
147 0 : if (sy0 > sy1)
148 0 : std::swap(sy0, sy1);
149 :
150 0 : CmpPtr<ICmpRangeManager> cmpRangeManager(simulation, SYSTEM_ENTITY);
151 0 : ENSURE(cmpRangeManager);
152 :
153 0 : std::vector<entity_id_t> hitEnts;
154 :
155 0 : if (owner != INVALID_PLAYER)
156 : {
157 0 : CComponentManager& componentManager = simulation.GetSimContext().GetComponentManager();
158 0 : std::vector<entity_id_t> ents = cmpRangeManager->GetEntitiesByPlayer(owner);
159 0 : for (std::vector<entity_id_t>::iterator it = ents.begin(); it != ents.end(); ++it)
160 : {
161 0 : if (CheckEntityVisibleAndInRect(componentManager.LookupEntityHandle(*it), cmpRangeManager, camera, sx0, sy0, sx1, sy1, owner, allowEditorSelectables))
162 0 : hitEnts.push_back(*it);
163 : }
164 0 : }
165 : else // owner == INVALID_PLAYER; Used when selecting units in Atlas or other mods that allow all kinds of selectables to be selected.
166 : {
167 0 : const CSimulation2::InterfaceListUnordered& selectableEnts = simulation.GetEntitiesWithInterfaceUnordered(IID_Selectable);
168 0 : for (CSimulation2::InterfaceListUnordered::const_iterator it = selectableEnts.begin(); it != selectableEnts.end(); ++it)
169 : {
170 0 : if (CheckEntityVisibleAndInRect(it->second->GetEntityHandle(), cmpRangeManager, camera, sx0, sy0, sx1, sy1, owner, allowEditorSelectables))
171 0 : hitEnts.push_back(it->first);
172 : }
173 : }
174 :
175 0 : return hitEnts;
176 0 : }
177 :
178 0 : std::vector<entity_id_t> EntitySelection::PickNonGaiaEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables)
179 : {
180 0 : PROFILE2("PickNonGaiaEntitiesInRect");
181 :
182 : // Make sure sx0 <= sx1, and sy0 <= sy1
183 0 : if (sx0 > sx1)
184 0 : std::swap(sx0, sx1);
185 0 : if (sy0 > sy1)
186 0 : std::swap(sy0, sy1);
187 :
188 0 : CmpPtr<ICmpRangeManager> cmpRangeManager(simulation, SYSTEM_ENTITY);
189 0 : ENSURE(cmpRangeManager);
190 :
191 0 : std::vector<entity_id_t> hitEnts;
192 :
193 0 : CComponentManager& componentManager = simulation.GetSimContext().GetComponentManager();
194 0 : for (entity_id_t ent : cmpRangeManager->GetNonGaiaEntities())
195 0 : if (CheckEntityInRect(componentManager.LookupEntityHandle(ent), camera, sx0, sy0, sx1, sy1, allowEditorSelectables))
196 0 : hitEnts.push_back(ent);
197 :
198 0 : return hitEnts;
199 0 : }
200 :
201 0 : std::vector<entity_id_t> EntitySelection::PickSimilarEntities(CSimulation2& simulation, const CCamera& camera,
202 : const std::string& templateName, player_id_t owner, bool includeOffScreen, bool matchRank,
203 : bool allowEditorSelectables, bool allowFoundations)
204 : {
205 0 : PROFILE2("PickSimilarEntities");
206 0 : CmpPtr<ICmpTemplateManager> cmpTemplateManager(simulation, SYSTEM_ENTITY);
207 0 : CmpPtr<ICmpRangeManager> cmpRangeManager(simulation, SYSTEM_ENTITY);
208 :
209 0 : std::vector<entity_id_t> hitEnts;
210 :
211 0 : const CSimulation2::InterfaceListUnordered& ents = simulation.GetEntitiesWithInterfaceUnordered(IID_Selectable);
212 0 : for (CSimulation2::InterfaceListUnordered::const_iterator it = ents.begin(); it != ents.end(); ++it)
213 : {
214 0 : entity_id_t ent = it->first;
215 0 : CEntityHandle handle = it->second->GetEntityHandle();
216 :
217 : // Check if this entity is only selectable in Atlas
218 0 : if (static_cast<ICmpSelectable*>(it->second)->IsEditorOnly() && !allowEditorSelectables)
219 0 : continue;
220 :
221 0 : if (matchRank)
222 : {
223 : // Exact template name matching, optionally also allowing foundations
224 0 : const std::string curTemplateName = cmpTemplateManager->GetCurrentTemplateName(ent);
225 0 : bool matches = (curTemplateName == templateName ||
226 : (allowFoundations &&
227 0 : std::string_view{curTemplateName}.substr(0, 11) == "foundation|" &&
228 0 : std::string_view{curTemplateName}.substr(11) == templateName));
229 0 : if (!matches)
230 0 : continue;
231 0 : }
232 :
233 : // Ignore entities hidden by LOS (or otherwise hidden, e.g. when not IsInWorld)
234 : // In this case, the checking is done to avoid selecting garrisoned units
235 0 : if (cmpRangeManager->GetLosVisibility(handle, owner) == LosVisibility::HIDDEN)
236 0 : continue;
237 :
238 : // Ignore entities not owned by 'owner'
239 0 : CmpPtr<ICmpOwnership> cmpOwnership(simulation.GetSimContext(), ent);
240 0 : if (owner != INVALID_PLAYER && (!cmpOwnership || cmpOwnership->GetOwner() != owner))
241 0 : continue;
242 :
243 : // Ignore off screen entities
244 0 : if (!includeOffScreen)
245 : {
246 : // Find the current interpolated model position.
247 0 : CmpPtr<ICmpVisual> cmpVisual(simulation.GetSimContext(), ent);
248 0 : if (!cmpVisual)
249 0 : continue;
250 0 : CVector3D position = cmpVisual->GetPosition();
251 :
252 : // Reject if it's not on-screen (e.g. it's behind the camera)
253 0 : if (!camera.GetFrustum().IsPointVisible(position))
254 0 : continue;
255 : }
256 :
257 0 : if (!matchRank)
258 : {
259 : // Match by selection group name
260 : // (This is relatively expensive since it involves script calls, so do it after all other tests)
261 0 : CmpPtr<ICmpIdentity> cmpIdentity(simulation.GetSimContext(), ent);
262 0 : if (!cmpIdentity || cmpIdentity->GetSelectionGroupName() != templateName)
263 0 : continue;
264 : }
265 :
266 0 : hitEnts.push_back(ent);
267 : }
268 :
269 0 : return hitEnts;
270 0 : }
|