Page 4 of 5

Re: Musings about stuff that I could do to the source code..

Posted: Mon Dec 10, 2012 1:27 pm
by another_commander
Some very strange stuff is about to happen if you use this device in Galaxy 1 (internal numbering 0). Hint: The problem is here:

Code: Select all

// Decrease Galaxy Number variable by 1:
      galaxy_number = galaxy_number - 1;
      
      // Check if Galaxy Number variable has gone wrong:
      if (galaxy_number > 7)
      {
         // If Galaxy Number variable has gone wrong then set to 7:
         galaxy_number = 7;
      }

Re: Musings about stuff that I could do to the source code..

Posted: Mon Dec 10, 2012 1:30 pm
by Pleb
The reason of that is when you travel backwards from Galaxy 1 it thinks its in Galaxy 256 - therefore I added that bit of code to solve the problem so that instead of thinking its in Galaxy 256 it sets the number to Galaxy 8. Of course as the code has the number 0 for Galaxy 1 it would be setting the variable to 7... There's probably a tidier way of doing!

Re: Musings about stuff that I could do to the source code..

Posted: Mon Dec 10, 2012 1:48 pm
by another_commander
It will actually work here, but only because you are manipulating an unsigned integer in an unorthodox way. Once the unsigned galaxy_number drops below 0 it will indeed turn to 255 (maximum short integer value) and then all weird stuff can go wrong. Think what would happen if you were using the galaxy_number as an array index. In your particular case it works because you are comparing for > 7, but a more kosher and easily understandable by whomever reads the code way of doing it could be:

Code: Select all

// Decrease galaxy number and do it in a safe way:
if (galaxy_number > 0)
{
    galaxy_number--;
}
else  // we are at galaxy 0, now we need to cycle to 7
{
    galaxy_number = 7;
}
The idea is that you should not let an unsigned integer drop below 0 and should check for that event before it actually takes place. Back in the days of v1.72, this very thing was the reason for a weird and intermittent memory overflow crash bug that had costed plenty of sleepless nights.

Re: Musings about stuff that I could do to the source code..

Posted: Mon Dec 10, 2012 5:56 pm
by Pleb
Ah now that looks much tidier. I wasn't sure you could use a '--' instead of '++' at the end of the galaxy_number variable and couldn't find a reference for it on Google. I'll update the code before with your correction, thanks AC!

Looking back through this thread there were some other interesting ideas that got sort of shelved whilst I was dealing with the whole getting married/having a baby saga. Now that I'm married and baby Marshall is here (lol still sounds weird!) I shall go back to the drawing board and see what other crazy things can be accomplished (time permitting of course!)...

Re: Musings about stuff that I could do to the source code..

Posted: Mon Dec 10, 2012 11:58 pm
by Pleb
Wildeblood wrote:
It might be nice to see one or other list extended: perhaps planets with government type "Utopian", or economy type "post-scarcity" or "subsistence".
Creating more government types is indeed possible, in fact you could have up to 16 governments in the game easily and still have the little icon when pressing 'i' on the Short Range Chart screen, as long as the 9th government uses a star symbol (see oolite-font.png in the textures directory). You can then define their colour in the HeadUpDisplay.m file in the source.

The only problem I'm having, is getting the game to generate a number between 0 and 15 that always stays the same. The current method of generating a government is in the Universe.m file at line 5886:

Code: Select all

OOGovernmentID government = (s_seed.c / 8) & 7;
There must be a way of generating a number between 0 and 15 in a similar way. As a test (using my galaxies 9-16 code as a base) I modified the code with the following:

Code: Select all

	// Store the current galaxy number as a variable:
	OOGalaxyID gnum = [PLAYER currentGalaxyID];
	
	OOGovernmentID government = (s_seed.c / 8) & 7;
	
	// If player is in a galaxy beyond galaxy 8:
	if (gnum > 7)
	{
		// Set government type to 'Utopian':
		government = 8;
	}
Then went down to line 9157 and added the following:

Code: Select all

	OOGalaxyID gnum = [PLAYER currentGalaxyID];

	int anarchy = (8 - government);
	if (gnum > 7)
	{
		anarchy = (9 - government);
	}
Then opened the HeadUpDisplay.m file and went to line 2962 and changed it to the following:

