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