2019-09-11 09:09:36 +02:00
|
|
|
// Copyright (c) 2019 Lawnjelly
|
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
#include "lroom_manager.h"
|
2019-09-12 12:07:43 +02:00
|
|
|
#include "core/engine.h"
|
2019-09-12 16:51:33 +02:00
|
|
|
#include "scene/3d/camera.h"
|
2019-09-13 20:15:25 +02:00
|
|
|
#include "scene/3d/mesh_instance.h"
|
2019-09-15 16:39:01 +02:00
|
|
|
#include "lroom_converter.h"
|
2019-09-18 12:04:02 +02:00
|
|
|
#include "ldebug.h"
|
2019-09-11 09:09:36 +02:00
|
|
|
|
|
|
|
LRoomManager::LRoomManager()
|
|
|
|
{
|
2019-09-12 16:51:33 +02:00
|
|
|
m_cameraID = 0;
|
2019-09-13 20:15:25 +02:00
|
|
|
m_uiFrameCounter = 0;
|
2019-09-18 12:04:02 +02:00
|
|
|
m_iLoggingLevel = 2;
|
|
|
|
m_bActive = true;
|
|
|
|
m_bFrustumOnly = false;
|
2019-09-15 17:16:09 +02:00
|
|
|
|
|
|
|
// to know which rooms to hide we keep track of which were shown this, and the previous frame
|
2019-09-16 15:23:10 +02:00
|
|
|
m_pCurr_VisibleRoomList = &m_VisibleRoomList_A;
|
|
|
|
m_pPrev_VisibleRoomList = &m_VisibleRoomList_B;
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int LRoomManager::FindClosestRoom(const Vector3 &pt) const
|
|
|
|
{
|
2019-09-17 15:30:31 +02:00
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
//print_line("FindClosestRoom");
|
|
|
|
int closest = -1;
|
|
|
|
float closest_dist = FLT_MAX;
|
|
|
|
|
2019-09-17 15:30:31 +02:00
|
|
|
// uses bounds if this is available
|
|
|
|
int closest_within = -1;
|
|
|
|
float within_dist = FLT_MAX;
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
for (int n=0; n<m_Rooms.size(); n++)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
const LRoom &lroom = m_Rooms[n];
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
float d = pt.distance_squared_to(lroom.m_ptCentre);
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
if (d < closest_dist)
|
|
|
|
{
|
|
|
|
closest = n;
|
|
|
|
closest_dist = d;
|
|
|
|
}
|
2019-09-17 15:30:31 +02:00
|
|
|
|
|
|
|
// is there a bound?
|
|
|
|
if (lroom.m_Bound.IsActive())
|
|
|
|
{
|
|
|
|
// is it within the aabb?
|
|
|
|
if (lroom.m_AABB.has_point(pt))
|
|
|
|
{
|
|
|
|
// is it within the convex hull?
|
|
|
|
float dist = lroom.m_Bound.GetClosestDistance(pt);
|
|
|
|
|
|
|
|
// find the lowest within distance of the nearby room convex hulls
|
|
|
|
if (dist < within_dist)
|
|
|
|
{
|
|
|
|
closest_within = n;
|
|
|
|
within_dist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 15:30:31 +02:00
|
|
|
// some logic whether to use the hulls or the closest dist
|
|
|
|
if (within_dist < 1.0f)
|
|
|
|
{
|
|
|
|
return closest_within;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
const LRoom * LRoomManager::GetRoom(int i) const
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
if ((unsigned int) i >= m_Rooms.size())
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("LRoomManager::GetRoom out of range");
|
2019-09-13 20:15:25 +02:00
|
|
|
return 0;
|
2019-09-15 16:39:01 +02:00
|
|
|
}
|
|
|
|
return &m_Rooms[i];
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
LRoom * LRoomManager::GetRoom(int i)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
if ((unsigned int) i >= m_Rooms.size())
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
WARN_PRINT_ONCE("LRoomManager::GetRoom out of range");
|
|
|
|
return 0;
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
2019-09-15 16:39:01 +02:00
|
|
|
return &m_Rooms[i];
|
|
|
|
}
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
|
|
|
|
LRoom &LRoomManager::Portal_GetLinkedRoom(const LPortal &port)
|
|
|
|
{
|
|
|
|
return m_Rooms[port.m_iRoomNum];
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LRoomManager::Obj_SetRoomNum(Node * pNode, int num)
|
|
|
|
{
|
|
|
|
pNode->set_meta("_lroom", num);
|
|
|
|
|
|
|
|
assert (Obj_GetRoomNum(pNode) == num);
|
|
|
|
}
|
|
|
|
|
|
|
|
int LRoomManager::Obj_GetRoomNum(Node * pNode) const
|
|
|
|
{
|
|
|
|
//assert (pNode->has_meta("_lroom"));
|
|
|
|
Variant v = pNode->get_meta("_lroom");
|
|
|
|
if (v.get_type() == Variant::NIL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
LRoom * LRoomManager::GetRoomFromDOB(Node * pNode)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
|
|
|
int iRoom = Obj_GetRoomNum(pNode);
|
|
|
|
if (iRoom == -1)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("LRoomManager::GetRoomFromDOB : metadata is empty");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
LRoom * pRoom = GetRoom(iRoom);
|
2019-09-13 20:15:25 +02:00
|
|
|
if (pRoom == 0)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("LRoomManager::GetRoomFromDOB : pRoom is NULL");
|
|
|
|
}
|
|
|
|
return pRoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
// register but let LPortal know which room the dob should start in
|
|
|
|
bool LRoomManager::dob_register_hint(Node * pDOB, float radius, Node * pRoom)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
2019-09-18 12:04:02 +02:00
|
|
|
if (!pDOB)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("dob_register_hint : pDOB is NULL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPRINT(3, "dob_register_hint " + pDOB->get_name());
|
|
|
|
|
|
|
|
if (!pRoom)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("dob_register_hint : pRoom is NULL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int iRoom = Obj_GetRoomNum(pRoom);
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
|
|
|
|
if (!pSpat)
|
2019-09-18 12:04:02 +02:00
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("dob_register_hint : DOB is not a spatial");
|
2019-09-17 10:32:18 +02:00
|
|
|
return false;
|
2019-09-18 12:04:02 +02:00
|
|
|
}
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
return DobRegister(pSpat, radius, iRoom);
|
|
|
|
}
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
|
|
|
|
bool LRoomManager::DobRegister(Spatial * pDOB, float radius, int iRoom)
|
|
|
|
{
|
|
|
|
//LPRINT(3, "register_dob " + pDOB->get_name());
|
|
|
|
|
|
|
|
if (iRoom == -1)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("LRoomManager::DobRegister : room ID is -1");
|
2019-09-17 10:32:18 +02:00
|
|
|
return false;
|
2019-09-18 12:04:02 +02:00
|
|
|
}
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
LRoom * pRoom = GetRoom(iRoom);
|
2019-09-13 20:15:25 +02:00
|
|
|
if (!pRoom)
|
2019-09-17 10:32:18 +02:00
|
|
|
return false;
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-16 15:23:10 +02:00
|
|
|
LDob dob;
|
2019-09-18 12:04:02 +02:00
|
|
|
dob.m_ID = pDOB->get_instance_id();
|
2019-09-16 15:23:10 +02:00
|
|
|
dob.m_fRadius = radius;
|
|
|
|
|
|
|
|
pRoom->DOB_Add(dob);
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
// save the room ID on the dob metadata
|
2019-09-18 12:04:02 +02:00
|
|
|
Obj_SetRoomNum(pDOB, iRoom);
|
2019-09-17 10:32:18 +02:00
|
|
|
return true;
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:53:24 +02:00
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
bool LRoomManager::dob_register(Node * pDOB, float radius)
|
|
|
|
{
|
|
|
|
if (!pDOB)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("dob_register : pDOB is NULL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPRINT(3, "dob_register " + pDOB->get_name());
|
|
|
|
|
|
|
|
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
|
|
|
|
if (!pSpat)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("dob_register : DOB is not a spatial");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 pt = pSpat->get_global_transform().origin;
|
|
|
|
|
|
|
|
int iRoomNum = FindClosestRoom(pt);
|
|
|
|
LPRINT(2, "dob_register closest room " + itos(iRoomNum));
|
|
|
|
|
|
|
|
return DobRegister(pSpat, radius, iRoomNum);
|
|
|
|
// if (iRoomNum == -1)
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// LRoom * pRoom = GetRoom(iRoomNum);
|
|
|
|
// if (!pRoom)
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// LDob dob;
|
|
|
|
// dob.m_ID = pSpat->get_instance_id();
|
|
|
|
// dob.m_fRadius = radius;
|
|
|
|
|
|
|
|
// pRoom->DOB_Add(dob);
|
|
|
|
|
|
|
|
// // save the room ID on the dob metadata
|
|
|
|
// Obj_SetRoomNum(pSpat, iRoomNum);
|
|
|
|
// return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-17 10:32:18 +02:00
|
|
|
int LRoomManager::dob_update(Node * pDOB)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
|
|
|
// find the room the object is attached to
|
|
|
|
LRoom * pRoom = GetRoomFromDOB(pDOB);
|
|
|
|
if (!pRoom)
|
2019-09-17 10:32:18 +02:00
|
|
|
return -1;
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
|
|
|
|
if (!pSpat)
|
2019-09-17 10:32:18 +02:00
|
|
|
return -1;
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-15 20:26:18 +02:00
|
|
|
LRoom * pNewRoom = pRoom->DOB_Update(*this, pSpat);
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
if (pNewRoom)
|
|
|
|
{
|
|
|
|
// remove from the list in old room and add to list in new room, and change the metadata
|
2019-09-15 16:39:01 +02:00
|
|
|
int iRoomNum = pNewRoom->m_RoomID;
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-16 15:23:10 +02:00
|
|
|
// get dob data to move to new room
|
|
|
|
unsigned int dob_id = pRoom->DOB_Find(pDOB);
|
|
|
|
assert (dob_id != -1);
|
|
|
|
|
|
|
|
// copy across data before removing
|
|
|
|
const LDob &data = pRoom->DOB_Get(dob_id);
|
|
|
|
pNewRoom->DOB_Add(data);
|
|
|
|
|
|
|
|
// remove from old room
|
|
|
|
pRoom->DOB_Remove(dob_id);
|
|
|
|
|
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
// save the room ID on the dob metadata
|
|
|
|
Obj_SetRoomNum(pSpat, iRoomNum);
|
2019-09-15 20:26:18 +02:00
|
|
|
|
2019-09-17 10:32:18 +02:00
|
|
|
// new room number
|
|
|
|
return iRoomNum;
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 10:32:18 +02:00
|
|
|
// still in the same room
|
|
|
|
return pRoom->m_RoomID;
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
bool LRoomManager::dob_teleport_hint(Node * pDOB, Node * pRoom)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
2019-09-18 12:04:02 +02:00
|
|
|
if (!pDOB)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("dob_teleport_hint : pDOB is NULL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPRINT(1, "dob_teleport_hint " + pDOB->get_name());
|
|
|
|
|
|
|
|
if (!pRoom)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("dob_teleport_hint : pRoom is NULL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int iRoom = Obj_GetRoomNum(pRoom);
|
|
|
|
|
2019-09-17 10:32:18 +02:00
|
|
|
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
|
|
|
|
if (!pSpat)
|
2019-09-18 12:04:02 +02:00
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("dob_teleport_hint : DOB is not a spatial");
|
2019-09-17 10:32:18 +02:00
|
|
|
return false;
|
2019-09-18 12:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return DobTeleport(pSpat, iRoom);
|
|
|
|
}
|
|
|
|
|
2019-09-17 10:32:18 +02:00
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
bool LRoomManager::DobTeleport(Spatial * pDOB, int iNewRoomID)
|
|
|
|
{
|
2019-09-17 10:32:18 +02:00
|
|
|
// old room
|
|
|
|
LRoom * pOldRoom = GetRoomFromDOB(pDOB);
|
|
|
|
if (!pOldRoom)
|
2019-09-18 12:04:02 +02:00
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("LRoomManager::DobTeleport : pOldRoom is NULL");
|
2019-09-17 10:32:18 +02:00
|
|
|
return false;
|
2019-09-18 12:04:02 +02:00
|
|
|
}
|
2019-09-17 10:32:18 +02:00
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
if (iNewRoomID == -1)
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("LRoomManager::DobTeleport : iNewRoomID is -1");
|
2019-09-17 10:32:18 +02:00
|
|
|
return false;
|
2019-09-18 12:04:02 +02:00
|
|
|
}
|
2019-09-17 10:32:18 +02:00
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
LRoom * pNewRoom = GetRoom(iNewRoomID);
|
2019-09-17 10:32:18 +02:00
|
|
|
if (!pNewRoom)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// detach from old room, add to new room
|
|
|
|
// get dob data to move to new room
|
|
|
|
unsigned int dob_id = pOldRoom->DOB_Find(pDOB);
|
|
|
|
assert (dob_id != -1);
|
|
|
|
|
|
|
|
// copy across data before removing
|
|
|
|
const LDob &data = pOldRoom->DOB_Get(dob_id);
|
|
|
|
pNewRoom->DOB_Add(data);
|
|
|
|
|
|
|
|
// remove from old room
|
|
|
|
pOldRoom->DOB_Remove(dob_id);
|
|
|
|
|
|
|
|
// save the room ID on the dob metadata
|
2019-09-18 12:04:02 +02:00
|
|
|
Obj_SetRoomNum(pDOB, iNewRoomID);
|
2019-09-17 10:32:18 +02:00
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
return true;
|
2019-09-11 09:09:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
// not tested...
|
|
|
|
bool LRoomManager::dob_teleport(Node * pDOB)
|
|
|
|
{
|
|
|
|
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
|
|
|
|
if (!pSpat)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Vector3 pt = pSpat->get_global_transform().origin;
|
|
|
|
|
|
|
|
int iRoomNum = FindClosestRoom(pt);
|
|
|
|
//print_line("dob_teleport closest room " + itos(iRoomNum));
|
|
|
|
|
|
|
|
if (iRoomNum == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return DobTeleport(pSpat, iRoomNum);
|
|
|
|
|
|
|
|
// // old room
|
|
|
|
// LRoom * pOldRoom = GetRoomFromDOB(pDOB);
|
|
|
|
// if (!pOldRoom)
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
|
|
|
|
// LRoom * pNewRoom = GetRoom(iRoomNum);
|
|
|
|
// if (!pNewRoom)
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// // detach from old room, add to new room
|
|
|
|
// // get dob data to move to new room
|
|
|
|
// unsigned int dob_id = pOldRoom->DOB_Find(pDOB);
|
|
|
|
// assert (dob_id != -1);
|
|
|
|
|
|
|
|
// // copy across data before removing
|
|
|
|
// const LDob &data = pOldRoom->DOB_Get(dob_id);
|
|
|
|
// pNewRoom->DOB_Add(data);
|
|
|
|
|
|
|
|
// // remove from old room
|
|
|
|
// pOldRoom->DOB_Remove(dob_id);
|
|
|
|
|
|
|
|
// // save the room ID on the dob metadata
|
|
|
|
// Obj_SetRoomNum(pSpat, iRoomNum);
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-17 10:32:18 +02:00
|
|
|
bool LRoomManager::dob_unregister(Node * pDOB)
|
2019-09-13 20:15:25 +02:00
|
|
|
{
|
|
|
|
LRoom * pRoom = GetRoomFromDOB(pDOB);
|
2019-09-16 15:23:10 +02:00
|
|
|
|
|
|
|
if (pRoom)
|
|
|
|
{
|
|
|
|
unsigned int dob_id = pRoom->DOB_Find(pDOB);
|
2019-09-17 10:32:18 +02:00
|
|
|
return pRoom->DOB_Remove(dob_id);
|
2019-09-16 15:23:10 +02:00
|
|
|
}
|
2019-09-17 10:32:18 +02:00
|
|
|
|
|
|
|
return false;
|
2019-09-13 20:15:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 20:26:18 +02:00
|
|
|
int LRoomManager::dob_get_room_id(Node * pDOB)
|
|
|
|
{
|
|
|
|
return Obj_GetRoomNum(pDOB);
|
|
|
|
}
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-15 20:26:18 +02:00
|
|
|
Node * LRoomManager::rooms_get_room(int room_id)
|
|
|
|
{
|
|
|
|
const LRoom * pRoom = GetRoom(room_id);
|
|
|
|
|
|
|
|
if (!pRoom)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return pRoom->GetGodotRoom();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
// turn on and off culling for debugging
|
|
|
|
void LRoomManager::rooms_set_active(bool bActive)
|
|
|
|
{
|
|
|
|
if (bActive == m_bActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_bActive = bActive;
|
|
|
|
|
|
|
|
if (m_bActive)
|
|
|
|
{
|
|
|
|
// clear these to ensure the system is initialized
|
|
|
|
m_pCurr_VisibleRoomList->clear();
|
|
|
|
m_pPrev_VisibleRoomList->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// show all
|
|
|
|
for (int n=0; n<m_Rooms.size(); n++)
|
|
|
|
{
|
|
|
|
LRoom &lroom = m_Rooms[n];
|
|
|
|
lroom.Show_All();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LRoomManager::rooms_set_logging(int level)
|
|
|
|
{
|
|
|
|
// 0 is no logging, 6 is max logging (i.e. reverse of the priorities in the code)
|
|
|
|
Lawn::LDebug::m_iLoggingLevel = 6-level;
|
|
|
|
}
|
|
|
|
|
|
|
|
// provide debugging output on the next frame
|
|
|
|
void LRoomManager::rooms_log_frame()
|
|
|
|
{
|
|
|
|
Lawn::LDebug::m_bRunning = false;
|
|
|
|
}
|
|
|
|
|
2019-09-15 20:26:18 +02:00
|
|
|
|
|
|
|
void LRoomManager::rooms_set_camera(Node * pCam)
|
2019-09-12 16:51:33 +02:00
|
|
|
{
|
|
|
|
m_cameraID = 0;
|
|
|
|
|
|
|
|
if (!pCam)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Camera * pCamera = Object::cast_to<Camera>(pCam);
|
|
|
|
if (!pCamera)
|
|
|
|
{
|
|
|
|
WARN_PRINT("Not a camera");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cameraID = pCam->get_instance_id();
|
2019-09-13 20:15:25 +02:00
|
|
|
|
|
|
|
// use this temporarily to force debug
|
2019-09-18 12:04:02 +02:00
|
|
|
// rooms_log_frame();
|
2019-09-12 16:51:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:09:36 +02:00
|
|
|
// convert empties and meshes to rooms and portals
|
2019-09-15 20:26:18 +02:00
|
|
|
void LRoomManager::rooms_convert()
|
2019-09-11 09:09:36 +02:00
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
LRoomConverter conv;
|
|
|
|
conv.Convert(*this);
|
2019-09-11 09:09:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
// debugging emulate view frustum
|
|
|
|
void LRoomManager::FrameUpdate_FrustumOnly()
|
|
|
|
{
|
|
|
|
// NYI
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:07:43 +02:00
|
|
|
void LRoomManager::FrameUpdate()
|
|
|
|
{
|
2019-09-12 16:51:33 +02:00
|
|
|
if (Engine::get_singleton()->is_editor_hint())
|
|
|
|
{
|
|
|
|
WARN_PRINT_ONCE("LRoomManager::FrameUpdate should not be called in editor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
// could turn off internal processing? not that important
|
|
|
|
if (!m_bActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_bFrustumOnly)
|
|
|
|
{
|
|
|
|
// debugging emulate view frustum
|
|
|
|
FrameUpdate_FrustumOnly();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// we keep a frame counter to prevent visiting things multiple times on the same frame in recursive functions
|
2019-09-13 20:15:25 +02:00
|
|
|
m_uiFrameCounter++;
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
LPRINT(5, "\nFRAME " + itos(m_uiFrameCounter));
|
|
|
|
|
2019-09-15 17:16:09 +02:00
|
|
|
// clear the visible room list to write to each frame
|
|
|
|
m_pCurr_VisibleRoomList->clear();
|
|
|
|
|
2019-09-13 20:15:25 +02:00
|
|
|
// get the camera desired and make into lcamera
|
|
|
|
Camera * pCamera = 0;
|
|
|
|
if (m_cameraID)
|
|
|
|
{
|
|
|
|
Object *pObj = ObjectDB::get_instance(m_cameraID);
|
|
|
|
pCamera = Object::cast_to<Camera>(pObj);
|
|
|
|
}
|
|
|
|
else
|
2019-09-15 16:39:01 +02:00
|
|
|
// camera not set .. do nothing
|
2019-09-13 20:15:25 +02:00
|
|
|
return;
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// camera not a camera?? shouldn't happen but we'll check
|
2019-09-13 20:15:25 +02:00
|
|
|
if (!pCamera)
|
2019-09-12 12:07:43 +02:00
|
|
|
return;
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// Which room is the camera currently in?
|
2019-09-13 20:15:25 +02:00
|
|
|
LRoom * pRoom = GetRoomFromDOB(pCamera);
|
2019-09-12 12:07:43 +02:00
|
|
|
|
|
|
|
if (!pRoom)
|
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
WARN_PRINT_ONCE("LRoomManager::FrameUpdate : Camera is not in an LRoom");
|
2019-09-12 12:07:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// as we hit visible rooms we will mark them in a bitset, so we can hide any rooms
|
|
|
|
// that are showing that haven't been hit this frame
|
2019-09-12 16:51:33 +02:00
|
|
|
m_BF_visible_rooms.Blank();
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// lcamera contains the info needed for culling
|
2019-09-12 12:07:43 +02:00
|
|
|
LCamera cam;
|
|
|
|
cam.m_ptPos = Vector3(0, 0, 0);
|
|
|
|
cam.m_ptDir = Vector3 (-1, 0, 0);
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// reset the planes pool for another frame
|
2019-09-12 20:53:24 +02:00
|
|
|
m_Pool.Reset();
|
2019-09-15 16:39:01 +02:00
|
|
|
|
|
|
|
// the first set of planes are allocated and filled with the view frustum planes
|
|
|
|
// Note that the visual server doesn't actually need to do view frustum culling as a result...
|
|
|
|
// (but is still doing it for now)
|
2019-09-12 20:53:24 +02:00
|
|
|
unsigned int pool_member = m_Pool.Request();
|
|
|
|
assert (pool_member != -1);
|
|
|
|
|
|
|
|
LVector<Plane> &planes = m_Pool.Get(pool_member);
|
|
|
|
planes.clear();
|
2019-09-12 12:07:43 +02:00
|
|
|
|
2019-09-12 16:51:33 +02:00
|
|
|
// get the camera desired and make into lcamera
|
2019-09-13 20:15:25 +02:00
|
|
|
assert (pCamera);
|
2019-09-15 16:39:01 +02:00
|
|
|
Transform tr = pCamera->get_global_transform();
|
|
|
|
cam.m_ptPos = tr.origin;
|
|
|
|
cam.m_ptDir = tr.basis.get_row(2); // or possibly get_axis .. z is what we want
|
2019-09-12 16:51:33 +02:00
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// luckily godot already has a function to return a list of the camera clipping planes
|
|
|
|
planes.copy_from(pCamera->get_frustum());
|
2019-09-12 16:51:33 +02:00
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// the whole visibility algorithm is recursive, spreading out from the camera room,
|
|
|
|
// rendering through any portals in view into other rooms, etc etc
|
|
|
|
pRoom->DetermineVisibility_Recursive(*this, 0, cam, planes);
|
2019-09-12 16:51:33 +02:00
|
|
|
|
|
|
|
// finally hide all the rooms that are currently visible but not in the visible bitfield as having been hit
|
2019-09-15 17:16:09 +02:00
|
|
|
|
|
|
|
// to get started
|
|
|
|
if (!m_pPrev_VisibleRoomList->size())
|
2019-09-12 16:51:33 +02:00
|
|
|
{
|
2019-09-15 17:16:09 +02:00
|
|
|
// NOTE this will be done more efficiently, but is okay to start with
|
|
|
|
for (int n=0; n<m_Rooms.size(); n++)
|
2019-09-12 16:51:33 +02:00
|
|
|
{
|
2019-09-15 17:16:09 +02:00
|
|
|
if (!m_BF_visible_rooms.GetBit(n))
|
|
|
|
{
|
2019-09-16 15:23:10 +02:00
|
|
|
m_Rooms[n].Hide_All();
|
2019-09-15 17:16:09 +02:00
|
|
|
}
|
2019-09-12 16:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-15 17:16:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// hide all rooms that were visible last frame but aren't visible this frame
|
|
|
|
for (int n=0; n<m_pPrev_VisibleRoomList->size(); n++)
|
|
|
|
{
|
|
|
|
int r = (*m_pPrev_VisibleRoomList)[n];
|
|
|
|
|
|
|
|
if (!m_BF_visible_rooms.GetBit(r))
|
2019-09-16 15:23:10 +02:00
|
|
|
m_Rooms[r].Hide_All();
|
2019-09-15 17:16:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-09-12 12:07:43 +02:00
|
|
|
|
2019-09-16 15:23:10 +02:00
|
|
|
|
|
|
|
// and hide all the dobs that are in visible rooms that haven't been made visible
|
|
|
|
// if (m_pCurr_VisibleRoomList->size() == 0)
|
|
|
|
// print_line("WARNING : vis room list size is 0");
|
|
|
|
|
|
|
|
for (int n=0; n<m_pCurr_VisibleRoomList->size(); n++)
|
|
|
|
{
|
|
|
|
int r = (*m_pCurr_VisibleRoomList)[n];
|
|
|
|
m_Rooms[r].FinalizeVisibility(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// swap the current and previous visible room list
|
|
|
|
LVector<int> * pTemp = m_pCurr_VisibleRoomList;
|
|
|
|
m_pCurr_VisibleRoomList = m_pPrev_VisibleRoomList;
|
|
|
|
m_pPrev_VisibleRoomList = pTemp;
|
|
|
|
|
|
|
|
|
|
|
|
// hide all the DOB
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// when running, emit less debugging output so as not to choke the IDE
|
2019-09-18 12:04:02 +02:00
|
|
|
Lawn::LDebug::m_bRunning = true;
|
2019-09-12 12:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LRoomManager::_notification(int p_what) {
|
|
|
|
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2019-09-15 16:39:01 +02:00
|
|
|
// turn on process, unless we are in the editor
|
2019-09-12 12:07:43 +02:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint())
|
|
|
|
set_process_internal(true);
|
2019-09-12 16:51:33 +02:00
|
|
|
else
|
|
|
|
set_process_internal(false);
|
2019-09-12 12:07:43 +02:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_INTERNAL_PROCESS: {
|
|
|
|
FrameUpdate();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:09:36 +02:00
|
|
|
|
|
|
|
void LRoomManager::_bind_methods()
|
|
|
|
{
|
2019-09-15 16:39:01 +02:00
|
|
|
// main functions
|
2019-09-15 20:26:18 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("rooms_convert"), &LRoomManager::rooms_convert);
|
|
|
|
ClassDB::bind_method(D_METHOD("rooms_set_camera"), &LRoomManager::rooms_set_camera);
|
|
|
|
ClassDB::bind_method(D_METHOD("rooms_get_room"), &LRoomManager::rooms_get_room);
|
2019-09-13 20:15:25 +02:00
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
// debugging
|
|
|
|
ClassDB::bind_method(D_METHOD("rooms_set_logging"), &LRoomManager::rooms_set_logging);
|
|
|
|
ClassDB::bind_method(D_METHOD("rooms_log_frame"), &LRoomManager::rooms_log_frame);
|
|
|
|
ClassDB::bind_method(D_METHOD("rooms_set_active"), &LRoomManager::rooms_set_active);
|
|
|
|
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// functions to add dynamic objects to the culling system
|
|
|
|
// Note that these should not be placed directly in rooms, the system will 'soft link' to them
|
|
|
|
// so they can be held, e.g. in pools elsewhere in the scene graph
|
2019-09-15 20:26:18 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("dob_register"), &LRoomManager::dob_register);
|
|
|
|
ClassDB::bind_method(D_METHOD("dob_unregister"), &LRoomManager::dob_unregister);
|
|
|
|
ClassDB::bind_method(D_METHOD("dob_update"), &LRoomManager::dob_update);
|
|
|
|
ClassDB::bind_method(D_METHOD("dob_teleport"), &LRoomManager::dob_teleport);
|
|
|
|
|
2019-09-18 12:04:02 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("dob_register_hint"), &LRoomManager::dob_register_hint);
|
|
|
|
ClassDB::bind_method(D_METHOD("dob_teleport_hint"), &LRoomManager::dob_teleport_hint);
|
|
|
|
|
|
|
|
|
2019-09-15 20:26:18 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("dob_get_room_id"), &LRoomManager::dob_get_room_id);
|
2019-09-11 09:09:36 +02:00
|
|
|
}
|