News:

Simutrans Tools
Know our tools that can help you to create add-ons, install and customize Simutrans.

Get tile_list () from building_x ()

Started by Yona-TYT, August 23, 2021, 03:27:55 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Yona-TYT

 
I was trying to implement this, to make life easier for scripts:
        local tile_list = build.get_tile_list() //It is not implemented
        foreach(tile in tile_list){
            tile.find_object(mo_building).mark()
        }


But I found that it is not as easy as I expected hehehehe. ;D 
I was trying to generate a list in "gebaeude_t", but I have no idea how to make it work.

Attached my experiment so far: 

Mariculous

See gebaeude_t::set_building_tiles, which is not a setter btw, in simutrans extended

Yona-TYT

Quote from: Freahk on August 23, 2021, 09:35:01 AM
See gebaeude_t::set_building_tiles, which is not a setter btw, in simutrans extended
However this will not work since I am only able to obtain arrays for coordinates, since there is no real tile_t class.

Mariculous

#3
Why doesn't it work?
Surely you cannot use it as-is but it should point you in the right direction:
Either use it as-is and build a C++ function that maps the planquadrat_t to their koord

for(planquadrat_t* quadrat : building_tiles){
  result.append(quadrat->get_kartenboden()->get_pos());
}

or build a function on top of it that obtains a list of coordinates instead of planquadrat_t directly.

prissi

#4
foundations (the ground below building) cannot match to koord, only to koord3d. All tiles of the building are at the same height level, hence x,y,fixed hight. So you must lookup the ground at the right height, as there could be more than one ground at one height (stations in tunnel or bridges for instance)

Moreover there is already the function building_get_size();

Anyway, you need to be aware that the tile might not be the rigin. Look fr instance at "void stadt_t::add_gebaeude_to_stadt(" So assuming you are passing a building_x to this routine which is converted to gb Maybe you want something like the attached?

Yona-TYT

#5
I also thought that, a "koord3d" makes more sense, but I was getting carried away by this factory code in simfab.cc:
void fabrik_t::get_tile_list( vector_tpl<koord> &tile_list ) const
{
    tile_list.clear();

    koord pos_2d = pos.get_2d();
    koord size = this->get_desc()->get_building()->get_size(this->get_rotate());
    koord test;
    // Which tiles belong to the fab?
    for( test.x = 0; test.x < size.x; test.x++ ) {
        for( test.y = 0; test.y < size.y; test.y++ ) {
            if( fabrik_t::get_fab( pos_2d+test ) == this ) {
                tile_list.append( pos_2d+test );
            }
        }
    }
}

Although now that I think about it, there are no factories over other factories, it would still be more useful to have a z coordinate.


Your patch works great! Thank you very much!.
Although I'm afraid we are giving the function a bad name since we are not offering a "tile_x" but a "coord3d", so in order not to confuse people it is better to call it "get_coord_list ()", what do you think? .
We must also change "export_types [" building_x :: get_tile_list "] =" array <coord> () "" by "export_types [" building_x :: get_tile_list "] =" array <coord3d> () ""

This is a functional example:  8)

    local t = tile_x(pos.x,pos.y,pos.z)
    local build = t.find_object(mo_building)

    if(build){
        currt_pos = pos
        build.mark()
        local c_list = build.get_coord_list()

        foreach(c in c_list){
            tile_x(c.x, c.y, c.z).find_object(mo_building).mark()
        }
    }


Well I attach my modification:
 

prissi


prissi


Yona-TYT

Quote from: prissi on September 12, 2021, 06:32:53 AM
And r10098 should return a tile tist.
Wow prisi, you have excelled optimizing and reusing the tile list, congratulations ... and now I have to do some tests 😜.

Yona-TYT


        local t_list = build.get_tile_list()

        foreach(t in t_list){
            t.find_object(mo_building).mark()
        }

I am happy that this works well  ;D .