The International Simutrans Forum

Development => Scripting Scenarios and AI => Topic started by: Yona-TYT on May 21, 2017, 03:42:22 AM

Title: save convoy_x
Post by: Yona-TYT on May 21, 2017, 03:42:22 AM
Quote from: Dwachs on May 20, 2017, 08:27:19 AM
You could also save the convoy_x instances and check them later:
How do I save "convoy_x" using "persistent"?  ???

This does not work:  :-[


datasave <- {
    cov = cov_save
}

class data_save {
     
    // Convoys
    function convoys_save() {return datasave.cov;}
     
    function _save() { return "data_save()"; }
}

persistent.datasave <- datasave


function resume_game()
{

// Datos guardados
//-----------------------------------------------------
     
    foreach(key,value in persistent.datasave)
    {
        datasave.rawset(key,value)
    }
    persistent.datasave = datasave

    // Se obtienen los datos guardados
    cov_save  = data_save().convoys_save()


//-------------------------------------------------------
}

Title: Re: save convoy_x
Post by: Dwachs on May 21, 2017, 07:03:08 AM
You can have a look here:

https://github.com/aburch/simutrans/blob/master/simutrans/ai/sqai/save.nut
Title: Re: save convoy_x
Post by: Yona-TYT on May 24, 2017, 06:14:57 AM
I have problems with the list of saved convoys.

Script: http://files.simutrans.com/index.php/s/MnMVUIgYmHetqaa (http://files.simutrans.com/index.php/s/MnMVUIgYmHetqaa)
Savegame: http://files.simutrans.com/index.php/s/nHumABiXC5G5kuB (http://files.simutrans.com/index.php/s/nHumABiXC5G5kuB)

I have no problem saving convoys, the problem is when the list is loaded empty.

At the time of filling the list the convoys are doubled.

(http://files.simutrans.com/index.php/apps/files_sharing/ajax/publicpreview.php?x=1440&y=372&a=true&file=error_%2520convoys%2520list.png&t=olpMThQBcQmKH0x&scalingup=0)

For replica only start the vehicles that are in the road deposit.

I am sure the problem is caused by this code: scenario.nut (line 388)
datasave <- {
    cov = cov_save
}

class data_save {
     
    // Convoys
    function convoys_save() {return datasave.cov;}
     
    function _save() { return "data_save()"; }
}

persistent.datasave <- datasave

convoy_x._save <- function ()
{
    return "convoy_x(" + id + ")"
}
//-----------------------------------------------------------

function resume_game()
{

// Datos guardados
//-----------------------------------------------------     
    // copy it piece by piece otherwise the reference 
    foreach(key,value in persistent.datasave)
    {
        datasave.rawset(key,value)
    }
    persistent.datasave = datasave

    // Se obtienen los datos guardados
    cov_save  = data_save().convoys_save()
    //cov_save  = array(50)

//-------------------------------------------------------

}


If you change "cov_save = data_save().convoys_save()" to "cov_save = array(50)" duplicates no longer appear.

Title: Re: save convoy_x
Post by: Yona-TYT on May 25, 2017, 04:04:44 PM
@Dwachs
Can you replicate the error? ???
Title: Re: save convoy_x
Post by: Dwachs on May 25, 2017, 04:49:37 PM
No. I get a list of 10 NUL!!!! entries.

If you look into the log, you see the loaded data from the persistent table, there are no double entries:

...
,
        datasave = {
                cov = [
                        convoy_x(1),
                        convoy_x(7),
                        convoy_x(8),
                        convoy_x(18),
                        convoy_x(6),
                        convoy_x(2),
                        convoy_x(3),
                        convoy_x(11),
                        convoy_x(10),
                        convoy_x(9),
                        convoy_x(5),
                        null,
                        null,
...
Title: Re: save convoy_x
Post by: Yona-TYT on May 25, 2017, 05:10:40 PM

Quote from: Dwachs on May 25, 2017, 04:49:37 PM
No. I get a list of 10 NUL!!!! entries.
Sorry for the confusion, it's actually that "Null !!!!" Is to indicate that the vehicle fails or is not in the depot.
This is the code that shows the list:

function get_goal_text(pl)
{
        local tx = ""
        local j=0
        for(j;j<gcov_nr;j++){
            local result = true
            // cnv - convoy_x instance saved somewhat earlier
            try {
                 cov_save[j].get_pos() // will fail if cnv is no longer existent
                 // do your checks
            }
            catch(ev) {
                result = false
            }
            if (result){
                if (cov_save[j].is_in_depot()){
                    result = false
                }
            }

            if (result) {
                tx += ">["+j+"] <a href=\"("+cov_save[j].get_pos().tostring()+")\"> ("+cov_save[j].get_pos().tostring()+")[/url] "+cov_save[j].get_name()+"[br /]"
            }
            else
                tx += "<st>["+j+"]</st> Null !!!! [br /]"
        }
        return tx 
}


Quote from: Dwachs on May 25, 2017, 04:49:37 PM
If you look into the log, you see the loaded data from the persistent table, there are no double entries:

...
,
        datasave = {
                cov = [
                        convoy_x(1),
                        convoy_x(7),
                        convoy_x(8),
                        convoy_x(18),
                        convoy_x(6),
                        convoy_x(2),
                        convoy_x(3),
                        convoy_x(11),
                        convoy_x(10),
                        convoy_x(9),
                        convoy_x(5),
                        null,
                        null,
...


Edit.
At first the list is correct, the duplicates appear after starting the second vehicle.
No doubt something strange happens here. :o
Title: Re: save convoy_x
Post by: Dwachs on May 25, 2017, 06:31:30 PM
So what exactly are the steps to reproduce which wrong behavior?
Title: Re: save convoy_x
Post by: Yona-TYT on May 25, 2017, 08:08:54 PM

Oops! Forget to uncomment the line "cov_save = data_save() .convoys_save()", change to this script: http://files.simutrans.com/index.php/s/GhSVyKx8V3J39gh (http://files.simutrans.com/index.php/s/GhSVyKx8V3J39gh)

Quote from: Dwachs on May 25, 2017, 06:31:30 PM
So what exactly are the steps to reproduce which wrong behavior?
1. Load the savegame and do not close the window "current scenario details".
2. On the road deposit, start the first bus, note that the space "0" in the list stops being null.
3.Start the second bus, note that space [1] and [6] is no longer null, this should not happen.

As I mentioned before
QuoteIf you change "cov_save = data_save().convoys_save()" to "cov_save = array(50)" duplicates no longer appear.
therefore is not a problem with the scenario.
Title: Re: save convoy_x
Post by: Dwachs on May 26, 2017, 06:13:30 AM
Loading and saving is correct and does not create double entries. There must be a bug elsewhere in your script.
Title: Re: save convoy_x
Post by: Yona-TYT on May 27, 2017, 01:05:00 AM
Quote from: Dwachs on May 26, 2017, 06:13:30 AM
Loading and saving is correct and does not create double entries. There must be a bug elsewhere in your script.
It does not seem.

If you remove the line (421) "cov_save = data_save().convoys_save()" and load the game again this works fine. How is this possible then? ???