← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~aber/widelands/jetAnotherEvictButton into lp:widelands

 

David Allwicher has proposed merging lp:~aber/widelands/jetAnotherEvictButton into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~aber/widelands/jetAnotherEvictButton/+merge/107662

I wanted to implement the evict worker button, but everything I have achieved is a segfault 😭. Maybe someone is able to find the time and can give me a hint on how to implement this?

Best,

David


backtrace: http://wolkentempel.de/daten/log-1205281803.txt
-- 
https://code.launchpad.net/~aber/widelands/jetAnotherEvictButton/+merge/107662
Your team Widelands Developers is requested to review the proposed merge of lp:~aber/widelands/jetAnotherEvictButton into lp:widelands.
=== added file 'pics/menu_evict.png'
Binary files pics/menu_evict.png	1970-01-01 00:00:00 +0000 and pics/menu_evict.png	2012-05-28 16:12:21 +0000 differ
=== modified file 'src/logic/game.cc'
--- src/logic/game.cc	2012-04-06 19:26:17 +0000
+++ src/logic/game.cc	2012-05-28 16:12:21 +0000
@@ -836,6 +836,15 @@
 		 	(get_gametime(), b.owner().player_number(), b, val));
 }
 
+
+void Game::send_player_evict(Building & building, Worker & worker)
+{
+	send_player_command
+		(*new Cmd_EvictWorker
+			(get_gametime(), building.owner().player_number(), building, worker));
+}
+
+
 /////////////////////// TESTING STUFF
 void Game::send_player_enemyflagaction
 	(Flag  const &       flag,

=== modified file 'src/logic/game.h'
--- src/logic/game.h	2012-02-15 21:25:34 +0000
+++ src/logic/game.h	2012-05-28 16:12:21 +0000
@@ -159,7 +159,8 @@
 	void send_player_set_ware_max_fill
 		(PlayerImmovable &, Ware_Index index, uint32_t);
 	void send_player_change_training_options(TrainingSite &, int32_t, int32_t);
-	void send_player_drop_soldier(Building &, int32_t);
+	void send_player_drop_soldier           (Building &, int32_t);
+	void send_player_evict                  (Widelands::Building &, Worker &);
 	void send_player_change_soldier_capacity(Building &, int32_t);
 	void send_player_enemyflagaction
 		(Flag const &, Player_Number, uint32_t count, uint8_t retreat);

=== modified file 'src/logic/player.cc'
--- src/logic/player.cc	2012-04-06 19:26:17 +0000
+++ src/logic/player.cc	2012-05-28 16:12:21 +0000
@@ -753,6 +753,20 @@
 		ctrl->dropSoldier(soldier);
 }
 
+void Player::evict_worker(PlayerImmovable & player_immovable, Worker & worker)
+{
+	std::cout << "Ejecting " << &worker << std::endl;
+	Game & game = ref_cast<Game, Editor_Game_Base>(egbase());
+	ProductionSite & productionsite = dynamic_cast<ProductionSite & >(player_immovable);
+	//Worker * anotherWorker =
+	const ProductionSite::Working_Position * wp = productionsite.working_positions();
+	Worker * anotherWorker = wp->worker;
+	std::cout << "Should we eject: " << anotherWorker << "?" << std::endl;
+	worker.reset_tasks(game);
+	worker.start_task_leavebuilding(game, true);
+}
+
+
 /*
 ===========
 ===========

=== modified file 'src/logic/player.h'
--- src/logic/player.h	2012-02-28 22:10:23 +0000
+++ src/logic/player.h	2012-05-28 16:12:21 +0000
@@ -474,6 +474,8 @@
 	void drop_soldier(PlayerImmovable &, Soldier &);
 	void change_training_options(TrainingSite &, int32_t atr, int32_t val);
 
+	void evict_worker(PlayerImmovable &, Worker &);
+
 	uint32_t findAttackSoldiers
 		(Flag                   &,
 		 std::vector<Soldier *> * soldiers = 0,

=== modified file 'src/logic/playercommand.cc'
--- src/logic/playercommand.cc	2012-02-15 21:25:34 +0000
+++ src/logic/playercommand.cc	2012-05-28 16:12:21 +0000
@@ -63,6 +63,7 @@
 	PLCMD_SETSTOCKPOLICY,
 	PLCMD_SETWAREMAXFILL,
 	PLCMD_DISMANTLEBUILDING,
+	PLCMD_EVICTWORKER
 };
 
 /*** class PlayerCommand ***/
@@ -110,7 +111,7 @@
 /**
  * Write this player command to a file. Call this from base classes
  */
-#define PLAYER_COMMAND_VERSION 2
+#define PLAYER_COMMAND_VERSION 3
 void PlayerCommand::Write
 	(FileWrite & fw, Editor_Game_Base & egbase, Map_Map_Object_Saver & mos)
 {
@@ -1221,6 +1222,83 @@
 
 }
 
+/**
+ * Cmd_EvictWorker implementation
+ */
+Cmd_EvictWorker::Cmd_EvictWorker(StreamRead & des):
+PlayerCommand (0, des.Unsigned8())
+{
+	serial = des.Unsigned32(); //  Serial of the building
+	worker = des.Unsigned32(); // Serial of the worker
+}
+
+void Cmd_EvictWorker::execute (Game & game)
+{
+	if (upcast(PlayerImmovable, player_imm, game.objects().get_object(serial)))
+		if (upcast(Worker, s, game.objects().get_object(worker)))
+			game.player(sender()).evict_worker(*player_imm, *s);
+}
+
+void Cmd_EvictWorker::serialize (StreamWrite & ser)
+{
+	ser.Unsigned8 (PLCMD_EVICTWORKER);
+	ser.Unsigned8 (sender());
+	ser.Unsigned32(serial);
+	ser.Unsigned32(worker);
+}
+
+#define PLAYER_CMD_EVICTWORKER_VERSION 1
+void Cmd_EvictWorker::Read
+	(FileRead & fr, Editor_Game_Base & egbase, Map_Map_Object_Loader & mol)
+{
+	try {
+		uint16_t const packet_version = fr.Unsigned16();
+		if (packet_version == PLAYER_CMD_EVICTWORKER_VERSION) {
+			PlayerCommand::Read(fr, egbase, mol);
+			uint32_t const site_serial = fr.Unsigned32();
+			try {
+				serial  = mol.get<PlayerImmovable>(site_serial).serial();
+			} catch (_wexception const & e) {
+				throw game_data_error
+					(_("site %u: %s"),    site_serial, e.what());
+			}
+			uint32_t const worker_serial = fr.Unsigned32();
+			try {
+				worker = mol.get<Worker>        (worker_serial).serial();
+			} catch (_wexception const & e) {
+				throw game_data_error
+					(_("soldier %u: %s"), worker_serial, e.what());
+			}
+		} else
+			throw game_data_error
+				(_("unknown/unhandled version %u"), packet_version);
+	} catch (_wexception const & e) {
+		throw game_data_error(_("drop worker: %s"), e.what());
+	}
+}
+
+void Cmd_EvictWorker::Write
+	(FileWrite & fw, Editor_Game_Base & egbase, Map_Map_Object_Saver & mos)
+{
+	// First, write version
+	fw.Unsigned16(PLAYER_CMD_EVICTWORKER_VERSION);
+	// Write base classes
+	PlayerCommand::Write(fw, egbase, mos);
+
+	{ //  site serial
+		Map_Object const & obj = *egbase.objects().get_object(serial);
+		assert(mos.is_object_known(obj));
+		fw.Unsigned32(mos.get_object_file_index(obj));
+	}
+
+	{ //  worker serial
+		Map_Object const & obj = *egbase.objects().get_object(worker);
+		assert(mos.is_object_known(obj));
+		fw.Unsigned32(mos.get_object_file_index(obj));
+	}
+}
+
+
 /*** Cmd_ChangeSoldierCapacity ***/
 
 Cmd_ChangeSoldierCapacity::Cmd_ChangeSoldierCapacity(StreamRead & des)

=== modified file 'src/logic/playercommand.h'
--- src/logic/playercommand.h	2012-02-15 21:25:34 +0000
+++ src/logic/playercommand.h	2012-05-28 16:12:21 +0000
@@ -26,6 +26,7 @@
 #include "path.h"
 #include "trainingsite.h"
 #include "warehouse.h"
+#include "worker.h"
 
 namespace Widelands {
 
@@ -456,6 +457,32 @@
 	Serial soldier;
 };
 
