Advertisement
mwerle

ZekeMiner Shipscript

Dec 31st, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. this.name           = "ZekeMiner Ship Script"
  2. this.author         = "Micha"
  3. this.copyright      = "(C) 2011 Micha." // With thanks to Wyvern_Mommy and Wildeblood
  4. this.licence        = "CC-NC-by-SA 2.0"
  5. this.description    = "Ensures at least 1 Rock Hermit is in a system if the player is flying the Zeke Transporter Miner.";
  6. this.version        = "0.1"
  7.  
  8. ///////////////////////////////////////////////////////////////////////////////
  9. // Ship logic functions
  10. ///////////////////////////////////////////////////////////////////////////////
  11.  
  12. this.getSalt = function()
  13. {
  14.     // We only generate the salt once per save-game and store it in a
  15.     // mission variable.  This ensures that pseudo-randomly generated
  16.     // things (such as the location of the Rock Hermits) are always
  17.     // the same for a given save-game, but differs for each new save-game
  18.     // ensuring replay value.
  19.     // Also, we use a script-global variable to cache the value to reduce
  20.     // access to mission variables as that is expensive.
  21.     if (typeof this.random_salt === 'undefined') {
  22.         if (missionVariables.ZekeMiner_salt == null) {
  23.             missionVariables.ZekeMiner_salt = Math.random() * 1000;
  24.         }
  25.         this.random_salt = missionVariables.ZekeMiner_salt;
  26.         log("ZekeMiningTransporter", "New random salt: " + this.random_salt);
  27.     }
  28.     return this.random_salt;
  29. }
  30.  
  31. this.checkSpawnHermit = function()
  32. {
  33.     // This function ensures that a RockHermit exists in the system
  34.     var rockHermits = system.shipsWithRole("rockhermit");
  35.     if( rockHermits.length == 0 ) {
  36.         log("ZekeMiningTransporter", "No Rock Hermits in system, adding one");
  37.         // Randomly add to one of the routes
  38.         // NB: Use system.scrambledPseudoRandomNumber() to ensure it is always
  39.         //     generated in the same position for a given save-game.
  40.         var dice = system.scrambledPseudoRandomNumber(this.getSalt());
  41.         var route = "wp";
  42.         if( dice > 0.8 ) {
  43.             route = "ws";
  44.         } else if (dice > 0.5) {
  45.             route = "ps";
  46.         }
  47.         do {
  48.             dice = system.scrambledPseudoRandomNumber(dice*100);
  49.         } while(dice < 0.1 || dice > 0.9);
  50.  
  51.         rockHermits = system.addShipsToRoute("rockhermit", 1, dice, route);
  52.         var position = rockHermits[0].position;
  53.  
  54.         // Now add up to 5..20 asteroids near the hermitage.
  55.         system.addShips("asteroid", (Math.random() * 15) + 5, position);
  56.     }
  57. }
  58.  
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // Ship event handlers
  61. ///////////////////////////////////////////////////////////////////////////////
  62.  
  63. this.shipWillLaunchFromStation = function()
  64. {
  65.     log("ZekeMiningTransporter", "Player launching");
  66.     this.checkSpawnHermit();
  67. }
  68.  
  69. this.shipWillExitWitchspace = function()
  70. {
  71.     log("ZekeMiningTransporter", "Player exiting witchspace");
  72.     this.checkSpawnHermit();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement