Line data Source code
1 0 : /*
2 :
3 : OOWaypointEntity.m
4 :
5 : Oolite
6 : Copyright (C) 2004-2013 Giles C Williams and contributors
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License
10 : as published by the Free Software Foundation; either version 2
11 : of the License, or (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 : MA 02110-1301, USA.
22 :
23 : */
24 :
25 : #import "OOWaypointEntity.h"
26 : #import "Entity.h"
27 : #import "OOCollectionExtractors.h"
28 : #import "OOStringExpander.h"
29 : #import "Universe.h"
30 : #import "PlayerEntity.h"
31 : #import "OOPolygonSprite.h"
32 : #import "OOOpenGL.h"
33 : #import "OOMacroOpenGL.h"
34 :
35 0 : #define OOWAYPOINT_KEY_POSITION @"position"
36 0 : #define OOWAYPOINT_KEY_ORIENTATION @"orientation"
37 0 : #define OOWAYPOINT_KEY_SIZE @"size"
38 0 : #define OOWAYPOINT_KEY_CODE @"beaconCode"
39 0 : #define OOWAYPOINT_KEY_LABEL @"beaconLabel"
40 :
41 : @implementation OOWaypointEntity
42 :
43 : + (instancetype) waypointWithDictionary:(NSDictionary *)info
44 : {
45 : return [[[OOWaypointEntity alloc] initWithDictionary:info] autorelease];
46 : }
47 :
48 : - (id) initWithDictionary:(NSDictionary *)info
49 : {
50 : self = [super init];
51 : if (EXPECT_NOT(self == nil)) return nil;
52 :
53 : oriented = YES;
54 : position = [info oo_hpvectorForKey:OOWAYPOINT_KEY_POSITION];
55 : Quaternion q = [info oo_quaternionForKey:OOWAYPOINT_KEY_ORIENTATION];
56 : [self setOrientation:q];
57 : [self setSize:[info oo_nonNegativeFloatForKey:OOWAYPOINT_KEY_SIZE defaultValue:1000.0]];
58 : [self setBeaconCode:[info oo_stringForKey:OOWAYPOINT_KEY_CODE defaultValue:@"W"]];
59 : [self setBeaconLabel:[info oo_stringForKey:OOWAYPOINT_KEY_LABEL defaultValue:@"Waypoint"]];
60 :
61 : [self setStatus:STATUS_EFFECT];
62 : [self setScanClass:CLASS_NO_DRAW];
63 :
64 : return self;
65 : }
66 :
67 :
68 0 : - (void) dealloc
69 : {
70 : DESTROY(_beaconCode);
71 : DESTROY(_beaconLabel);
72 : DESTROY(_prevBeacon);
73 : DESTROY(_nextBeacon);
74 : DESTROY(_beaconDrawable);
75 :
76 : [super dealloc];
77 : }
78 :
79 :
80 : // override
81 0 : - (void) setOrientation:(Quaternion)q
82 : {
83 : if (quaternion_equal(q,kZeroQuaternion)) {
84 : q = kIdentityQuaternion;
85 : oriented = NO;
86 : } else {
87 : oriented = YES;
88 : }
89 : [super setOrientation:q];
90 : }
91 :
92 :
93 : - (BOOL) oriented
94 : {
95 : return oriented;
96 : }
97 :
98 :
99 : - (OOScalar) size
100 : {
101 : return _size;
102 : }
103 :
104 :
105 : - (void) setSize:(OOScalar)newSize
106 : {
107 : if (newSize > 0)
108 : {
109 : _size = newSize;
110 : no_draw_distance = newSize * newSize * NO_DRAW_DISTANCE_FACTOR * NO_DRAW_DISTANCE_FACTOR * 2;
111 : }
112 : }
113 :
114 :
115 :
116 0 : - (BOOL) isEffect
117 : {
118 : return YES;
119 : }
120 :
121 :
122 0 : - (BOOL) isWaypoint
123 : {
124 : return YES;
125 : }
126 :
127 :
128 0 : - (void) drawImmediate:(bool)immediate translucent:(bool)translucent
129 : {
130 : if (!translucent || no_draw_distance < cam_zero_distance)
131 : {
132 : return;
133 : }
134 :
135 : if (![PLAYER hasEquipmentItemProviding:@"EQ_ADVANCED_COMPASS"])
136 : {
137 : return;
138 : }
139 :
140 : int8_t i,j,k;
141 :
142 : GLfloat a = 0.75;
143 : if ([PLAYER compassTarget] != self)
144 : {
145 : a *= 0.25;
146 : }
147 : if (cam_zero_distance > _size * _size)
148 : {
149 : // dim out as gets further away; 2-D HUD display more
150 : // important at long range
151 : a -= 0.004f*(sqrtf(cam_zero_distance) / _size);
152 : }
153 : if (a < 0.01f)
154 : {
155 : return;
156 : }
157 :
158 : GLfloat s0 = _size;
159 : GLfloat s1 = _size * 0.75f;
160 :
161 : OO_ENTER_OPENGL();
162 : OOSetOpenGLState(OPENGL_STATE_TRANSLUCENT_PASS);
163 : OOGL(glEnable(GL_BLEND));
164 : GLScaledLineWidth(1.0);
165 :
166 : OOGL(glColor4f(0.0, 0.0, 1.0, a));
167 : OOGLBEGIN(GL_LINES);
168 : for (i = -1; i <= 1; i+=2)
169 : {
170 : for (j = -1; j <= 1; j+=2)
171 : {
172 : for (k = -1; k <= 1; k+=2)
173 : {
174 : glVertex3f(i*s0,j*s0,k*s1); glVertex3f(i*s0,j*s1,k*s0);
175 : glVertex3f(i*s0,j*s1,k*s0); glVertex3f(i*s1,j*s0,k*s0);
176 : glVertex3f(i*s1,j*s0,k*s0); glVertex3f(i*s0,j*s0,k*s1);
177 : }
178 : }
179 : }
180 : if (oriented)
181 : {
182 : while (s1 > 20.0f)
183 : {
184 : glVertex3f(-20.0,0,-s1-20.0f); glVertex3f(0,0,-s1);
185 : glVertex3f(20.0,0,-s1-20.0f); glVertex3f(0,0,-s1);
186 : glVertex3f(-20.0,0,s1-20.0f); glVertex3f(0,0,s1);
187 : glVertex3f(20.0,0,s1-20.0f); glVertex3f(0,0,s1);
188 : s1 *= 0.5;
189 : }
190 : }
191 : OOGLEND();
192 :
193 : OOGL(glDisable(GL_BLEND));
194 : OOVerifyOpenGLState();
195 : }
196 :
197 :
198 : /* beacons */
199 :
200 0 : - (NSComparisonResult) compareBeaconCodeWith:(Entity<OOBeaconEntity> *) other
201 : {
202 : return [[self beaconCode] compare:[other beaconCode] options: NSCaseInsensitiveSearch];
203 : }
204 :
205 :
206 0 : - (NSString *) beaconCode
207 : {
208 : return _beaconCode;
209 : }
210 :
211 :
212 0 : - (void) setBeaconCode:(NSString *)bcode
213 : {
214 : if ([bcode length] == 0) bcode = nil;
215 :
216 : if (_beaconCode != bcode)
217 : {
218 : [_beaconCode release];
219 : _beaconCode = [bcode copy];
220 :
221 : DESTROY(_beaconDrawable);
222 : }
223 : // if not blanking code and label is currently blank, default label to code
224 : if (bcode != nil && (_beaconLabel == nil || [_beaconLabel length] == 0))
225 : {
226 : [self setBeaconLabel:bcode];
227 : }
228 :
229 : }
230 :
231 :
232 0 : - (NSString *) beaconLabel
233 : {
234 : return _beaconLabel;
235 : }
236 :
237 :
238 0 : - (void) setBeaconLabel:(NSString *)blabel
239 : {
240 : if ([blabel length] == 0) blabel = nil;
241 :
242 : if (_beaconLabel != blabel)
243 : {
244 : [_beaconLabel release];
245 : _beaconLabel = [OOExpand(blabel) retain];
246 : }
247 : }
248 :
249 :
250 0 : - (BOOL) isBeacon
251 : {
252 : return [self beaconCode] != nil;
253 : }
254 :
255 :
256 0 : - (id <OOHUDBeaconIcon>) beaconDrawable
257 : {
258 : if (_beaconDrawable == nil)
259 : {
260 : NSString *beaconCode = [self beaconCode];
261 : NSUInteger length = [beaconCode length];
262 :
263 : if (length > 1)
264 : {
265 : NSArray *iconData = [[UNIVERSE descriptions] oo_arrayForKey:beaconCode];
266 : if (iconData != nil) _beaconDrawable = [[OOPolygonSprite alloc] initWithDataArray:iconData outlineWidth:0.5 name:beaconCode];
267 : }
268 :
269 : if (_beaconDrawable == nil)
270 : {
271 : if (length > 0) _beaconDrawable = [[beaconCode substringToIndex:1] retain];
272 : else _beaconDrawable = @"";
273 : }
274 : }
275 :
276 : return _beaconDrawable;
277 : }
278 :
279 :
280 0 : - (Entity <OOBeaconEntity> *) prevBeacon
281 : {
282 : return [_prevBeacon weakRefUnderlyingObject];
283 : }
284 :
285 :
286 0 : - (Entity <OOBeaconEntity> *) nextBeacon
287 : {
288 : return [_nextBeacon weakRefUnderlyingObject];
289 : }
290 :
291 :
292 0 : - (void) setPrevBeacon:(Entity <OOBeaconEntity> *)beaconShip
293 : {
294 : if (beaconShip != [self prevBeacon])
295 : {
296 : [_prevBeacon release];
297 : _prevBeacon = [beaconShip weakRetain];
298 : }
299 : }
300 :
301 :
302 0 : - (void) setNextBeacon:(Entity <OOBeaconEntity> *)beaconShip
303 : {
304 : if (beaconShip != [self nextBeacon])
305 : {
306 : [_nextBeacon release];
307 : _nextBeacon = [beaconShip weakRetain];
308 : }
309 : }
310 :
311 :
312 0 : - (BOOL) isJammingScanning
313 : {
314 : return NO;
315 : }
316 :
317 :
318 : @end
|