<commit_before>/*********************************
** Tsunagari Tile Engine        **
** world.cpp                    **
** Copyright 2011-2012 OmegaSDG **
*********************************/

#include <Gosu/Utility.hpp>

#include "area-tmx.h"
#include "common.h"
#include "log.h"
#include "python.h"
#include "resourcer.h"
#include "window.h"
#include "world.h"
#include "xml.h"

#define ASSERT(x)  if (!(x)) return false

static World* globalWorld = NULL;

World* World::instance()
{
	return globalWorld;
}

World::World()
	: player(NULL)
{
	globalWorld = this;
}

World::~World()
{
}

bool World::init()
{
	if (!processDescriptor()) // Try to load in descriptor.
		return false;

	music.reset(new Music());

	if (!player.init(playerentity))
		return false;
	player.setPhase("down");

	if (onLoadScript.size()) {
		pythonSetGlobal("Player", (Entity*)&player);
		Resourcer* rc = Resourcer::instance();
		rc->runPythonScript(onLoadScript);
	}

	view = new Viewport(viewport);
	view->trackEntity(&player);

	AreaPtr area = getArea(entry.area);
	if (!area)
		return false;
	focusArea(area, entry.coords);
	return true;
}

void World::buttonDown(const Gosu::Button btn)
{
	area->buttonDown(btn);
}

void World::buttonUp(const Gosu::Button btn)
{
	area->buttonUp(btn);
}

void World::draw()
{
	GameWindow& window = GameWindow::getWindow();
	Gosu::Graphics& graphics = window.graphics();

	drawLetterbox();
	drawAreaBorders();
	graphics.pushTransform(getTransform());
	area->draw();
	graphics.popTransform();
}

bool World::needsRedraw() const
{
	return area->needsRedraw();
}

void World::update(unsigned long dt)
{
	// Prevent the Area from being deleted during this function call.
	// Otherwise if it accesses its member variables and we've overwritten
	// the memory, bad things happen.
	boost::shared_ptr<Area> safe(area);

	area->update(dt);
}

AreaPtr World::getArea(const std::string& filename, int flags)
{
	if (conf.cacheEnabled && (flags & GETAREA_ALWAYS_CREATE) == false) {
		AreaMap::iterator entry = areas.find(filename);
		if (entry != areas.end())
			return entry->second;
	}

	AreaPtr newArea(
		new AreaTMX(view, &player, music.get(), filename)
	);

	if (!newArea->init())
		newArea = AreaPtr();
	if (conf.cacheEnabled)
		areas[filename] = newArea;
	return newArea;
}

AreaPtr World::getFocusedArea()
{
	return area;
}

void World::focusArea(AreaPtr area, vicoord playerPos)
{
	Log::info("World", area->getDescriptor() + ": focused");
	this->area = area;
	player.setArea(area.get());
	player.setTileCoords(playerPos);
	view->setArea(area.get());
	area->focus();
}

std::string World::getAreaLoadScript()
{
	return onAreaLoadScript;
}

bool World::processDescriptor()
{
	XMLRef doc;
	XMLNode root;

	Resourcer* rc = Resourcer::instance();
	ASSERT(doc = rc->getXMLDoc("world.conf", "world.dtd"));<commit_msg>remove area focus log msg<commit_after>/*********************************
** Tsunagari Tile Engine        **
** world.cpp                    **
** Copyright 2011-2012 OmegaSDG **
*********************************/

#include <Gosu/Utility.hpp>

#include "area-tmx.h"
#include "common.h"
#include "log.h"
#include "python.h"
#include "resourcer.h"
#include "window.h"
#include "world.h"
#include "xml.h"

#define ASSERT(x)  if (!(x)) return false

static World* globalWorld = NULL;

World* World::instance()
{
	return globalWorld;
}

World::World()
	: player(NULL)
{
	globalWorld = this;
}

World::~World()
{
}

bool World::init()
{
	if (!processDescriptor()) // Try to load in descriptor.
		return false;

	music.reset(new Music());

	if (!player.init(playerentity))
		return false;
	player.setPhase("down");

	if (onLoadScript.size()) {
		pythonSetGlobal("Player", (Entity*)&player);
		Resourcer* rc = Resourcer::instance();
		rc->runPythonScript(onLoadScript);
	}

	view = new Viewport(viewport);
	view->trackEntity(&player);

	AreaPtr area = getArea(entry.area);
	if (!area)
		return false;
	focusArea(area, entry.coords);
	return true;
}

void World::buttonDown(const Gosu::Button btn)
{
	area->buttonDown(btn);
}

void World::buttonUp(const Gosu::Button btn)
{
	area->buttonUp(btn);
}

void World::draw()
{
	GameWindow& window = GameWindow::getWindow();
	Gosu::Graphics& graphics = window.graphics();

	drawLetterbox();
	drawAreaBorders();
	graphics.pushTransform(getTransform());
	area->draw();
	graphics.popTransform();
}

bool World::needsRedraw() const
{
	return area->needsRedraw();
}

void World::update(unsigned long dt)
{
	// Prevent the Area from being deleted during this function call.
	// Otherwise if it accesses its member variables and we've overwritten
	// the memory, bad things happen.
	boost::shared_ptr<Area> safe(area);

	area->update(dt);
}

AreaPtr World::getArea(const std::string& filename, int flags)
{
	if (conf.cacheEnabled && (flags & GETAREA_ALWAYS_CREATE) == false) {
		AreaMap::iterator entry = areas.find(filename);
		if (entry != areas.end())
			return entry->second;
	}

	AreaPtr newArea(
		new AreaTMX(view, &player, music.get(), filename)
	);

	if (!newArea->init())
		newArea = AreaPtr();
	if (conf.cacheEnabled)
		areas[filename] = newArea;
	return newArea;
}

AreaPtr World::getFocusedArea()
{
	return area;
}

void World::focusArea(AreaPtr area, vicoord playerPos)
{
	// Log::info("World", area->getDescriptor() + ": focused");
	this->area = area;
	player.setArea(area.get());
	player.setTileCoords(playerPos);
	view->setArea(area.get());
	area->focus();
}

std::string World::getAreaLoadScript()
{
	return onAreaLoadScript;
}

bool World::processDescriptor()
{
	XMLRef doc;
	XMLNode root;

	Resourcer* rc = Resourcer::instance();
	ASSERT(doc = rc->getXMLDoc("world.conf", "world.dtd"));
{'fraction_non_alphanumeric': 0.18481147105682422, 'fraction_numerical': 0.0028323597096831295, 'mean_word_length': 4.997876857749469, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6222003', 'n_tokens_mistral': 1998, 'n_tokens_neox': 1913, 'n_words': 435}