Scriptable Space Background Colour

An area for discussing new ideas and additions to Oolite.

Moderators: winston, another_commander

Post Reply
User avatar
Pleb
---- E L I T E ----
---- E L I T E ----
Posts: 908
Joined: Sun Apr 29, 2012 2:23 pm
Location: United Kingdom

Scriptable Space Background Colour

Post by Pleb »

I've posted this here rather than in my usual thread for random code hacks as I reckon this has more feasible uses than more than 8 galaxies or a Galactic Hyperdrive that allows you to travel in both directions by either pressing g to go forward or Shift-G to go backwards (although I would love to see these features in the game, but let's not start that argument again!).

By making the following changes to the source code, it would enable OXP writers to produce different coloured space backgrounds in different systems either by editing the planetinfo.plist file in the 'Config' directory or by Javascript. I'm using the source code for version 1.76.1 but this can also be done in the Trunk. Firstly, open up the Universe.m file in the 'src\Core' directory and go to line 632 and replace the following code:

Code: Select all

	[UNIVERSE setSkyColorRed:0.0f		// back to black
					   green:0.0f
						blue:0.0f
					   alpha:0.0f];
With the following code:

Code: Select all

	NSDictionary		*systeminfo = nil;
	systeminfo = [self generateSystemData:system_seed];
	skyClearColor[0] = [systeminfo oo_floatForKey:@"space_color_red" defaultValue:0.0];
	skyClearColor[1] = [systeminfo oo_floatForKey:@"space_color_green" defaultValue:0.0];
	skyClearColor[2] = [systeminfo oo_floatForKey:@"space_color_blue" defaultValue:0.0];
	skyClearColor[3] = [systeminfo oo_floatForKey:@"space_color_alpha" defaultValue:0.0];
Now go to about line 862 in the same file and find the following code:

Code: Select all

	[self setSkyColorRed:0.0f
				   green:0.0f
					blue:0.0f
				   alpha:0.0f];
And replace the above code with the following:

Code: Select all

	skyClearColor[0] = [systeminfo oo_floatForKey:@"space_color_red" defaultValue:0.0];
	skyClearColor[1] = [systeminfo oo_floatForKey:@"space_color_green" defaultValue:0.0];
	skyClearColor[2] = [systeminfo oo_floatForKey:@"space_color_blue" defaultValue:0.0];
	skyClearColor[3] = [systeminfo oo_floatForKey:@"space_color_alpha" defaultValue:0.0];
Now go down to about line 5092 and replace the following code:

Code: Select all

			skyClearColor[0] = 0.0;
			skyClearColor[1] = 0.0;
			skyClearColor[2] = 0.0;
			skyClearColor[3] = 0.0;
With the code below:

Code: Select all

			NSDictionary		*systeminfo = nil;
			systeminfo = [self generateSystemData:system_seed];
			
			skyClearColor[0] = [systeminfo oo_floatForKey:@"space_color_red" defaultValue:0.0];
			skyClearColor[1] = [systeminfo oo_floatForKey:@"space_color_green" defaultValue:0.0];
			skyClearColor[2] = [systeminfo oo_floatForKey:@"space_color_blue" defaultValue:0.0];
			skyClearColor[3] = [systeminfo oo_floatForKey:@"space_color_alpha" defaultValue:0.0];
Now go down to about line 9071 and insert the following code:

Code: Select all

	// Define space background color or set to black as default:
	skyClearColor[0] = [systeminfo oo_floatForKey:@"space_color_red" defaultValue:0.0];
	skyClearColor[1] = [systeminfo oo_floatForKey:@"space_color_green" defaultValue:0.0];
	skyClearColor[2] = [systeminfo oo_floatForKey:@"space_color_blue" defaultValue:0.0];
	skyClearColor[3] = [systeminfo oo_floatForKey:@"space_color_alpha" defaultValue:0.0];
Now compile the game and you're ready to test. You can now define space colours either by using planetinfo.plist file and using the following code:

Code: Select all

	"0 7" =
	{
		space_color_blue = 0.2;
	};
Which will now make space in the Lave system blue, or you could use a script command like System.infoForSystem(galaxyNumber, player.ship.targetSystem).space_color_red = 0.2; to make the next system you jump into red! The above code also ensures that even when launching from the station the colour is what you have defined it to be, and even if you go into the planet's atmosphere (which turns the background colour blue) and come back up again, it will revert to the colour defined in the planetinfo.plist file or by Javascript.
Desktop PC: CPU: Intel i7-4790K Quad Core 4.4GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1080Ti RAM: 32GB DDR3

Laptop PC: CPU: Intel i5-10300H Quad Core 4.5GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1650 RAM: 32GB DDR4
User avatar
Pleb
---- E L I T E ----
---- E L I T E ----
Posts: 908
Joined: Sun Apr 29, 2012 2:23 pm
Location: United Kingdom

Re: Scriptable Space Background Colour

Post by Pleb »

Okay I've improved the code for this so that it is now much simpler and tidier. It also now works better with the trunk (which is what I've tested this in).

First open Universe.h and insert the following at around line 363:

Code: Select all

- (void) setSpaceBackgroundColor;
Now open Universe.m and go to line 650. Replace the code below:

Code: Select all

   [UNIVERSE setSkyColorRed:0.0f      // back to black
                  green:0.0f
                  blue:0.0f
                  alpha:0.0f];
