Filter:   InfoImg
download world.cc
Language: C++
License: GPL
LOC: 246
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
 *  @class World
 *  @brief Represents the game world
 *  @author Samuel Kaufman
 *  @date 2002
 */
/**
 *  @class Block
 *  @brief Represents a single block within the map
 *  @author Samuel Kaufman
 *  @date 2002
 */
/**
 *  @class Cell
 *  @brief Represents a single cell within a map block
 *  @author Samuel Kaufman
 *  @date 2002
 */
/*****
 *
 *  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
 *
 *****/

// Debugging variables
#define TARGETZ 10

#define STDH 512
#define TDH 200

#define CalcBID(x,y) ((x*STDH)+y)
#define CalcTDBID(x,y) ((x*TDH)+y)

#include "uop.h"
#include <SDL/SDL.h>
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include "artfactory.h"
#include "sprite.h"
#include "tiledata.h"
#include "uopconfig.h"
#include "world.h"
#include "entity.h"

extern SDL_Surface *screen;
extern TileData *tileData;
extern UOPConfig *config;
extern World *world;


/**
 * Check if entities are in right rendering order.
 */
bool Cell::right_order(){
	list<Entity*>::iterator cur;
        list<Entity*>::iterator prev;

        for (cur=entities.begin(),prev=cur++; cur != entities.end(); cur++,prev++)
        {
                 if( (**prev) > (**cur)) return false;
        }
	return true;
	
}


/**
 * Adds specified entity to cell.
 */
void Cell::add(Entity *entity)
{
	list<Entity*>::iterator cur = entities.begin();
	
	if (entity->inCell)
		entity->inCell->remove(entity);
	entity->inCell = this;
	
	if (entities.empty() || ((**cur) > (*entity)))
	{
		entities.push_front(entity);
		assert(right_order());
		return;
	}
	for (; cur != entities.end(); cur++)
	{
		if ((**cur) > (*entity))
		{
			entities.insert(cur, entity);
			assert(right_order());
			return;
		}
	}

	entities.push_back(entity);
	assert(right_order());
}

/**
 * Will blit all entities within the cell to the screen.
 * @param x The middle X location on the screen to blit.
 * @param y The Y location on the screen where a land tile
 * at zero altitude would be blit, should it exist.
 */
void Cell::blit(int x, int y)
{	
	if (entities.empty())
		return;
	
	for (list<Entity*>::iterator cur = entities.begin();
			 cur != entities.end(); cur++)
	{
		(*cur)->blit(x, y-((*cur)->z-TARGETZ)*4);
	}
}

/**
 * A factory production function that will load
 * needed cell data from the map file.
 * @param bid The block ID; usually produced with
 * the CalcBID or CalcTDBID macros.
 * @param tdmap Wether or not to read data from the
 * Ilshenar (Third Dawn) map.
 * @param block A reference to the block to store
 * data in.
 */
void Block::produce(Uint32 bid, bool tdmap, Block &block)
{	
	ifstream *mfile;
	ifstream *sifile;
	ifstream *statfile;
	if (tdmap) {
		if (!world->tdmapfile.is_open() ||
				!world->tdstaidxfile.is_open() ||
				!world->tdstaticsfile.is_open())
			return;
		mfile = &world->tdmapfile;
		sifile = &world->tdstaidxfile;
		statfile = &world->tdstaticsfile;
	}
	else {
		mfile = &world->mapfile;
		sifile = &world->staidxfile;
		statfile = &world->staticsfile;
	}
	
	mfile->seekg((bid*196)+4);
	sifile->seekg(bid*12);
	
	for (int cy = 0; cy < 8; cy++)
	{
		for (int cx = 0; cx < 8; cx++)
		{
			LandTile *tile = new LandTile;
			Uint16 color;
			mfile->read((char*)&color, 2);
			mfile->read((char*)&tile->z, 1);
			tile->index = color;
			
			block.cells[cx][cy].landTile = tile;
			block.cells[cx][cy].add(tile);
		}
	}

	// Load static objects into the block
	Uint32 staticlookup, staticlength;
	sifile->read((char*)&staticlookup, 4);
	sifile->read((char*)&staticlength, 4);
	if (staticlookup != 0xffffffff)
	{
		statfile->seekg(staticlookup);
		for (unsigned int cs = 0; cs < (staticlength/7); cs++)
		{
			Uint16 oid;
			Uint8 x, y;
			Sint8 alt;
			statfile->read((char*)&oid, 2);
			statfile->read((char*)&x, 1);
			statfile->read((char*)&y, 1);
			statfile->read((char*)&alt, 1);
			statfile->seekg(2, ios::cur);
			Static *object = new Static;
			object->index = 0x4000 + oid;
			object->z = alt;
			if ((x < 8) && (y < 8))
				block.cells[x][y].add(object);
			else
				cerr << "Warning: Static object ignored because of"
						 << " invalid coordinates." << endl;
		}
	}

	block.id = bid;
	block.tdmap = tdmap;
	block.rendered = false;
}

void Block::recycle(Block &block)
{
	block.~Block();
}

/**
 * Will graphically render all cells inside the
 * block by calling LandTile::render().
 */
