From 66868e2eefe97b7a6d35fa3fecc895b9c7bbc4cc Mon Sep 17 00:00:00 2001 From: "Y. Wang" Date: Thu, 19 Sep 2024 18:39:19 +0000 Subject: Address wagon aliasing issues As it turns out, not fully testing new features is not necessarily a good idea ... This patch follows up 1F616EMO's patch by * Making get_wagon_prototype return the resolved alias, * Handling recursive wagon alises (in particular, loops), and * Adding (partial) unittest for the wagon aliasing system. [v2]: The testcases are complemented a bit more to cover situations where the alias resolution system should return nil. [v2]: This patch should hopefully also warn about not spawning wagons. Note that this only warns about the missing wagon entity and does _not_ actually fix the issue. How to test: * In a world with both advtrains_train_subway and advtrains_train_japan enabled, place a subway wagon, a Japanese engine, and a regular Japanese wagon. * Add the test mod to the world; do NOT remove advtrains_train_japan. * Restart the world. Notice that the Japanese wagons still appear as Japanese wagons despite being aliased to subway wagons. * Restart the world without the advtrains_train_japan mod. Notice that the engine appears as the subway wagon while the regular Japanese wagon appears as the wagon placeholder. [v2]: Also note that the warning message about the missing wagon prototype still mentions the regular Japanese wagon. * Restart the world again with the advtrains_train_japan mod. Notice that both type of Japanese wagons reappear as Japanese wagons. * Observe that unittests work. Test mod: advtrains.register_wagon_alias("advtrains:engine_japan", "advtrains:subway_wagon") advtrains.register_wagon_alias("advtrains:wagon_japan", "advtrains:wagon_japan") --- advtrains/trainlogic.lua | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'advtrains/trainlogic.lua') diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 796aae9..3b006d2 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -1113,8 +1113,17 @@ function advtrains.spawn_wagons(train_id) if advtrains.position_in_range(pos, ablkrng) then --atdebug("wagon",w_id,"spawning") local wt = advtrains.get_wagon_prototype(data) - local wagon = minetest.add_entity(pos, wt):get_luaentity() - wagon:set_id(w_id) + local wobj = minetest.add_entity(pos, wt) + if not wobj then + atwarn("Failed to spawn wagon", w_id, "of type", wt) + else + local wagon = wobj:get_luaentity() + if not wagon then + atwarn("Wagon", w_id, "of type", wt, "spawned with nil luaentity") + else + wagon:set_id(w_id) + end + end end end else -- cgit v1.2.3