← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~borim/widelands/stockChart into lp:widelands

 

Borim has proposed merging lp:~borim/widelands/stockChart into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #970840 in widelands: "new graph: availability of wares"
  https://bugs.launchpad.net/widelands/+bug/970840

For more details, see:
https://code.launchpad.net/~borim/widelands/stockChart/+merge/104118

add a new ware chart, which display the amount of stored wares over the time

branch is related to Bug #970840
-- 
https://code.launchpad.net/~borim/widelands/stockChart/+merge/104118
Your team Widelands Developers is requested to review the proposed merge of lp:~borim/widelands/stockChart into lp:widelands.
=== added file 'pics/menu_tab_wares_stock.png'
Binary files pics/menu_tab_wares_stock.png	1970-01-01 00:00:00 +0000 and pics/menu_tab_wares_stock.png	2012-04-30 14:10:43 +0000 differ
=== modified file 'src/game_io/game_player_info_data_packet.cc'
--- src/game_io/game_player_info_data_packet.cc	2012-02-15 21:25:34 +0000
+++ src/game_io/game_player_info_data_packet.cc	2012-04-30 14:10:43 +0000
@@ -30,7 +30,7 @@
 
 namespace Widelands {
 
-#define CURRENT_PACKET_VERSION 14
+#define CURRENT_PACKET_VERSION 15
 
 
 void Game_Player_Info_Data_Packet::Read
@@ -111,7 +111,9 @@
 					if (packet_version >= 6)
 						player.setAI(fr.CString());
 
-					if (packet_version >= 14)
+					if (packet_version >= 15)
+						player.ReadStatistics(fr, 3);
+					else if (packet_version >= 14)
 						player.ReadStatistics(fr, 2);
 					else if (packet_version >= 12)
 						player.ReadStatistics(fr, 1);

=== modified file 'src/logic/player.cc'
--- src/logic/player.cc	2012-04-06 19:26:17 +0000
+++ src/logic/player.cc	2012-04-30 14:10:43 +0000
@@ -23,6 +23,7 @@
 #include "cmd_expire_message.h"
 #include "cmd_luacoroutine.h"
 #include "constructionsite.h"
+#include "economy/economy.h"
 #include "economy/flag.h"
 #include "economy/road.h"
 #include "findimmovable.h"
@@ -94,7 +95,8 @@
 	m_current_produced_statistics(tribe_descr.get_nrwares    ()),
 	m_current_consumed_statistics(tribe_descr.get_nrwares    ()),
 	m_ware_productions  (tribe_descr.get_nrwares    ()),
-	m_ware_consumptions  (tribe_descr.get_nrwares    ())
+	m_ware_consumptions  (tribe_descr.get_nrwares    ()),
+	m_ware_stocks  (tribe_descr.get_nrwares          ())
 {
 	set_name(name);
 }
@@ -1057,13 +1059,39 @@
 {
 	assert (m_ware_productions.size() == tribe().get_nrwares().value());
 	assert (m_ware_consumptions.size() == tribe().get_nrwares().value());
-
+	assert (m_ware_stocks.size() == tribe().get_nrwares().value());
+
+	//calculate stocks
+	std::vector<uint32_t> stocks(tribe().get_nrwares().value());
+
+	const uint32_t nrecos = get_nr_economies();
+	for (uint32_t i = 0; i < nrecos; ++i) {
+		const std::vector<Widelands::Warehouse *> & warehouses =
+			get_economy_by_number(i)->warehouses();
+
+		for
+			(std::vector<Widelands::Warehouse *>::const_iterator it =
+			 warehouses.begin();
+			 it != warehouses.end();
+			 ++it)
+		{
+			for (uint32_t id; id < stocks.size(); ++id) {
+				stocks[id] += (*it)->get_economy()->stock_ware
+					(Ware_Index(static_cast<size_t>(id)));
+			}
+		}
+	}
+
+
+	//update statistics
 	for (uint32_t i = 0; i < m_ware_productions.size(); ++i) {
 		m_ware_productions[i].push_back(m_current_produced_statistics[i]);
 		m_current_produced_statistics[i] = 0;
 
 		m_ware_consumptions[i].push_back(m_current_consumed_statistics[i]);
 		m_current_consumed_statistics[i] = 0;
+
+		m_ware_stocks[i].push_back(stocks[i]);
 	}
 }
 
@@ -1117,6 +1145,14 @@
 	return &m_ware_consumptions[ware];
 }
 
