← Back to team overview

widelands-dev team mailing list archive

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

 

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

Commit message:
Second and further traininsites of the type is built only if current one are busy enough...

Requested reviews:
  Widelands Developers (widelands-dev)

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

Second and further traininsites of the type is now built only if existing ones are training soldiers (5 soldiers within last 20 minutes)
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/ai_trainingsites into lp:widelands.
=== modified file 'src/ai/ai_help_structs.cc'
--- src/ai/ai_help_structs.cc	2017-08-18 00:17:39 +0000
+++ src/ai/ai_help_structs.cc	2017-08-18 20:03:17 +0000
@@ -198,18 +198,32 @@
 EventTimeQueue::EventTimeQueue() {
 }
 
-void EventTimeQueue::push(const uint32_t production_time) {
-	queue.push(production_time);
+void EventTimeQueue::push(const uint32_t production_time, const uint32_t additional_id) {
+	queue.push_front(std::make_pair(production_time, additional_id));
 }
 
-uint32_t EventTimeQueue::count(const uint32_t current_time) {
+// Return count of entries in log (deque), if id is provided, it counts corresponding
+// members. id here can be index of building, f.e. it count how many soldiers were
+// trained in particular type of training site
+uint32_t EventTimeQueue::count(const uint32_t current_time, const uint32_t additional_id) {
 	strip_old(current_time);
-	return queue.size();
+	if (additional_id == std::numeric_limits<uint32_t>::max()) {
+		return queue.size();
+	} else {
+		uint32_t cnt = 0;
+		// for (auto item : queue){
+		for (auto it : queue) {
+			if (it.second == additional_id) {
+				cnt += 1;
+			}
+		}
+		return cnt;
+	}
 }
 
 void EventTimeQueue::strip_old(const uint32_t current_time) {
-	while (!queue.empty() && queue.front() < current_time - duration_) {
-		queue.pop();
+	while (!queue.empty() && queue.back().first < current_time - duration_) {
+		queue.pop_back();
 	}
 }
 

=== modified file 'src/ai/ai_help_structs.h'
--- src/ai/ai_help_structs.h	2017-08-18 00:17:39 +0000
+++ src/ai/ai_help_structs.h	2017-08-18 20:03:17 +0000
@@ -276,16 +276,20 @@
 	int32_t distance;
 };
 
+// FIFO like structure for pairs <gametime,id>, where id is optional
+// used to count events within a time frame - duration_ (older ones are
+// stripped with strip_old function)
 struct EventTimeQueue {
 	EventTimeQueue();
 
-	void push(uint32_t);
-	uint32_t count(uint32_t);
+	void push(uint32_t, uint32_t = std::numeric_limits<uint32_t>::max());
+	uint32_t count(uint32_t, uint32_t = std::numeric_limits<uint32_t>::max());
 	void strip_old(uint32_t);
 
 private:
-	uint32_t duration_ = 20 * 60 * 1000;
-	std::queue<uint32_t> queue;
+	const uint32_t duration_ = 20 * 60 * 1000;
+	// FIFO contaner where newest goes to the front
+	std::deque<std::pair<uint32_t, uint32_t>> queue;
 };
 
 struct WalkableSpot {

=== modified file 'src/ai/defaultai.cc'
--- src/ai/defaultai.cc	2017-08-18 00:17:39 +0000
+++ src/ai/defaultai.cc	2017-08-18 20:03:17 +0000
@@ -489,7 +489,6 @@
 	const uint32_t gametime = game().get_gametime();
 
 	log("ComputerPlayer(%d): initializing as type %u%s\n", player_number(),
-	    // NOCOM(#codereview): Please add blank spaces.
 	    static_cast<unsigned int>(type_), (ai_training_mode_) ? ", in ai training mode" : "");
 	if (player_->team_number() > 0) {
 		log("    ... member of team %d\n", player_->team_number());
@@ -4314,6 +4313,10 @@
 			return BuildingNecessity::kNotNeeded;
 		} else if (ts_without_trainers_ > 1) {
 			return BuildingNecessity::kNotNeeded;
+		} else if (bo.total_count() > 0) {
+			if (soldier_trained_log.count(gametime, bo.id) / bo.total_count() < 5) {
+				return BuildingNecessity::kNotNeeded;
+			}
 		}
 
 		// It seems we might need it after all

=== modified file 'src/ai/defaultai_warfare.cc'
--- src/ai/defaultai_warfare.cc	2017-08-09 18:45:15 +0000
+++ src/ai/defaultai_warfare.cc	2017-08-18 20:03:17 +0000
@@ -1239,10 +1239,11 @@
 void DefaultAI::soldier_trained(const TrainingSite& site) {
 
 	const uint32_t gametime = game().get_gametime();
-	soldier_trained_log.push(gametime);
+
 
 	for (TrainingSiteObserver& trainingsite_obs : trainingsites) {
 		if (trainingsite_obs.site == &site) {
+			soldier_trained_log.push(gametime, trainingsite_obs.bo->id);
 			if (trainingsite_obs.site->soldier_control()->soldier_capacity() > 0) {
 				game().send_player_change_soldier_capacity(
 				   *trainingsite_obs.site,


References