Code: Select all

	GLfloat govcol[] = {	0.5, 0.0, 0.7,
							0.7, 0.5, 0.3,
							0.0, 1.0, 0.3,
							1.0, 0.8, 0.1,
							1.0, 0.0, 0.0,
							0.1, 0.5, 1.0,
							0.7, 0.7, 0.7,
							0.7, 1.0, 1.0,
							0.5, 1.0, 0.8};
I then opened up the descriptions.plist file and edited the following:

Code: Select all

	government =
	(
		"Anarchy",
		"Feudal",
		"Multi-Government",
		"Dictatorship",
		"Communist",
		"Confederacy",
		"Democracy",
		"Corporate State",
		"Utopian"
	);
Now all systems from Galaxy 9 to 16 are all Utopian governments! Pretty useless but it shows a new government type can be added. But the real problem is generating a static number between 0 and 15 so you could have up to 16 governments...

Re: Musings about stuff that I could do to the source code..

Posted: Tue Dec 11, 2012 10:10 pm
by Pleb
So actually it was really simple and I didn't really try hard enough! Replaced the following code from the example above:

Code: Select all

	// If player is in a galaxy beyond galaxy 8:
	if (gnum > 7)
	{
		// Set government type to 'Utopian':
		government = 8;
	}
With the following code:

Code: Select all

	// If player is in a galaxy beyond galaxy 8:
	if (gnum > 7)
	{
		// Set government types between 0 and 15:
		government = (s_seed.c / 16) & 15;
	}
And of course add new descriptions to the descriptions.plist file. Also new symbols and colours would have to be set using the post above as a guide, but it shows that more governments could be added to newer galaxies if wanted...

Re: Musings about stuff that I could do to the source code..

Posted: Fri Dec 14, 2012 2:59 pm
by Diziet Sma
Nice one.. 8)

Now try reworking it so instead of having to buy a Reverse Galactic Hyperdrive, you just buy a standard GH, and can choose whether you want to go backwards or forwards when you activate it.. :wink:

Re: Musings about stuff that I could do to the source code..

Posted: Fri Dec 14, 2012 5:32 pm
by Pleb
Diziet Sma wrote:
Nice one.. 8)

Now try reworking it so instead of having to buy a Reverse Galactic Hyperdrive, you just buy a standard GH, and can choose whether you want to go backwards or forwards when you activate it.. :wink:
:shock: Lol a challenge if ever I saw one...well they say nothing is impossible so I'll see what I can discover...

Re: Musings about stuff that I could do to the source code..

Posted: Fri Dec 14, 2012 7:42 pm
by Wildeblood
You're best bet would be to add three more options to galactic_hyperspace_behaviour. Prefix the current three with "ANTI_". There's a better possibility, but it's 4 a.m. here. :( Another time.

Re: Musings about stuff that I could do to the source code..

Posted: Fri Dec 14, 2012 7:49 pm
by Pleb
Okay much easier than I thought. First open the PlayerEntity.m file again and find the code I modified before:

Code: Select all

   // Check if the GH Reverse Unit equipment is present:
   if ([self hasEquipmentItem:@"EQ_GAL_DRIVE_REVERSE_UNIT"])
   {
      // Decrease galaxy number and do it in a safe way:
      if (galaxy_number > 0)
      {
             galaxy_number--;
      }
      else  // we are at galaxy 0, now we need to cycle to 7
      {
         galaxy_number = 7;
      }
      
      // Rotate Galaxy Seed right instead of left to change to previous Galaxy Seed:
      galaxy_seed.a = rotate_byte_right(galaxy_seed.a);
      galaxy_seed.b = rotate_byte_right(galaxy_seed.b);
      galaxy_seed.c = rotate_byte_right(galaxy_seed.c);
      galaxy_seed.d = rotate_byte_right(galaxy_seed.d);
      galaxy_seed.e = rotate_byte_right(galaxy_seed.e);
      galaxy_seed.f = rotate_byte_right(galaxy_seed.f);
      
      // Remove the GH Reverse Unit equipment:
      [self removeEquipmentItem:@"EQ_GAL_DRIVE_REVERSE_UNIT"];
   }
   else
   {
      galaxy_number++;
      galaxy_number &= 7;

      galaxy_seed.a = rotate_byte_left(galaxy_seed.a);
      galaxy_seed.b = rotate_byte_left(galaxy_seed.b);
      galaxy_seed.c = rotate_byte_left(galaxy_seed.c);
      galaxy_seed.d = rotate_byte_left(galaxy_seed.d);
      galaxy_seed.e = rotate_byte_left(galaxy_seed.e);
      galaxy_seed.f = rotate_byte_left(galaxy_seed.f);
   }
