Oolite
Loading...
Searching...
No Matches
Entity.h
Go to the documentation of this file.
1/*
2
3Entity.h
4
5Base class for entities, i.e. drawable world objects.
6
7Oolite
8Copyright (C) 2004-2013 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
27#pragma once
28
29#import "OOCacheManager.h"
30#import "OOColor.h"
31#import "OOFoundation.h"
32#import "OOMaths.h"
33#import "OOTypes.h"
34#import "OOWeakReference.h"
35
36#if OOJS_QUICKJS
37#include <quickjs.h>
38#endif
39
41
42#ifdef OO_DEBUG_MONITOR_ENABLED
43
44extern uint32_t gLiveEntityCount;
45extern size_t gTotalEntityMemory;
46
47#endif
48
49#define NO_DRAW_DISTANCE_FACTOR 1024.0
50#define ABSOLUTE_NO_DRAW_DISTANCE2 (2500.0 * 2500.0 * NO_DRAW_DISTANCE_FACTOR * NO_DRAW_DISTANCE_FACTOR)
51// ie. the furthest away thing we can draw is at 1280km (a 2.5km wide object would disappear at that range)
52
53#define SCANNER_MAX_RANGE 25600.0
54#define SCANNER_MAX_RANGE2 655360000.0
55
56#define CLOSE_COLLISION_CHECK_MAX_RANGE2 1000000000.0
57
58#define ENTRY(label, value) label = value,
59
60typedef enum OOEntityStatus {
61#include "OOEntityStatus.tbl"
63
64#ifndef OO_SCANCLASS_TYPE
65#define OO_SCANCLASS_TYPE
67#endif
68
70#include "OOScanClass.tbl"
71};
72
73#undef ENTRY
74
75@interface Entity : OOWeakRefObject {
76 // the base object for ships/stations/anything actually
78 //
79 // @public variables:
80 //
81 // we forego encapsulation for some variables in order to
82 // lose the overheads of Obj-C accessor methods...
83 //
84@public
85 OOUniversalID universalID; // used to reference the entity
86
87 unsigned isShip : 1,
88 isStation : 1,
89 isPlayer : 1,
90 isWormhole : 1,
91 isSubEntity : 1,
95 isSunlit : 1,
100 isVisualEffect : 1;
101
103
106 GLfloat no_draw_distance; // 10 km initially
108 HPVector position; // use high-precision vectors for global position
112
114
115 // Linked lists of entites, sorted by position on each (world) axis
119
121
123
125
126 CollisionRegion* collisionRegion; // initially nil - then maintained
127
128@protected
131
132 GLfloat distanceTravelled; // set to zero initially
133
135
137
138 GLfloat energy;
139 GLfloat maxEnergy;
140
142 GLfloat mass;
143
144 NSMutableArray* collidingEntities;
145
147
148#if OOJS_QUICKJS
149 JSValue _jsSelf; // Zero-filled (not an object) until created; test with JS_IsObject().
150#else
151 struct JSObject* _jsSelf;
152#endif
153 NSUInteger lastDrawCounter;
154
155@private
156 NSUInteger _sessionID;
157
160}
161
162// The session in which the entity was created.
163- (NSUInteger)sessionID;
164
165- (BOOL)isShip;
166- (BOOL)isDock;
167- (BOOL)isStation;
169- (BOOL)isPlayer;
170- (BOOL)isPlanet;
171- (BOOL)isSun;
172- (BOOL)isSunlit;
173- (BOOL)isStellarObject;
174- (BOOL)isSky;
176- (BOOL)isEffect;
178- (BOOL)isWaypoint;
179
181- (void)addToLinkedLists;
183
184- (void)updateLinkedLists;
185
186- (void)wasAddedToUniverse;
188
189- (void)warnAboutHostiles;
190
192- (void)setCollisionRegion:(CollisionRegion*)region;
193
194- (void)setUniversalID:(OOUniversalID)uid;
196
197- (BOOL)throwingSparks;
198- (void)setThrowSparks:(BOOL)value;
199- (void)throwSparks;
200
201- (void)setOwner:(Entity*)ent;
202- (id)owner;
203- (ShipEntity*)parentEntity; // owner if self is subentity of owner, otherwise nil.
204- (ShipEntity*)rootShipEntity; // like parentEntity, but recursive.
205
206- (void)setPosition:(HPVector)posn;
207- (void)setPositionX:(OOHPScalar)x y:(OOHPScalar)y z:(OOHPScalar)z;
210- (GLfloat)cameraRangeFront;
211- (GLfloat)cameraRangeBack;
212
214// gets a low-position relative vector
215- (Vector)vectorTo:(Entity*)entity;
216
218- (HPVector)absolutePositionForSubentityOffset:(HPVector)offset;
219
220- (double)zeroDistance;
221- (double)camZeroDistance;
222- (NSComparisonResult)compareZeroDistance:(Entity*)otherEntity;
223
225
226- (GLfloat)mass;
227
229- (void)setOrientation:(Quaternion)quat;
230- (Quaternion)normalOrientation; // Historical wart: orientation.w is reversed for player; -normalOrientation corrects this.
231- (void)setNormalOrientation:(Quaternion)quat;
232- (void)orientationChanged;
233
234- (void)setVelocity:(Vector)vel;
236- (double)speed;
237
239- (void)setDistanceTravelled:(GLfloat)value;
240
241- (void)setStatus:(OOEntityStatus)stat;
243
244- (void)setScanClass:(OOScanClass)sClass;
246
247- (void)setEnergy:(GLfloat)amount;
248- (GLfloat)energy;
249
250- (void)setMaxEnergy:(GLfloat)amount;
251- (GLfloat)maxEnergy;
252
253- (void)applyRoll:(GLfloat)roll andClimb:(GLfloat)climb;
254- (void)applyRoll:(GLfloat)roll climb:(GLfloat)climb andYaw:(GLfloat)yaw;
255- (void)moveForward:(double)amount;
256
261
262- (BOOL)canCollide;
263- (GLfloat)collisionRadius;
264- (GLfloat)frustumRadius;
265- (void)setCollisionRadius:(GLfloat)amount;
266- (NSMutableArray*)collisionArray;
267
268- (void)update:(OOTimeDelta)delta_t;
269
270- (void)applyVelocity:(OOTimeDelta)delta_t;
271- (BOOL)checkCloseCollisionWith:(Entity*)other;
272
273- (void)takeEnergyDamage:(double)amount from:(Entity*)ent becauseOf:(Entity*)other weaponIdentifier:(NSString*)weaponIdentifier;
274
275- (void)dumpState; // General "describe situtation verbosely in log" command.
276- (void)dumpSelfState; // Subclasses should override this, not -dumpState, and call throught to super first.
277
278- (NSUInteger)lastDrawCounter;
279- (void)setLastDrawCounter:(NSUInteger)drawCounter;
280
281// Subclass repsonsibilities
282- (double)findCollisionRadius;
283- (void)drawImmediate:(bool)immediate translucent:(bool)translucent;
284- (BOOL)isVisible;
285- (BOOL)isInSpace;
287
288// For shader bindings.
289- (GLfloat)universalTime;
290- (GLfloat)spawnTime;
291- (GLfloat)timeElapsedSinceSpawn;
292- (void)setAtmosphereFogging:(OOColor*)fogging;
294
295#ifdef OO_DEBUG_MONITOR_ENABLED
296- (NSString*)descriptionForObjDumpBasic;
297- (NSString*)descriptionForObjDump;
298
299- (NSSet*)allTextures;
300#endif
301
302@end
303
304@protocol OOHUDBeaconIcon;
305
306// Methods that must be supported by entities with beacons, regardless of type.
307@protocol OOBeaconEntity
308
309- (NSComparisonResult)compareBeaconCodeWith:(Entity<OOBeaconEntity>*)other;
310- (NSString*)beaconCode;
311- (void)setBeaconCode:(NSString*)bcode;
312- (NSString*)beaconLabel;
313- (void)setBeaconLabel:(NSString*)blabel;
314- (BOOL)isBeacon;
315- (id<OOHUDBeaconIcon>)beaconDrawable;
316- (Entity<OOBeaconEntity>*)prevBeacon;
317- (Entity<OOBeaconEntity>*)nextBeacon;
318- (void)setPrevBeacon:(Entity<OOBeaconEntity>*)beaconShip;
319- (void)setNextBeacon:(Entity<OOBeaconEntity>*)beaconShip;
321
322@end
323
324enum {
325 // Values used for unknown strings.
326 kOOEntityStatusDefault = STATUS_INACTIVE,
327 kOOScanClassDefault = CLASS_NOT_SET
328};
329
332
333NSString* OOStringFromScanClass(OOScanClass scanClass) CONST_FUNC;
@ kOOScanClassDefault
Definition Entity.h:327
@ kOOEntityStatusDefault
Definition Entity.h:326
OOEntityStatus
Definition Entity.h:60
OOEntityStatus OOEntityStatusFromString(NSString *string) PURE_FUNC
OOScanClass OOScanClassFromString(NSString *string) PURE_FUNC
NSString * OOStringFromScanClass(OOScanClass scanClass) CONST_FUNC
OOScanClass
Definition Entity.h:69
NSString * OOStringFromEntityStatus(OOEntityStatus status) CONST_FUNC
#define PURE_FUNC
#define CONST_FUNC
double OOHPScalar
Definition OOMaths.h:51
uint16_t OOUniversalID
Definition OOTypes.h:165
double OOTimeDelta
Definition OOTypes.h:193
double OOTimeAbsolute
Definition OOTypes.h:192
OOColor * fogUniform()
Definition Entity.m:996
unsigned isImmuneToBreakPatternHide
Definition Entity.m:1050
unsigned throw_sparks
Definition Entity.h:97
BOOL validForAddToUniverse()
Definition Entity.m:195
void throwSparks()
Definition Entity.m:537
OOWeakReference * _owner
Definition Entity.h:158
void wasAddedToUniverse()
Definition Entity.m:489
OOColor * atmosphereFogging
Definition Entity.h:111
unsigned hasRotated
Definition Entity.h:93
void removeFromLinkedLists()
Definition Entity.m:269
GLfloat collision_radius
Definition Entity.h:107
GLfloat maxEnergy
Definition Entity.m:763
unsigned isVisualEffect
Definition Entity.m:185
GLfloat timeElapsedSinceSpawn()
Definition Entity.m:985
CollisionRegion * collisionRegion
Definition Entity.m:505
OOUniversalID universalID
Definition Entity.m:522
HPVector absolutePositionForSubentity()
Definition Entity.m:627
Entity * z_next
Definition Entity.h:118
NSMutableArray * collisionArray()
Definition Entity.m:845
double camZeroDistance()
Definition Entity.m:653
void dumpState()
Definition Entity.m:903
NSMutableArray * collidingEntities
Definition Entity.h:144
Entity * z_previous
Definition Entity.h:118
Quaternion orientation
Definition Entity.m:682
unsigned isSubEntity
Definition Entity.m:140
BOOL isSun()
Definition Entity.m:155
GLfloat cameraRangeFront()
Definition Entity.m:586
void updateCameraRelativePosition()
Definition Entity.m:622
unsigned hasMoved
Definition Entity.h:92
int zero_index
Definition Entity.h:113
GLfloat energy
Definition Entity.m:753
GLfloat zero_distance
Definition Entity.h:104
unsigned isShip
Definition Entity.m:125
unsigned collisionTestFilter
Definition Entity.h:96
OOTimeAbsolute spawnTime
Definition Entity.m:980
double zeroDistance()
Definition Entity.m:648
GLfloat collisionRadius()
Definition Entity.m:830
unsigned hasCollided
Definition Entity.h:94
OOScanClass scanClass
Definition Entity.m:743
Entity * x_previous
Definition Entity.h:116
void wasRemovedFromUniverse()
Definition Entity.m:494
BOOL isDock()
Definition Entity.m:130
BOOL isInSpace()
Definition Entity.m:1033
void orientationChanged()
Definition Entity.m:697
Vector cameraRelativePosition
Definition Entity.m:581
OOEntityStatus status()
Definition Entity.m:733
unsigned isWormhole
Definition Entity.m:175
GLfloat frustumRadius()
Definition Entity.m:835
GLfloat cam_zero_distance
Definition Entity.h:105
GLfloat distanceTravelled
Definition Entity.m:718
OOMatrix transformationMatrix()
Definition Entity.m:813
OOMatrix drawTransformationMatrix()
Definition Entity.m:819
NSUInteger lastDrawCounter
Definition Entity.m:963
BOOL isSky()
Definition Entity.m:170
BOOL isEffect()
Definition Entity.m:180
void updateLinkedLists()
Definition Entity.m:393
void warnAboutHostiles()
Definition Entity.m:499
ShipEntity * rootShipEntity()
Definition Entity.m:566
BoundingBox boundingBox
Definition Entity.m:666
unsigned isStation
Definition Entity.m:135
unsigned isPlayer
Definition Entity.m:145
BOOL throwingSparks()
Definition Entity.m:527
void dumpSelfState()
Definition Entity.m:918
BOOL isVisible()
Definition Entity.m:1028
NSUInteger _sessionID
Definition Entity.h:156
HPVector position
Definition Entity.m:576
BOOL canCollide()
Definition Entity.m:825
GLfloat cameraRangeBack()
Definition Entity.m:591
Quaternion lastOrientation
Definition Entity.h:130
BOOL isWaypoint()
Definition Entity.m:190
Entity * x_next
Definition Entity.h:116
struct JSObject * _jsSelf
Definition Entity.h:151
GLfloat no_draw_distance
Definition Entity.h:106
OOMatrix rotationMatrix()
Definition Entity.m:803
Quaternion normalOrientation()
Definition Entity.m:687
BOOL isStellarObject()
Definition Entity.m:165
OOUniversalID shadingEntityID
Definition Entity.h:122
Vector velocity
Definition Entity.m:708
GLfloat universalTime()
Definition Entity.m:975
ShipEntity * parentEntity()
Definition Entity.m:553
BOOL isPlanet()
Definition Entity.m:150
unsigned isExplicitlyNotMainStation
Definition Entity.h:99
Entity * collider
Definition Entity.h:124
void addToLinkedLists()
Definition Entity.m:207
Entity * collision_chain
Definition Entity.h:120
OOEntityStatus _status
Definition Entity.h:159
HPVector lastPosition
Definition Entity.h:129
double speed()
Definition Entity.m:713
OOMatrix rotMatrix
Definition Entity.h:134
id owner()
Definition Entity.m:548
Entity * y_next
Definition Entity.h:117
unsigned isSunlit
Definition Entity.m:160
Entity * y_previous
Definition Entity.h:117
GLfloat mass
Definition Entity.m:671
double findCollisionRadius()
Definition Entity.m:888
OOMatrix drawRotationMatrix()
Definition Entity.m:808
Entity< OOBeaconEntity > * nextBeacon()
id< OOHUDBeaconIcon > beaconDrawable()
NSString * beaconCode()
Entity< OOBeaconEntity > * prevBeacon()
NSString * beaconLabel()