setDesiredRangeTo: not working properly?

For test results, bug reports, announcements of new builds etc.

Moderators: winston, another_commander, Getafix

Post Reply
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:

setDesiredRangeTo: not working properly?

Post by Commander McLane »

When writing Flying_Dutchman.oxp I noticed this:

I wanted the Wireframe Cobra to scan for the player and cause disasters, but only if the player is within a range of 15000 meters. So I did

Code: Select all

setDesiredRangeTo: 15000
scanForNearestShipWithPrimaryRole: player
Result: He found me and started his work as soon as he appeared on my scanner. Far beyond 15000 meters.

I tried to reverse the order (first the scanFor..., then setDesired...), as in many of the default AIs. Same result. I was found way before I reached the specified distance.

In the end I resorted to setting his scanner_range in shipdata. That works. He is now spotting me only if I am closer than 15000 meters.

But it seems to me now that setDesiredRangeTo does not do what it's intended for. At least not in connection with the scan-method.
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6683
Joined: Wed Feb 28, 2007 7:54 am

Re: setDesiredRangeTo: not working properly?

Post by another_commander »

Commander McLane wrote:
When writing Flying_Dutchman.oxp I noticed this:

I wanted the Wireframe Cobra to scan for the player and cause disasters, but only if the player is within a range of 15000 meters. So I did

Code: Select all

setDesiredRangeTo: 15000
scanForNearestShipWithPrimaryRole: player
Result: He found me and started his work as soon as he appeared on my scanner. Far beyond 15000 meters.

I tried to reverse the order (first the scanFor..., then setDesired...), as in many of the default AIs. Same result. I was found way before I reached the specified distance.

In the end I resorted to setting his scanner_range in shipdata. That works. He is now spotting me only if I am closer than 15000 meters.

But it seems to me now that setDesiredRangeTo does not do what it's intended for. At least not in connection with the scan-method.
scanForNearestShipWithPrimaryRole uses the entire scanner range to perform its checks. It does not matter what you set desired range to, as this is not used when scanning for ships. This is hardcoded behaviour and as far as I can tell, it works as expected.
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: setDesiredRangeTo: not working properly?

Post by Eric Walch »

Commander McLane wrote:
Result: He found me and started his work as soon as he appeared on my scanner. Far beyond 15000 meters.....But it seems to me now that setDesiredRangeTo does not do what it's intended for. At least not in connection with the scan-method.
A_C is right. it wont work this way. But now we have ship scripts. Those can do almost everything.

Use a "sendScriptMessage: checkDistance" instead.

And in the script you define

Code: Select all

this.checkDistance = function()
{
    if(player.position.distanceTo(this.ship) < 15000)  this.ship.reactToAIMessage("FOUND_PLAYER"); 
}
It will return the message "FOUND_PLAYER" to the AI when player is within 15000

The normal commands also define a "found target" after a scan. This method won't. When you also want the player targeted at this point you can use.

Code: Select all

this.checkDistance = function()
{
    if(player.position.distanceTo(this.ship) < 15000) 
    {
       this.ship.reactToAIMessage("FOUND_PLAYER"); 
       this.ship.target = player.ID
    }
}
Of cause you must skip the usual "setTargetToFoundTarget" command in the "FOUND_PLAYER" message as it is already the target when entering this line.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: setDesiredRangeTo: not working properly?

Post by JensAyton »

Eric Walch wrote:

Code: Select all

this.checkDistance = function()
{
    if(player.position.distanceTo(this.ship) < 15000) 
    {
       this.ship.reactToAIMessage("FOUND_PLAYER"); 
       this.ship.target = player.ID
    }
}
Other way around, surely? Also, “this.ship.target = player” is sufficient. I don’t think there’s any context where using the ID is actually useful, and I expect to remove it.
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Post by Eric Walch »

I don’t think there’s any context where using the ID is actually useful, and I expect to remove it.
In some situations the “player.target = this.ship” didn't work and “player.target = this.ship.Id” did work! That were test with 1.70.
And this week I created a function:

Code: Select all

this.addSalvage = function(ship)
{
    if(!this.salvageList) {this.salvageList = [ship]; return true;}
    for (let i=0; i<salvageList.length;i++)
    {
        if(this.salvageList[i] == ship) return false;
    }
    this.salvageList[this.salvageList.length] = [ship]
    return true;
}
When I transferred the ship as identity I could only access the first element in the array. Probably I do something wrong. It was very strange. With the debug oxp I could list all array elements. They look all the same but when I called for the properties I only could get them from the first. With the next it said that there were no properties.

I could not find the bug and choose the easiest solution: just storing the ID numbers. For me it was enough.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post by JensAyton »

Eric Walch wrote:

Code: Select all

    this.salvageList[this.salvageList.length] = [ship]
}
shouldn’t that be “this.salvageList[this.salvageList.length] = ship”? (Or, equivalently, “this.salvageList.push(ship)”.)
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: setDesiredRangeTo: not working properly?

Post by Commander McLane »

another_commander wrote:
scanForNearestShipWithPrimaryRole uses the entire scanner range to perform its checks. It does not matter what you set desired range to, as this is not used when scanning for ships. This is hardcoded behaviour and as far as I can tell, it works as expected.
Oh, I see. Then it was just my mistake to assume that all scan...-methods need a desired range. (In the OXP howto AI at least scanForNearestMerchantmen and scanForNearestShipWithRole are listed in the Locating Entities-section, which in its header says:
All these methods require a range to be set by setDesiredRange or by <scanner_range> in shipdata.plist,
I assumed (obviously mistakenly) that the same would be the case for all scan-methods, especially for scanForNearestShipWithPrimaryRole, which is something like the successor of scanForNearestShipWithRole.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post by JensAyton »

scanForNearestShipWithRole: is an alias for scanForNearestShipWithPrimaryRole: these days. The wiki is wrong; scanForNearestShipWithRole: does not respect setDesiredRangeTo: and never did (or at least, didn’t in 1.65).

The desired range is used in scanForNearestMerchantmen, checkCourseToDestination, checkDistanceTravelled, dealEnergyDamageWithinDesiredRange, and various AI behaviour states, namely BEHAVIOUR_INTERCEPT_TARGET, BEHAVIOUR_FLEE_TARGET, BEHAVIOUR_FLY_RANGE_FROM_DESTINATION, BEHAVIOUR_FLY_FROM_DESTINATION, BEHAVIOUR_FACE_DESTINATION, BEHAVIOUR_FORMATION_FORM_UP (performEscort), BEHAVIOUR_AVOID_COLLISION, BEHAVIOUR_FLY_THRU_NAVPOINTS. Many things, including some of the listed AI states, also change the desired range.
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Post by Eric Walch »

Ahruman wrote:
shouldn’t that be “this.salvageList[this.salvageList.length] = ship”? (Or, equivalently, “this.salvageList.push(ship)”.)
Thanks! It works now with the push(), I still have a lot of JS to learn.
Post Reply