summaryrefslogtreecommitdiff
path: root/src/mapblockobject.cpp
blob: df070265c7600441bf76b5e921ab086c3d10f708 (plain)
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
(c) 2010 Perttu Ahola <celeron55@gmail.com>
*/

#include "mapblockobject.h"
#include "mapblock.h"
// Only for ::getNodeBox, TODO: Get rid of this
#include "map.h"

/*
	MapBlockObject
*/

// This is here because it uses the MapBlock
v3f MapBlockObject::getAbsolutePos()
{
	if(m_block == NULL)
		return m_pos;
	
	// getPosRelative gets nodepos relative to map origin
	v3f blockpos = intToFloat(m_block->getPosRelative());
	return blockpos + m_pos;
}

void MapBlockObject::setBlockChanged()
{
	if(m_block)
		m_block->setChangedFlag();
}

/*
	MovingObject
*/
void MovingObject::move(float dtime, v3f acceleration)
{
	DSTACK("%s: typeid=%i, pos=(%f,%f,%f), speed=(%f,%f,%f)"
			", dtime=%f, acc=(%f,%f,%f)",
			__FUNCTION_NAME,
			getTypeId(),
			m_pos.X, m_pos.Y, m_pos.Z,
			m_speed.X, m_speed.Y, m_speed.Z,
			dtime,
			acceleration.X, acceleration.Y, acceleration.Z
			);
	
	v3s16 oldpos_i = floatToInt(m_pos);
	
	if(m_block->isValidPosition(oldpos_i) == false)
	{
		// Should have wrapped, cancelling further movement.
		return;
	}

	// No collisions if there is no collision box
	if(m_collision_box == NULL)
	{
		m_speed += dtime * acceleration;
		m_pos += m_speed * dtime;
		return;
	}
	
	// Set insane speed to zero
	// Otherwise there will be divides by zero and other silly stuff
	if(m_speed.getLength() > 1000.0*BS)
		m_speed = v3f(0,0,0);
		
	// Limit speed to a reasonable value
	float speed_limit = 20.0*BS;
	if(m_speed.getLength() > speed_limit)
		m_speed = m_speed * (speed_limit / m_speed.getLength());

	v3f position = m_pos;
	v3f oldpos = position;

	/*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
			<<oldpos_i.Z<<")"<<std::endl;*/

	// Maximum time increment (for collision detection etc)
	// Allow 0.1 blocks per increment
	// time = distance / speed
	// NOTE: In the loop below collisions are detected at 0.15*BS radius
	float speedlength = m_speed.getLength();
	f32 dtime_max_increment;
	if(fabs(speedlength) > 0.001)
		dtime_max_increment = 0.1*BS / speedlength;
	else
		dtime_max_increment = 0.5;
	
	m_touching_ground = false;
		
	u32 loopcount = 0;
	do
	{
		loopcount++;

		f32 dtime_part;
		if(dtime > dtime_max_increment)
			dtime_part = dtime_max_increment;
		else
			dtime_part = dtime;
		dtime -= dtime_part;

		// Begin of dtime limited code
		
		m_speed += acceleration * dtime_part;
		position += m_speed * dtime_part;

		/*
			Collision detection
		*/
		
		v3s16 pos_i = floatToInt(position);
		
		// The loop length is limited to the object moving a distance
		f32 d = (float)BS * 0.15;

		core::aabbox3d<f32> objectbox(
				m_collision_box->MinEdge + position,
				m_collision_box->MaxEdge + position
		);
		
		core::aabbox3d<f32> objectbox_old(
				m_collision_box->MinEdge + oldpos,
				m_collision_box->MaxEdge + oldpos
		);
		
		//TODO: Get these ranges from somewhere
		for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
		for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
		for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
		{
			try{
				if(m_block->getNodeParent(v3s16(x,y,z)).d == MATERIAL_AIR){
					continue;
				}
			}
			catch(InvalidPositionException &e)
			{
				// Doing nothing here will block the player from
				// walking over map borders
			}

			core::aabbox3d<f32> nodebox = Map::getNodeBox(
					v3s16(x,y,z));
			
			// See if the player is touching ground
			if(
					fabs(nodebox.MaxEdge.Y-objectbox.MinEdge.Y) < d
					&& nodebox.MaxEdge.X-d > objectbox.MinEdge.X
					&& nodebox.MinEdge.X+d < objectbox.MaxEdge.X
					&& nodebox.MaxEdge.Z-d > objectbox.MinEdge.Z
					&& nodebox.MinEdge.Z+d < objectbox.MaxEdge.Z
			){
				m_touching_ground = true;
			}
			
			if(objectbox.intersectsWithBox(nodebox))
			{
					
		v3f dirs[3] = {
			v3f(0,0,1), // back
			v3f(0,1,0), // top
			v3f(1,0,0), // right
		};
		for(u16 i=0; i<3; i++)
		{
			f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
			f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
			f32 playermax = objectbox.MaxEdge.dotProduct(dirs[i]);
			f32 playermin = objectbox.MinEdge.dotProduct(dirs[i]);
			f32 playermax_old = objectbox_old.MaxEdge.dotProduct(dirs[i]);
			f32 playermin_old = objectbox_old.MinEdge.dotProduct(dirs[i]);

			bool main_edge_collides = 
				((nodemax > playermin && nodemax <= playermin_old + d
					&& m_speed.dotProduct(dirs[i]) < 0)
				||
				(nodemin < playermax && nodemin >= playermax_old - d
					&& m_speed.dotProduct(dirs[i]) > 0));

			bool other_edges_collide = true;
			for(u16 j=0; j<3; j++)
			{
				if(j == i)
					continue;
				f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
				f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
				f32 playermax = objectbox.MaxEdge.dotProduct(dirs[j]);
				f32 playermin = objectbox.MinEdge.dotProduct(dirs[j]);
				if(!(nodemax - d > playermin && nodemin + d < playermax))
				{
					other_edges_collide = false;
					break;
				}
			}
			
			if(main_edge_collides && other_edges_collide)
			{
				m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
				position -= position.dotProduct(dirs[i]) * dirs[i];
				position += oldpos.dotProduct(dirs[i]) * dirs[i];
			}
		
		}
		
			} // if(objectbox.intersectsWithBox(nodebox))
		} // for y

	} // End of dtime limited loop
	while(dtime > 0.001);

	m_pos = position;
}

/*
	MapBlockObjectList
*/

MapBlockObjectList::MapBlockObjectList(MapBlock *block):
	m_block(block)
{
	m_mutex.Init();
}

MapBlockObjectList::~MapBlockObjectList()
{
	clear();
}

/*
	The serialization format:
	[0] u16 number of entries
	[2] entries (id, typeId, parameters)
*/

void MapBlockObjectList::serialize(std::ostream &os, u8 version)
{
	JMutexAutoLock lock(m_mutex);

	u8 buf[2];
	writeU16(buf, m_objects.size());
	os.write((char*)buf, 2);

	for(core::map<s16, MapBlockObject*>::Iterator
			i = m_objects.getIterator();
			i.atEnd() == false; i++)
	{
		i.getNode()->getValue()->serialize(os, version);
	}
}

void MapBlockObjectList::update(std::istream &is, u8 version,
		scene::ISceneManager *smgr)
{
	JMutexAutoLock lock(m_mutex);

	/*
		Collect all existing ids to a set.

		As things are updated, they are removed from this.

		All remaining ones are deleted.
	*/
	core::map<s16, bool> ids_to_delete;
	for(core::map<s16, MapBlockObject*>::Iterator
			i = m_objects.getIterator();
			i.atEnd() == false; i++)
	{
		ids_to_delete.insert(i.getNode()->getKey(), true);
	}
	
	u8 buf[6];
	
	is.read((char*)buf, 2);
	u16 count = readU16(buf);

	for(u16 i=0; i<count; i++)
	{
		// Read id
		is.read((char*)buf, 2);
		s16 id = readS16(buf);
		
		// Read position
		// stored as x1000/BS v3s16
		is.read((char*)buf, 6);
		v3s16 pos_i = readV3S16(buf);
		v3f pos((f32)pos_i.X/1000*BS,
				(f32)pos_i.Y/1000*BS,
				(f32)pos_i.Z/1000*BS);

		// Read typeId
		is.read((char*)buf, 2);
		u16 type_id = readU16(buf);
		
		bool create_new = false;

		// Find an object with the id
		core::map<s16, MapBlockObject*>::Node *n;
		n = m_objects.find(id);
		// If no entry is found for id
		if(n == NULL)
		{
			// Insert dummy pointer node
			m_objects.insert(id, NULL);
			// Get node
			n = m_objects.find(id);
			// A new object will be created at this node
			create_new = true;
		}
		// If type_id differs
		else if(n->getValue()->getTypeId() != type_id)
		{
			// Delete old object
			delete n->getValue();
			// A new object will be created at this node
			create_new = true;
		}

		MapBlockObject *obj = NULL;

		if(create_new)
		{
			/*dstream<<"MapBlockObjectList adding new object"
					" id="<<id
					<<std::endl;*/

			if(type_id == MAPBLOCKOBJECT_TYPE_TEST)
			{
				// The constructors of objects shouldn't need
				// any more parameters than this.
				obj = new TestObject(m_block, id, pos);
			}
			else if(type_id == MAPBLOCKOBJECT_TYPE_TEST2)
			{
				obj = new Test2Object(m_block, id, pos);
			}
			else if(type_id == MAPBLOCKOBJECT_TYPE_SIGN)
			{
				obj = new SignObject(m_block, id, pos);
			}
			else if(type_id == MAPBLOCKOBJECT_TYPE_RAT)
			{
				obj = new RatObject(m_block, id, pos);
			}
			else
			{
				throw SerializationError
				("MapBlockObjectList::update(): Unknown MapBlockObject type");
			}

			if(smgr != NULL)
				obj->addToScene(smgr);

			n->setValue(obj);
		}
		else
		{
			obj = n->getValue();
			obj->updatePos(pos);
		}

		// Now there is an object in obj.
		// Update it.
		
		obj->update(is, version);
		
		// Remove from deletion list
		if(ids_to_delete.find(id) != NULL)
			ids_to_delete.remove(id);
	}

	// Delete all objects whose ids_to_delete remain in ids_to_delete
	for(core::map<s16, bool>::Iterator
			i = ids_to_delete.getIterator();
			i.atEnd() == false; i++)
	{
		s16 id = i.getNode()->getKey();

		/*dstream<<"MapBlockObjectList deleting object"
				" id="<<id
				<<std::endl;*/

		MapBlockObject *obj = m_objects[id];
		obj->removeFromScene();
		delete obj;
		m_objects.remove(id);
	}
}

s16 MapBlockObjectList::getFreeId() throw(ContainerFullException)
{
	s16 id = 0;
	for(;;)
	{
		if(m_objects.find(id) == NULL)
			return id;
		if(id == 32767)
			throw ContainerFullException
					("MapBlockObjectList doesn't fit more objects");
		id++;
	}
}

void MapBlockObjectList::add(MapBlockObject *object)
		throw(ContainerFullException, AlreadyExistsException)
{
	if(object == NULL)
	{
		dstream<<"MapBlockObjectList::add(): NULL object"<<std::endl;
		return;
	}

	JMutexAutoLock lock(m_mutex);

	// Create unique id if id==-1
	if(object->m_id == -1)
	{
		object->m_id = getFreeId();
	}

	if(m_objects.find(object->m_id) != NULL)
	{
		dstream<<"MapBlockObjectList::add(): "
				"object with same id already exists"<<std::endl;
		throw AlreadyExistsException
				("MapBlockObjectList already has given id");
	}
	
	object->m_block = m_block;
	
	/*v3f p = object->m_pos;
	dstream<<"MapBlockObjectList::add(): "
			<<"m_block->getPos()=("
			<<m_block->getPos().X<<","
			<<m_block->getPos().Y<<","
			<<m_block->getPos().Z<<")"
			<<" inserting object with id="<<object->m_id
			<<" pos="
			<<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
			<<std::endl;*/
	
	m_objects.insert(object->m_id, object);
}

void MapBlockObjectList::clear()
{
	JMutexAutoLock lock(m_mutex);

	for(core::map<s16, MapBlockObject*>::Iterator
			i = m_objects.getIterator();
			i.atEnd() == false; i++)
	{
		MapBlockObject *obj = i.getNode()->getValue();
		//FIXME: This really shouldn't be NULL at any time,
		//       but this condition was added because it was.
		if(obj != NULL)
		{
			obj->removeFromScene();
			delete obj;
		}
	}

	m_objects.clear();
}

void MapBlockObjectList::remove(s16 id)
{
	JMutexAutoLock lock(m_mutex);

	core::map<s16, MapBlockObject*>::Node *n;
	n = m_objects.find(id);
	if(n == NULL)
		return;
	
	n->getValue()->removeFromScene();
	delete n->getValue();
	m_objects.remove(id);
}

MapBlockObject * MapBlockObjectList::get(s16 id)
{
	core::map<s16, MapBlockObject*>::Node *n;
	n = m_objects.find(id);
	if(n == NULL)
		return NULL;
	else
		return n->getValue();
}

