Comment 12 for bug 977980

Revision history for this message
xxx-deleted (janosch-peters-deactivatedaccount) wrote :

I fixed this bug in lp:~janosch-peters/widelands/editor-remove-invalid-resources. I pushed several revisions. To see the complete diff got to http://bazaar.launchpad.net/~janosch-peters/widelands/editor-remove-invalid-resources/revision/7663?remember=7657&compare_revid=7657

Basically I did two things:

1. Map::change_terrain verifies now if the new terrain matches the current resource. If it doesen't, the resource is deleted. This fixes the bug in the editor.

2. A new method Map::check_res_consistency is called when a map is loaded. The method looks for invalid resources on every node and if present, deletes them. This "fixes" broken maps on load and affects both the editor and the game.

The second one turned out to be more work than I thought. I discovered that in some places the resource of a field was set to "no resource" by doing this: "field.set_resources(0, 0)". But this actually means (type:coal, amount:0), because the DescriptionIndex of resources starts at 0. It appears to me that (type:coal, amount:0) is currenty the semantic for "No Resource" in Widelands. This is a cumberson convention in my eyes and it also has negative side-effects. For instance, you cannot use TerrainDescription::is_resource_valid like this:

world.terrains().get(field.get_terrains().d).is_resource_valid(m_fields.get_resources())

because the convention isn't recognized in this method.

To have a clear distinction between "coal" and no resource, I changed all occurences of set_resources(0, 0) to set_resource(Widelands::kNoResource, 0). This also fixes TerrainDescription::is_resource_valid. I think this makes the code more readable and maintainable. However, I had to adjust the code in several places and also change test cases to make this work.

Testing:
1. Start the editor. Add "water" resource on a grass terrain. Then, change the terrain to mountain. The water resource should vanish.

2. I attached the map "invalid_resources.wmf". If you load the map the invalid resource "water" on a mountain terrain node (7,5) are removed. Likewise, the invalid resource "gold" is removed from (2,3), a water node.

Codestyle check and regression test suite passed successfuly.