And replace it with the following code:

Code: Select all

	// Check if going forward or backwards:
	if (!galactic_witchjump_reverse)
	{
		galaxy_number++;
		galaxy_number &= 7;

		galaxy_seed.a = rotate_byte_left(galaxy_seed.a);
		galaxy_seed.b = rotate_byte_left(galaxy_seed.b);
		galaxy_seed.c = rotate_byte_left(galaxy_seed.c);
		galaxy_seed.d = rotate_byte_left(galaxy_seed.d);
		galaxy_seed.e = rotate_byte_left(galaxy_seed.e);
		galaxy_seed.f = rotate_byte_left(galaxy_seed.f);
	}
	else
	{
		// Decrease galaxy number and do it in a safe way:
		if (galaxy_number > 0)
		{
			galaxy_number--;
		}
		else  // we are at galaxy 0, now we need to cycle to 7
		{
			galaxy_number = 7;
		}
		
		// Rotate Galaxy Seed right instead of left to change to previous Galaxy Seed:
		galaxy_seed.a = rotate_byte_right(galaxy_seed.a);
		galaxy_seed.b = rotate_byte_right(galaxy_seed.b);
		galaxy_seed.c = rotate_byte_right(galaxy_seed.c);
		galaxy_seed.d = rotate_byte_right(galaxy_seed.d);
		galaxy_seed.e = rotate_byte_right(galaxy_seed.e);
		galaxy_seed.f = rotate_byte_right(galaxy_seed.f);
		
		// Ensure that the next gal-jump will not automatically go in reverse:
		galactic_witchjump_reverse = NO;
	}
Now noticed I've referenced a variable that hasn't yet been defined, 'galactic_witchjump_reverse'. We'll sort that in a moment.

Now open up the PlayerEntity.h file and go to line 404, finding the following code:

Code: Select all

	OOKeyCode				key_galactic_hyperspace;
And insert below this line:

Code: Select all

	OOKeyCode				key_galactic_hyperspace_rev;
Next go down to about line 495 and find the following code:

Code: Select all

							galactic_witchjump: 1,
And insert below this line:

Code: Select all

							galactic_witchjump_reverse: 1,
Now open the PlayerEntityControls.m file and go to line 226 finding the following code:

Code: Select all

	LOAD_KEY_SETTING(key_galactic_hyperspace,	'g'			);
And insert below this line:

Code: Select all

	LOAD_KEY_SETTING(key_galactic_hyperspace_rev,	'G'			);
Now go down to line 1228 and replace the following code:

Code: Select all

				exceptionContext = @"galactic hyperspace";
				// Galactic hyperspace 'g'
				if (([gameView isDown:key_galactic_hyperspace] || joyButtonState[BUTTON_GALACTICDRIVE]) &&
					([self hasEquipmentItem:@"EQ_GAL_DRIVE"]))// look for the 'g' key
				{
					if (!galhyperspace_pressed)
					{
						if ([self status] == STATUS_WITCHSPACE_COUNTDOWN)
						{
							// abort!
							[self setStatus:STATUS_IN_FLIGHT];
							[self playHyperspaceAborted];
							// say it!
							[UNIVERSE clearPreviousMessage];
							
							if (galactic_witchjump)
							{
								galactic_witchjump = NO;
								[UNIVERSE addMessage:DESC(@"witch-user-galactic-abort") forCount:3.0];
							}
							else
							{
								[UNIVERSE addMessage:DESC(@"witch-user-abort") forCount:3.0];
							}
							[self doScriptEvent:OOJSID("playerCancelledJumpCountdown")];
						}
						else
						{
							galactic_witchjump = YES;
							
							// even if we don't have a witchspace motor, we can still do a default galactic jump (!)
							if(EXPECT([self hasHyperspaceMotor])) witchspaceCountdown = hyperspaceMotorSpinTime;
							else witchspaceCountdown = DEFAULT_HYPERSPACE_SPIN_TIME;
							
							[self setStatus:STATUS_WITCHSPACE_COUNTDOWN];
							[self playGalacticHyperspace];
							// say it!
							[UNIVERSE addMessage:[NSString stringWithFormat:DESC(@"witch-galactic-in-f-seconds"), witchspaceCountdown] forCount:1.0];
							// FIXME: how to preload target system for hyperspace jump?
							
							[self doScriptEvent:OOJSID("playerStartedJumpCountdown")
								  withArguments:[NSArray arrayWithObjects:@"galactic", [NSNumber numberWithFloat:witchspaceCountdown], nil]];
						}
					}
					galhyperspace_pressed = YES;
				}
				else
					galhyperspace_pressed = NO;
				
