-
Notifications
You must be signed in to change notification settings - Fork 6
/
MCMapOpenGLView.mm
executable file
·3002 lines (2541 loc) · 91.1 KB
/
MCMapOpenGLView.mm
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MCMapOpenGLView.mm
* MCMap Live
*
* Created by DK on 10/13/10.
*
*/
#import "MCMapOpenGLView.h"
#include "MapChunk.h"
#include "extractcolors.h"
// ==================================
typedef enum RenderLightingModes {UNIFORM, DAY, NIGHT, TORCHLIGHT, CAVE} LightingMode;
static NSString *OVERWORLD_DIMENSION_PATH = @"region";
static NSString *END_DIMENSION_PATH = @"DIM1/region";
static NSString *NETHER_DIMENSION_PATH = @"DIM-1/region";
recVec gOrigin = {0.0, 0.0, 0.0};
float zoom_level = 0;
LightingMode lighting_mode = UNIFORM;
int maxdepth = 256;
int newdepth = 256;
int noise_level = 0;
int orientation = 0; // 0=SE, 1=NE, 2=NW, 3=SW
bool showBiomes = NO;
// single set of interaction flags and states
bool useMipMaps = YES;
bool worldLoaded = NO;
std::set< std::pair<int,int> > hasContent;
MapChunk **worldmap = NULL;
NSString *dimensionPath = [OVERWORLD_DIMENSION_PATH retain];
NSInteger mystAge = 0;
NSSavePanel* currentsavepanel;
int nox,noy,nmx,nmy;
int ox,oy,mx,my; // Offset X,Y - Mapsize X,Y (in my code I call the map Z coord Y)
// It became convention before I knew it was Z internally, sorry!
bool processing = NO;
NSTask* processingTask = Nil;
bool saveSliceSequence = NO;
int slice_minx;
int slice_miny;
int slice_maxx;
int slice_maxy;
int clean_x = 0;
int clean_y = 0;
NSString* slice_basename;
bool saveWorld = NO;
bool saveChunk = NO;
bool extractBiome = NO;
NSString* current_world_path = @"none";
bool drawnOnce = false;
// Debug vars
BOOL lock_zoom = NO;
float dzoom = 0;
// Special textures
GLuint renderingTexture = 0;
GLuint saveChunkTexture = 0;
GLuint saveWorldTexture = 0;
GLuint biomeExtractTexture = 0;
GLuint beginTexture = 0;
float ffx = 0.998; // 0.978; // The amount by which we tweak tile spacing to avoid seams.
float ffy = 0.998; //0.974;
NSString* mcmap_path;
NSString* temp_dir;
// Colors system
NSString* colors_path;
BOOL default_colors;
NSMenuItem * storedColorStateMenuItem = Nil;
NSString* storedColorsPath;
BOOL slowblending = false;
// Listing of all the worlds
NSMutableArray* mcWorlds;
NSMutableArray * render_settings;
int renders = 0;
std::list<MapChunk*> renderers;
int MAX_SIMULTANEOUS_RENDERS = 4;
GLint gDollyPanStartPoint[2] = {0, 0};
GLboolean gDolly = GL_FALSE;
GLboolean gPan = GL_FALSE;
GLboolean gTrackball = GL_FALSE;
MCMapOpenGLView * gTrackingViewInfo = NULL;
BOOL isGesture = NO;
int userTimer = 0;
int userTimerReset = 20;
// time and message info
CFAbsoluteTime gMsgPresistance = 10.0f;
// error output
float gErrorTime;
// ==================================
#pragma mark ---- Utilities ----
static CFAbsoluteTime gStartTime = 0.0f;
static int max(int a, int b)
{
if (a > b)
return a;
return b;
}
static int min(int a, int b)
{
if (a < b)
return a;
return b;
}
// set app start time
static void setStartTime (void)
{
gStartTime = CFAbsoluteTimeGetCurrent ();
}
// ---------------------------------
// return float elpased time in seconds since app start
static CFAbsoluteTime getElapsedTime (void)
{
return CFAbsoluteTimeGetCurrent () - gStartTime;
}
#pragma mark ---- Error Reporting ----
// error reporting as both window message and debugger string
void reportError (char * strError)
{
NSMutableDictionary *attribs = [NSMutableDictionary dictionary];
[attribs setObject: [NSFont fontWithName: @"Monaco" size: 9.0f] forKey: NSFontAttributeName];
[attribs setObject: [NSColor whiteColor] forKey: NSForegroundColorAttributeName];
gErrorTime = getElapsedTime ();
NSString * errString = [NSString stringWithFormat:@"Error: %s (at time: %0.1f secs).", strError, gErrorTime];
NSLog (@"%@\n", errString);
}
// ---------------------------------
// if error dump gl errors to debugger string, return error
GLenum glReportError (void)
{
GLenum err = glGetError();
if (GL_NO_ERROR != err)
{
char *reportError = ((char *) gluErrorString (err));
NSLog(@"OpenGL Error: %s",reportError);
}
return err;
}
#pragma mark ---- OpenGL Utils ----
// --------------------------------
//
// This function is for loading plain old textures, nothing fancy.
GLuint loadTexture(NSString* filepath)
{
NSBitmapImageRep *theImage;
int width, height, bytesPRow;
unsigned char *fixedImageData;
// Load the image into an NSBitmapImageRep
theImage = [ NSBitmapImageRep imageRepWithContentsOfFile:filepath ];
if( theImage != nil )
{
// Get some key info on the texture that was just loaded.
bytesPRow = [ theImage bytesPerRow ];
width = [ theImage pixelsWide ];
height = [ theImage pixelsHigh ];
// Convert the NSImage to a CGImage
CGImageRef image = [theImage CGImage];
// Use CGImage to mask out the pure black areas
//CGFloat mask[] = {0, 0, 0, 0, 0, 0}; // Change black to transparent
//CGImageRef masked_image = CGImageCreateWithMaskingColors(image,mask);
//Set up a Core Graphics context that's compatible with OpenGL's RGBA
fixedImageData = (unsigned char*)calloc(width * 4, height);
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
CGContextRef myBitmapContext = CGBitmapContextCreate(
fixedImageData, // pointer raw storage
width, // the width of the context
height, // the height of the context
8, // bits per pixel
width*4, // bytes per row
color_space, // what color space to use
kCGImageAlphaPremultipliedLast // the format of the alpha channel
);
CGContextSetInterpolationQuality(myBitmapContext,kCGInterpolationNone);
CGContextDrawImage(myBitmapContext, CGRectMake(0,0, width, height), image);
glEnable(GL_TEXTURE_2D);
GLuint texture[1];
glGenTextures(1,texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width,height, 0, GL_RGBA, GL_UNSIGNED_BYTE, fixedImageData);
// Release all the stuff we allocated
CGContextRelease(myBitmapContext);
CGColorSpaceRelease(color_space);
free(fixedImageData);
return texture[0];
} else {
NSLog(@"File not found: %@",filepath);
return renderingTexture;
}
}
// ---------------------------------
static void block2screen(int bx, int by, float* screenCoords)
{
// This function will need to change if we're working with a rotation
screenCoords[0] = ffx*256*(bx+by);
screenCoords[1] = ffy*128*(-bx+by);
}
static void screen2block(float x, float y, int* boxCoords)
{
boxCoords[0] = floor(x/512-y/256);
boxCoords[1] = floor(x/512+y/256);
}
static void block2screenf(float bx, float by, float* screenCoords)
{
// This function will need to change if we're working with a rotation
screenCoords[0] = ffx*256*(bx+by);
screenCoords[1] = ffy*128*(-bx+by);
}
static void screen2blockf(float x, float y, float* boxCoords)
{
boxCoords[0] = x/512-y/256;
boxCoords[1] = x/512+y/256;
}
// ===================================
@implementation MCMapOpenGLView
// pixel format definition
+ (NSOpenGLPixelFormat*) basicPixelFormat
{
NSOpenGLPixelFormatAttribute attributes [] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer, // double buffered
//NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16, // 16 bit depth buffer
(NSOpenGLPixelFormatAttribute)nil
};
return [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
}
-(void) drawLoadMsg
{
// First reset the projection to ensure the message is onscreen
glLoadIdentity ();
float msg_size = 261*exp(zoom_level);
GLuint texture;
if (saveChunk)
texture = saveChunkTexture;
else if (saveWorld)
texture = saveWorldTexture;
else if (extractBiome)
texture = biomeExtractTexture;
else
texture = beginTexture;
glBindTexture(GL_TEXTURE_2D, texture);
glColor3f (1.0, 1.0, 1.0);
glBegin (GL_QUADS);
// Left, Top
glTexCoord2f(0, 0);
glVertex2f(-msg_size,-msg_size);
// Right top
glTexCoord2f(1, 0);
glVertex2f(msg_size, -msg_size);
// Right, bottom
glTexCoord2f(1, 1);
glVertex2f(msg_size,msg_size);
// Left, Bottom
glTexCoord2f(0, 1);
glVertex2f(-msg_size,msg_size);
glEnd ();
}
// draw a chunk based on current parameters
- (void) drawChunk:(GLuint)texture X:(int)bx Y:(int)by
{
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
float sc[2];
block2screen(bx,by,sc);
glTranslatef(sc[0],sc[1],0);
glColor3f (1.0, 1.0, 1.0);
glBegin (GL_QUADS);
// Left, Top
glTexCoord2f(0,1);
glVertex2f(-256,256);
// Right top
glTexCoord2f(1,1);
glVertex2f(255, 256);
// Right, bottom
glTexCoord2f(1,0);
glVertex2f(255,-255);
// Left, Bottom
glTexCoord2f(0,0);
glVertex2f(-256,-255);
glEnd ();
glPopMatrix();
}
// ---------------------------------
// update the projection matrix based on camera and view info
- (void) updateProjection
{
[[self openGLContext] makeCurrentContext];
// set projection
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
float zoom = exp(zoom_level);
float debug_zoom = exp(dzoom);
glOrtho(-camera.viewWidth*debug_zoom*zoom/2, camera.viewWidth*debug_zoom*zoom/2, camera.viewHeight*debug_zoom*zoom/2, -camera.viewHeight*debug_zoom*zoom/2, -1, 200);
}
// ---------------------------------
// updates the contexts model view matrix for object and camera moves
- (void) updateModelView
{
[[self openGLContext] makeCurrentContext];
// move view
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (-camera.viewPos.x, -camera.viewPos.y,0);
}
// ---------------------------------
// handles resizing of GL need context update and if the window dimensions change, a
// a window dimension update, reseting of viewport and an update of the projection matrix
- (void) resizeGL
{
NSRect rectView = [self bounds];
// ensure camera knows size changed
if ((camera.viewHeight != rectView.size.height) ||
(camera.viewWidth != rectView.size.width)) {
camera.viewHeight = rectView.size.height;
camera.viewWidth = rectView.size.width;
glViewport (0, 0, camera.viewWidth, camera.viewHeight);
[self updateProjection]; // update projection matrix
}
}
// ---------------------------------
// move camera in x/y plane
- (void)mousePan: (NSPoint)location isDelta:(BOOL)isdelta
{
GLfloat panX;
GLfloat panY;
if (isdelta)
{
panX = (location.x);
panY = (location.y);
}
else
{
panX = (gDollyPanStartPoint[0] - location.x);
panY = (gDollyPanStartPoint[1] - location.y);
gDollyPanStartPoint[0] = location.x;
gDollyPanStartPoint[1] = location.y;
}
camera.viewPos.x += (panX * exp(zoom_level));
camera.viewPos.y += (panY * exp(zoom_level));
// Place a debug string in the notification area
int bp[2];
screen2block(camera.viewPos.x,camera.viewPos.y,bp);
//[statusTextField setTitleWithMnemonic: [NSString stringWithFormat: @"&S( %f , %f ) B( %i , %i )",camera.viewPos.x,camera.viewPos.y,bp[0],bp[1]]];
}
// ---------------------------------
// per-window timer function, basic time based animation preformed here
- (void)animationTimer:(NSTimer *)timer
{
if (userTimer > 0)
userTimer--;
[ self setNeedsDisplay: YES ] ;
}
- (void) rebuildMystCraftMenu:(NSString*)worldPath
{
NSMenu *mystMenu = mystCraftAgesMenuItem.submenu;
[mystMenu removeAllItems];
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSString *file in [fileManager contentsOfDirectoryAtPath:worldPath error:nil])
{
NSRange range = [file rangeOfString:@"DIM_MYST"];
if (range.location == NSNotFound) continue;
NSInteger ageNumber = [[file substringFromIndex:(range.location + range.length)] integerValue];
if (!ageNumber) continue;
NSString *menuItemTitle = [NSString stringWithFormat:@"Age %ld", (long)ageNumber];
NSMenuItem *nextAgeItem = [[NSMenuItem alloc] initWithTitle:menuItemTitle
action:@selector(setDimensionToMystCraftAge:) keyEquivalent:@""];
nextAgeItem.tag = ageNumber;
[mystMenu addItem:nextAgeItem];
[mystCraftAgesMenuItem setEnabled:YES];
}
}
// ---------------------------------
#pragma mark ---- IB Actions ----
-(IBAction) showPlayerLocations: (id) sender
{
show_players = !show_players;
if (show_players)
[showplayersMenuItem setState: NSOnState];
else
[showplayersMenuItem setState: NSOffState];
}
-(IBAction) setHeight: (id) sender
{
newdepth = [depthSlider intValue];
if (newdepth < 1)
newdepth = 1;
else if (newdepth > 255)
newdepth = 255;
// Newdepth will be set by the draw function if it decides this would be prudent.
}
-(void) startBlockFinder:(NSArray*)blocksToShow inColor:(NSColor*)color xray:(double)xray
{
if (xray)
slowblending = YES;
else
slowblending = NO;
CGFloat r,g,b,a;
int red,green,blue,alpha;
[color getRed:&r green:&g blue:&b alpha:&a];
red = r*255.0f;
green = g*255.0f;
blue = b*255.0f;
alpha = a*255.0f;
colors_path = @"/tmp/block_finder.txt";
MinecraftColors colors;
// Load up some colors.
[self createColorArrayFromTextFile:[ NSString stringWithFormat:@"%@/Colors/Minecraft.txt",[ [ NSBundle mainBundle ] resourcePath ]] colorArray:&colors];
// Alter them so everything but selected blocks are the given color.
for(int i=0;i<MINECRAFT_TILE_COUNT;i++)
{
if([blocksToShow containsObject:[NSNumber numberWithInt:i]])
{
//NSLog(@"Set up custom color on block ID %i",i);
colors.c[i][0] = red;
colors.c[i][1] = green;
colors.c[i][2] = blue;
colors.c[i][3] = alpha;
}
else
{
int brightness = (colors.c[i][0] + 1.2*colors.c[i][1] + 0.8*colors.c[i][2])/3;
//brightness = 128;
colors.c[i][0] = brightness;
colors.c[i][1] = brightness;
colors.c[i][2] = brightness;
colors.c[i][3] = 255*(exp(-4*xray)); // Since the values close to 0 are more important.
}
}
// Write them out
[self writeColorsFromArray:&colors savePath:colors_path];
// Tell the map that new colors were set
[self setColors:Nil];
}
-(void) storeColor
{
storedColorStateMenuItem = Nil;
storedColorsPath = [colors_path copy];
NSArray* items = [colorsMenu itemArray];
for (int i=0; i<[items count];i++)
{
if ([[items objectAtIndex:i] state] == NSOnState)
storedColorStateMenuItem = [items objectAtIndex:i];
}
}
-(void) restoreColor
{
slowblending = NO;
if (storedColorStateMenuItem == Nil || storedColorStateMenuItem == defaultcolorsMenuItem)
{
colors_path = @"placeholder";
[self setColors: Nil];
[self setColors: defaultcolorsMenuItem];
}
else
{
colors_path = storedColorsPath;
[self setColors: Nil];
[storedColorStateMenuItem setState: NSOnState];
}
}
-(IBAction) setColors: (id) sender
{
BOOL changed = NO;
if (sender == Nil)
{
changed = YES;
default_colors = NO;
}
else if (sender == defaultcolorsMenuItem)
{
if (!default_colors)
{
changed = YES;
default_colors = YES;
}
}
else if (sender == customcolorsMenuItem)
{
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:YES];
[panel setAllowedFileTypes:[NSArray arrayWithObjects:@"txt",@"png",nil]];
[panel setCanChooseDirectories:NO];
[panel setResolvesAliases:YES];
[panel setAllowsMultipleSelection:NO];
[panel setPrompt:@"Set Colors"];
if ([panel runModal] == NSOKButton)
{
if ([[panel filename] hasSuffix: @".txt"])
{
colors_path = [[panel filename] retain];
default_colors = NO;
changed = YES;
}
else if ([[panel filename] hasSuffix: @".png"])
{
MinecraftColors colors;
if([self createColorArrayFromPng:[panel filename] colorArray:&colors])
{
[self writeColorsFromArray:&colors savePath:@"/tmp/terrain.txt"];
colors_path = @"/tmp/terrain.txt";
default_colors = NO;
changed = YES;
}
}
}
}
else
{
if (![colors_path hasSuffix: [ NSString stringWithFormat:@"%@.txt",[sender title]]] || default_colors )
{
if ([sender tag] == 1)
colors_path = [ NSString stringWithFormat:@"%@/Colors/%@.txt",[ [ NSBundle mainBundle ] resourcePath ],[sender title]];
else if ([sender tag] == 2)
colors_path = [ NSString stringWithFormat:[@"~/Library/Application Support/MCMap Live/%@.txt" stringByExpandingTildeInPath],[sender title]];
default_colors = NO;
changed = YES;
}
}
if (changed)
{
// Uncheck all items
NSArray* items = [colorsMenu itemArray];
for (int i=0; i<[items count];i++)
{
[[items objectAtIndex:i] setState:NSOffState];
}
if( sender != Nil)
[sender setState: NSOnState];
[self setupMapChunk];
if (worldLoaded)
{
[self invalidateAllChunks];
}
}
}
-(void) resetLightingModeMenu
{
[uniformMenuItem setState: NSOffState];
[dayMenuItem setState: NSOffState];
[torchlightMenuItem setState: NSOffState];
[nightMenuItem setState: NSOffState];
[caveMenuItem setState: NSOffState];
if (lighting_mode == UNIFORM)
[uniformMenuItem setState: NSOnState];
else if (lighting_mode == DAY)
[dayMenuItem setState: NSOnState];
else if (lighting_mode == NIGHT)
[nightMenuItem setState: NSOnState];
else if (lighting_mode == TORCHLIGHT)
[torchlightMenuItem setState: NSOnState];
else if (lighting_mode == CAVE)
[caveMenuItem setState: NSOnState];
}
-(IBAction) setLightingMode: (id) sender
{
[uniformMenuItem setState: NSOffState];
[dayMenuItem setState: NSOffState];
[torchlightMenuItem setState: NSOffState];
[nightMenuItem setState: NSOffState];
[caveMenuItem setState: NSOffState];
LightingMode newmode = UNIFORM;
if (sender == uniformMenuItem)
{
newmode = UNIFORM;
[uniformMenuItem setState: NSOnState];
}
else if (sender == dayMenuItem)
{
newmode = DAY;
[dayMenuItem setState: NSOnState];
}
else if (sender == nightMenuItem)
{
newmode = NIGHT;
[nightMenuItem setState: NSOnState];
}
else if (sender == torchlightMenuItem)
{
newmode = TORCHLIGHT;
[torchlightMenuItem setState: NSOnState];
}
else if (sender == caveMenuItem)
{
newmode = CAVE;
[caveMenuItem setState: NSOnState];
}
if (lighting_mode != newmode)
{
lighting_mode = newmode;
// Mode changed. Reset everything.
[self setupMapChunk];
if (worldLoaded)
{
[self invalidateAllChunks];
}
}
}
-(void) resetNoiseLevelMenu
{
[noise0menuItem setState: NSOffState];
[noise5menuItem setState: NSOffState];
[noise10menuItem setState: NSOffState];
[noise15menuItem setState: NSOffState];
[noise20menuItem setState: NSOffState];
if(noise_level == 0)
[noise0menuItem setState: NSOnState];
else if(noise_level == 5)
[noise5menuItem setState: NSOnState];
else if(noise_level == 10)
[noise10menuItem setState: NSOnState];
else if(noise_level == 15)
[noise15menuItem setState: NSOnState];
else if(noise_level == 20)
[noise20menuItem setState: NSOnState];
}
-(IBAction) setNoiseLevel: (id) sender
{
int new_level = 0;
[noise0menuItem setState: NSOffState];
[noise5menuItem setState: NSOffState];
[noise10menuItem setState: NSOffState];
[noise15menuItem setState: NSOffState];
[noise20menuItem setState: NSOffState];
if(sender == noise0menuItem)
{
new_level = 0;
[noise0menuItem setState: NSOnState];
}
else if(sender == noise5menuItem)
{
new_level = 5;
[noise5menuItem setState: NSOnState];
}
else if(sender == noise10menuItem)
{
new_level = 10;
[noise10menuItem setState: NSOnState];
}
else if(sender == noise15menuItem)
{
new_level = 15;
[noise15menuItem setState: NSOnState];
}
else if(sender == noise20menuItem)
{
new_level = 20;
[noise20menuItem setState: NSOnState];
}
if (noise_level != new_level)
{
noise_level = new_level;
// Mode changed. Reset everything.
[self setupMapChunk];
if (worldLoaded)
{
[self invalidateAllChunks];
}
}
}
-(void) resetOrientationMenu
{
[seUpMenuItem setState: NSOffState];
[swUpMenuItem setState: NSOffState];
[nwUpMenuItem setState: NSOffState];
[neUpMenuItem setState: NSOffState];
if (orientation == 0)
[seUpMenuItem setState: NSOnState];
else if (orientation == 3)
[swUpMenuItem setState: NSOnState];
else if (orientation == 2)
[nwUpMenuItem setState: NSOnState];
else if (orientation == 1)
[neUpMenuItem setState: NSOnState];
}
-(IBAction) setOrientation: (id) sender
{
[seUpMenuItem setState: NSOffState];
[swUpMenuItem setState: NSOffState];
[nwUpMenuItem setState: NSOffState];
[neUpMenuItem setState: NSOffState];
int newmode = 0;
if (sender == seUpMenuItem)
{
newmode = 0;
[seUpMenuItem setState: NSOnState];
}
else if (sender == swUpMenuItem)
{
newmode = 3;
[swUpMenuItem setState: NSOnState];
}
else if (sender == nwUpMenuItem)
{
newmode = 2;
[nwUpMenuItem setState: NSOnState];
}
else if (sender == neUpMenuItem)
{
newmode = 1;
[neUpMenuItem setState: NSOnState];
}
if (orientation != newmode)
{
[self reorientCamera:newmode];
orientation = newmode;
// Mode changed. Reset everything.
[self setupMapChunk];
if (worldLoaded)
{
[self invalidateAllChunks];
}
}
}
-(IBAction) toggleHideWater: (id) sender
{
if ([hideWaterMenuItem state] == NSOffState) {
[hideWaterMenuItem setState:NSOnState];
colors_path = @"/tmp/colors.txt";
MinecraftColors colors;
// Load the current color set
[self createColorArrayFromTextFile:[NSString stringWithFormat:@"%@/Colors/Minecraft.txt", [[NSBundle mainBundle] resourcePath]] colorArray:&colors];
// Lower transparency and lighten color of water
colors.c[8][0] = 82;
colors.c[8][1] = 121;
colors.c[8][2] = 188;
colors.c[8][3] = 8; // Water
colors.c[9][0] = 82;
colors.c[9][1] = 121;
colors.c[9][2] = 188;
colors.c[9][3] = 8; // Standing Water
// Write out the new file
[self writeColorsFromArray:&colors savePath:colors_path];
// Tell the map that new colors were set
[self setColors:Nil];
} else {
[hideWaterMenuItem setState:NSOffState];
colors_path = @"placeholder";
[self setColors:Nil];
[self setColors: defaultcolorsMenuItem];
}
}
-(IBAction) resetView: (id) sender
{
// 0 = (x,y)
// 1 = (y,-x)
// 2 = (-x,-y)
// 3 = (-y,x)
/*
if (orientation == 0)
ind = i + j*nmx;
else if (orientation == 1)
ind = j+((nmx-i-1)*nmx);
else if (orientation == 2)
ind = (nmx-i-1)+(nmy-j-1)*nmx;
else if (orientation == 3)
ind = (nmy-j-1) + i*nmx;
*/
float screenpos[2];
if (orientation==0)
block2screen(nox-1,noy-1,screenpos);
else if (orientation==1)
block2screen(nmx-noy+1,nox-1,screenpos);
else if (orientation==2)
block2screen(nmx-nox-1,nmy-noy+1,screenpos);
else if (orientation==3)
block2screen(noy+1,nmy-nox+1,screenpos);
camera.viewPos.x = screenpos[0];
camera.viewPos.y = screenpos[1];
//zoom_level = 0;
[self updateProjection];
[self setNeedsDisplay: YES];
}
-(IBAction) setUseBiomes: (id) sender
{
[self toggleUseBiomes:NO];
}
-(void)toggleUseBiomes:(BOOL)forceon
{
BOOL changed = NO;
// If the world is loaded
if ((worldLoaded || forceon) && !processing)
{
// and biomes are not being shown...
if (!showBiomes || forceon)
{
// Check if biomes folder exists
BOOL isdir;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat: @"%@/biomes",current_world_path] isDirectory:&isdir];
NSString* mcjarpath = [@"~/Library/Application Support/minecraft/bin/minecraft.jar" stringByExpandingTildeInPath];
BOOL mcexists = [[NSFileManager defaultManager] fileExistsAtPath:mcjarpath];
if (isdir && exists)
{
showBiomes = YES;
changed = YES;
}
else if (mcexists)
{
int result = NSRunAlertPanel (@"Biome Extraction Notice",
@"This world has no saved biome data. Would you like to extract it?\n\nNOTE: This feature is experimental. It may break with a Minecraft update.\n\nAt the moment, it works best with the Default Colors.\n\nIf your world expands, you will need to select Update Biome Data from the Tools menu.",
@"Generate Biome Data",
@"Cancel", nil );
if (result == NSAlertDefaultReturn)
{
[self startGenerateBiomes:forceon];
showBiomes = YES;
changed = YES;
}
}
else {
NSRunAlertPanel (@"Biome Extraction Notice",
@"Biome rendering requires singleplayer minecraft is installed.",
@"Sorry",nil , nil );
}
}
else
{
showBiomes = NO;
changed = YES;
}
}
else
{
// There's no world to check on, so just toggle and let scanAndInitWorld call this function again when it's time.
if (!showBiomes)
{
showBiomes = YES;
changed = YES;
}
else
{
showBiomes = NO;