void Block::renderTiles()
{
  char alt;
  int x,y;

  Block *b2;
  Block *b3;
  Block *b4;

	if (!tdmap) {
		b2 = &world->mapBlocks[id+STDH];
		b3 = &world->mapBlocks[id+STDH+1];
		b4 = &world->mapBlocks[id+1];
	}
	else {
		b2 = &world->mapBlocksTD[id+TDH];
		b3 = &world->mapBlocksTD[id+TDH+1];
		b4 = &world->mapBlocksTD[id+1];
	}

  for (y=0;y<7;y++)
    for (x=0;x<7;x++)
    {
      alt=cells[x][y].landTile->z;
      int ty2=22+((alt-cells[x+1][y].landTile->z)<<2);
      int ty3=43+((alt-cells[x+1][y+1].landTile->z)<<2);
      int ty4=22+((alt-cells[x][y+1].landTile->z)<<2);
			cells[x][y].landTile->render(ty2, ty3, ty4);
    }
  for (x=0;x<7;x++)
  {
    alt=cells[x][7].landTile->z;
    int ty2=22+((alt-cells[x+1][7].landTile->z)<<2);
    int ty3=43+((alt-b4->cells[x+1][0].landTile->z)<<2);
    int ty4=22+((alt-b4->cells[x][0].landTile->z)<<2);
		cells[x][7].landTile->render(ty2, ty3, ty4);
  }
  for (y=0;y<7;y++)
  {
    alt=cells[7][y].landTile->z;
    int ty2=22+((alt-b2->cells[0][y].landTile->z)<<2);
    int ty3=43+((alt-b2->cells[0][y+1].landTile->z)<<2);
    int ty4=22+((alt-cells[7][y+1].landTile->z)<<2);
		cells[7][y].landTile->render(ty2, ty3, ty4);
  }
  alt=cells[7][7].landTile->z;
  int ty2=22+((alt-b2->cells[0][7].landTile->z)<<2);
  int ty3=43+((alt-b3->cells[0][0].landTile->z)<<2);
  int ty4=22+((alt-b4->cells[7][0].landTile->z)<<2);
	cells[7][7].landTile->render(ty2, ty3, ty4);

	rendered = true;
}

World::World() : artSprites(*(new ArtFactory(config->encapDatLoc("artidx.mul"), config->encapDatLoc("art.mul")))), mapBlocks(256)
{
	
	/*	
	string artidxname = config->encapDatLoc("artidx.mul");
	artidx.open(artidxname.c_str(), ios::in|ios::binary);
	if (!artidx.is_open())
	{
		cerr << "Couldn't open " << artidxname << endl;
		exit(1);
	}
	string artfilename = config->encapDatLoc("art.mul");
	artfile.open(artfilename.c_str(), ios::in|ios::binary);
	if (!artfile.is_open())
	{
		cerr << "Couldn't open " << artfilename << endl;
		exit(1);
	}
	*/
	//artSprites = (new Cache<Uint32, Sprite, ArtFactory> (*(new ArtFactory(config->encapDatLoc("artidx.mul"), config->encapDatLoc("art.mul")))));	
	string mapfilestr = config->encapDatLoc("map0.mul");
	mapfile.open(mapfilestr.c_str(), ios::in|ios::binary);
	if (!mapfile.is_open())
	{
		cerr << "Couldn't open " << mapfilestr << endl;
		exit(1);
	}
	string staticsfilestr = config->encapDatLoc("statics0.mul");
	staticsfile.open(staticsfilestr.c_str(), ios::in|ios::binary);
	if (!staticsfile.is_open())
	{
		cerr << "Couldn't open " << staticsfilestr << endl;
		exit(1);
	}
	string tdstaticsfilestr = config->encapDatLoc("statics2.mul");
	tdstaticsfile.open(tdstaticsfilestr.c_str(), ios::in|ios::binary);

	string staidxfilestr = config->encapDatLoc("staidx0.mul");
	staidxfile.open(staidxfilestr.c_str(), ios::in|ios::binary);
	if (!staidxfile.is_open())
	{
		cerr << "Couldn't open " << staidxfilestr << endl;
		exit(1);
	}
	string tdstaidxfilestr = config->encapDatLoc("staidx2.mul");
	tdstaidxfile.open(tdstaidxfilestr.c_str(), ios::in|ios::binary);
	
	focus = NULL;
	is_paused = true;
}

World::~World()
{
	//artidx.close();
	//artfile.close();
	mapfile.close();
	tdmapfile.close();
	staidxfile.close();
	tdstaidxfile.close();
	staticsfile.close();
	tdstaticsfile.close();
}

/**
 * Locates a cell within the map by global
 * coordinates.
 * @param globalX The global map X position of the cell.
 * @param globalY The global map Y position of the cell.
 * @param tdmap Wether or not the cell is in Ilshenar.
 * @param renderBlock Wehter or not the cell's containing
 * block should be graphically rendered and stored in memory.
 */
Cell *World::getCell(Uint16 globalX, Uint16 globalY, bool tdmap, bool renderBlock)
{
	Uint16 blockx = globalX / 8;
	Uint16 blocky = globalY / 8;
	Uint8 localx = globalX - (blockx*8);
	Uint8 localy = globalY - (blocky*8);
	if (tdmap) {
		if (renderBlock&& !mapBlocksTD[CalcTDBID(blockx, blocky)].rendered)
			mapBlocksTD[CalcTDBID(blockx, blocky)].renderTiles();
		return &mapBlocksTD[CalcTDBID(blockx, blocky)].cells[localx][localy];
	}
	else {
		if (renderBlock && !mapBlocks[CalcBID(blockx, blocky)].rendered)
			mapBlocks[CalcBID(blockx, blocky)].renderTiles();
		return &mapBlocks[CalcBID(blockx, blocky)].cells[localx][localy];
	}
}

bool World::paused() const{
	return is_paused;
}

void World::pause(){
	is_paused = true;
}

void World::unpause(){
	is_paused = false;
}