With the following code:

Code: Select all

				exceptionContext = @"galactic hyperspace";
				// Galactic hyperspace 'g'
				if (([gameView isDown:key_galactic_hyperspace] || joyButtonState[BUTTON_GALACTICDRIVE]) && ([self hasEquipmentItem:@"EQ_GAL_DRIVE"]))// look for the 'g' key
				{
					if (!galhyperspace_pressed)
					{
						if ([self status] == STATUS_WITCHSPACE_COUNTDOWN)
						{
							// abort!
							[self setStatus:STATUS_IN_FLIGHT];
							[self playHyperspaceAborted];
							// say it!
							[UNIVERSE clearPreviousMessage];
							
							if (galactic_witchjump)
							{
								galactic_witchjump = NO;
								[UNIVERSE addMessage:DESC(@"witch-user-galactic-abort") forCount:3.0];
							}
							else
							{
								[UNIVERSE addMessage:DESC(@"witch-user-abort") forCount:3.0];
							}
							[self doScriptEvent:OOJSID("playerCancelledJumpCountdown")];
						}
						else
						{
							galactic_witchjump = YES;
							
							// even if we don't have a witchspace motor, we can still do a default galactic jump (!)
							if(EXPECT([self hasHyperspaceMotor])) witchspaceCountdown = hyperspaceMotorSpinTime;
							else witchspaceCountdown = DEFAULT_HYPERSPACE_SPIN_TIME;
							
							[self setStatus:STATUS_WITCHSPACE_COUNTDOWN];
							[self playGalacticHyperspace];
							// say it!
							[UNIVERSE addMessage:[NSString stringWithFormat:DESC(@"witch-galactic-in-f-seconds"), witchspaceCountdown] forCount:1.0];
							// FIXME: how to preload target system for hyperspace jump?
							
							[self doScriptEvent:OOJSID("playerStartedJumpCountdown")
								  withArguments:[NSArray arrayWithObjects:@"galactic", [NSNumber numberWithFloat:witchspaceCountdown], nil]];
						}
					}
					galhyperspace_pressed = YES;
				}
				else if (([gameView isDown:key_galactic_hyperspace_rev] && ([self hasEquipmentItem:@"EQ_GAL_DRIVE"])))// look for the 'G' key
				{
					if (!galhyperspace_pressed)
					{
						if ([self status] == STATUS_WITCHSPACE_COUNTDOWN)
						{
							// abort!
							[self setStatus:STATUS_IN_FLIGHT];
							[self playHyperspaceAborted];
							// say it!
							[UNIVERSE clearPreviousMessage];
							
							if (galactic_witchjump)
							{
								galactic_witchjump = NO;
								galactic_witchjump_reverse = NO;
								[UNIVERSE addMessage:DESC(@"witch-user-galactic-abort") forCount:3.0];
							}
							else
							{
								[UNIVERSE addMessage:DESC(@"witch-user-abort") forCount:3.0];
							}
							[self doScriptEvent:OOJSID("playerCancelledJumpCountdown")];
						}
						else
						{
							galactic_witchjump = YES;
							galactic_witchjump_reverse = YES;
							
							// even if we don't have a witchspace motor, we can still do a default galactic jump (!)
							if(EXPECT([self hasHyperspaceMotor])) witchspaceCountdown = hyperspaceMotorSpinTime;
							else witchspaceCountdown = DEFAULT_HYPERSPACE_SPIN_TIME;
							
							[self setStatus:STATUS_WITCHSPACE_COUNTDOWN];
							[self playGalacticHyperspace];
							// say it!
							[UNIVERSE addMessage:[NSString stringWithFormat:DESC(@"witch-galactic-in-f-seconds"), witchspaceCountdown] forCount:1.0];
							// FIXME: how to preload target system for hyperspace jump?
							
							[self doScriptEvent:OOJSID("playerStartedJumpCountdown")
								  withArguments:[NSArray arrayWithObjects:@"galactic", [NSNumber numberWithFloat:witchspaceCountdown], nil]];
						}
					}
					galhyperspace_pressed = YES;
				}
				else
				{
					galhyperspace_pressed = NO;
				}
				
