← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/bug-978138 into lp:widelands

 

TiborB has proposed merging lp:~widelands-dev/widelands/bug-978138 into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #978138 in widelands: "Ship is under fisher's hut"
  https://bugs.launchpad.net/widelands/+bug/978138

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/bug-978138/+merge/257005

This is relatively simple fix. For every possible field where a ship could be built the game counts immovables within radius 2 and picks the spot with lowest count of immovables. Up to now the game flatly used first spot (though order of spots were quasi random)
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/bug-978138 into lp:widelands.
=== modified file 'src/logic/production_program.cc'
--- src/logic/production_program.cc	2015-01-30 23:10:35 +0000
+++ src/logic/production_program.cc	2015-04-21 21:17:56 +0000
@@ -1625,14 +1625,39 @@
 
 	// No object found, look for a field where we can build
 	std::vector<Coords> fields;
+	Map  & map = g.map();
 	FindNodeAnd fna;
 	fna.add(FindNodeShore());
 	fna.add(FindNodeImmovableSize(FindNodeImmovableSize::sizeNone));
 	if
-		(g.map().find_reachable_fields
+		(map.find_reachable_fields
 			(area, &fields, cstep, fna))
 	{
-		state.coord = fields[0];
+		//testing received fields to get one with less immovables
+		//nearby
+		Coords best_coords = fields.back(); //just to initialize it
+		uint32_t best_score = std::numeric_limits<uint32_t>::max();
+		while (!fields.empty()) {
+			Coords coords = fields.back();
+
+			//counting immovables nearby
+			std::vector<ImmovableFound> found_immovables;
+			const uint32_t imm_count =
+				map.find_immovables(Area<FCoords>(map.get_fcoords(coords), 2), &found_immovables);
+			if (best_score > imm_count){
+				best_score = imm_count;
+				best_coords = coords;
+			}
+
+			//no need to go on, it cannot be better
+			if (imm_count == 0) {
+				break;
+				}
+
+			fields.pop_back();
+		}
+
+		state.coord = best_coords;
 
 		psite.m_working_positions[0].worker->update_task_buildingwork(g);
 		return;


Follow ups