With the following:

Code: Select all

[self setSpaceBackgroundColor];
Now go to line 735 and replace the code below:

Code: Select all

   [UNIVERSE setSkyColorRed:0.0f
                  green:0.0f
                  blue:0.0f
                  alpha:0.0f];
With the following:

Code: Select all

[self setSpaceBackgroundColor];
Now go to line 1213 and insert the following:

Code: Select all

- (void) setSpaceBackgroundColor
{
	NSDictionary *systeminfo = nil;
	systeminfo = [self generateSystemData:system_seed];
	NSArray	*systemSkyColor = nil;
	systemSkyColor = [systeminfo oo_arrayForKey:@"space_background_color"];
	GLfloat systemSkyColorRed = [systemSkyColor oo_floatAtIndex:0 defaultValue:0.0];
	GLfloat systemSkyColorGreen = [systemSkyColor oo_floatAtIndex:1 defaultValue:0.0];
	GLfloat systemSkyColorBlue = [systemSkyColor oo_floatAtIndex:2 defaultValue:0.0];
	GLfloat systemSkyColorAlpha = [systemSkyColor oo_floatAtIndex:3 defaultValue:0.0];
	[UNIVERSE setSkyColorRed:systemSkyColorRed
					green:systemSkyColorGreen
					blue:systemSkyColorBlue
					alpha:systemSkyColorAlpha];
}
Finally go to line 6153 and replace the code below:

Code: Select all

         skyClearColor[0] = 0.0;
         skyClearColor[1] = 0.0;
         skyClearColor[2] = 0.0;
         skyClearColor[3] = 0.0;
With the following:

Code: Select all

[self setSpaceBackgroundColor];
Now compile the game. If you now enter an entry in planetinfo.plist similar to the one below, you can specify the space background colour of a specific system (the example changes Lave to blue):

Code: Select all

	"0 7" = 
	{
		space_background_color = (0.0, 0.0, 0.1, 0.0);
	};


Image

However you can also set the background colour for every system if you chose, using the following (the example changes all system backgrounds to red):

Code: Select all

	universal =
	{
		space_background_color = (0.1, 0.0, 0.0, 0.0);
	};
Image
Desktop PC: CPU: Intel i7-4790K Quad Core 4.4GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1080Ti RAM: 32GB DDR3

Laptop PC: CPU: Intel i5-10300H Quad Core 4.5GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1650 RAM: 32GB DDR4
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Scriptable Space Background Colour

Post by JensAyton »

Pleb wrote:

Code: Select all

        systemSkyColor = [systeminfo oo_arrayForKey:@"space_background_color"];
	GLfloat systemSkyColorRed = [systemSkyColor oo_floatAtIndex:0 defaultValue:0.0];
	GLfloat systemSkyColorGreen = [systemSkyColor oo_floatAtIndex:1 defaultValue:0.0];
	GLfloat systemSkyColorBlue = [systemSkyColor oo_floatAtIndex:2 defaultValue:0.0];
	GLfloat systemSkyColorAlpha = [systemSkyColor oo_floatAtIndex:3 defaultValue:0.0];
Don’t do this. Use [OOColor colorWithDescription:[systeminfo objectForKey:@"space_background_color"]] like every other thing in the game that takes a colour from a plist.
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6683
Joined: Wed Feb 28, 2007 7:54 am

Re: Scriptable Space Background Colour

Post by another_commander »

What Jens said. Not having the proposal included in the mainline yet is because of this.
User avatar
Pleb
---- E L I T E ----
---- E L I T E ----
Posts: 908
Joined: Sun Apr 29, 2012 2:23 pm
Location: United Kingdom

Re: Scriptable Space Background Colour

Post by Pleb »

Okay I think this will work much better. Replace the section at line 1213 (the setSpaceBackgroundColor function) with the following:

Code: Select all

- (void) setSpaceBackgroundColor
{
	OOColor *defaultColor = [OOColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
	id systemSkyColorDesc = nil;
	OOColor	*systemSkyColor = nil;
	NSDictionary *systeminfo = nil;
	systeminfo = [self generateSystemData:system_seed];
	systemSkyColorDesc = [systeminfo objectForKey:@"space_background_color"];
	if (systemSkyColorDesc != nil)
	{
		systemSkyColor = [OOColor colorWithDescription:systemSkyColorDesc];
		if (systemSkyColor == nil)
		{
			systemSkyColor = defaultColor;
			OOLogWARN(@"spaceBGColor.fromDict", @"could not interpret \"%@\" as a colour.", systemSkyColorDesc);
		}
	}
	else
	{
		systemSkyColor = defaultColor;
	}
	[UNIVERSE setSkyColorRed:[systemSkyColor redComponent]
					green:[systemSkyColor greenComponent]
					blue:[systemSkyColor blueComponent]
					alpha:[systemSkyColor alphaComponent]];
}
This now allows you to use the colour descriptions (redColor, blueColor, blackColor, etc...) or an RGBA input, and if this doesn't work it will reset to default (black) and flag up in the log. :)
Desktop PC: CPU: Intel i7-4790K Quad Core 4.4GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1080Ti RAM: 32GB DDR3

Laptop PC: CPU: Intel i5-10300H Quad Core 4.5GHz (Turbo-Charged) GPU: Nvidia GeForce GTX 1650 RAM: 32GB DDR4
Post Reply