Finally, open the keyconfig.plist file in the Config directory and find the following line:

Code: Select all

	key_galactic_hyperspace		= "g";
And insert below this line:

Code: Select all

	key_galactic_hyperspace_rev		= "G";
Now when you press Shift-G in game you should go in reverse rather than forward, and no extra equipment is required! 8)

Re: Musings about stuff that I could do to the source code..

Posted: Fri Dec 14, 2012 8:00 pm
by Pleb
One thing I tried to implement was a way of saying to the player that "Reverse Intergalactic jump in %.0f s." but by changing the witch-galactic-in-f-seconds reference above to a different one and defining it in descriptions.plist didn't work. This would be helpful, so the player knows which direction they are going in! :lol:

Re: Musings about stuff that I could do to the source code..

Posted: Sat Dec 15, 2012 7:08 am
by Diziet Sma
Pleb wrote:
Now when you press Shift-G in game you should go in reverse rather than forward, and no extra equipment is required! 8)
Applauds! 8) :D
Pleb wrote:
One thing I tried to implement was a way of saying to the player that "Reverse Intergalactic jump in %.0f s." but by changing the witch-galactic-in-f-seconds reference above to a different one and defining it in descriptions.plist didn't work. This would be helpful, so the player knows which direction they are going in! :lol:
That would indeed be nice.. :)

Re: Musings about stuff that I could do to the source code..

Posted: Sat Dec 15, 2012 10:38 am
by Pleb
Okay open up the PlayerEntity.m file and go to line 2492 and find the following code:

Code: Select all

	if (galactic_witchjump)
	{
		[UNIVERSE displayCountdownMessage:[NSString stringWithFormat:DESC(@"witch-galactic-in-f-seconds"), witchspaceCountdown] forCount:1.0];
	}
And replace with the following code:

Code: Select all

	if (galactic_witchjump)
	{
		if (galactic_witchjump_reverse)
		{
			[UNIVERSE displayCountdownMessage:[NSString stringWithFormat:DESC(@"witch-rev-galactic-in-f-seconds"), witchspaceCountdown] forCount:1.0];
		}
		else
		{
			[UNIVERSE displayCountdownMessage:[NSString stringWithFormat:DESC(@"witch-galactic-in-f-seconds"), witchspaceCountdown] forCount:1.0];
		}
	}
Now open up the PlayerEntityControls.m file and go to line 1309 and replace this code:

Code: Select all

                     [UNIVERSE addMessage:[NSString stringWithFormat:DESC(@"witch-galactic-in-f-seconds"), witchspaceCountdown] forCount:1.0];
With the following code:

Code: Select all

                     [UNIVERSE addMessage:[NSString stringWithFormat:DESC(@"witch-rev-galactic-in-f-seconds"), witchspaceCountdown] forCount:1.0];
Now finally open up the descriptions.plist file in the Config directory and insert this code:

Code: Select all

	"witch-rev-galactic-in-f-seconds"	= "Reverse Intergalactic jump in %.0f s.";
Now the player has a better idea of which direction they are going. Would be annoying if you didn't mean to go forwards/backwards and had to hunt for a new Galactic Hyperdrive supplier! :lol:

Re: Musings about stuff that I could do to the source code..

Posted: Sat Dec 15, 2012 1:34 pm
by Diziet Sma
Well done.. it would be nice to see some of your work make it into trunk. 8)

Re: Musings about stuff that I could do to the source code..

Posted: Sat Dec 15, 2012 2:18 pm
by Pleb
Diziet Sma wrote:
Well done.. it would be nice to see some of your work make it into trunk. 8)
Thanks Diziet Sma, most of what I post in this thread I'd honestly like to see make it into the trunk, but I know a lot of people would not approve of the idea of 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. However two of my more recent ideas I have put forward as suggestions, such as making the System Populator editable by Javascript and a Scriptable Space Background Colour. Who knows... :mrgreen: