<commit_before>/*
 * Copyright (c) 2012-2015 <NAME> and individual contributors.
 * License: https://github.com/taylor001/crown/blob/master/LICENSE
 */

#include "world.h"
#include "error.h"
#include "resource_manager.h"
#include "debug_line.h"
#include "actor.h"
#include "lua_environment.h"
#include "level_resource.h"
#include "memory.h"
#include "matrix4x4.h"
#include "int_setting.h"
#include <new>

namespace crown
{

static IntSetting g_physics_debug("physics.debug", "Enable physics debug rendering.", 0, 0, 1);

World::World(ResourceManager& rm, LuaEnvironment& env)
	: _resource_manager(&rm)
	, _lua_environment(&env)
	, m_unit_pool(default_allocator(), CE_MAX_UNITS, sizeof(Unit), CE_ALIGNOF(Unit))
	, m_camera_pool(default_allocator(), CE_MAX_CAMERAS, sizeof(Camera), CE_ALIGNOF(Camera))
	, _scenegraph_manager(NULL)
	, _sprite_animation_player(NULL)
	, _render_world(NULL)
	, _physics_world(NULL)
	, _sound_world(NULL)
	, _events(default_allocator())
	, _lines(NULL)
{
	_scenegraph_manager = CE_NEW(default_allocator(), SceneGraphManager);
	_sprite_animation_player = CE_NEW(default_allocator(), SpriteAnimationPlayer);
	_render_world = CE_NEW(default_allocator(), RenderWorld);
	_physics_world = CE_NEW(default_allocator(), PhysicsWorld)(*this);
	_sound_world = SoundWorld::create(default_allocator());
	_lines = create_debug_line(false);
}

World::~World()
{
	// Destroy all units
	for (uint32_t i = 0; i < id_array::size(m_units); i++)
	{
		CE_DELETE(m_unit_pool, m_units[i]);
	}

	destroy_debug_line(_lines);
	SoundWorld::destroy(default_allocator(), _sound_world);
	CE_DELETE(default_allocator(), _physics_world);
	CE_DELETE(default_allocator(), _render_world);
	CE_DELETE(default_allocator(), _sprite_animation_player);
	CE_DELETE(default_allocator(), _scenegraph_manager);
}

UnitId World::spawn_unit(const UnitResource* ur, const Vector3& pos, const Quaternion& rot)
{
	Unit* u = (Unit*) m_unit_pool.allocate(sizeof(Unit), CE_ALIGNOF(Unit));
	const UnitId unit_id = id_array::create(m_units, u);
	new (u) Unit(*this, unit_id, ur, Matrix4x4(rot, pos));

	post_unit_spawned_event(unit_id);
	return unit_id;
}

UnitId World::spawn_unit(StringId64 name, const Vector3& pos, const Quaternion& rot)
{
	UnitResource* ur = (UnitResource*)_resource_manager->get(UNIT_TYPE, name);
	return spawn_unit(ur, pos, rot);
}

void World::destroy_unit(UnitId id)
{
	CE_DELETE(m_unit_pool, id_array::get(m_units, id));
	id_array::destroy(m_units, id);
	post_unit_destroyed_event(id);
}

void World::reload_units(UnitResource* old_ur, UnitResource* new_ur)
{
	for (uint32_t i = 0; i < id_array::size(m_units); i++)
	{
		if (m_units[i]->resource() == old_ur)
		{
			m_units[i]->reload(new_ur);
		}
	}
}

uint32_t World::num_units() const
{
	return id_array::size(m_units);
}

void World::units(Array<UnitId>& units) const
{
	for (uint32_t i = 0; i < id_array::size(m_units); i++)
	{
		array::push_back(units, m_units[i]->id());
	}
}

void World::link_unit(UnitId child, UnitId parent, int32_t node)
{
	Unit* parent_unit = get_unit(parent);
	parent_unit->link_node(0, node);
}

void World::unlink_unit(UnitId /*child*/)
{
}

Unit* World::get_unit(UnitId id)
{
	return id_array::get(m_units, id);
}

Camera* World::get_camera(CameraId id)
{
	return id_array::get(m_cameras, id);
}

void World::update_animations(float dt)
{
	_sprite_animation_player->update(dt);
}

void World::update_scene(float dt)
{
	_physics_world->update(dt);
	_scenegraph_manager->update();

	for (uint32_t i = 0; i < id_array::size(m_units); i++)
	{
		m_units[i]->update();
	}

	_sound_world->update();

	process_physics_events();
}

void World::update(float dt)
{
	update_animations(dt);
	update_scene(dt);
}

void World::render(Camera* camera)
{
	_render_world->update(camera->view_matrix(), camera->projection_matrix(), camera->_view_x, camera->_view_y,
		camera->_view_width, camera->_view_height);

	if (g_physics_debug == 1)
		_physics_world->draw_debug(*_lines);
}

CameraId World::create_camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far)
{
	Camera* camera = CE_NEW(m_camera_pool, Camera)(sg, node, type, near, far);

	return id_array::create(m_cameras, camera);
}

void World::destroy_camera(CameraId id)
{
	CE_DELETE(m_camera_pool, id_array::get(m_cameras, id));
	id_array::destroy(m_cameras, id);
}

SoundInstanceId World::play_sound(const SoundResource* sr, const bool loop, const float volume, const Vector3& pos, const float range)
{
	return _sound_world->play(sr, loop, volume, pos);
}

SoundInstanceId World::play_sound(StringId64 name, const bool loop, const float volume, const Vector3& pos, const float range)
{
	const SoundResource* sr = (const SoundResource*)_resource_manager->get(SOUND_TYPE, name);
	play_sound(sr, loop, volume, pos, range);
}

void World::stop_sound(SoundInstanceId id)
{<commit_msg>Fix missing return value<commit_after>/*
 * Copyright (c) 2012-2015 <NAME> and individual contributors.
 * License: https://github.com/taylor001/crown/blob/master/LICENSE
 */

#include "world.h"
#include "error.h"
#include "resource_manager.h"
#include "debug_line.h"
#include "actor.h"
#include "lua_environment.h"
#include "level_resource.h"
#include "memory.h"
#include "matrix4x4.h"
#include "int_setting.h"
#include <new>

namespace crown
{

static IntSetting g_physics_debug("physics.debug", "Enable physics debug rendering.", 0, 0, 1);

World::World(ResourceManager& rm, LuaEnvironment& env)
	: _resource_manager(&rm)
	, _lua_environment(&env)
	, m_unit_pool(default_allocator(), CE_MAX_UNITS, sizeof(Unit), CE_ALIGNOF(Unit))
	, m_camera_pool(default_allocator(), CE_MAX_CAMERAS, sizeof(Camera), CE_ALIGNOF(Camera))
	, _scenegraph_manager(NULL)
	, _sprite_animation_player(NULL)
	, _render_world(NULL)
	, _physics_world(NULL)
	, _sound_world(NULL)
	, _events(default_allocator())
	, _lines(NULL)
{
	_scenegraph_manager = CE_NEW(default_allocator(), SceneGraphManager);
	_sprite_animation_player = CE_NEW(default_allocator(), SpriteAnimationPlayer);
	_render_world = CE_NEW(default_allocator(), RenderWorld);
	_physics_world = CE_NEW(default_allocator(), PhysicsWorld)(*this);
	_sound_world = SoundWorld::create(default_allocator());
	_lines = create_debug_line(false);
}

World::~World()
{
	// Destroy all units
	for (uint32_t i = 0; i < id_array::size(m_units); i++)
	{
		CE_DELETE(m_unit_pool, m_units[i]);
	}

	destroy_debug_line(_lines);
	SoundWorld::destroy(default_allocator(), _sound_world);
	CE_DELETE(default_allocator(), _physics_world);
	CE_DELETE(default_allocator(), _render_world);
	CE_DELETE(default_allocator(), _sprite_animation_player);
	CE_DELETE(default_allocator(), _scenegraph_manager);
}

UnitId World::spawn_unit(const UnitResource* ur, const Vector3& pos, const Quaternion& rot)
{
	Unit* u = (Unit*) m_unit_pool.allocate(sizeof(Unit), CE_ALIGNOF(Unit));
	const UnitId unit_id = id_array::create(m_units, u);
	new (u) Unit(*this, unit_id, ur, Matrix4x4(rot, pos));

	post_unit_spawned_event(unit_id);
	return unit_id;
}

UnitId World::spawn_unit(StringId64 name, const Vector3& pos, const Quaternion& rot)
{
	UnitResource* ur = (UnitResource*)_resource_manager->get(UNIT_TYPE, name);
	return spawn_unit(ur, pos, rot);
}

void World::destroy_unit(UnitId id)
{
	CE_DELETE(m_unit_pool, id_array::get(m_units, id));
	id_array::destroy(m_units, id);
	post_unit_destroyed_event(id);
}

void World::reload_units(UnitResource* old_ur, UnitResource* new_ur)
{
	for (uint32_t i = 0; i < id_array::size(m_units); i++)
	{
		if (m_units[i]->resource() == old_ur)
		{
			m_units[i]->reload(new_ur);
		}
	}
}

uint32_t World::num_units() const
{
	return id_array::size(m_units);
}

void World::units(Array<UnitId>& units) const
{
	for (uint32_t i = 0; i < id_array::size(m_units); i++)
	{
		array::push_back(units, m_units[i]->id());
	}
}

void World::link_unit(UnitId child, UnitId parent, int32_t node)
{
	Unit* parent_unit = get_unit(parent);
	parent_unit->link_node(0, node);
}

void World::unlink_unit(UnitId /*child*/)
{
}

Unit* World::get_unit(UnitId id)
{
	return id_array::get(m_units, id);
}

Camera* World::get_camera(CameraId id)
{
	return id_array::get(m_cameras, id);
}

void World::update_animations(float dt)
{
	_sprite_animation_player->update(dt);
}

void World::update_scene(float dt)
{
	_physics_world->update(dt);
	_scenegraph_manager->update();

	for (uint32_t i = 0; i < id_array::size(m_units); i++)
	{
		m_units[i]->update();
	}

	_sound_world->update();

	process_physics_events();
}

void World::update(float dt)
{
	update_animations(dt);
	update_scene(dt);
}

void World::render(Camera* camera)
{
	_render_world->update(camera->view_matrix(), camera->projection_matrix(), camera->_view_x, camera->_view_y,
		camera->_view_width, camera->_view_height);

	if (g_physics_debug == 1)
		_physics_world->draw_debug(*_lines);
}

CameraId World::create_camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far)
{
	Camera* camera = CE_NEW(m_camera_pool, Camera)(sg, node, type, near, far);

	return id_array::create(m_cameras, camera);
}

void World::destroy_camera(CameraId id)
{
	CE_DELETE(m_camera_pool, id_array::get(m_cameras, id));
	id_array::destroy(m_cameras, id);
}

SoundInstanceId World::play_sound(const SoundResource* sr, const bool loop, const float volume, const Vector3& pos, const float range)
{
	return _sound_world->play(sr, loop, volume, pos);
}

SoundInstanceId World::play_sound(StringId64 name, const bool loop, const float volume, const Vector3& pos, const float range)
{
	const SoundResource* sr = (const SoundResource*)_resource_manager->get(SOUND_TYPE, name);
	return play_sound(sr, loop, volume, pos, range);
}

void World::stop_sound(SoundInstanceId id)
{
{'fraction_non_alphanumeric': 0.146900826446281, 'fraction_numerical': 0.009504132231404958, 'mean_word_length': 7.087719298245614, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6163263', 'n_tokens_mistral': 3729, 'n_tokens_neox': 3624, 'n_words': 682}