Filter:   InfoImg
download tiledata.cc
Language: C++
License: GPL
LOC: 83
Project Info
Ultima Online Project(uop)
Server: SourceForge
Type: cvs
SourceForge\u\uop\uop\uop\
   acinclude.m4
   aclocal.m4
   action.cc
   action.h
   animation.cc
   animation.h
   artexport.cc
   artfactory.cc
   artfactory.h
   audio.cc
   audio.h
   btree.cc
   btree.h
   cache.h
   cliloc.cc
   cliloc.h
   config.h.in
   configure.in
   cvs2cl.pl
   entity.cc
   entity.h
   gumps.cc
   gumps.h
   interface.cc
   interface.h
   interfacegumps.cc
   interfacegumps.h
   interfacehandler.cc
   interfacehandler.h
   intrface.def
   Makefile.am
   Makefile.in
   message.cc
   message.h
   musicmap.def
   network.cc
   network.h
   patchhandler.cc
   patchhandler.h
   readable.pl
   serverhandler.cc
   serverhandler.h
   sprite.cc
   sprite.h
   stamp-h.in
   texthandler.cc
   texthandler.h
   tiledata.cc
   tiledata.h
   unreadable.pl
   uop.cc
   uop.cfg
   uop.dox
   uop.h
   uopconfig.cc
   uopconfig.h
   uosprite.h
   uostub.pl
   world.cc
   world.h

/*****
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *****/
 
#include "uop.h"
#include <SDL/SDL.h>
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include "uopconfig.h"
#include "tiledata.h"

extern UOPConfig *config;

/**
 * Reads and stores all important data in
 * tiledata.mul.
 */
TileData::TileData()
{
	string tiledatafilestr = config->encapDatLoc("tiledata.mul");
	tiledatafile.open(tiledatafilestr.c_str(), ios::in|ios::binary);
	if (!tiledatafile.is_open())
	{
		cerr << "Couldn't open " << tiledatafilestr << endl;
		exit(1);
	}

	// Find size and test for oddly-sized files
	tiledatafile.seekg(0, ios::end);
	Uint32 fileSize = tiledatafile.tellg();
	// TODO: make sure this equation actually works
	if (!((fileSize >= 512*32*26) && ((fileSize - 512*32*26)%41)))
	{
		cerr << "Tiledata.mul is an odd size, and is likely corrupt." << endl;
		exit(1);
	}
	tiledatafile.seekg(0, ios::beg);

	entryCount = (512*32) + ((((int)(fileSize - 512*32*26)/1188))*32);
	entry = new TileDataEntry*[entryCount];
	
	char nameBuf[20];
	
	for (short a = 0; a < 512; a++)
	{
		tiledatafile.seekg(4, ios::cur);
		for (short c = 0; c < 32; c++)
		{
			LandEntry *le = new LandEntry;
			tiledatafile.read((char*)&le->flags, 4);
			tiledatafile.read((char*)&le->textureID, 2);
			tiledatafile.read((char*)nameBuf, 20);
			le->name = nameBuf;
			entry[32*a+c] = le;
		}
	}
	
	unsigned int eOff = 0;
	while (!tiledatafile.eof())
	{
		tiledatafile.seekg(4, ios::cur);
		for (short c = 0; c < 32; c++)
		{
			StaticEntry *le = new StaticEntry;
			tiledatafile.read((char*)&le->flags, 4);
			tiledatafile.read((char*)&le->weight, 1);
			tiledatafile.read((char*)&le->quality, 1);
			tiledatafile.seekg(3, ios::cur);
			tiledatafile.read((char*)&le->quantity, 1);
			tiledatafile.read((char*)&le->animation, 2);
			tiledatafile.seekg(1, ios::cur);
			tiledatafile.read((char*)&le->hue, 1);
			tiledatafile.seekg(2, ios::cur);
			tiledatafile.read((char*)&le->height, 1);
			tiledatafile.read((char*)nameBuf, 20);
			le->name = nameBuf;
			entry[512*32 + eOff] = le;
			eOff++;
		}
	}
	
	tiledatafile.close();
}

TileData::~TileData()
{
	delete [] entry;
}

TileDataEntry *TileData::operator[](Uint32 index)
{
	if (index < entryCount)
		return entry[index];
	else
	{
		cerr << "Warning: Tiledata entry " << index
				 << " greater than " << entryCount
				 << " accessed." << endl;
		return entry[entryCount-1];
	}
}