<commit_before>/*
 *  Map.cpp
 *  openc2e
 *
 *  Created by <NAME> on Tue May 25 2004.
 *  Copyright (c) 2004 <NAME>. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 */

#include "Map.h"
#include "openc2e.h"
#include <iostream>

void Map::Reset() {
	for (std::vector<MetaRoom *>::iterator i = metarooms.begin(); i != metarooms.end(); i++) {
		delete *i;
	}
	metarooms.clear();
	// todo: metarooms should be responsible for deleting rooms, so use the following instead of clear:
	// assert(rooms.empty());
	rooms.clear();
}

void Map::SetMapDimensions(unsigned int w, unsigned int h) {
	// todo: check for outlying metarooms
	width = w;
	height = h;
}

int Map::addMetaRoom(MetaRoom *m) {
	// todo: check if it's outlying
	metarooms.push_back(m);
	while (getMetaRoom(metaroom_base))
		metaroom_base++;
	m->id = metaroom_base++;
	return m->id;
}

MetaRoom *Map::getMetaRoom(unsigned int room) {
	for (std::vector<MetaRoom *>::iterator i = metarooms.begin(); i != metarooms.end(); i++)
		if ((*i)->id == room)
			return *i;
	return 0;
}

Room *Map::getRoom(unsigned int r) {
	for (std::vector<Room *>::iterator i = rooms.begin(); i != rooms.end(); i++)
		if ((*i)->id == r)
			return *i;
	return 0;
}

unsigned int Map::getMetaRoomCount() {
	return metarooms.size();
}

unsigned int Map::getRoomCount() {
	return rooms.size();
}

MetaRoom *Map::metaRoomAt(unsigned int _x, unsigned int _y) {
	for (std::vector<MetaRoom *>::iterator i = metarooms.begin(); i != metarooms.end(); i++) {
		MetaRoom *r = *i;
		if ((_x >= r->x()) && (_y >= r->y()))
			if ((_x <= (r->x() + r->width())) && (_y <= (r->y() + r->height())))
				return r;
	}
	return 0;
}

Room *Map::roomAt(unsigned int _x, unsigned int _y) {
	MetaRoom *m = metaRoomAt(_x, _y);
	if (!m) return 0;
	for (std::vector<Room *>::iterator i = m->rooms.begin(); i != m->rooms.end(); i++) {
		Room *r = *i;
//		std::cerr << (void *)r << " -> contains " << _x << "," << _y << std::endl;
		if (r->containsPoint(_x, _y)) return r;
	}
	return 0;
}

/* vim: set noet: */
<commit_msg>Don't insert the metaroom until *after* we set a valid id, otherwise getMetaRoom might produce incorrect results.<commit_after>/*
 *  Map.cpp
 *  openc2e
 *
 *  Created by <NAME> on Tue May 25 2004.
 *  Copyright (c) 2004 <NAME>. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 */

#include "Map.h"
#include "openc2e.h"
#include <iostream>

void Map::Reset() {
	for (std::vector<MetaRoom *>::iterator i = metarooms.begin(); i != metarooms.end(); i++) {
		delete *i;
	}
	metarooms.clear();
	// todo: metarooms should be responsible for deleting rooms, so use the following instead of clear:
	// assert(rooms.empty());
	rooms.clear();
}

void Map::SetMapDimensions(unsigned int w, unsigned int h) {
	// todo: check for outlying metarooms
	width = w;
	height = h;
}

int Map::addMetaRoom(MetaRoom *m) {
	// todo: check if it's outlying
	while (getMetaRoom(metaroom_base))
		metaroom_base++;
	m->id = metaroom_base++;
	metarooms.push_back(m);
	return m->id;
}

MetaRoom *Map::getMetaRoom(unsigned int room) {
	for (std::vector<MetaRoom *>::iterator i = metarooms.begin(); i != metarooms.end(); i++)
		if ((*i)->id == room)
			return *i;
	return 0;
}

Room *Map::getRoom(unsigned int r) {
	for (std::vector<Room *>::iterator i = rooms.begin(); i != rooms.end(); i++)
		if ((*i)->id == r)
			return *i;
	return 0;
}

unsigned int Map::getMetaRoomCount() {
	return metarooms.size();
}

unsigned int Map::getRoomCount() {
	return rooms.size();
}

MetaRoom *Map::metaRoomAt(unsigned int _x, unsigned int _y) {
	for (std::vector<MetaRoom *>::iterator i = metarooms.begin(); i != metarooms.end(); i++) {
		MetaRoom *r = *i;
		if ((_x >= r->x()) && (_y >= r->y()))
			if ((_x <= (r->x() + r->width())) && (_y <= (r->y() + r->height())))
				return r;
	}
	return 0;
}

Room *Map::roomAt(unsigned int _x, unsigned int _y) {
	MetaRoom *m = metaRoomAt(_x, _y);
	if (!m) return 0;
	for (std::vector<Room *>::iterator i = m->rooms.begin(); i != m->rooms.end(); i++) {
		Room *r = *i;
//		std::cerr << (void *)r << " -> contains " << _x << "," << _y << std::endl;
		if (r->containsPoint(_x, _y)) return r;
	}
	return 0;
}

/* vim: set noet: */

{'fraction_non_alphanumeric': 0.17203258339798294, 'fraction_numerical': 0.0069821567106283944, 'mean_word_length': 3.79275092936803, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 51, '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': '266969', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1814, 'n_words': 600}