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