News:

SimuTranslator
Make Simutrans speak your language.

squirrel script - tutorial astar.nut

Started by Andarix, August 31, 2026, 06:49:57 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andarix

Quote from: Yona-TYT on August 30, 2026, 10:54:36 PMThe truth is, I don't really understand astar.nut, but implementing common functions in the tutorial should be the right approach. Any bugs in the tutorial should be easy to fix, or so I think.

I'm a bit rusty when it comes to scripting, I'm afraid.

The functions in `astar.nut` were originally created by Dwachs; you do not necessarily need to understand them in detail.

The important thing is that they handle pathfinding:
a) New pathfinding (i.e., without an existing way)
b) Pathfinding for an existing way

The pathfinding function returns either 'err' or an array containing the tiles that make up the way.

In the tutorial, scenario b) is used to check whether the player has built a way from the starting tile to the target tile.

For the tutorial, the original `astar.nut` functions have been extended so that the last tile of an existing way is stored in `check_way_last_tile`. This is necessary to mark this tile for further construction by the player.

function test_select_way(start, end, wt = wt_rail)
.../class/astar.nut#L769

This function is already used for road construction in Chapter 2.

In the case of railway construction in Chapter 3, there was a problem: the construction area could not be properly defined.
see https://forum.simutrans.com/index.php?msg=212287

I am having trouble with the path validation around 'r_way.' so far.

Yona-TYT

Quote from: Andarix on August 31, 2026, 06:49:57 AMI am having trouble with the path validation around 'r_way.' so far.
             else if (pot[1]==1 && pot[2]==0){
          persistent.ch_sub_step = 1  // sub step finish
          //Comprueba la conexion de la via
          local coora = coord3d(way1_coords.a.x,way1_coords.a.y,way1_coords.a.z)
          local coorb = coord3d(way1_coords.b.x,way1_coords.b.y,way1_coords.b.z)
          local dir = way1_coords.dir
          local obj = false
          local r_way = get_fullway(coora, coorb, dir, obj)

          //check_way_last_tile
          //test_select_way(my_tile(way1_coords.a), my_tile(way1_coords.b), wt_road)


          //Para marcar inicio y fin de la via
          local waya = tile_x(coora.x,coora.y,coora.z).find_object(mo_way)
          local wayb = tile_x(coorb.x,coorb.y,coorb.z).find_object(mo_way)
          if (waya) waya.mark()
          if (wayb) wayb.mark()

          if (r_way.r){
            //Para desmarcar inicio y fin de la carretera
            waya.unmark()
            wayb.unmark()

            //way1_coords.a.remove_object(player_x(1), mo_label)
            //way1_coords.b.remove_object(player_x(1), mo_label)

            //Elimina cuadro label
            local opt = 0
            label_bord(c_way_limit1.a, c_way_limit1.b, opt, true, "X")

            //Creea un cuadro label
            label_bord(city1_limit1.a, city1_limit1.b, opt, false, "X")
            label_bord(city2_limit1.a, city2_limit1.b, opt, false, "X")

            pot[2]=1
          }
        }

         

The old function is still being used in chapter 2

Andarix

#2
Quote from: Yona-TYT on Today at 01:25:48 AM...

The old function is still being used in chapter 2

That's exactly the point. The check involving `r_way.` is no longer necessary, but I couldn't remove it without breaking the rest of the script.

I haven't fully grasped this function yet.

[edit]

I actually thought Chapter 2 used `astar.nut`. It's possible it got lost or I didn't upload it to the repo.

I need to check. I tend to lose track of things with all the testing.

Chapter 5 uses both functions.

.../class/class_chapter_05.nut#L370