← Back to team overview

widelands-dev team mailing list archive

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

 

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

Commit message:
Reworking the way how AI internally calculates utilization of productionsites.

Requested reviews:
  Widelands Developers (widelands-dev)

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

This is change in productionsites object, but the output is used only by AI. It tries to reflect the duration of production programs, or time of skipping or failing... We can discuss this, but should be more accurate than what is in code now...
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/ai_productionsites_statistics into lp:widelands.
=== modified file 'src/logic/map_objects/tribes/productionsite.cc'
--- src/logic/map_objects/tribes/productionsite.cc	2019-05-18 11:58:43 +0000
+++ src/logic/map_objects/tribes/productionsite.cc	2019-05-19 20:55:45 +0000
@@ -269,6 +269,7 @@
      statistics_(STATISTICS_VECTOR_LENGTH, false),
      last_stat_percent_(0),
      crude_percent_(0),
+     last_program_end_time(0),
      is_stopped_(false),
      default_anim_("idle"),
      main_worker_(-1) {
@@ -954,24 +955,28 @@
 		top_state().phase = result;
 	}
 
+	const uint32_t current_duration = game.get_gametime() - last_program_end_time;
+	assert(game.get_gametime() >= last_program_end_time);
+	last_program_end_time = game.get_gametime();
+
 	switch (result) {
 	case ProgramResult::kFailed:
 		statistics_.erase(statistics_.begin(), statistics_.begin() + 1);
 		statistics_.push_back(false);
 		calc_statistics();
-		crude_percent_ = crude_percent_ * 8 / 10;
+		update_crude_statistics(current_duration, false);
 		break;
 	case ProgramResult::kCompleted:
 		skipped_programs_.erase(program_name);
 		statistics_.erase(statistics_.begin(), statistics_.begin() + 1);
 		statistics_.push_back(true);
 		train_workers(game);
-		crude_percent_ = crude_percent_ * 8 / 10 + 1000000 * 2 / 10;
+		update_crude_statistics(current_duration, true);
 		calc_statistics();
 		break;
 	case ProgramResult::kSkipped:
 		skipped_programs_[program_name] = game.get_gametime();
-		crude_percent_ = crude_percent_ * 98 / 100;
+		update_crude_statistics(current_duration, false);
 		break;
 	case ProgramResult::kNone:
 		skipped_programs_.erase(program_name);
@@ -1032,4 +1037,17 @@
 
 	default_anim_ = anim;
 }
+
+void ProductionSite::update_crude_statistics(uint32_t duration, const bool produced) {
+	static const uint32_t duration_cap = 90 * 1000; //This is highest allowed program duration
+	// just for case something went very wrong...
+	static const uint32_t entire_duration = 5 * 60 *1000;
+	if (duration > duration_cap) {
+		duration = duration_cap;
+	};
+	const uint32_t old_duration = entire_duration - duration;
+	crude_percent_ = (crude_percent_ * old_duration + produced * duration * 10000) / entire_duration;
+	assert(crude_percent_ <= 10000); //be sure we do not go above 100 %
+	}
+
 }  // namespace Widelands

=== modified file 'src/logic/map_objects/tribes/productionsite.h'
--- src/logic/map_objects/tribes/productionsite.h	2019-05-11 12:37:45 +0000
+++ src/logic/map_objects/tribes/productionsite.h	2019-05-19 20:55:45 +0000
@@ -190,8 +190,13 @@
 	uint8_t get_statistics_percent() {
 		return last_stat_percent_;
 	}
+
+	// receives the duration of the last period and the result (true if something was produced)
+	// and sets crude_percent_ to new value
+	void update_crude_statistics(uint32_t, bool);
+
 	uint8_t get_crude_statistics() {
-		return (crude_percent_ + 5000) / 10000;
+		return crude_percent_  / 100;
 	}
 
 	const std::string& production_result() const {
@@ -323,7 +328,8 @@
 	uint8_t last_stat_percent_;
 	// integer 0-10000000, to be divided by 10000 to get a percent, to avoid float (target range:
 	// 0-10)
-	uint32_t crude_percent_;
+	uint32_t crude_percent_; // basically it is percent * 100 to avoid floats
+	uint32_t last_program_end_time;
 	bool is_stopped_;
 	std::string default_anim_;  // normally "idle", "empty", if empty mine.
 


Follow ups