<commit_before>/*
 * Copyright (c) 2009, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "Level.h"

#include <assert.h>

#include "logic/race/Checkpoint.h"

namespace Race {

Level::Level() :
	m_loaded(false),
	m_width(0),
	m_height(0)
{
}

void Level::initialize(const CL_String &p_filename)
{
	loadFromFile(p_filename);
}

void Level::destroy()
{
	m_blocks.clear();
	m_track.clear();
	m_loaded = false;
}

Level::~Level() {
	m_blocks.clear();
}

CL_String8 Level::readLine(CL_File& p_file) {
	CL_String8 line = p_file.read_string_text("", "\n", false);
	p_file.seek(1, CL_File::seek_cur);

	return line;
}

void Level::loadFromFile(const CL_String& p_filename)
{
	assert(!m_loaded && "level is already loaded");

	try {
		CL_File file(p_filename, CL_File::open_existing, CL_File::access_read);


		CL_DomDocument document(file);
		const CL_DomElement root = document.get_document_element();

		// load meta element
		const CL_DomNode metaNode = root.named_item("meta");
		loadMetaElement(metaNode);

		// load level's content
		const CL_DomNode contentNode = root.named_item("content");

		// load track
		const CL_DomNode trackNode = contentNode.named_item("track");
		loadTrackElement(trackNode);

		// load track bounds
		const CL_DomNode boundsNode = contentNode.named_item("bounds");
		loadBoundsElement(boundsNode);

		file.close();
		m_loaded = true;

	} catch (CL_Exception e) {
		CL_Console::write_line(e.message);
	}

}

void Level::loadMetaElement(const CL_DomNode &p_metaNode)
{
	m_width = p_metaNode.select_int("size/width");
	m_height = p_metaNode.select_int("size/height");

	cl_log_event("race", "level size set to %1 x %2", m_width, m_height);
}

void Level::loadTrackElement(const CL_DomNode &p_trackNode)
{
	// build block type map
	typedef std::map<CL_String, Common::GroundBlockType> blockMap_t;
	blockMap_t blockMap;
	blockMap_t::iterator blockMapItor;

	blockMap["vert"] = Common::BT_STREET_VERT;
	blockMap["horiz"] = Common::BT_STREET_HORIZ;
	blockMap["turn_bottom_right"] = Common::BT_TURN_BOTTOM_RIGHT;
	blockMap["turn_bottom_left"] = Common::BT_TURN_BOTTOM_LEFT;
	blockMap["turn_top_right"] = Common::BT_TURN_TOP_RIGHT;
	blockMap["turn_top_left"] = Common::BT_TURN_TOP_LEFT;
	blockMap["start_line_up"] = Common::BT_START_LINE_UP;

	// prepare level blocks
	const int blocksCount = m_width * m_height;
	m_blocks.clear();
	m_blocks.reserve(blocksCount);

	for (int i = 0; i < blocksCount; ++i) {
		m_blocks.push_back(CL_SharedPtr<Block>(new Block(Common::BT_GRASS)));
	}

	// read blocks
	const CL_DomNodeList blockList = p_trackNode.get_child_nodes();
	const int blockListSize = blockList.get_length();

	cl_log_event("debug", "Track node child count: %1", blockListSize);

	CL_Pointf lastCP; // last checkpoint

	for (int i = 0; i < blockListSize; ++i) {
		const CL_DomNode blockNode = blockList.item(i);

		if (blockNode.get_node_name() == "block") {
			CL_DomNamedNodeMap attrs = blockNode.get_attributes();

			const int x = CL_StringHelp::local8_to_int(attrs.get_named_item("x").get_node_value());
			const int y = CL_StringHelp::local8_to_int(attrs.get_named_item("y").get_node_value());
			const CL_String typeStr = attrs.get_named_item("type").get_node_value();

			if (x < 0 || y < 0 || x >= m_width || y >= m_height) {
				cl_log_event("debug", "coords x=%1, y=%2", x, y);
				throw CL_Exception("Blocks coords out of bounds");
			}

			blockMapItor = blockMap.find(typeStr);

			if (blockMapItor != blockMap.end()) {

				const Common::GroundBlockType blockType = blockMapItor->second;
				m_blocks[m_width * y + x]->setType(blockType);

				// add checkpoint to track
				if (blockType == Common::BT_START_LINE_UP) {
					lastCP = CL_Pointf((x + 0.5f) * Block::WIDTH, (y + 0.2f) * Block::WIDTH);
					const CL_Pointf firstCP((x + 0.5f) * Block::WIDTH, (y + 0.2 - 0.01f) * Block::WIDTH);

					m_track.addCheckpointAtPosition(firstCP);
				} else {
					const CL_Pointf checkPosition((x + 0.5f) * Block::WIDTH, (y + 0.5f) * Block::WIDTH);

					m_track.addCheckpointAtPosition(checkPosition);
				}

				// add resistance geometry based on block
				CL_SharedPtr<RaceResistance::Geometry> resGeom = buildResistanceGeometry(x, y, blockType);
				m_resistanceMap.addGeometry(resGeom, 0.3f);

			} else {
				cl_log_event("race", "Unknown block type: %1", typeStr);
			}

		} else {
			cl_log_event("race", "Unknown node '%1', ignoring", blockNode.get_node_name());
		}

	}

	m_track.addCheckpointAtPosition(lastCP);
	m_track.close();

}

CL_SharedPtr<RaceResistance::Geometry> Level::buildResistanceGeometry(int p_x, int p_y, Common::GroundBlockType p_blockType) const
{
	CL_SharedPtr<RaceResistance::Geometry> geom(new RaceResistance::Geometry());

	CL_Pointf p, q;<commit_msg>Added global resistance and it looks good<commit_after>/*
 * Copyright (c) 2009, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "Level.h"

#include <assert.h>

#include "logic/race/Checkpoint.h"

namespace Race {

Level::Level() :
	m_loaded(false),
	m_width(0),
	m_height(0)
{
}

void Level::initialize(const CL_String &p_filename)
{
	loadFromFile(p_filename);
}

void Level::destroy()
{
	m_blocks.clear();
	m_track.clear();
	m_loaded = false;
}

Level::~Level() {
	m_blocks.clear();
}

CL_String8 Level::readLine(CL_File& p_file) {
	CL_String8 line = p_file.read_string_text("", "\n", false);
	p_file.seek(1, CL_File::seek_cur);

	return line;
}

void Level::loadFromFile(const CL_String& p_filename)
{
	assert(!m_loaded && "level is already loaded");

	try {
		CL_File file(p_filename, CL_File::open_existing, CL_File::access_read);


		CL_DomDocument document(file);
		const CL_DomElement root = document.get_document_element();

		// load meta element
		const CL_DomNode metaNode = root.named_item("meta");
		loadMetaElement(metaNode);

		// load level's content
		const CL_DomNode contentNode = root.named_item("content");

		// load track
		const CL_DomNode trackNode = contentNode.named_item("track");
		loadTrackElement(trackNode);

		// load track bounds
		const CL_DomNode boundsNode = contentNode.named_item("bounds");
		loadBoundsElement(boundsNode);

		file.close();
		m_loaded = true;

	} catch (CL_Exception e) {
		CL_Console::write_line(e.message);
	}

}

void Level::loadMetaElement(const CL_DomNode &p_metaNode)
{
	m_width = p_metaNode.select_int("size/width");
	m_height = p_metaNode.select_int("size/height");

	cl_log_event("race", "level size set to %1 x %2", m_width, m_height);
}

void Level::loadTrackElement(const CL_DomNode &p_trackNode)
{
	// build block type map
	typedef std::map<CL_String, Common::GroundBlockType> blockMap_t;
	blockMap_t blockMap;
	blockMap_t::iterator blockMapItor;

	blockMap["vert"] = Common::BT_STREET_VERT;
	blockMap["horiz"] = Common::BT_STREET_HORIZ;
	blockMap["turn_bottom_right"] = Common::BT_TURN_BOTTOM_RIGHT;
	blockMap["turn_bottom_left"] = Common::BT_TURN_BOTTOM_LEFT;
	blockMap["turn_top_right"] = Common::BT_TURN_TOP_RIGHT;
	blockMap["turn_top_left"] = Common::BT_TURN_TOP_LEFT;
	blockMap["start_line_up"] = Common::BT_START_LINE_UP;

	// prepare level blocks
	const int blocksCount = m_width * m_height;
	m_blocks.clear();
	m_blocks.reserve(blocksCount);

	for (int i = 0; i < blocksCount; ++i) {
		m_blocks.push_back(CL_SharedPtr<Block>(new Block(Common::BT_GRASS)));
	}

	// create global resistance geometry
	CL_SharedPtr<RaceResistance::Geometry> globalResGeom(new RaceResistance::Geometry());
	globalResGeom->addRectangle(CL_Rectf(real(0), real(0), real(m_width), real(m_height)));

	m_resistanceMap.addGeometry(globalResGeom, 1.0f);

	// read blocks
	const CL_DomNodeList blockList = p_trackNode.get_child_nodes();
	const int blockListSize = blockList.get_length();

	cl_log_event("debug", "Track node child count: %1", blockListSize);

	CL_Pointf lastCP; // last checkpoint

	for (int i = 0; i < blockListSize; ++i) {
		const CL_DomNode blockNode = blockList.item(i);

		if (blockNode.get_node_name() == "block") {
			CL_DomNamedNodeMap attrs = blockNode.get_attributes();

			const int x = CL_StringHelp::local8_to_int(attrs.get_named_item("x").get_node_value());
			const int y = CL_StringHelp::local8_to_int(attrs.get_named_item("y").get_node_value());
			const CL_String typeStr = attrs.get_named_item("type").get_node_value();

			if (x < 0 || y < 0 || x >= m_width || y >= m_height) {
				cl_log_event("debug", "coords x=%1, y=%2", x, y);
				throw CL_Exception("Blocks coords out of bounds");
			}

			blockMapItor = blockMap.find(typeStr);

			if (blockMapItor != blockMap.end()) {

				const Common::GroundBlockType blockType = blockMapItor->second;
				m_blocks[m_width * y + x]->setType(blockType);

				// add checkpoint to track
				if (blockType == Common::BT_START_LINE_UP) {
					lastCP = CL_Pointf((x + 0.5f) * Block::WIDTH, (y + 0.2f) * Block::WIDTH);
					const CL_Pointf firstCP((x + 0.5f) * Block::WIDTH, (y + 0.2 - 0.01f) * Block::WIDTH);

					m_track.addCheckpointAtPosition(firstCP);
				} else {
					const CL_Pointf checkPosition((x + 0.5f) * Block::WIDTH, (y + 0.5f) * Block::WIDTH);

					m_track.addCheckpointAtPosition(checkPosition);
				}

				// add resistance geometry based on block
				CL_SharedPtr<RaceResistance::Geometry> resGeom = buildResistanceGeometry(x, y, blockType);
				m_resistanceMap.addGeometry(resGeom, 0.0f);

			} else {
				cl_log_event("race", "Unknown block type: %1", typeStr);
			}

		} else {
			cl_log_event("race", "Unknown node '%1', ignoring", blockNode.get_node_name());
		}

	}

	m_track.addCheckpointAtPosition(lastCP);
	m_track.close();

}

CL_SharedPtr<RaceResistance::Geometry> Level::buildResistanceGeometry(int p_x, int p_y, Common::GroundBlockType p_blockType) const
{
	CL_SharedPtr<RaceResistance::Geometry> geom(new RaceResistance::Geometry());

	CL_Pointf p, q;
{'fraction_non_alphanumeric': 0.11790939676081684, 'fraction_numerical': 0.006415773413660903, 'mean_word_length': 4.847209515096066, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '7116430', 'n_tokens_mistral': 4572, 'n_tokens_neox': 4150, 'n_words': 1100}