+struct Cmd_EvictWorker : public PlayerCommand {
+	Cmd_EvictWorker () : PlayerCommand() {} //  for savegames
+	Cmd_EvictWorker
+		(int32_t    const t,
+		 int32_t    const p,
+		 Building &       b,
+		 Worker & w)
+		: PlayerCommand(t, p), serial(b.serial()), worker(w.serial())
+	{}
+
+	// Write these commands to a file (for savegames)
+	void Write(FileWrite &, Editor_Game_Base &, Map_Map_Object_Saver  &);
+	void Read (FileRead  &, Editor_Game_Base &, Map_Map_Object_Loader &);
+
+	virtual uint8_t id() const {return QUEUE_CMD_EVICT_WORKER;}
+
+	Cmd_EvictWorker(StreamRead &);
+
+	virtual void execute (Game &);
+	virtual void serialize (StreamWrite &);
+
+private:
+	Serial serial;
+	Serial worker;
+};
+
 struct Cmd_ChangeSoldierCapacity : public PlayerCommand {
 	Cmd_ChangeSoldierCapacity () : PlayerCommand() {} //  for savegames
 	Cmd_ChangeSoldierCapacity

=== modified file 'src/logic/queue_cmd_ids.h'
--- src/logic/queue_cmd_ids.h	2012-02-15 21:25:34 +0000
+++ src/logic/queue_cmd_ids.h	2012-05-28 16:12:21 +0000
@@ -35,46 +35,47 @@
 #define QUEUE_CMD_NONE                   0
 
 /* PLAYER COMMANDS BELOW */
-#define QUEUE_CMD_BUILD                  1
-#define QUEUE_CMD_FLAG                   2
-#define QUEUE_CMD_BUILDROAD              3
-#define QUEUE_CMD_FLAGACTION             4
-#define QUEUE_CMD_STOPBUILDING           5
-#define QUEUE_CMD_ENHANCEBUILDING        6
-#define QUEUE_CMD_BULLDOZE               7
-#define QUEUE_CMD_CHANGETRAININGOPTIONS  8
-#define QUEUE_CMD_DROPSOLDIER            9
-#define QUEUE_CMD_CHANGESOLDIERCAPACITY 10
-#define QUEUE_CMD_ENEMYFLAGACTION       11
-#define QUEUE_CMD_SETWAREPRIORITY       12
+#define QUEUE_CMD_BUILD                      1
+#define QUEUE_CMD_FLAG                       2
+#define QUEUE_CMD_BUILDROAD                  3
+#define QUEUE_CMD_FLAGACTION                 4
+#define QUEUE_CMD_STOPBUILDING               5
+#define QUEUE_CMD_ENHANCEBUILDING            6
+#define QUEUE_CMD_BULLDOZE                   7
+#define QUEUE_CMD_CHANGETRAININGOPTIONS      8
+#define QUEUE_CMD_DROPSOLDIER                9
+#define QUEUE_CMD_CHANGESOLDIERCAPACITY     10
+#define QUEUE_CMD_ENEMYFLAGACTION           11
+#define QUEUE_CMD_SETWAREPRIORITY           12
 #define QUEUE_CMD_SETWARETARGETQUANTITY     13
 #define QUEUE_CMD_RESETWARETARGETQUANTITY   14
 #define QUEUE_CMD_SETWORKERTARGETQUANTITY   15
 #define QUEUE_CMD_RESETWORKERTARGETQUANTITY 16
 
-#define QUEUE_CMD_CHANGEMILITARYCONFIG  17
-#define QUEUE_CMD_SETWAREMAXFILL        18
-
-#define QUEUE_CMD_MESSAGESETSTATUSREAD       21
-#define QUEUE_CMD_MESSAGESETSTATUSARCHIVED   22
-
-#define QUEUE_CMD_SETSTOCKPOLICY        23
-#define QUEUE_CMD_DISMANTLEBUILDING     24
-
-#define QUEUE_CMD_DESTROY_MAPOBJECT    127
-#define QUEUE_CMD_ACT                  128
+#define QUEUE_CMD_CHANGEMILITARYCONFIG      17
+#define QUEUE_CMD_SETWAREMAXFILL            18
+
+#define QUEUE_CMD_MESSAGESETSTATUSREAD      21
+#define QUEUE_CMD_MESSAGESETSTATUSARCHIVED  22
+
+#define QUEUE_CMD_SETSTOCKPOLICY            23
+#define QUEUE_CMD_DISMANTLEBUILDING         24
+#define QUEUE_CMD_EVICT_WORKER              25
+
+#define QUEUE_CMD_DESTROY_MAPOBJECT        127
+#define QUEUE_CMD_ACT                      128
 // 129 was a command related to old events. removed
-#define QUEUE_CMD_INCORPORATE          130
-#define QUEUE_CMD_LUASCRIPT            131
-#define QUEUE_CMD_LUACOROUTINE         132
-#define QUEUE_CMD_CALCULATE_STATISTICS 133
-
-#define QUEUE_CMD_CALL_ECONOMY_BALANCE 200
-#define QUEUE_CMD_EXPIREMESSAGE             201
-
-#define QUEUE_CMD_NETCHECKSYNC         250
-#define QUEUE_CMD_REPLAYSYNCWRITE      251
-#define QUEUE_CMD_REPLAYSYNCREAD       252
-#define QUEUE_CMD_REPLAYEND            253
+#define QUEUE_CMD_INCORPORATE              130
+#define QUEUE_CMD_LUASCRIPT                131
+#define QUEUE_CMD_LUACOROUTINE             132
+#define QUEUE_CMD_CALCULATE_STATISTICS     133
+
+#define QUEUE_CMD_CALL_ECONOMY_BALANCE     200
+#define QUEUE_CMD_EXPIREMESSAGE            201
+
+#define QUEUE_CMD_NETCHECKSYNC             250
+#define QUEUE_CMD_REPLAYSYNCWRITE          251
+#define QUEUE_CMD_REPLAYSYNCREAD           252
+#define QUEUE_CMD_REPLAYEND                253
 
 #endif

=== modified file 'src/wui/buildingwindow.h'
--- src/wui/buildingwindow.h	2012-02-15 21:25:34 +0000
+++ src/wui/buildingwindow.h	2012-05-28 16:12:21 +0000
@@ -73,10 +73,10 @@
 	virtual void create_capsbuttons(UI::Box * buttons);
 
 	UI::Window * & m_registry;
+	Widelands::Building & m_building;
 
 private:
 	UI::UniqueWindow::Registry m_helpwindow_registry;
-	Widelands::Building & m_building;
 
 	UI::Tab_Panel * m_tabs;
 

=== modified file 'src/wui/productionsitewindow.cc'
--- src/wui/productionsitewindow.cc	2012-02-15 21:25:34 +0000
+++ src/wui/productionsitewindow.cc	2012-05-28 16:12:21 +0000
@@ -89,6 +89,37 @@
 	}
 }
 
