Scripters cove

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: Scripters cove

Post by Eric Walch »

cim wrote:
Quick question: is there a way with script to find out how much cargo a station has in its market, and/or at what price?
You are right that this information is missing. I remember discussing this with Kaks that we need the local price of commodities as a read-only value in order to charge/refund the player with the correct local values when awarding/removing stuff from the manifest.
But, at the end neither of us found time/motivation to actually add it. :(
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: Scripters cove

Post by Smivs »

A quick question regarding the 'shipTakingDamage' handler. You can specify amount, whom, and type. How do you specify the amount? Specifically I want an event to occur when the ship (NPC) starts throwing sparks. I'm not too bothered about who is inflicting the damage or how, although it would normally be the player using lasers.
Commander Smivs, the friendliest Gourd this side of Riedquat.
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Scripters cove

Post by cim »

Amount is the amount of damage taken in that hit, so you don't need to care about that (much) either.
Something like this should trigger the event when the ship first is damaged to spark throwing levels.

Code: Select all

this.sparks = 0;

this.shipTakingDamage = function(amount,whom,type) {
  if (ship.energy/ship.maxEnergy < 0.25 && this.sparks == 0) {
    this.sparks = 1;
    // do something
  }
}
I think the spark threshold is 25% remaining, but I can't check right now.

You may want to also need a && ship.energy > 0 check in to that function, if the event requires the ship to still exist, since collisions and high-power attacks can destroy a ship in a single strike.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Scripters cove

Post by Thargoid »

shipEnergyIsLow might be more appropriate to your needs perhaps?

For example use it to start a timer which does whatever you want whilst throwing sparks, and checks on the ship energy levels and self-terminates when the level gets sufficiently high.

Also to be clear, amount in shipTakingDamage is passed to the function - you don't "specify" it (other than using it as a variable in the code of the event). As Cim says it's the amount of damage done in the attack or incident that triggered the event.
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: Scripters cove

Post by Smivs »

Thanks chaps. I'll try both suggestions and see what's best for my project.
Commander Smivs, the friendliest Gourd this side of Riedquat.
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Re: Scripters cove

Post by Commander McLane »

What's the most efficient way of setting up a set of re-usable arrays of strings in a world script?

In randomshipnames 0.9 the names are constructed by calling a couple of nested functions. Each function defines a list of names and randomly returns one of them:

Code: Select all

this.$randomPirateName3b = function()
{
    var name = ["Darkness", "Destruction", "Death", "Pain", "Torture", "Sorrow", "Tears", "Fear", "Fury", "Inferno", "Fate", "Despair", "Terror", "Horror", "Wrath", "Madness", "Rage", "Hellfire", "Storm", "Tragedy", "Misfortune", "Misery"];
    return name[Math.floor(Math.random() * name.length)];
}
I suppose that this is the least efficient way of doing things, because the same functions are called for every single ship, so the same arrays have to be constructed again and again.

What I am currently doing is to set up my list arrays globally. Outside of any function I have a bunch of lists like this:

Code: Select all

this.randomMoneySingleName = ["Treasure", "Fortune", "Joker", "Aces", "Deuces", "Broadway", "Jackpot", "Surprise", "Prize", "Winner"];
Later I refer to them only by name.

My specific questions: What is the difference between using this. vs. var randomMoneySingleName = [...]?

What is the difference between doing it at the top of my script vs. doing it in the startUp handler?

And what is the difference between declaring the arrays like I do vs. this.randomMoneySingleName = new Array(...) or var randomMoneySingleName = new Array(...)?

In short: which of these is the most efficient for my purpose of creating a number of lists of ship name parts, and assembling names from these list whenever a ship is spawned? Which causes the least hiccup?
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Scripters cove

Post by cim »

Commander McLane wrote:
this. vs. var randomMoneySingleName = [...]?
As you say, this. gets defined once, whereas var is defined every time you call the function and so is less efficient in processor power. (More efficient in memory usage, though, since you're not keeping the array around when it's not being used, but it's rare that Oolite runs out of its Javascript memory allocation)
Commander McLane wrote:
top of my script vs. doing it in the startUp handler?
Top of script means the array content is available to the startUp handlers of other worldscripts without needing to worry about whether your startUp has run. Not a particularly important distinction here.
Commander McLane wrote:
And what is the difference between declaring the arrays like I do vs. this.randomMoneySingleName = new Array(...) or var randomMoneySingleName = new Array(...)?
One is more readable than the other. Which one depends on your personal taste.
Commander McLane wrote:
In short: which of these is the most efficient for my purpose
Doing it this way, probably putting them in a this. definition at the top of your script. However, in practice, unless you're defining names for thousands of ships at once (or randomly naming every asteroid in the system?) all approaches are likely to be sufficiently fast.

Personally, I'd just stick all the name definitions in descriptions.plist and use a single expandDescription() call whenever I wanted a random name. It might be faster than the purely-scripting approach; it certainly won't be much slower, and my personal taste says it's more readable and less error-prone too.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Scripters cove

Post by Thargoid »

The problem is that using expandDescription and description.plist is notoriously non-random (at least from experience of a few OXP'ers, including me).

That said as my Stellar Serpents also seem to be repeatably non-random using JS, it gets a bit more complex... ;)
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Scripters cove

Post by Switeck »

If you want to give ships random names but be very unlikely to reuse the same name twice...at least for awhile, it seems like the Behemoth OXP does something like that with few lines of code.
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Re: Scripters cove

Post by Commander McLane »

cim wrote:
Personally, I'd just stick all the name definitions in descriptions.plist and use a single expandDescription() call whenever I wanted a random name. It might be faster than the purely-scripting approach; it certainly won't be much slower, and my personal taste says it's more readable and less error-prone too.
That's yet another possibility (it's how Random Hits creates its contracts). However, I seem to remember that accessing descriptions.plist from a script is generally less efficient than keeping it inside the script. And perhaps also less random, especially for long lists, where many of the items may never get chosen. I could be wrong, though.
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Scripters cove

Post by cim »

Thargoid wrote:
The problem is that using expandDescription and description.plist is notoriously non-random (at least from experience of a few OXP'ers, including me).
Hmm, checking the source that's not at all surprising. I presume the reason it uses that particular random number generator is because description.plist data is also used for planetary descriptions which have to be badly random for compatibility with Elite?
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Scripters cove

Post by JensAyton »

cim wrote:
Hmm, checking the source that's not at all surprising. I presume the reason it uses that particular random number generator is because description.plist data is also used for planetary descriptions which have to be badly random for compatibility with Elite?
Not really. The PRNGs are low-quality, but not bad enough to be obvious in this sort of use. The issue in this case is that expandDescription() always uses the “system seed” and thus always produces the same value in a given system, for backwards compatibility with the way things worked in the old scripting system (which was slightly less deterministic, but in a bad way).

There really ought to be a random variant of expandDescription(), but, well, there isn’t.
User avatar
mandoman
---- E L I T E ----
---- E L I T E ----
Posts: 1385
Joined: Thu Apr 28, 2011 3:17 pm

Re: Scripters cove

Post by mandoman »

I'm not a scripter. Having said that, I am still stupidly trying to script. Question, is the following a valid script? It's a mishmash of script I got from Smivs and Phantor Gorth, but I'll put money down that I still haven't got a valid script.

Code: Select all

// Standard attributes 
this.name           = "UFOship"; 
this.author         = "mandoman"; 
this.copyright      = "This script is hereby placed in the public domain."; 
this.version        = "1.0.1"; 
this.description    = "Script to spawn equal number of UFOships as number of Thargoid ships if player ship with score 512, or higher exits Witchspace."

//Setup actions


this.role1 = "UFOship";


this.shipExitedWitchspace
{
      this.counter1 (player.score >= 512); //declare required score level of player for this script to run
      this.counter2 = 0; //declare a property of the script for the counter and set it to zero

this.shipSpawned = function(this_ship) //this_ship (could have called it anything I liked) is now available to the function body
{
  if (this_ship.isThargoid) this.counter1, this.counter2++; //check if this_ship is a Thargoid (isThargoid is a property of a ship object) if it is, increment this.counter by 1
}

if (this.counter2 >= 3) //Thargoid Counter
  {
    system.addShips(this.role1, this.counter2, player.ship.position, 20000);
  }
}

this.shipWillEnterWitchspace = function()
{
  this.counter = 0; //reset the counter before you arrive at a new system.
}

this.shipDockedWithStation = function()
{
  this.counter = 0; //reset the counter before you leave a station.
}
Mandotech Industries Wiki Page.

http://wiki.alioth.net/index.php/User:Mandoman
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Scripters cove

Post by Switeck »

this.shipExitedWitchspace
is missing
= function()
after it, like so:
this.shipExitedWitchspace = function()
User avatar
mandoman
---- E L I T E ----
---- E L I T E ----
Posts: 1385
Joined: Thu Apr 28, 2011 3:17 pm

Re: Scripters cove

Post by mandoman »

Oh yeah, sheesh, I've even worked that much of it out before on other scripts I've tried. Thanks. It isn't getting any warnings from either the DebugConsole, or Latest.log, but I haven't noticed that it works either. I'll give it a fly with your addition. :)
Mandotech Industries Wiki Page.

http://wiki.alioth.net/index.php/User:Mandoman
Post Reply