+const std::vector<uint32_t> * Player::get_ware_stock_statistics
+		(Ware_Index const ware) const
+{
+	assert(ware.value() < m_ware_stocks.size());
+
+	return &m_ware_stocks[ware];
+}
+
 
 /**
  * Add or remove the given building from building statistics.
@@ -1193,12 +1229,13 @@
  *   0 - old style statistics (before WiHack 2010)
  *   1 - statistics with ware names
  *   2 - with consumption statistics
+ *   3 - with stock statistics
  */
 void Player::ReadStatistics(FileRead & fr, uint32_t const version)
 {
-	 //version 1 and 2 only differs in an additional statistic.
-	 //Use version 1 code for both
-	if ((version == 2) || (version == 1)) {
+	 //version 1, 2 and 3 only differs in an additional statistic.
+	 //Use version 1 code for all of them
+	if ((version == 2) || (version == 1) || (version == 3)) {
 		uint16_t nr_wares = fr.Unsigned16();
 		uint16_t nr_entries = fr.Unsigned16();
 
@@ -1222,7 +1259,7 @@
 		}
 
 		//read consumption statistics if it exists
-		if (version == 2) {
+		if ((version == 2) || (version == 3)) {
 			nr_wares = fr.Unsigned16();
 			nr_entries = fr.Unsigned16();
 
@@ -1234,7 +1271,7 @@
 				Ware_Index idx = tribe().ware_index(name);
 				if (!idx) {
 					log
-						("Player %u statistics: unknown ware name %s",
+						("Player %u consumption statistics: unknown ware name %s",
 						player_number(), name.c_str());
 					continue;
 				}
@@ -1244,6 +1281,29 @@
 				for (uint32_t j = 0; j < nr_entries; ++j)
 					m_ware_consumptions[idx][j] = fr.Unsigned32();
 			}
+
+			//read stock statistics if it exists
+			if (version == 3) {
+				nr_wares = fr.Unsigned16();
+				nr_entries = fr.Unsigned16();
+
+				for (uint32_t i = 0; i < m_ware_stocks.size(); ++i)
+					m_ware_stocks[i].resize(nr_entries);
+
+				for (uint16_t i = 0; i < nr_wares; ++i) {
+					std::string name = fr.CString();
+					Ware_Index idx = tribe().ware_index(name);
+					if (!idx) {
+						log
+							("Player %u stock statistics: unknown ware name %s",
+							player_number(), name.c_str());
+						continue;
+					}
+
+					for (uint32_t j = 0; j < nr_entries; ++j)
+						m_ware_stocks[idx][j] = fr.Unsigned32();
+				}
+			}
 		}
 	} else if (version == 0) {
 		uint16_t nr_wares = fr.Unsigned16();
@@ -1293,8 +1353,24 @@
 		}
 	}
 
+	//create empty stock statistic if it is missing
+	if (version < 3) {
+		uint16_t nr_entries = m_ware_productions[0].size();
+
+		for (uint32_t i = 0; i < m_current_consumed_statistics.size(); ++i) {
+			m_ware_stocks[i].resize(nr_entries);
+
+			for (uint32_t j = 0; j < nr_entries; ++j)
+				m_ware_stocks[i][j] = 0;
+		}
+	}
+
+	//all statistics should have the same size
 	assert(m_ware_productions.size() == m_ware_consumptions.size());
 	assert(m_ware_productions[0].size() == m_ware_consumptions[0].size());
+
+	assert(m_ware_productions.size() == m_ware_stocks.size());
+	assert(m_ware_productions[0].size() == m_ware_stocks[0].size());
 }
 
 
@@ -1327,6 +1403,18 @@
 		for (uint32_t j = 0; j < m_ware_consumptions[i].size(); ++j)
 			fw.Unsigned32(m_ware_consumptions[i][j]);
 	}
+
+	//write stock statistics
+	fw.Unsigned16(m_ware_stocks.size());
+	fw.Unsigned16(m_ware_stocks[0].size());
+
+	for (uint8_t i = 0; i < m_ware_stocks.size(); ++i) {
+		fw.CString
+			(tribe().get_ware_descr
+			 (Ware_Index(static_cast<Ware_Index::value_t>(i)))->name());
+		for (uint32_t j = 0; j < m_ware_stocks[i].size(); ++j)
+			fw.Unsigned32(m_ware_stocks[i][j]);
+	}
 }
 
 }

=== modified file 'src/logic/player.h'
--- src/logic/player.h	2012-02-28 22:10:23 +0000
+++ src/logic/player.h	2012-04-30 14:10:43 +0000
@@ -534,6 +534,9 @@
 	std::vector<uint32_t> const * get_ware_consumption_statistics
 		(Ware_Index const) const;
 