void MapBlockObjectList::step(float dtime, bool server)
{
	DSTACK(__FUNCTION_NAME);
	
	JMutexAutoLock lock(m_mutex);
	
	core::map<s16, bool> ids_to_delete;

	{
		DSTACK("%s: stepping objects", __FUNCTION_NAME);

		for(core::map<s16, MapBlockObject*>::Iterator
				i = m_objects.getIterator();
				i.atEnd() == false; i++)
		{
			MapBlockObject *obj = i.getNode()->getValue();
			
			DSTACK("%s: stepping object type %i", __FUNCTION_NAME,
					obj->getTypeId());

			if(server)
			{
				bool to_delete = obj->serverStep(dtime);

				if(to_delete)
					ids_to_delete.insert(obj->m_id, true);
			}
			else
			{
				obj->clientStep(dtime);
			}
		}
	}

	{
		DSTACK("%s: deleting objects", __FUNCTION_NAME);

		// Delete objects in delete queue
		for(core::map<s16, bool>::Iterator
				i = ids_to_delete.getIterator();
				i.atEnd() == false; i++)
		{
			s16 id = i.getNode()->getKey();

			MapBlockObject *obj = m_objects[id];
			obj->removeFromScene();
			delete obj;
			m_objects.remove(id);
		}
	}
	
	/*
		Wrap objects on server
	*/

	if(server == false)
		return;
	
	{
		DSTACK("%s: object wrap loop", __FUNCTION_NAME);

		for(core::map<s16, MapBlockObject*>::Iterator
				i = m_objects.getIterator();
				i.atEnd() == false; i++)
		{
			MapBlockObject *obj = i.getNode()->getValue();

			v3s16 pos_i = floatToInt(obj->m_pos);

			if(m_block->isValidPosition(pos_i))
			{
				// No wrap
				continue;
			}

			bool impossible = wrapObject(obj);

			if(impossible)
			{
				// No wrap
				continue;
			}

			// Restart find
			i = m_objects.getIterator();
		}
	}
}

bool MapBlockObjectList::wrapObject(MapBlockObject *object)
{
	DSTACK(__FUNCTION_NAME);
	
	// No lock here; this is called so that the lock is already locked.
	//JMutexAutoLock lock(m_mutex);

	assert(object->m_block == m_block);
	assert(m_objects.find(object->m_id) != NULL);
	assert(m_objects[object->m_id] == object);

	NodeContainer *parentcontainer = m_block->getParent();
	// This will only work if the parent is the map
	if(parentcontainer->nodeContainerId() != NODECONTAINER_ID_MAP)
	{
		dstream<<"WARNING: Wrapping object not possible: "
				"MapBlock's parent is not map"<<std::endl;
		return true;
	}
	// OK, we have the map!
	Map *map = (Map*)parentcontainer;
	
	// Calculate blockpos on map
	v3s16 oldblock_pos_i_on_map = m_block->getPosRelative();
	v3f pos_f_on_oldblock = object->m_pos;
	v3s16 pos_i_on_oldblock = floatToInt(pos_f_on_oldblock);
	v3s16 pos_i_on_map = pos_i_on_oldblock + oldblock_pos_i_on_map;
	v3s16 pos_blocks_on_map = getNodeBlockPos(pos_i_on_map);

	// Get new block
	MapBlock *newblock;
	try{
		newblock = map->getBlockNoCreate(pos_blocks_on_map);
	}
	catch(InvalidPositionException &e)
	{
		// Couldn't find block -> not wrapping
		/*dstream<<"WARNING: Wrapping object not possible: "
				<<"could not find new block"
				<<"("<<pos_blocks_on_map.X
				<<","<<pos_blocks_on_map.Y
				<<","<<pos_blocks_on_map.Z
				<<")"<<std::endl;*/
		/*dstream<<"pos_f_on_oldblock=("
				<<pos_f_on_oldblock.X<<","
				<<pos_f_on_oldblock.Y<<","
				<<pos_f_on_oldblock.Z<<")"
				<<std::endl;*/
		return true;
	}

	if(newblock == m_block)
	{
		dstream<<"WARNING: Wrapping object not possible: "
				"newblock == oldblock"<<std::endl;
		return true;
	}
	
	// Calculate position on new block
	v3f oldblock_pos_f_on_map = intToFloat(oldblock_pos_i_on_map);
	v3s16 newblock_pos_i_on_map = newblock->getPosRelative();
	v3f newblock_pos_f_on_map = intToFloat(newblock_pos_i_on_map);
	v3f pos_f_on_newblock = pos_f_on_oldblock
			- newblock_pos_f_on_map + oldblock_pos_f_on_map;

	// Remove object from this block
	m_objects.remove(object->m_id);
	
	// Add object to new block
	object->m_pos = pos_f_on_newblock;
	object->m_id = -1;
	object->m_block = NULL;
	newblock->addObject(object);

	//dstream<<"NOTE: Wrapped object"<<std::endl;

	return false;
}

void MapBlockObjectList::getObjects(v3f origin, f32 max_d,
		core::array<DistanceSortedObject> &dest)
{
	for(core::map<s16, MapBlockObject*>::Iterator
			i = m_objects.getIterator();
			i.atEnd() == false; i++)
	{
		MapBlockObject *obj = i.getNode()->getValue();

		f32 d = (obj->m_pos - origin).getLength();

		if(d > max_d)
			continue;

		DistanceSortedObject dso(obj, d);

		dest.push_back(dso);
	}
}

//END