Files
funnygame/game/server/worldsystem.cpp
2026-03-18 18:41:28 +02:00

123 lines
2.9 KiB
C++

#include "worldsystem.h"
#include "game.h"
#include "entitysystem.h"
#include "baseentity.h"
#include "netprotocol.h"
void CWorldSystem::LoadEntity( IJSONValue *pObject )
{
if (pObject->GetType() != JSON_PARAMETER_OBJECT)
return;
IJSONObject *pObjectRoot = pObject->GetObject();
const char *szClassName = NULL;
for ( int i = 0; i < pObjectRoot->GetCount(); i++)
{
if (V_strcmp(pObjectRoot->GetParameterName(i),"classname"))
continue;
IJSONValue *pValue = pObjectRoot->GetParameter(i);
if (pValue->GetType() != JSON_PARAMETER_STRING)
{
V_printf("classname must be a string\n");
break;
}
szClassName = pValue->GetStringValue();
break;
}
if (szClassName == NULL)
return;
CBaseEntity *pEntity = EntitySystem()->CreateByClassname(szClassName, NULL);
if (!pEntity)
return;
for ( int i = 0; i < pObjectRoot->GetCount(); i++)
{
if (!V_strcmp(pObjectRoot->GetParameterName(i),"classname"))
continue;
IJSONValue *pValue = pObjectRoot->GetParameter(i);
typedescription_t *pDataMap = pEntity->FindDataByMapName(pObjectRoot->GetParameterName(i));
if (!pDataMap)
continue;
union {
void *pData;
float *pfData;
Vector *pvData;
char *pcData;
};
pData = (char*)pEntity+pDataMap->m_iFieldOffset;
switch (pDataMap->m_eFieldType)
{
case FIELD_STRING:
if (pValue->GetType() != JSON_PARAMETER_STRING)
continue;
V_strncpy(pcData, pValue->GetStringValue(), pDataMap->m_uFieldSize);
continue;
case FIELD_FLOAT3:
if (pValue->GetType() != JSON_PARAMETER_ARRAY)
continue;
if (pValue->GetArray()->GetCount() == 3)
{
if ( pValue->GetArray()->GetParameter(0)->GetType()
== JSON_PARAMETER_NUMBER )
pvData->x = pValue->GetArray()->GetParameter(0)->GetNumberValue();
if ( pValue->GetArray()->GetParameter(1)->GetType()
== JSON_PARAMETER_NUMBER )
pvData->y = pValue->GetArray()->GetParameter(1)->GetNumberValue();
if ( pValue->GetArray()->GetParameter(2)->GetType()
== JSON_PARAMETER_NUMBER )
pvData->z = pValue->GetArray()->GetParameter(2)->GetNumberValue();
}
continue;
default:
break;
}
}
pEntity->Spawn();
}
bool CWorldSystem::LoadMap( const char *szName )
{
// unload the map
IFileHandle *pHandle = filesystem->Open(szName, FILEMODE_READ);
if (!pHandle)
return 0;
CUtlString szProperties = filesystem->ReadString(pHandle);
IJSONValue *pRoot = JSONManager()->ReadString(szProperties);
filesystem->Close(pHandle);
if (!pRoot)
return 0;
// can't fail at this point
switch (pRoot->GetType())
{
case JSON_PARAMETER_ARRAY:
{
for ( uint32_t u = 0; u < pRoot->GetArray()->GetCount(); u++ )
{
IJSONValue *pObjectValue = pRoot->GetArray()->GetParameter(u);
if ( !pObjectValue )
continue;
LoadEntity(pObjectValue);
}
JSONManager()->FreeValue(pRoot);
return 1;
}
default:
return 0;
}
}
static CWorldSystem s_worldSystem;
CWorldSystem *g_pWorldSystem = &s_worldSystem;