Oolite
Loading...
Searching...
No Matches
SkyEntity.m
Go to the documentation of this file.
1/*
2
3SkyEntity.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "SkyEntity.h"
26#import "OOSkyDrawable.h"
27#import "PlayerEntity.h"
28
29#import "MyOpenGLView.h"
31#import "OOColor.h"
32#import "OOMaterial.h"
33#import "OOMaths.h"
34#import "OOStringParsing.h"
35#import "Universe.h"
36
37#define SKY_BASIS_STARS 4800
38#define SKY_BASIS_BLOBS 1280
39#define SKY_clusterChance 0.80
40#define SKY_alpha 0.10
41#define SKY_scale 10.0
42
43@interface SkyEntity (OOPrivate)
44
45- (BOOL)readColor1:(OOColor**)ioColor1 andColor2:(OOColor**)ioColor2 andColor3:(OOColor**)ioColor3 andColor4:(OOColor**)ioColor4 fromDictionary:(NSDictionary*)dictionary;
46
47@end
48
49@implementation SkyEntity
50
51- (id)initWithColors:(OOColor*)col1 col2:(OOColor*)col2 andSystemInfo:(NSDictionary*)systemInfo
52{
53 OOSkyDrawable* skyDrawable;
54 float clusterChance,
55 alpha,
56 scale,
57 starCountMultiplier,
58 nebulaCountMultiplier;
59 signed starCount, // Need to be able to hold -1...
60 nebulaCount;
61
62 self = [super init];
63 if (self == nil)
64 return nil;
65
68
69 // Load colours
70 BOOL nebulaColorSet = [self readColor1:&col1 andColor2:&col2 andColor3:&col3 andColor4:&col4 fromDictionary:systemInfo];
71
72 skyColor = [[OOColor colorWithDescription:[systemInfo objectForKey:@"sun_color"]] retain];
73 if (skyColor == nil) {
74 skyColor = [[col2 blendedColorWithFraction:0.5 ofColor:col1] retain];
75 }
76
77 // Load distribution values
78 clusterChance = [systemInfo oo_floatForKey:@"sky_blur_cluster_chance" defaultValue:SKY_clusterChance];
79 alpha = [systemInfo oo_floatForKey:@"sky_blur_alpha" defaultValue:SKY_alpha];
80 scale = [systemInfo oo_floatForKey:@"sky_blur_scale" defaultValue:SKY_scale];
81
82 // Load star count
83 starCount = [systemInfo oo_floatForKey:@"sky_n_stars" defaultValue:-1];
84 starCountMultiplier = [systemInfo oo_floatForKey:@"star_count_multiplier" defaultValue:1.0f];
85 if (starCountMultiplier < 0.0f)
86 starCountMultiplier *= -1.0f;
87 if (0 <= starCount) {
88 // changed for 1.82, default set to 1
89 // lets OXPers modify the broad number without stopping variation
90 starCount *= starCountMultiplier;
91 } else {
92 starCount = starCountMultiplier * SKY_BASIS_STARS * (0.5 + randf());
93 }
94
95 // ...and nebula count. (Note: simplifying this would change the appearance of stars/blobs.)
96 nebulaCount = [systemInfo oo_floatForKey:@"sky_n_blurs" defaultValue:-1];
97 nebulaCountMultiplier = [systemInfo oo_floatForKey:@"nebula_count_multiplier" defaultValue:1.0f];
98 if (nebulaCountMultiplier < 0.0f)
99 nebulaCountMultiplier *= -1.0f;
100 if (0 <= nebulaCount) {
101 // changed for 1.82, default set to 1
102 // lets OXPers modify the broad number without stopping variation
103 nebulaCount *= nebulaCountMultiplier;
104 } else {
105 nebulaCount = nebulaCountMultiplier * SKY_BASIS_BLOBS * (0.5 + randf());
106 }
107
108 if ([UNIVERSE reducedDetail]) {
109 // limit stars and blobs to basis levels, and halve stars again
110 if (starCount > SKY_BASIS_STARS) {
111 starCount = SKY_BASIS_STARS;
112 }
113 starCount /= 2;
114 if (nebulaCount > SKY_BASIS_BLOBS) {
115 nebulaCount = SKY_BASIS_BLOBS;
116 }
117 }
118
119 skyDrawable = [[OOSkyDrawable alloc]
120 initWithColor1:col1
121 Color2:col2
122 Color3:col3
123 Color4:col4
124 starCount:starCount
125 nebulaCount:nebulaCount
126 nebulaHueFix:nebulaColorSet
127 clusterFactor:clusterChance
128 alpha:alpha
129 scale:scale];
130 [self setDrawable:skyDrawable];
131 [skyDrawable release];
132
133 [self setStatus:STATUS_EFFECT];
134
135 return self;
136}
137
138- (void)dealloc
139{
140 [skyColor release];
141
142 [super dealloc];
143}
144
146{
147 return skyColor;
148}
149
150- (BOOL)changeProperty:(NSString*)key withDictionary:(NSDictionary*)dict
151{
152 id object = [dict objectForKey:key];
153
154 // TODO: properties requiring reInit?
155 if ([key isEqualToString:@"sun_color"]) {
156 OOColor* col = [[OOColor colorWithDescription:object] retain];
157 if (col != nil) {
158 [skyColor release];
159 skyColor = [col copy];
160 [col release];
161 [UNIVERSE setLighting];
162 }
163 } else {
164 OOLogWARN(@"script.warning", @"Change to property '%@' not applied, will apply only on leaving and re-entering this system.", key);
165 return NO;
166 }
167 return YES;
168}
169
170- (void)update:(OOTimeDelta)delta_t
171{
172 PlayerEntity* player = PLAYER;
175 if (player != nil) {
177 } else {
178 OOLog(@"sky.warning", @"%@", @"PLAYER is nil");
179 }
180}
181
182- (BOOL)isSky
183{
184 return YES;
185}
186
188{
189 return YES;
190}
191
193{
194 return NO;
195}
196
198{
199 return MAX_CLEAR_DEPTH;
200}
201
203{
204 return MAX_CLEAR_DEPTH;
205}
206
207- (void)drawImmediate:(bool)immediate translucent:(bool)translucent
208{
209 if ([UNIVERSE breakPatternHide])
210 return;
211
212 [super drawImmediate:immediate translucent:translucent];
213
214 OOCheckOpenGLErrors(@"SkyEntity after drawing %@", self);
215}
216
217#ifdef OO_DEBUG_MONITOR_ENABLED
218- (NSString*)descriptionForObjDump
219{
220 // Don't include range and visibility flag as they're irrelevant.
221 return [self descriptionForObjDumpBasic];
222}
223#endif
224
225@end
226
227@implementation SkyEntity (OOPrivate)
228
229- (BOOL)readColor1:(OOColor**)ioColor1 andColor2:(OOColor**)ioColor2 andColor3:(OOColor**)ioColor3 andColor4:(OOColor**)ioColor4 fromDictionary:(NSDictionary*)dictionary
230{
231 NSString* string = nil;
232 NSArray* tokens = nil;
233 id colorDesc = nil;
234 OOColor* color = nil;
235 BOOL nebulaSet = NO;
236
237 assert(ioColor1 != NULL && ioColor2 != NULL);
238
239 string = [dictionary oo_stringForKey:@"sky_rgb_colors"];
240 if (string != nil) {
241 tokens = ScanTokensFromString(string);
242
243 if ([tokens count] == 6) {
244 float r1 = OOClamp_0_1_f([tokens oo_floatAtIndex:0]);
245 float g1 = OOClamp_0_1_f([tokens oo_floatAtIndex:1]);
246 float b1 = OOClamp_0_1_f([tokens oo_floatAtIndex:2]);
247 float r2 = OOClamp_0_1_f([tokens oo_floatAtIndex:3]);
248 float g2 = OOClamp_0_1_f([tokens oo_floatAtIndex:4]);
249 float b2 = OOClamp_0_1_f([tokens oo_floatAtIndex:5]);
250 *ioColor1 = [OOColor colorWithRed:r1 green:g1 blue:b1 alpha:1.0];
251 *ioColor2 = [OOColor colorWithRed:r2 green:g2 blue:b2 alpha:1.0];
252 } else {
253 OOLogWARN(@"sky.fromDict", @"could not interpret \"%@\" as two RGB colours (must be six numbers).", string);
254 }
255 }
256 colorDesc = [dictionary objectForKey:@"sky_color_1"];
257 if (colorDesc != nil) {
258 color = [[OOColor colorWithDescription:colorDesc] premultipliedColor];
259 if (color != nil)
260 *ioColor1 = color;
261 else
262 OOLogWARN(@"sky.fromDict", @"could not interpret \"%@\" as a colour.", colorDesc);
263 }
264 colorDesc = [dictionary objectForKey:@"sky_color_2"];
265 if (colorDesc != nil) {
266 color = [[OOColor colorWithDescription:colorDesc] premultipliedColor];
267 if (color != nil)
268 *ioColor2 = color;
269 else
270 OOLogWARN(@"sky.fromDict", @"could not interpret \"%@\" as a colour.", colorDesc);
271 }
272
273 colorDesc = [dictionary objectForKey:@"nebula_color_1"];
274 if (colorDesc != nil) {
275 color = [[OOColor colorWithDescription:colorDesc] premultipliedColor];
276 if (color != nil) {
277 *ioColor3 = color;
278 nebulaSet = YES;
279 } else
280 OOLogWARN(@"sky.fromDict", @"could not interpret \"%@\" as a colour.", colorDesc);
281 } else {
282 colorDesc = [dictionary objectForKey:@"sky_color_1"];
283 if (colorDesc != nil) {
284 color = [[OOColor colorWithDescription:colorDesc] premultipliedColor];
285 if (color != nil)
286 *ioColor3 = color;
287 else
288 OOLogWARN(@"sky.fromDict", @"could not interpret \"%@\" as a colour.", colorDesc);
289 }
290 }
291
292 colorDesc = [dictionary objectForKey:@"nebula_color_2"];
293 if (colorDesc != nil) {
294 color = [[OOColor colorWithDescription:colorDesc] premultipliedColor];
295 if (color != nil) {
296 *ioColor4 = color;
297 nebulaSet = YES;
298 } else
299 OOLogWARN(@"sky.fromDict", @"could not interpret \"%@\" as a colour.", colorDesc);
300 } else {
301 colorDesc = [dictionary objectForKey:@"sky_color_2"];
302 if (colorDesc != nil) {
303 color = [[OOColor colorWithDescription:colorDesc] premultipliedColor];
304 if (color != nil)
305 *ioColor4 = color;
306 else
307 OOLogWARN(@"sky.fromDict", @"could not interpret \"%@\" as a colour.", colorDesc);
308 }
309 }
310 return nebulaSet;
311}
312
313@end
#define MAX_CLEAR_DEPTH
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define OOLogWARN(class, format,...)
Definition OOLogging.h:126
#define OOLog(class, format,...)
Definition OOLogging.h:84
BOOL OOCheckOpenGLErrors(NSString *format,...)
Definition OOOpenGL.m:38
unsigned count
return nil
NSMutableArray * ScanTokensFromString(NSString *values)
double OOTimeDelta
Definition OOTypes.h:193
#define PLAYER
#define SKY_BASIS_STARS
Definition SkyEntity.m:37
#define SKY_BASIS_BLOBS
Definition SkyEntity.m:38
#define UNIVERSE
Definition Universe.h:827
GLfloat zero_distance
Definition Entity.h:104
GLfloat cam_zero_distance
Definition Entity.h:105
HPVector position
Definition Entity.m:576
id init()
Definition Entity.m:64
void setStatus:(OOEntityStatus stat)
Definition Entity.m:728
OOColor * colorWithRed:green:blue:alpha:(float red,[green] float green,[blue] float blue,[alpha] float alpha)
Definition OOColor.m:107
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
OOColor * premultipliedColor()
Definition OOColor.m:447
OOColor * blendedColorWithFraction:ofColor:(float fraction,[ofColor] OOColor *color)
Definition OOColor.m:309
void setDrawable:(OODrawable *drawable)
void drawImmediate:translucent:(bool immediate, [translucent] bool translucent)
HPVector viewpointPosition()
GLfloat cameraRangeFront()
Definition SkyEntity.m:197
GLfloat cameraRangeBack()
Definition SkyEntity.m:202
BOOL canCollide()
Definition SkyEntity.m:192
BOOL isSky()
Definition SkyEntity.m:182
BOOL isVisible()
Definition SkyEntity.m:187
void dealloc()
Definition SkyEntity.m:138
BOOL readColor1:andColor2:andColor3:andColor4:fromDictionary:(OOColor **ioColor1,[andColor2] OOColor **ioColor2,[andColor3] OOColor **ioColor3,[andColor4] OOColor **ioColor4,[fromDictionary] NSDictionary *dictionary)
Definition SkyEntity.m:229
OOColor * skyColor
Definition SkyEntity.m:145
float randf(void)