+void ProductionSite_Window::create_capsbuttons(UI::Box * capsbuttons)
+{
+	Building_Window::create_capsbuttons(capsbuttons);
+	UI::Button * aButton = new UI::Button
+					(capsbuttons, "evict", 0, 0, 34, 34,
+	                                      g_gr->get_picture(PicMod_UI, "pics/but4.png"),
+	                                      g_gr->get_picture(PicMod_UI, "pics/menu_evict.png"),
+	                                      _("Evict"));
+	aButton->sigclicked.connect((boost::bind(&ProductionSite_Window::act_evict, boost::ref(*this))));
+	capsbuttons->add(aButton, UI::Box::AlignCenter);
+}
+
+/**
+ * Callback to evict a worker
+ */
+void ProductionSite_Window::act_evict()
+{
+	if (m_worker_table->has_selection()) {
+		Widelands::Worker * worker =
+		        productionsite().working_positions()[m_worker_table->get_selected()].worker;
+		if (worker != NULL) {
+			igbase().game().send_player_evict(Building_Window::m_building, boost::ref(*worker));
+			std::cout << "We have " << &worker << " here, he was in slot " << m_worker_table->get_selected() << std::endl;
+		} else {
+			std::cout << "unable to find worker number: " << m_worker_table->get_selected() << std::endl;
+		}
+	} else {
+		std::cout << "You need to select one worker" << std::endl;
+	}
+}
+
 void ProductionSite_Window::think()
 {
 	Building_Window::think();

=== modified file 'src/wui/productionsitewindow.h'
--- src/wui/productionsitewindow.h	2012-02-15 21:25:34 +0000
+++ src/wui/productionsitewindow.h	2012-05-28 16:12:21 +0000
@@ -36,7 +36,8 @@
 
 protected:
 	virtual void think();
-
+	virtual void create_capsbuttons(UI::Box * buttons);
+	virtual void act_evict();
 private:
 	UI::Table<uintptr_t> * m_worker_table;
 };


Follow ups