Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 48dd949756d04836b48e3f86ee394639d27c3e2c (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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*
   core/entity.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2.
*/

#ifndef __INCLUDED_CORE_ENTITY_H__
#define __INCLUDED_CORE_ENTITY_H__

#include <iostream>
#include <string>
#include <map>
#include <list>

#include "model/model.h"
#include "math/axis.h"
#include "math/mathlib.h"

namespace core
{

class Entity;
class EntityControlable;

}

#include "core/extension.h"
#include "core/descriptions.h"
#include "core/inventory.h"
#include "core/label.h"
#include "core/physics.h"
#include "core/player.h"
#include "core/zone.h"

namespace core
{

/// The base world entity. All gameworld entities must derive from this class.
class Entity : public Label
{
	friend class Extension;

public:
	/**
	 * @brief entity flags
	 */
	enum Flags {NonSolid = 2, Bright = 4, Dockable = 8, ShowOnMap = 16, KeepAlive = 32};

	/// Entity type constants
	enum Type {Default = 0, Dynamic = 1, Controlable = 2, Globe = 3};

	/// Entity shape constants
	enum Shape {Diamond = 0, Sphere = 1, Cube = 2, Axis = 3};

	/// EntityDynamic State constants
	enum State {Normal = 0, NoPower = 1, ImpulseInitiate = 2, Impulse = 3, JumpInitiate = 4, Jump = 5, Docked = 6, Destroyed = 7};

	/// entity menus collection typedef
	typedef std::list<MenuDescription *> Menus;

	/// create a new entity and add it to the registry
	Entity();

	/// create an entity from stream data
	Entity(std::istream & is);

	/// destroy an entity
	virtual ~Entity();

	/*----- inspectors ------------------------------------------------ */

	/// entity id
	inline const unsigned int id() const {
		return entity_id;
	}

	/// module type id
	inline const unsigned int moduletype() const {
		return entity_moduletypeid;
	}

	///  core type id
	virtual inline const unsigned int type() const {
		return Default;
	}

	/// entity flags
	inline const unsigned int flags() const {
		return entity_flags;
	}

	/// returns true of a flag is set
	inline const bool flag_is_set(const Flags flag) const {
		return ((entity_flags & (unsigned int)flag) == (unsigned int)flag);
	}

	/// pointer to the model, is used client-side
	inline model::Model * model() const {
		return entity_model;
	}

	/// modelname
	inline const std::string & modelname() const {
		return entity_modelname;
	}

	/// pointer to the zone the entity belongs to
	inline Zone *zone() const {
		return entity_zone;
	}

	/// the zone the entity left in case of a zone change
	inline Zone *oldzone() const {
		return entity_oldzone;
	}

	/// dirty flag
	inline bool dirty() const {
		return entity_dirty;
	}

	/// entity location
	inline const math::Vector3f& location() const {
		return entity_location;
	}

	/// local coordinate system of the entity
	inline const math::Axis& axis() const {
		return entity_axis;
	}

	/// primary color of the entity
	inline const math::Color& color() const {
		return entity_color;
	}

	/// secondary
	inline const math::Color& color_second() const {
		return entity_color_second;
	}

	/// base shape of the entity
	inline const Shape shape() const {
		return entity_shape;
	}

	/// base radius of the entity
	inline const float radius() const {
		return entity_radius;
	}

	/**
	 * @brief current speed of the entity in game units per second
	 * For a normal entity, speed is always 0. Use the EntityDynamic 
	 * and EntityControlable classes to create moving entities
	 */
	inline const float speed() const {
		return entity_speed;
	}

	/// mass of the entity
	inline const float mass() const {
		return entity_mass;
	}
	
	/// physics body
	inline btRigidBody *body() {
		return entity_body;
	}
	
	/// physics collision shape
	inline btCollisionShape *collision_shape() {
		return entity_collision_shape;
	}
	
	/// physics motion info
	inline btMotionState *motionstate() {
		return entity_motionstate;
	}
		
	/// indicates a server-side entity
	inline const bool serverside() const {
		return entity_serverside;
	}

	/// general visibility
	inline const bool visible() const {
		return entity_visible;
	}

	///entity inventory
	inline Inventory *inventory() const {
		return entity_inventory;
	}

	/// entity info
	inline Info *info() const {
		return entity_info;
	}
	
	/// entity menus
	inline Menus &menus() {
		return entity_menus;
	}

	/// extensions
	inline Extension *extension(size_t type) const {
		return entity_extension[type];
	}

	/// find a menu
	MenuDescription *find_menu(const std::string &label);
	
	/// true if the entity is to be deleted
	inline bool destroyed() const {
		return entity_destroyed;
	}

	/// time when the entity was last alive
	inline unsigned long keepalive() const {
		return entity_keepalive;
	}
	
	/// list inventory, if available, to console
	void list_inventory() const;

	/* ---- mutators -------------------------------------------------- */

	/// assign shape
	inline void set_shape(Shape shape) {
		entity_shape = shape;
	}
	
	/// assign entity color
	inline void set_color(const math::Color &color) {
		entity_color.assign(color);
	}

	/// assign entity secondary color
	inline void set_color_second(const math::Color &color) {
		entity_color_second.assign(color);
	}

	/**
	 * @brief set dirty flag
	 * setting the dirty flag will cause the server to broadcast changes
	 * to the clients.
	 */
	inline void set_dirty(const bool dirty = true) {
		entity_dirty = dirty;
	}

	/**
	 * @brief mark the entity as destroyed
	 * die() should be called by the game module when it needs to destroy an entity,
	 * the game server will broadcast the delete event to the clients.
	 * The game module should not delete an entity directly.
	 */
	virtual void die();

	/**
	 * @brief runs one game frame for the entity
	 * The default implementation does nothing
	 */
	virtual void frame(float seconds);

	/**
	 * @brief runs one upkeep frame for the entity
	 * The upkeep frame will be executed by the engine if the entity has the KeepAlive flag set
	 * and the keepalive timeout has been elapsed.
	 * The default implementation does nothing
	 * @param timestamp game timestamp for the current upkeep frame
	 */
	virtual void upkeep(const unsigned long timestamp);
	
	/**
	 * @brief set the zone the entity is currently in
	 * this fuction removes the entity from its previous zone
	 * and removes it to the new one, if it is not 0
	 */
	virtual void set_zone(Zone *zone);

	/// set visibility
	inline void set_visible(const bool visible = true) {
		entity_visible = visible;
	}

	/// set as server-side entity
	inline void set_serverside(const bool serverside = true) {
		entity_serverside = serverside;
	}

	/// set the model name and load the model
	void set_modelname(const std:: string &model);

	/// set the model
	void set_model(model::Model *model);

	/// set entity radius
	inline void set_radius(const float radius) {
		entity_radius = radius;
	}
	
		
	/// set location
	inline void set_location(const math::Vector3f &location) {
		entity_location.assign(location);
	}
	
	/// set location
	inline void set_axis(const math::Axis &axis) {
		entity_axis.assign(axis);
	}

	/// set flag
	inline void set_flag(Flags flag) {
		entity_flags |= flag;
	}

	/// unset flag
	inline void unset_flag(Flags flag) {
		entity_flags &= ~flag;
	}

	/**
	 * @brief add an inventory to this entity
	 * Entity takes ownership over the inventory pointer
	 */
	 void set_inventory(Inventory *inventory);
	 
	 void set_info(Info *info);
	 
	/// set the timestamp when the entity was last alive
	void set_keepalive(unsigned long timestamp) {
		entity_keepalive = timestamp;
	}

	/* ---- actors ---------------------------------------------------- */

	/**
	 * @brief reset the physics state
	 */
	virtual void reset();
	
	/// add an entity menu
	void add_menu(MenuDescription *menu);

	/// remove an entity menu
	void remove_menu(std::string const &label);

	/// clear all update flags
	virtual void clear_updates();

	/**
	 * @brief mutable reference to the location
	 */
	inline math::Vector3f& get_location() {
		return entity_location;
	}

	/**
	 * @brief mutable reference to the axis
	 */
	inline math::Axis& get_axis() {
		return entity_axis;
	}

	/**
	 * @brief mutable reference to the primary color
	 */
	inline math::Color& get_color() {
		return entity_color;
	}

	/**
	 * @brief mutable reference to the secondary color
	 */
	inline math::Color& get_color_second() {
		return entity_color_second;
	}
 
	/* ---- deserializers -------------------------------------- */

	/// receive a client-to-server update from a stream
	virtual void receive_client_update(std::istream &is);

	/// receive a server-to-client create from a stream
	virtual void receive_server_create(std::istream &is);

	/// receive a server-to-client update from a stream
	virtual void receive_server_update(std::istream &is);

	/* ---- serializers ---------------------------------------- */

	/// serialize the entity to a stream
	virtual void serialize_server_create(std::ostream & os) const;

	/// serialize a client-to-server update on a stream
	virtual void serialize_client_update(std::ostream & os) const;

	/// serialize a server-to-client update on a stream
	virtual void serialize_server_update(std::ostream & os) const;

	/* ---- static --------------------------------------------- */

	/// type definition for the entity registry
	typedef std::map<unsigned int, Entity*> Registry;

	/// find an entity in the registry
	static Entity *find(unsigned int id);

	/// add an entity to the registry
	static void add(Entity *ent, unsigned int it);

	/// erase an entity from the registry and delete it
	static void erase(unsigned int entity_id);

	/// print the list header
	static void list_header();

	/// list the entity registry
	static void list(const Entity *entity);
	
	/// list the entity registry
	static void list();

	/// clear the entity registry
	static void clear();

	/// the entity registry
	static inline Registry & registry() {
		return entity_registry;
	}

	/* entity_ variables can be set by the module */

	unsigned int		entity_moduletypeid;

	bool 			entity_created;
	bool			entity_destroyed;

	/// timestamp when entity data was received from the server
	float			entity_servertimestamp;

protected:
	btRigidBody					*entity_body;
	btRigidBody::btRigidBodyConstructionInfo	*entity_body_info;
	btCollisionShape				*entity_collision_shape;
	btMotionState					*entity_motionstate;

		// the zone the entity belongs to
	Zone*			entity_zone;
	// the previous zone the entity belonged too
	Zone*			entity_oldzone;

	float			entity_mass;	
	float			entity_speed;

private:
	
	unsigned int		entity_id;
	unsigned int 		entity_flags;

	bool			entity_dirty;
	bool			entity_visible;
	bool			entity_serverside;

	math::Vector3f 		entity_location;
	math::Axis		entity_axis;

	float 			entity_radius;

	Shape 			entity_shape;

	math::Color 		entity_color;
	math::Color 		entity_color_second;

	std::string		entity_modelname;

	model::Model*		entity_model;

	Menus			entity_menus;
	
	Inventory*		entity_inventory;
	Info*			entity_info;

	Extension*		entity_extension[4];
	
	unsigned long		entity_keepalive;
	
	static Registry 	entity_registry;
	static size_t		entity_nextid;

	static void add(Entity *ent);
};


/// an entity that can move around in the game world
class EntityDynamic : public Entity
{
public:
	/// create a dynamic entity
	EntityDynamic();

	/// create a dynamic entity from stream data
	EntityDynamic(std::istream & is);

	virtual ~EntityDynamic();

	/*----- inspectors ------------------------------------------------ */
	/// core type id
	virtual inline const unsigned int type() const {
		return Entity::Dynamic;
	}

	/// event state
	inline const int state() const {
		return entity_state;
	}

	/// event state timer
	inline const float timer() const {
		return entity_timer;
	}

	/*----- mutators -------------------------------------------------- */

	/// mass of the entity
	inline void set_mass(const float mass) {
		entity_mass = mass;
	}

	/// current speed of the entity in game units per second
	inline void set_speed(const float speed) {
		entity_speed = speed;
	}

	/*----- serializers ----------------------------------------------- */

	/// serialize the entity to a stream
	virtual void serialize_server_create(std::ostream & os) const;

	/// serialize a client-to-server update on a stream
	virtual void serialize_client_update(std::ostream & os) const ;

	/// serialize a server-to-client update on a stream
	virtual void serialize_server_update(std::ostream & os) const;

	/*----- mutators -------------------------------------------------- */

	/// receive a client-to-server update from a stream
	virtual void receive_client_update(std::istream &is);

	/// receive a server-to-client create from a stream
	virtual void receive_server_create(std::istream &is);

	/// receive a server-to-client update from a stream
	virtual void receive_server_update(std::istream &is);

	/// set event state
	virtual void set_state(int state);

	/// runs one game frame for the entity
	/**
	 * The default implementation will update the position() of the entity,
	 * determined by its speed() and axis()
	 */
	virtual void frame(float seconds);

	/**
	 * @brief reset the physics state
	 */
	virtual void reset();

protected:
	float			entity_timer;
	int			entity_state;
};

/// an entity that can be controlled by a player
class EntityControlable : public EntityDynamic
{
	friend class Player;
public:
	/// bullet action interface class
	class ActionInterface: public btActionInterface {
	public:
		ActionInterface(EntityControlable *entity);
		virtual ~ActionInterface();
 
         	virtual void updateAction( btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
 
         	virtual void debugDraw(btIDebugDraw* debugDrawer);
		
	private:
		EntityControlable *actioninterface_entity;
	};
	
	/// server-side constructor, create a controlable entity
	EntityControlable();

	/// create a controlable entity from stream data
	EntityControlable(std::istream & is);

	virtual ~EntityControlable();

	/*----- inspectors ------------------------------------------------ */

	/// core type id
	virtual inline const unsigned int type() const {
		return Entity::Controlable;
	}

	/// owner of this entity
	inline Player *owner() const {
		return entity_owner;
	}

	/// thrust
	inline float thrust() const {
		return entity_thrust;
	}

	/// movement indicator
	inline float movement() const {
		return entity_movement;
	}

	/// physics action
	inline ActionInterface *actioninterface() {
		return entity_actioninterface;
	}
	
	/*----- serializers ----------------------------------------------- */

	/// serialize the entity to a stream
	virtual void serialize_server_create(std::ostream & os) const;

	/// serialize a client-to-server update on a stream
	virtual void serialize_client_update(std::ostream & os) const;

	/// serialize a server-to-client update on a stream
	virtual void serialize_server_update(std::ostream & os) const;

	/*----- mutators -------------------------------------------------- */

	/**
	 * @brief set the zone the entity is currently in
	 * this fuction removes the entity from its previous zone
	 * and removes it to the new one, if it is not 0
	 */
	virtual void set_zone(Zone *zone);
	
	/**
	 * @brief reset the physics state
	 */
	virtual void reset();
	
	/// receive a client-to-server update from a stream
	virtual void receive_client_update(std::istream &is);

	/// receive a server-to-client create from a stream
	virtual void receive_server_create(std::istream &is);

	/// receive a server-to-client update from a stream
	virtual void receive_server_update(std::istream &is);

	/// set the player who owns this entity
	void set_owner(Player *owner);

	/// set the target thrust
	void set_thrust(float thrust);

	/// set the target direction
	void set_direction(float direction);

	/// set the target pitch
	void set_pitch(float pitch);

	/// set target roll
	void set_roll(float roll);

	/// set target strafe
	void set_strafe(float strafe);
	
	/// set target vertical strafe
	void set_vstrafe(float vstrafe);

	/// set afterburner/reverse
	void set_afterburner(float afterburner);

	/**
	 * @brief runs one game frame for the entity
	 * The default implementation will set direction() and thrust() to the desired targets
	 * and calls its parent frame() funcion.
	 */
	virtual void frame(float seconds);

	/// current thrust
	float			entity_thrust;

protected:
	
	/// physics action interface callback
	virtual void action (btScalar seconds);
	
	/* target_ variables can be set by the client */
	
	/**
	 * @brief target thrust as set by the client
	 */
	float			target_thrust;
	
	/** 
	 * @brief target direction as set by the client
	 * target_direction must be in the [-1, 1] range
	 */
	float			target_direction;
	
	/**
	 * @brief target pitch as set by the client
	 * target_pitch must be in the [-1, 1] range
	 */
	float 			target_pitch;
	
	/**
	 * @brief target roll as set by the client
	 * target_roll must be in the [-1, 1] range
	 */
	float 			target_roll;

	float 			target_afterburner;

	float			target_strafe;
	
	float			target_vstrafe;

	float 			entity_movement;

	ActionInterface		*entity_actioninterface;	
	
private:
	// owner of the entity
	Player			*entity_owner;
};

/// a Globe entity
class EntityGlobe : public Entity
{
public:
	/// server-side constructor
	EntityGlobe();
	EntityGlobe(std::istream & is);

	virtual ~EntityGlobe();

	/*----- inspectors ----------------------------------------------- */
	/// texture name
	inline const std::string &texture() const {
		return entity_texture;
	}

	/// rotation speed in degrees per second
	inline float rotationspeed() const {
		return entity_rotationspeed;
	}

	/*----- serializers ----------------------------------------------- */

	/// serialize the entity to a stream
	virtual void serialize_server_create(std::ostream & os) const;

	/// receive a server-to-client create from a stream
	virtual void receive_server_create(std::istream &is);

	/*----- inspectors ------------------------------------------------ */

	/// core type id
	virtual inline const unsigned int type() const {
		return Entity::Globe;
	}

	std::string		entity_texture;

	/// client side, texture id
	unsigned int		render_texture;

	/// rotation speed in degrees/second;
	float 			entity_rotationspeed;
};

}

#endif // __INCLUDED_CORE_ENTITY_H__