+	std::vector<uint32_t> const * get_ware_stock_statistics
+		(Ware_Index const) const;
+
 	void ReadStatistics(FileRead &, uint32_t version);
 	void WriteStatistics(FileWrite &) const;
 	void sample_statistics();
@@ -620,6 +623,13 @@
 	 */
 	std::vector< std::vector<uint32_t> > m_ware_consumptions;
 
+	/**
+	 * Statistics of wares stored inside of warehouses over the
+	 * life of the game, indexed as
+	 * m_ware_stocks[ware_id][time_index]
+	 */
+	std::vector< std::vector<uint32_t> > m_ware_stocks;
+
 	BuildingStats m_building_stats;
 };
 

=== modified file 'src/wui/ware_statistics_menu.cc'
--- src/wui/ware_statistics_menu.cc	2012-02-15 21:25:34 +0000
+++ src/wui/ware_statistics_menu.cc	2012-04-30 14:10:43 +0000
@@ -47,6 +47,7 @@
 static const char pic_tab_production[] = "pics/menu_tab_wares_production.png";
 static const char pic_tab_consumption[] = "pics/menu_tab_wares_consumption.png";
 static const char pic_tab_economy[] = "pics/menu_tab_wares_econ_health.png";
+static const char pic_tab_stock[] = "pics/menu_tab_wares_stock.png"; //TODO replace place holder
 
 static const RGBColor colors[] = {
 	RGBColor(115, 115, 115), //inactive
@@ -203,6 +204,16 @@
 		("economy_health", g_gr->get_picture(PicMod_UI, pic_tab_economy),
 			m_plot_economy, _("Economy Health"));
 
+	m_plot_stock = new WUIPlot_Area
+			(tabs,
+			 0, 0, plot_width, plot_height);
+	m_plot_stock->set_sample_rate(STATISTICS_SAMPLE_TIME);
+	m_plot_stock->set_plotmode(WUIPlot_Area::PLOTMODE_RELATIVE);
+
+	tabs->add
+		("stock", g_gr->get_picture(PicMod_UI, pic_tab_stock),
+			m_plot_stock, _("Stock"));
+
 	tabs->activate(0);
 
 	//add tabbed environment to box
@@ -232,6 +243,12 @@
 			(cur_ware,
 				parent.get_player()->get_ware_consumption_statistics
 				(Widelands::Ware_Index(cur_ware)));
+
+		m_plot_stock->register_plot_data
+			(cur_ware,
+				parent.get_player()->get_ware_stock_statistics
+				(Widelands::Ware_Index(cur_ware)),
+				colors[cur_ware]);
 	}
 
 	box->add
@@ -273,6 +290,7 @@
 		m_plot_production->set_plotcolor(static_cast<size_t>(id), colors[color_index]);
 		m_plot_consumption->set_plotcolor(static_cast<size_t>(id), colors[color_index]);
 		m_plot_economy->set_plotcolor(static_cast<size_t>(id), colors[color_index]);
+		m_plot_stock->set_plotcolor(static_cast<size_t>(id), colors[color_index]);
 
 	} else { //deactivate ware
 		uint8_t old_color = m_color_map[static_cast<size_t>(id)];
@@ -285,6 +303,7 @@
 	m_plot_production->show_plot(static_cast<size_t>(id), what);
 	m_plot_consumption->show_plot(static_cast<size_t>(id), what);
 	m_plot_economy->show_plot(static_cast<size_t>(id), what);
+	m_plot_stock->show_plot(static_cast<size_t>(id), what);
 }
 
 /**
@@ -295,5 +314,6 @@
 	m_plot_production->set_time_id(timescale);
 	m_plot_consumption->set_time_id(timescale);
 	m_plot_economy->set_time_id(timescale);
+	m_plot_stock->set_time_id(timescale);
 }
 

=== modified file 'src/wui/ware_statistics_menu.h'
--- src/wui/ware_statistics_menu.h	2012-02-15 21:25:34 +0000
+++ src/wui/ware_statistics_menu.h	2012-04-30 14:10:43 +0000
@@ -38,6 +38,7 @@
 	Interactive_Player * m_parent;
 	WUIPlot_Area       * m_plot_production;
 	WUIPlot_Area       * m_plot_consumption;
+	WUIPlot_Area       * m_plot_stock;
 	DifferentialPlot_Area       * m_plot_economy;
 	std::vector<uint8_t> m_color_map; //maps ware index to colors
 	std::vector<bool> m_active_colors;