2012-11-19 18:17:05 -07:00
# include "Core.h"
# include "Console.h"
# include "Export.h"
# include "PluginManager.h"
# include "DataDefs.h"
2013-03-16 20:43:57 -06:00
# include "modules/EventManager.h"
2012-11-19 18:17:05 -07:00
# include "modules/World.h"
2013-03-16 20:43:57 -06:00
2012-11-19 18:17:05 -07:00
# include "df/global_objects.h"
# include <vector>
2013-03-16 20:43:57 -06:00
2012-11-19 18:17:05 -07:00
using namespace std ;
using namespace DFHack ;
DFHACK_PLUGIN ( " workNow " ) ;
2014-12-06 16:47:35 -07:00
REQUIRE_GLOBAL ( process_jobs ) ;
REQUIRE_GLOBAL ( process_dig ) ;
2012-11-19 18:17:05 -07:00
2013-03-16 20:43:57 -06:00
static int mode = 0 ;
2012-11-19 18:17:05 -07:00
2012-12-08 10:50:33 -07:00
DFhackCExport command_result workNow ( color_ostream & out , vector < string > & parameters ) ;
2013-03-16 20:43:57 -06:00
void jobCompletedHandler ( color_ostream & out , void * ptr ) ;
EventManager : : EventHandler handler ( jobCompletedHandler , 1 ) ;
2012-12-08 10:50:33 -07:00
DFhackCExport command_result plugin_init ( color_ostream & out , std : : vector < PluginCommand > & commands ) {
2014-08-11 14:07:52 -06:00
if ( ! process_jobs | | ! process_dig )
return CR_FAILURE ;
2013-05-25 10:23:38 -06:00
commands . push_back ( PluginCommand ( " workNow " , " makes dwarves look for jobs whever they finish one, or every time you pause " , workNow , false , " When workNow is active, every time the game pauses, DF will make dwarves perform any appropriate available jobs. This includes when you one step through the game using the pause menu. When workNow is in mode 2, it will make dwarves look for jobs every time a job completes. \n "
2013-03-16 20:43:57 -06:00
" workNow \n "
" print workNow status \n "
2012-12-08 10:50:33 -07:00
" workNow 0 \n "
2013-03-16 20:43:57 -06:00
" deactivate workNow \n "
" workNow 1 \n "
" activate workNow (look for jobs on pause, and only then) \n "
" workNow 2 \n "
" make dwarves look for jobs whenever a job completes \n "
) ) ;
2015-02-14 20:53:06 -07:00
2012-12-08 10:50:33 -07:00
return CR_OK ;
}
2012-11-19 18:17:05 -07:00
DFhackCExport command_result plugin_shutdown ( color_ostream & out ) {
2013-03-16 20:43:57 -06:00
mode = 0 ;
2012-11-19 18:17:05 -07:00
return CR_OK ;
}
2012-12-08 10:50:33 -07:00
DFhackCExport command_result plugin_onstatechange ( color_ostream & out , state_change_event e ) {
2013-03-16 20:43:57 -06:00
if ( ! mode )
2012-12-08 10:50:33 -07:00
return CR_OK ;
if ( e = = DFHack : : SC_WORLD_UNLOADED ) {
2013-03-16 20:43:57 -06:00
mode = 0 ;
2012-12-08 10:50:33 -07:00
return CR_OK ;
}
if ( e ! = DFHack : : SC_PAUSED )
2012-11-19 18:17:05 -07:00
return CR_OK ;
2015-02-14 20:53:06 -07:00
2014-08-11 14:07:52 -06:00
* process_jobs = true ;
* process_dig = true ;
2015-02-14 20:53:06 -07:00
2012-11-19 18:17:05 -07:00
return CR_OK ;
}
DFhackCExport command_result workNow ( color_ostream & out , vector < string > & parameters ) {
2012-12-08 10:50:33 -07:00
if ( parameters . size ( ) = = 0 ) {
2013-03-16 20:43:57 -06:00
out . print ( " workNow status = %d \n " , mode ) ;
2012-12-08 10:50:33 -07:00
return CR_OK ;
}
if ( parameters . size ( ) > 1 ) {
return CR_WRONG_USAGE ;
}
int32_t a = atoi ( parameters [ 0 ] . c_str ( ) ) ;
2015-02-14 20:53:06 -07:00
2013-03-16 20:43:57 -06:00
if ( a < 0 | | a > 2 )
2012-12-08 10:50:33 -07:00
return CR_WRONG_USAGE ;
2013-03-16 20:43:57 -06:00
if ( a = = 2 & & mode ! = 2 ) {
EventManager : : registerListener ( EventManager : : EventType : : JOB_COMPLETED , handler , plugin_self ) ;
} else if ( mode = = 2 & & a ! = 2 ) {
EventManager : : unregister ( EventManager : : EventType : : JOB_COMPLETED , handler , plugin_self ) ;
}
mode = a ;
out . print ( " workNow status = %d \n " , mode ) ;
2012-11-19 18:17:05 -07:00
return CR_OK ;
}
2013-03-16 20:43:57 -06:00
void jobCompletedHandler ( color_ostream & out , void * ptr ) {
if ( mode < 2 )
return ;
2015-02-14 20:53:06 -07:00
2014-08-11 14:07:52 -06:00
* process_jobs = true ;
* process_dig = true ;
2013-03-16 20:43:57 -06:00
}