← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/ai_target_quantity into lp:widelands

 

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

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/ai_target_quantity/+merge/292714

I found that wares that have no default targets in init.lua give default_ware_quantity 254, and this is accepted by AI and leads to bad behaviour. Now, in such cases, value 10 is used instead. 
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/ai_target_quantity into lp:widelands.
=== modified file 'src/ai/defaultai.cc'
--- src/ai/defaultai.cc	2016-04-11 06:45:29 +0000
+++ src/ai/defaultai.cc	2016-04-23 18:59:50 +0000
@@ -5810,22 +5810,36 @@
 	tribe_ = &player_->tribe();
 
 	// to avoid floats real multiplicator is multiplicator/10
-	uint16_t multiplicator = 10;
-	if ((productionsites.size() + num_ports * 5) > 50) {
-		multiplicator = (productionsites.size() + num_ports * 5) / 5;
-	}
+	const uint16_t multiplicator =
+		std::max<uint16_t>((productionsites.size() + num_ports * 5) / 5, 10);
 
 	for (EconomyObserver* observer : economies) {
 		DescriptionIndex nritems = player_->egbase().tribes().nrwares();
 		for (Widelands::DescriptionIndex id = 0; id < nritems; ++id) {
-			const uint16_t default_target = tribe_->get_ware_descr(id)->default_target_quantity(tribe_->name());
+
+			// Just skip wares that are not used by a tribe
+			if (!tribe_->has_ware(id)) {
+				continue;
+			}
+
+			uint16_t default_target =
+				tribe_->get_ware_descr(id)->default_target_quantity(tribe_->name());
+
+			// It seems that when default target for ware is not set, it returns
+			// kInvalidWare (=254), this is confusing for AI so we change it to 10
+			if (default_target == Widelands::kInvalidWare) {
+				default_target = 10;
+			}
+
+			uint16_t new_target = std::max<uint16_t>(default_target * multiplicator / 10, 2);
+			assert(new_target>1);
 
 			game().send_player_command(*new Widelands::CmdSetWareTargetQuantity(
 			                              gametime,
 			                              player_number(),
 			                              player_->get_economy_number(&observer->economy),
 			                              id,
-			                              default_target * multiplicator / 10));
+			                              new_target));
 		}
 	}
 }


Follow ups