1
0
forked from 0ad/0ad

Adds a helper function to pick entities with a given component and a given filter on screen.

Reviewed By: wraitii
Commented By: Stan, elexis
Differential Revision: https://code.wildfiregames.com/D2207
This was SVN commit r22939.
This commit is contained in:
Vladislav Belov 2019-09-20 07:45:55 +00:00
parent 76f82bbcac
commit c17e7ee92f
7 changed files with 80 additions and 21 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games. /* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -48,11 +48,7 @@ public:
// Template state: // Template state:
enum { EObstructionType m_Type;
STATIC,
UNIT,
CLUSTER
} m_Type;
entity_pos_t m_Size0; // radius or width entity_pos_t m_Size0; // radius or width
entity_pos_t m_Size1; // radius or depth entity_pos_t m_Size1; // radius or depth
@ -462,6 +458,11 @@ public:
return (m_TemplateFlags & ICmpObstructionManager::FLAG_BLOCK_MOVEMENT) != 0; return (m_TemplateFlags & ICmpObstructionManager::FLAG_BLOCK_MOVEMENT) != 0;
} }
virtual EObstructionType GetObstructionType() const
{
return m_Type;
}
virtual ICmpObstructionManager::tag_t GetObstruction() const virtual ICmpObstructionManager::tag_t GetObstruction() const
{ {
return m_Tag; return m_Tag;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games. /* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -38,6 +38,12 @@ public:
FOUNDATION_CHECK_FAIL_TERRAIN_CLASS FOUNDATION_CHECK_FAIL_TERRAIN_CLASS
}; };
enum EObstructionType {
STATIC,
UNIT,
CLUSTER
};
virtual ICmpObstructionManager::tag_t GetObstruction() const = 0; virtual ICmpObstructionManager::tag_t GetObstruction() const = 0;
/** /**
@ -56,6 +62,8 @@ public:
virtual entity_pos_t GetUnitRadius() const = 0; virtual entity_pos_t GetUnitRadius() const = 0;
virtual EObstructionType GetObstructionType() const = 0;
virtual void SetUnitClearance(const entity_pos_t& clearance) = 0; virtual void SetUnitClearance(const entity_pos_t& clearance) = 0;
virtual bool IsControlPersistent() const = 0; virtual bool IsControlPersistent() const = 0;

View File

@ -31,6 +31,7 @@ public:
virtual bool GetPreviousObstructionSquare(ICmpObstructionManager::ObstructionSquare& UNUSED(out)) const { return true; } virtual bool GetPreviousObstructionSquare(ICmpObstructionManager::ObstructionSquare& UNUSED(out)) const { return true; }
virtual entity_pos_t GetSize() const { return entity_pos_t::Zero(); } virtual entity_pos_t GetSize() const { return entity_pos_t::Zero(); }
virtual entity_pos_t GetUnitRadius() const { return entity_pos_t::Zero(); } virtual entity_pos_t GetUnitRadius() const { return entity_pos_t::Zero(); }
virtual EObstructionType GetObstructionType() const { return ICmpObstruction::STATIC; }
virtual void SetUnitClearance(const entity_pos_t& UNUSED(clearance)) { } virtual void SetUnitClearance(const entity_pos_t& UNUSED(clearance)) { }
virtual bool IsControlPersistent() const { return true; } virtual bool IsControlPersistent() const { return true; }
virtual bool CheckShorePlacement() const { return true; } virtual bool CheckShorePlacement() const { return true; }

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games. /* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -20,7 +20,8 @@
#include "Selection.h" #include "Selection.h"
#include "graphics/Camera.h" #include "graphics/Camera.h"
#include "simulation2/Simulation2.h" #include "ps/CLogger.h"
#include "ps/Profiler2.h"
#include "simulation2/components/ICmpIdentity.h" #include "simulation2/components/ICmpIdentity.h"
#include "simulation2/components/ICmpOwnership.h" #include "simulation2/components/ICmpOwnership.h"
#include "simulation2/components/ICmpRangeManager.h" #include "simulation2/components/ICmpRangeManager.h"
@ -29,8 +30,6 @@
#include "simulation2/components/ICmpVisual.h" #include "simulation2/components/ICmpVisual.h"
#include "simulation2/components/ICmpUnitRenderer.h" #include "simulation2/components/ICmpUnitRenderer.h"
#include "simulation2/system/ComponentManager.h" #include "simulation2/system/ComponentManager.h"
#include "ps/CLogger.h"
#include "ps/Profiler2.h"
entity_id_t EntitySelection::PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables) entity_id_t EntitySelection::PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables)
{ {
@ -92,7 +91,7 @@ entity_id_t EntitySelection::PickEntityAtPoint(CSimulation2& simulation, const C
* Returns true if the given entity is visible in the given screen area. * Returns true if the given entity is visible in the given screen area.
* If the entity is a decorative, the function will only return true if allowEditorSelectables. * If the entity is a decorative, the function will only return true if allowEditorSelectables.
*/ */
static bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables) bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables)
{ {
// Check if this entity is only selectable in Atlas // Check if this entity is only selectable in Atlas
CmpPtr<ICmpSelectable> cmpSelectable(handle); CmpPtr<ICmpSelectable> cmpSelectable(handle);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games. /* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -18,19 +18,18 @@
#ifndef INCLUDED_SELECTION #ifndef INCLUDED_SELECTION
#define INCLUDED_SELECTION #define INCLUDED_SELECTION
/** #include "simulation2/components/ICmpObstruction.h"
* @file #include "simulation2/helpers/Player.h"
* Helper functions related to entity selection #include "simulation2/Simulation2.h"
*/
#include "simulation2/system/Entity.h" #include "simulation2/system/Entity.h"
#include "Player.h"
#include <vector> #include <vector>
class CSimulation2; class CSimulation2;
class CCamera; class CCamera;
bool CheckEntityInRect(CEntityHandle handle, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
namespace EntitySelection namespace EntitySelection
{ {
@ -70,6 +69,42 @@ std::vector<entity_id_t> PickEntitiesInRect(CSimulation2& simulation, const CCam
*/ */
std::vector<entity_id_t> PickNonGaiaEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables); std::vector<entity_id_t> PickNonGaiaEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
/**
* Finds all entities with a given component belonging to any given player.
*/
struct DefaultComponentFilter
{
bool operator()(IComponent* UNUSED(cmp))
{
return true;
}
};
template<typename Filter = DefaultComponentFilter>
std::vector<entity_id_t> GetEntitiesWithComponentInRect(CSimulation2& simulation, int cid, const CCamera& camera, int sx0, int sy0, int sx1, int sy1)
{
PROFILE2("GetEntitiesWithObstructionInRect");
// Make sure sx0 <= sx1, and sy0 <= sy1
if (sx0 > sx1)
std::swap(sx0, sx1);
if (sy0 > sy1)
std::swap(sy0, sy1);
std::vector<entity_id_t> hitEnts;
Filter filter;
const CSimulation2::InterfaceListUnordered& entities = simulation.GetEntitiesWithInterfaceUnordered(cid);
for (const std::pair<const entity_id_t, IComponent*>& item : entities)
{
if (!filter(item.second))
continue;
if (CheckEntityInRect(item.second->GetEntityHandle(), camera, sx0, sy0, sx1, sy1, false))
hitEnts.push_back(item.first);
}
return hitEnts;
}
/** /**
* Finds all entities with the given entity template name, belonging to the given player. * Finds all entities with the given entity template name, belonging to the given player.
* *

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games. /* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -117,6 +117,19 @@ std::vector<entity_id_t> JSI_Simulation::PickNonGaiaEntitiesOnScreen(ScriptInter
return EntitySelection::PickNonGaiaEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres, false); return EntitySelection::PickNonGaiaEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres, false);
} }
std::vector<entity_id_t> JSI_Simulation::GetEntitiesWithStaticObstructionOnScreen(ScriptInterface::CxPrivate* pCxPrivate)
{
struct StaticObstructionFilter
{
bool operator()(IComponent* cmp)
{
ICmpObstruction* cmpObstruction = static_cast<ICmpObstruction*>(cmp);
return cmpObstruction->GetObstructionType() == ICmpObstruction::STATIC;
}
};
return EntitySelection::GetEntitiesWithComponentInRect<StaticObstructionFilter>(*g_Game->GetSimulation2(), IID_Obstruction, *g_Game->GetView()->GetCamera(), 0, 0, g_xres, g_yres);
}
std::vector<entity_id_t> JSI_Simulation::PickSimilarPlayerEntities(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& templateName, bool includeOffScreen, bool matchRank, bool allowFoundations) std::vector<entity_id_t> JSI_Simulation::PickSimilarPlayerEntities(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& templateName, bool includeOffScreen, bool matchRank, bool allowFoundations)
{ {
return EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), templateName, g_Game->GetViewedPlayerID(), includeOffScreen, matchRank, false, allowFoundations); return EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), templateName, g_Game->GetViewedPlayerID(), includeOffScreen, matchRank, false, allowFoundations);
@ -143,6 +156,7 @@ void JSI_Simulation::RegisterScriptFunctions(const ScriptInterface& scriptInterf
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickPlayerEntitiesInRect>("PickPlayerEntitiesInRect"); scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickPlayerEntitiesInRect>("PickPlayerEntitiesInRect");
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickPlayerEntitiesOnScreen>("PickPlayerEntitiesOnScreen"); scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickPlayerEntitiesOnScreen>("PickPlayerEntitiesOnScreen");
scriptInterface.RegisterFunction<std::vector<entity_id_t>, &PickNonGaiaEntitiesOnScreen>("PickNonGaiaEntitiesOnScreen"); scriptInterface.RegisterFunction<std::vector<entity_id_t>, &PickNonGaiaEntitiesOnScreen>("PickNonGaiaEntitiesOnScreen");
scriptInterface.RegisterFunction<std::vector<entity_id_t>, &GetEntitiesWithStaticObstructionOnScreen>("GetEntitiesWithStaticObstructionOnScreen");
scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarPlayerEntities>("PickSimilarPlayerEntities"); scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarPlayerEntities>("PickSimilarPlayerEntities");
scriptInterface.RegisterFunction<void, bool, &SetBoundingBoxDebugOverlay>("SetBoundingBoxDebugOverlay"); scriptInterface.RegisterFunction<void, bool, &SetBoundingBoxDebugOverlay>("SetBoundingBoxDebugOverlay");
} }

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games. /* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@ -31,6 +31,7 @@ namespace JSI_Simulation
std::vector<entity_id_t> PickPlayerEntitiesInRect(ScriptInterface::CxPrivate* pCxPrivate, int x0, int y0, int x1, int y1, int player); std::vector<entity_id_t> PickPlayerEntitiesInRect(ScriptInterface::CxPrivate* pCxPrivate, int x0, int y0, int x1, int y1, int player);
std::vector<entity_id_t> PickPlayerEntitiesOnScreen(ScriptInterface::CxPrivate* pCxPrivate, int player); std::vector<entity_id_t> PickPlayerEntitiesOnScreen(ScriptInterface::CxPrivate* pCxPrivate, int player);
std::vector<entity_id_t> PickNonGaiaEntitiesOnScreen(ScriptInterface::CxPrivate* pCxPrivate); std::vector<entity_id_t> PickNonGaiaEntitiesOnScreen(ScriptInterface::CxPrivate* pCxPrivate);
std::vector<entity_id_t> GetEntitiesWithStaticObstructionOnScreen(ScriptInterface::CxPrivate* pCxPrivate);
std::vector<entity_id_t> PickSimilarPlayerEntities(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName, bool includeOffScreen, bool matchRank, bool allowFoundations); std::vector<entity_id_t> PickSimilarPlayerEntities(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName, bool includeOffScreen, bool matchRank, bool allowFoundations);
JS::Value GetAIs(ScriptInterface::CxPrivate* pCxPrivate); JS::Value GetAIs(ScriptInterface::CxPrivate* pCxPrivate);
void SetBoundingBoxDebugOverlay(ScriptInterface::CxPrivate* pCxPrivate, bool enabled); void SetBoundingBoxDebugOverlay(ScriptInterface::CxPrivate* pCxPrivate, bool enabled);