2012-03-16 22:45:52 -06:00
|
|
|
// This is a generic plugin that does nothing useful apart from acting as an example... of a plugin that does nothing :D
|
|
|
|
|
|
|
|
// some headers required for a plugin. Nothing special, just the basics.
|
|
|
|
#include "Core.h"
|
|
|
|
#include <Console.h>
|
|
|
|
#include <Export.h>
|
|
|
|
#include <PluginManager.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
// DF data structure definition headers
|
|
|
|
#include "DataDefs.h"
|
|
|
|
#include <df/ui.h>
|
|
|
|
#include <df/world.h>
|
|
|
|
#include <df/unit.h>
|
|
|
|
#include <df/unit_soul.h>
|
|
|
|
#include <df/unit_labor.h>
|
|
|
|
#include <df/unit_skill.h>
|
|
|
|
#include <df/job.h>
|
2012-03-22 01:33:18 -06:00
|
|
|
#include <df/building.h>
|
|
|
|
#include <df/workshop_type.h>
|
2012-03-16 22:45:52 -06:00
|
|
|
#include <df/unit_misc_trait.h>
|
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
using std::string;
|
|
|
|
using std::endl;
|
2012-03-16 22:45:52 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
using df::global::ui;
|
|
|
|
using df::global::world;
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))
|
2012-03-16 22:45:52 -06:00
|
|
|
|
2012-04-04 00:26:15 -06:00
|
|
|
/*
|
|
|
|
* Autolabor module for dfhack
|
|
|
|
*
|
|
|
|
* The idea behind this module is to constantly adjust labors so that the right dwarves
|
|
|
|
* are assigned to new tasks. The key is that, for almost all labors, once a dwarf begins
|
|
|
|
* a job it will finish that job even if the associated labor is removed. Thus the
|
|
|
|
* strategy is to frequently decide, for each labor, which dwarves should possibly take
|
|
|
|
* a new job for that labor if it comes in and which shouldn't, and then set the labors
|
|
|
|
* appropriately. The updating should happen as often as can be reasonably done without
|
|
|
|
* causing lag.
|
|
|
|
*
|
|
|
|
* The obvious thing to do is to just set each labor on a single idle dwarf who is best
|
|
|
|
* suited to doing new jobs of that labor. This works in a way, but it leads to a lot
|
|
|
|
* of idle dwarves since only one dwarf will be dispatched for each labor in an update
|
|
|
|
* cycle, and dwarves that finish tasks will wait for the next update before being
|
|
|
|
* dispatched. An improvement is to also set some labors on dwarves that are currently
|
|
|
|
* doing a job, so that they will immediately take a new job when they finish. The
|
|
|
|
* details of which dwarves should have labors set is mostly a heuristic.
|
|
|
|
*
|
|
|
|
* A complication to the above simple scheme is labors that have associated equipment.
|
|
|
|
* Enabling/disabling these labors causes dwarves to change equipment, and disabling
|
|
|
|
* them in the middle of a job may cause the job to be abandoned. Those labors
|
|
|
|
* (mining, hunting, and woodcutting) need to be handled carefully to minimize churn.
|
|
|
|
*/
|
|
|
|
|
2012-03-31 19:46:17 -06:00
|
|
|
static int enable_autolabor = 0;
|
2012-03-16 22:45:52 -06:00
|
|
|
|
2012-04-02 21:55:02 -06:00
|
|
|
static bool print_debug = 0;
|
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
static std::vector<int> state_count(5);
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
// Here go all the command declarations...
|
|
|
|
// mostly to allow having the mandatory stuff on top of the file and commands on the bottom
|
|
|
|
command_result autolabor (color_ostream &out, std::vector <std::string> & parameters);
|
|
|
|
|
|
|
|
// A plugin must be able to return its name and version.
|
|
|
|
// The name string provided must correspond to the filename - autolabor.plug.so or autolabor.plug.dll in this case
|
|
|
|
DFHACK_PLUGIN("autolabor");
|
|
|
|
|
|
|
|
enum labor_mode {
|
2012-04-02 21:55:02 -06:00
|
|
|
DISABLE,
|
2012-03-16 22:45:52 -06:00
|
|
|
HAULERS,
|
2012-04-02 23:12:06 -06:00
|
|
|
AUTOMATIC,
|
2012-03-16 22:45:52 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
enum dwarf_state {
|
|
|
|
// Ready for a new task
|
|
|
|
IDLE,
|
|
|
|
|
|
|
|
// Busy with a useful task
|
|
|
|
BUSY,
|
|
|
|
|
|
|
|
// In the military, can't work
|
|
|
|
MILITARY,
|
|
|
|
|
|
|
|
// Child or noble, can't work
|
|
|
|
CHILD,
|
|
|
|
|
|
|
|
// Doing something that precludes working, may be busy for a while
|
|
|
|
OTHER
|
|
|
|
};
|
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
const int NUM_STATE = 5;
|
|
|
|
|
2012-04-02 21:55:02 -06:00
|
|
|
static const char *state_names[] = {
|
|
|
|
"IDLE",
|
|
|
|
"BUSY",
|
|
|
|
"MILITARY",
|
|
|
|
"CHILD",
|
|
|
|
"OTHER",
|
|
|
|
};
|
|
|
|
|
2012-03-16 22:45:52 -06:00
|
|
|
static const dwarf_state dwarf_states[] = {
|
|
|
|
BUSY /* CarveFortification */,
|
|
|
|
BUSY /* DetailWall */,
|
|
|
|
BUSY /* DetailFloor */,
|
|
|
|
BUSY /* Dig */,
|
|
|
|
BUSY /* CarveUpwardStaircase */,
|
|
|
|
BUSY /* CarveDownwardStaircase */,
|
|
|
|
BUSY /* CarveUpDownStaircase */,
|
|
|
|
BUSY /* CarveRamp */,
|
|
|
|
BUSY /* DigChannel */,
|
|
|
|
BUSY /* FellTree */,
|
|
|
|
BUSY /* GatherPlants */,
|
|
|
|
BUSY /* RemoveConstruction */,
|
|
|
|
BUSY /* CollectWebs */,
|
|
|
|
BUSY /* BringItemToDepot */,
|
|
|
|
BUSY /* BringItemToShop */,
|
|
|
|
OTHER /* Eat */,
|
|
|
|
OTHER /* GetProvisions */,
|
|
|
|
OTHER /* Drink */,
|
|
|
|
OTHER /* Drink2 */,
|
|
|
|
OTHER /* FillWaterskin */,
|
|
|
|
OTHER /* FillWaterskin2 */,
|
|
|
|
OTHER /* Sleep */,
|
|
|
|
BUSY /* CollectSand */,
|
|
|
|
BUSY /* Fish */,
|
|
|
|
BUSY /* Hunt */,
|
|
|
|
OTHER /* HuntVermin */,
|
|
|
|
BUSY /* Kidnap */,
|
|
|
|
BUSY /* BeatCriminal */,
|
|
|
|
BUSY /* StartingFistFight */,
|
|
|
|
BUSY /* CollectTaxes */,
|
|
|
|
BUSY /* GuardTaxCollector */,
|
|
|
|
BUSY /* CatchLiveLandAnimal */,
|
|
|
|
BUSY /* CatchLiveFish */,
|
|
|
|
BUSY /* ReturnKill */,
|
|
|
|
BUSY /* CheckChest */,
|
|
|
|
BUSY /* StoreOwnedItem */,
|
|
|
|
BUSY /* PlaceItemInTomb */,
|
|
|
|
BUSY /* StoreItemInStockpile */,
|
|
|
|
BUSY /* StoreItemInBag */,
|
|
|
|
BUSY /* StoreItemInHospital */,
|
|
|
|
BUSY /* StoreItemInChest */,
|
|
|
|
BUSY /* StoreItemInCabinet */,
|
|
|
|
BUSY /* StoreWeapon */,
|
|
|
|
BUSY /* StoreArmor */,
|
|
|
|
BUSY /* StoreItemInBarrel */,
|
|
|
|
BUSY /* StoreItemInBin */,
|
|
|
|
BUSY /* SeekArtifact */,
|
|
|
|
BUSY /* SeekInfant */,
|
|
|
|
OTHER /* AttendParty */,
|
|
|
|
OTHER /* GoShopping */,
|
|
|
|
OTHER /* GoShopping2 */,
|
|
|
|
BUSY /* Clean */,
|
|
|
|
OTHER /* Rest */,
|
|
|
|
BUSY /* PickupEquipment */,
|
|
|
|
BUSY /* DumpItem */,
|
|
|
|
OTHER /* StrangeMoodCrafter */,
|
|
|
|
OTHER /* StrangeMoodJeweller */,
|
|
|
|
OTHER /* StrangeMoodForge */,
|
|
|
|
OTHER /* StrangeMoodMagmaForge */,
|
|
|
|
OTHER /* StrangeMoodBrooding */,
|
|
|
|
OTHER /* StrangeMoodFell */,
|
|
|
|
OTHER /* StrangeMoodCarpenter */,
|
|
|
|
OTHER /* StrangeMoodMason */,
|
|
|
|
OTHER /* StrangeMoodBowyer */,
|
|
|
|
OTHER /* StrangeMoodTanner */,
|
|
|
|
OTHER /* StrangeMoodWeaver */,
|
|
|
|
OTHER /* StrangeMoodGlassmaker */,
|
|
|
|
OTHER /* StrangeMoodMechanics */,
|
|
|
|
BUSY /* ConstructBuilding */,
|
|
|
|
BUSY /* ConstructDoor */,
|
|
|
|
BUSY /* ConstructFloodgate */,
|
|
|
|
BUSY /* ConstructBed */,
|
|
|
|
BUSY /* ConstructThrone */,
|
|
|
|
BUSY /* ConstructCoffin */,
|
|
|
|
BUSY /* ConstructTable */,
|
|
|
|
BUSY /* ConstructChest */,
|
|
|
|
BUSY /* ConstructBin */,
|
|
|
|
BUSY /* ConstructArmorStand */,
|
|
|
|
BUSY /* ConstructWeaponRack */,
|
|
|
|
BUSY /* ConstructCabinet */,
|
|
|
|
BUSY /* ConstructStatue */,
|
|
|
|
BUSY /* ConstructBlocks */,
|
|
|
|
BUSY /* MakeRawGlass */,
|
|
|
|
BUSY /* MakeCrafts */,
|
|
|
|
BUSY /* MintCoins */,
|
|
|
|
BUSY /* CutGems */,
|
|
|
|
BUSY /* CutGlass */,
|
|
|
|
BUSY /* EncrustWithGems */,
|
|
|
|
BUSY /* EncrustWithGlass */,
|
|
|
|
BUSY /* DestroyBuilding */,
|
|
|
|
BUSY /* SmeltOre */,
|
|
|
|
BUSY /* MeltMetalObject */,
|
|
|
|
BUSY /* ExtractMetalStrands */,
|
|
|
|
BUSY /* PlantSeeds */,
|
|
|
|
BUSY /* HarvestPlants */,
|
|
|
|
BUSY /* TrainHuntingAnimal */,
|
|
|
|
BUSY /* TrainWarAnimal */,
|
|
|
|
BUSY /* MakeWeapon */,
|
|
|
|
BUSY /* ForgeAnvil */,
|
|
|
|
BUSY /* ConstructCatapultParts */,
|
|
|
|
BUSY /* ConstructBallistaParts */,
|
|
|
|
BUSY /* MakeArmor */,
|
|
|
|
BUSY /* MakeHelm */,
|
|
|
|
BUSY /* MakePants */,
|
|
|
|
BUSY /* StudWith */,
|
|
|
|
BUSY /* ButcherAnimal */,
|
|
|
|
BUSY /* PrepareRawFish */,
|
|
|
|
BUSY /* MillPlants */,
|
|
|
|
BUSY /* BaitTrap */,
|
|
|
|
BUSY /* MilkCreature */,
|
|
|
|
BUSY /* MakeCheese */,
|
|
|
|
BUSY /* ProcessPlants */,
|
|
|
|
BUSY /* ProcessPlantsBag */,
|
|
|
|
BUSY /* ProcessPlantsVial */,
|
|
|
|
BUSY /* ProcessPlantsBarrel */,
|
|
|
|
BUSY /* PrepareMeal */,
|
|
|
|
BUSY /* WeaveCloth */,
|
|
|
|
BUSY /* MakeGloves */,
|
|
|
|
BUSY /* MakeShoes */,
|
|
|
|
BUSY /* MakeShield */,
|
|
|
|
BUSY /* MakeCage */,
|
|
|
|
BUSY /* MakeChain */,
|
|
|
|
BUSY /* MakeFlask */,
|
|
|
|
BUSY /* MakeGoblet */,
|
|
|
|
BUSY /* MakeInstrument */,
|
|
|
|
BUSY /* MakeToy */,
|
|
|
|
BUSY /* MakeAnimalTrap */,
|
|
|
|
BUSY /* MakeBarrel */,
|
|
|
|
BUSY /* MakeBucket */,
|
|
|
|
BUSY /* MakeWindow */,
|
|
|
|
BUSY /* MakeTotem */,
|
|
|
|
BUSY /* MakeAmmo */,
|
|
|
|
BUSY /* DecorateWith */,
|
|
|
|
BUSY /* MakeBackpack */,
|
|
|
|
BUSY /* MakeQuiver */,
|
|
|
|
BUSY /* MakeBallistaArrowHead */,
|
|
|
|
BUSY /* AssembleSiegeAmmo */,
|
|
|
|
BUSY /* LoadCatapult */,
|
|
|
|
BUSY /* LoadBallista */,
|
|
|
|
BUSY /* FireCatapult */,
|
|
|
|
BUSY /* FireBallista */,
|
|
|
|
BUSY /* ConstructMechanisms */,
|
|
|
|
BUSY /* MakeTrapComponent */,
|
|
|
|
BUSY /* LoadCageTrap */,
|
|
|
|
BUSY /* LoadStoneTrap */,
|
|
|
|
BUSY /* LoadWeaponTrap */,
|
|
|
|
BUSY /* CleanTrap */,
|
|
|
|
BUSY /* CastSpell */,
|
|
|
|
BUSY /* LinkBuildingToTrigger */,
|
|
|
|
BUSY /* PullLever */,
|
|
|
|
BUSY /* BrewDrink */,
|
|
|
|
BUSY /* ExtractFromPlants */,
|
|
|
|
BUSY /* ExtractFromRawFish */,
|
|
|
|
BUSY /* ExtractFromLandAnimal */,
|
|
|
|
BUSY /* TameVermin */,
|
|
|
|
BUSY /* TameAnimal */,
|
|
|
|
BUSY /* ChainAnimal */,
|
|
|
|
BUSY /* UnchainAnimal */,
|
|
|
|
BUSY /* UnchainPet */,
|
|
|
|
BUSY /* ReleaseLargeCreature */,
|
|
|
|
BUSY /* ReleasePet */,
|
|
|
|
BUSY /* ReleaseSmallCreature */,
|
|
|
|
BUSY /* HandleSmallCreature */,
|
|
|
|
BUSY /* HandleLargeCreature */,
|
|
|
|
BUSY /* CageLargeCreature */,
|
|
|
|
BUSY /* CageSmallCreature */,
|
|
|
|
BUSY /* RecoverWounded */,
|
|
|
|
BUSY /* DiagnosePatient */,
|
|
|
|
BUSY /* ImmobilizeBreak */,
|
|
|
|
BUSY /* DressWound */,
|
|
|
|
BUSY /* CleanPatient */,
|
|
|
|
BUSY /* Surgery */,
|
|
|
|
BUSY /* Suture */,
|
|
|
|
BUSY /* SetBone */,
|
|
|
|
BUSY /* PlaceInTraction */,
|
|
|
|
BUSY /* DrainAquarium */,
|
|
|
|
BUSY /* FillAquarium */,
|
|
|
|
BUSY /* FillPond */,
|
|
|
|
BUSY /* GiveWater */,
|
|
|
|
BUSY /* GiveFood */,
|
|
|
|
BUSY /* GiveWater2 */,
|
|
|
|
BUSY /* GiveFood2 */,
|
|
|
|
BUSY /* RecoverPet */,
|
|
|
|
BUSY /* PitLargeAnimal */,
|
|
|
|
BUSY /* PitSmallAnimal */,
|
|
|
|
BUSY /* SlaughterAnimal */,
|
|
|
|
BUSY /* MakeCharcoal */,
|
|
|
|
BUSY /* MakeAsh */,
|
|
|
|
BUSY /* MakeLye */,
|
|
|
|
BUSY /* MakePotashFromLye */,
|
|
|
|
BUSY /* FertilizeField */,
|
|
|
|
BUSY /* MakePotashFromAsh */,
|
|
|
|
BUSY /* DyeThread */,
|
|
|
|
BUSY /* DyeCloth */,
|
|
|
|
BUSY /* SewImage */,
|
|
|
|
BUSY /* MakePipeSection */,
|
|
|
|
BUSY /* OperatePump */,
|
|
|
|
OTHER /* ManageWorkOrders */,
|
|
|
|
OTHER /* UpdateStockpileRecords */,
|
|
|
|
OTHER /* TradeAtDepot */,
|
|
|
|
BUSY /* ConstructHatchCover */,
|
|
|
|
BUSY /* ConstructGrate */,
|
|
|
|
BUSY /* RemoveStairs */,
|
|
|
|
BUSY /* ConstructQuern */,
|
|
|
|
BUSY /* ConstructMillstone */,
|
|
|
|
BUSY /* ConstructSplint */,
|
|
|
|
BUSY /* ConstructCrutch */,
|
|
|
|
BUSY /* ConstructTractionBench */,
|
|
|
|
BUSY /* CleanSelf */,
|
|
|
|
BUSY /* BringCrutch */,
|
|
|
|
BUSY /* ApplyCast */,
|
|
|
|
BUSY /* CustomReaction */,
|
|
|
|
BUSY /* ConstructSlab */,
|
|
|
|
BUSY /* EngraveSlab */,
|
|
|
|
BUSY /* ShearCreature */,
|
|
|
|
BUSY /* SpinThread */,
|
|
|
|
BUSY /* PenLargeAnimal */,
|
|
|
|
BUSY /* PenSmallAnimal */,
|
|
|
|
BUSY /* MakeTool */,
|
|
|
|
BUSY /* CollectClay */,
|
|
|
|
BUSY /* InstallColonyInHive */,
|
|
|
|
BUSY /* CollectHiveProducts */,
|
|
|
|
OTHER /* CauseTrouble */,
|
|
|
|
OTHER /* DrinkBlood */,
|
|
|
|
OTHER /* ReportCrime */,
|
|
|
|
OTHER /* ExecuteCriminal */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct labor_info
|
|
|
|
{
|
|
|
|
labor_mode mode;
|
|
|
|
bool is_exclusive;
|
|
|
|
int minimum_dwarfs;
|
2012-04-02 23:12:06 -06:00
|
|
|
int maximum_dwarfs;
|
|
|
|
int active_dwarfs;
|
2012-03-16 22:45:52 -06:00
|
|
|
};
|
|
|
|
|
2012-03-28 09:25:36 -06:00
|
|
|
static struct labor_info* labor_infos;
|
|
|
|
|
|
|
|
static const struct labor_info default_labor_infos[] = {
|
2012-04-02 23:12:06 -06:00
|
|
|
/* MINE */ {AUTOMATIC, true, 2, 200, 0},
|
|
|
|
/* HAUL_STONE */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* HAUL_WOOD */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* HAUL_BODY */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* HAUL_FOOD */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* HAUL_REFUSE */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* HAUL_ITEM */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* HAUL_FURNITURE */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* HAUL_ANIMAL */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* CLEAN */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* CUTWOOD */ {AUTOMATIC, true, 1, 200, 0},
|
|
|
|
/* CARPENTER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* DETAIL */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* MASON */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* ARCHITECT */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* ANIMALTRAIN */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* ANIMALCARE */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* DIAGNOSE */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* SURGERY */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* BONE_SETTING */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* SUTURING */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* DRESSING_WOUNDS */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* FEED_WATER_CIVILIANS */ {AUTOMATIC, false, 200, 200, 0},
|
|
|
|
/* RECOVER_WOUNDED */ {HAULERS, false, 1, 200, 0},
|
|
|
|
/* BUTCHER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* TRAPPER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* DISSECT_VERMIN */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* LEATHER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* TANNER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* BREWER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* ALCHEMIST */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* SOAP_MAKER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* WEAVER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* CLOTHESMAKER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* MILLER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* PROCESS_PLANT */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* MAKE_CHEESE */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* MILK */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* COOK */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* PLANT */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* HERBALIST */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* FISH */ {AUTOMATIC, false, 1, 1, 0},
|
|
|
|
/* CLEAN_FISH */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* DISSECT_FISH */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* HUNT */ {AUTOMATIC, true, 1, 1, 0},
|
|
|
|
/* SMELT */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* FORGE_WEAPON */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* FORGE_ARMOR */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* FORGE_FURNITURE */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* METAL_CRAFT */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* CUT_GEM */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* ENCRUST_GEM */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* WOOD_CRAFT */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* STONE_CRAFT */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* BONE_CARVE */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* GLASSMAKER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* EXTRACT_STRAND */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* SIEGECRAFT */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* SIEGEOPERATE */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* BOWYER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* MECHANIC */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* POTASH_MAKING */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* LYE_MAKING */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* DYER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* BURN_WOOD */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* OPERATE_PUMP */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* SHEARER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* SPINNER */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* POTTERY */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* GLAZING */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* PRESSING */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* BEEKEEPING */ {AUTOMATIC, false, 1, 200, 0},
|
|
|
|
/* WAX_WORKING */ {AUTOMATIC, false, 1, 200, 0},
|
2012-03-16 22:45:52 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static const df::job_skill noble_skills[] = {
|
|
|
|
df::enums::job_skill::APPRAISAL,
|
|
|
|
df::enums::job_skill::ORGANIZATION,
|
|
|
|
df::enums::job_skill::RECORD_KEEPING,
|
|
|
|
};
|
|
|
|
|
2012-03-29 12:00:26 -06:00
|
|
|
struct dwarf_info_t
|
2012-03-16 22:45:52 -06:00
|
|
|
{
|
|
|
|
int highest_skill;
|
|
|
|
int total_skill;
|
|
|
|
bool is_best_noble;
|
|
|
|
int mastery_penalty;
|
|
|
|
int assigned_jobs;
|
|
|
|
dwarf_state state;
|
|
|
|
bool has_exclusive_labor;
|
|
|
|
};
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
|
|
|
{
|
2012-03-28 18:49:44 -06:00
|
|
|
// initialize labor infos table from default table
|
|
|
|
if(ARRAY_COUNT(default_labor_infos) != ENUM_LAST_ITEM(unit_labor) + 1)
|
|
|
|
return CR_FAILURE;
|
|
|
|
labor_infos = new struct labor_info[ARRAY_COUNT(default_labor_infos)];
|
|
|
|
for (int i = 0; i < ARRAY_COUNT(default_labor_infos); i++) {
|
|
|
|
labor_infos[i] = default_labor_infos[i];
|
|
|
|
}
|
|
|
|
// Fill the command list with your commands.
|
2012-03-22 01:33:18 -06:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"autolabor", "Automatically manage dwarf labors.",
|
|
|
|
autolabor, false, /* true means that the command can't be used from non-interactive user interface */
|
|
|
|
// Extended help string. Used by CR_WRONG_USAGE and the help command:
|
|
|
|
" autolabor enable\n"
|
|
|
|
" autolabor disable\n"
|
|
|
|
" Enables or disables the plugin.\n"
|
2012-04-02 23:12:06 -06:00
|
|
|
" autolabor <labor> <minimum> [<maximum>]\n"
|
|
|
|
" Set number of dwarves assigned to a labor.\n"
|
|
|
|
" autolabor <labor> haulers\n"
|
|
|
|
" Set a labor to be handled by hauler dwarves.\n"
|
|
|
|
" autolabor <labor> disable\n"
|
|
|
|
" Turn off autolabor for a specific labor.\n"
|
|
|
|
" autolabor list\n"
|
|
|
|
" List current status of all labors.\n"
|
2012-03-22 01:33:18 -06:00
|
|
|
"Function:\n"
|
|
|
|
" When enabled, autolabor periodically checks your dwarves and enables or\n"
|
|
|
|
" disables labors. It tries to keep as many dwarves as possible busy but\n"
|
|
|
|
" also tries to have dwarves specialize in specific skills.\n"
|
|
|
|
" Warning: autolabor will override any manual changes you make to labors\n"
|
|
|
|
" while it is enabled.\n"
|
2012-04-02 23:12:06 -06:00
|
|
|
"Examples:\n"
|
|
|
|
" autolabor MINE 2\n"
|
|
|
|
" Keep at least 2 dwarves with mining enabled.\n"
|
|
|
|
" autolabor CUT_GEM 1 1\n"
|
|
|
|
" Keep exactly 1 dwarf with gemcutting enabled.\n"
|
|
|
|
" autolabor FEED_WATER_CIVILIANS haulers\n"
|
|
|
|
" Have haulers feed and water wounded dwarves.\n"
|
|
|
|
" autolabor CUTWOOD disable\n"
|
|
|
|
" Turn off autolabor for wood cutting.\n"
|
2012-03-22 01:33:18 -06:00
|
|
|
));
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
|
|
|
{
|
2012-03-28 09:25:36 -06:00
|
|
|
// release the labor info table;
|
|
|
|
delete [] labor_infos;
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-29 12:00:26 -06:00
|
|
|
// sorting objects
|
|
|
|
struct dwarfinfo_sorter
|
|
|
|
{
|
|
|
|
dwarfinfo_sorter(std::vector <dwarf_info_t> & info):dwarf_info(info){};
|
|
|
|
bool operator() (int i,int j)
|
|
|
|
{
|
|
|
|
if (dwarf_info[i].state == IDLE && dwarf_info[j].state != IDLE)
|
|
|
|
return true;
|
|
|
|
if (dwarf_info[i].state != IDLE && dwarf_info[j].state == IDLE)
|
|
|
|
return false;
|
|
|
|
return dwarf_info[i].mastery_penalty > dwarf_info[j].mastery_penalty;
|
|
|
|
};
|
|
|
|
std::vector <dwarf_info_t> & dwarf_info;
|
|
|
|
};
|
|
|
|
struct laborinfo_sorter
|
|
|
|
{
|
|
|
|
bool operator() (int i,int j)
|
|
|
|
{
|
|
|
|
return labor_infos[i].mode < labor_infos[j].mode;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct values_sorter
|
|
|
|
{
|
|
|
|
values_sorter(std::vector <int> & values):values(values){};
|
|
|
|
bool operator() (int i,int j)
|
|
|
|
{
|
|
|
|
return values[i] > values[j];
|
|
|
|
};
|
|
|
|
std::vector<int> & values;
|
|
|
|
};
|
|
|
|
|
2012-03-16 22:45:52 -06:00
|
|
|
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
|
|
|
|
{
|
|
|
|
static int step_count = 0;
|
2012-03-31 19:46:17 -06:00
|
|
|
// check run conditions
|
|
|
|
if(!world->map.block_index || !enable_autolabor)
|
|
|
|
{
|
|
|
|
// give up if we shouldn't be running'
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
if (++step_count < 60)
|
|
|
|
return CR_OK;
|
|
|
|
step_count = 0;
|
|
|
|
|
|
|
|
uint32_t race = ui->race_id;
|
|
|
|
uint32_t civ = ui->civ_id;
|
|
|
|
|
|
|
|
std::vector<df::unit *> dwarfs;
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
bool has_butchers = false;
|
|
|
|
bool has_fishery = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < world->buildings.all.size(); ++i)
|
|
|
|
{
|
|
|
|
df::building *build = world->buildings.all[i];
|
|
|
|
auto type = build->getType();
|
|
|
|
if (df::enums::building_type::Workshop == type)
|
|
|
|
{
|
|
|
|
auto subType = build->getSubtype();
|
|
|
|
if (df::enums::workshop_type::Butchers == subType)
|
|
|
|
has_butchers = true;
|
|
|
|
if (df::enums::workshop_type::Fishery == subType)
|
|
|
|
has_fishery = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-16 22:45:52 -06:00
|
|
|
for (int i = 0; i < world->units.all.size(); ++i)
|
|
|
|
{
|
|
|
|
df::unit* cre = world->units.all[i];
|
2012-03-30 11:01:33 -06:00
|
|
|
if (cre->race == race && cre->civ_id == civ && !cre->flags1.bits.marauder && !cre->flags1.bits.diplomat && !cre->flags1.bits.merchant &&
|
|
|
|
!cre->flags1.bits.dead && !cre->flags1.bits.forest) {
|
2012-03-16 22:45:52 -06:00
|
|
|
dwarfs.push_back(cre);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int n_dwarfs = dwarfs.size();
|
|
|
|
|
|
|
|
if (n_dwarfs == 0)
|
|
|
|
return CR_OK;
|
|
|
|
|
2012-03-29 12:00:26 -06:00
|
|
|
std::vector<dwarf_info_t> dwarf_info(n_dwarfs);
|
2012-03-16 22:45:52 -06:00
|
|
|
|
2012-03-23 18:33:29 -06:00
|
|
|
std::vector<int> best_noble(ARRAY_COUNT(noble_skills));
|
|
|
|
std::vector<int> highest_noble_skill(ARRAY_COUNT(noble_skills));
|
|
|
|
std::vector<int> highest_noble_experience(ARRAY_COUNT(noble_skills));
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
// Find total skill and highest skill for each dwarf. More skilled dwarves shouldn't be used for minor tasks.
|
|
|
|
|
|
|
|
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
|
|
|
|
{
|
|
|
|
assert(dwarfs[dwarf]->status.souls.size() > 0);
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
for (auto s = dwarfs[dwarf]->status.souls[0]->skills.begin(); s != dwarfs[dwarf]->status.souls[0]->skills.end(); s++)
|
2012-03-16 22:45:52 -06:00
|
|
|
{
|
|
|
|
df::job_skill skill = (*s)->id;
|
|
|
|
|
|
|
|
df::job_skill_class skill_class = ENUM_ATTR(job_skill, type, skill);
|
|
|
|
|
|
|
|
int skill_level = (*s)->rating;
|
|
|
|
int skill_experience = (*s)->experience;
|
|
|
|
|
|
|
|
// Track the dwarfs with the best Appraisal, Organization, and Record Keeping skills.
|
|
|
|
// They are likely to have appointed noble positions, so should be kept free where possible.
|
|
|
|
|
|
|
|
int noble_skill_id = -1;
|
2012-03-23 18:33:29 -06:00
|
|
|
for (int i = 0; i < ARRAY_COUNT(noble_skills); i++)
|
2012-03-16 22:45:52 -06:00
|
|
|
{
|
|
|
|
if (skill == noble_skills[i])
|
|
|
|
noble_skill_id = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noble_skill_id >= 0)
|
|
|
|
{
|
2012-03-23 18:33:29 -06:00
|
|
|
assert(noble_skill_id < ARRAY_COUNT(noble_skills));
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
if (highest_noble_skill[noble_skill_id] < skill_level ||
|
|
|
|
(highest_noble_skill[noble_skill_id] == skill_level &&
|
|
|
|
highest_noble_experience[noble_skill_id] < skill_experience))
|
|
|
|
{
|
|
|
|
highest_noble_skill[noble_skill_id] = skill_level;
|
|
|
|
highest_noble_experience[noble_skill_id] = skill_experience;
|
|
|
|
best_noble[noble_skill_id] = dwarf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track total & highest skill among normal/medical skills. (We don't care about personal or social skills.)
|
|
|
|
|
|
|
|
if (skill_class != df::enums::job_skill_class::Normal && skill_class != df::enums::job_skill_class::Medical)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (dwarf_info[dwarf].highest_skill < skill_level)
|
|
|
|
dwarf_info[dwarf].highest_skill = skill_level;
|
|
|
|
dwarf_info[dwarf].total_skill += skill_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the best nobles, so we try to keep them non-busy. (It would be better to find the actual assigned nobles.)
|
|
|
|
|
2012-03-23 18:33:29 -06:00
|
|
|
for (int i = 0; i < ARRAY_COUNT(noble_skills); i++)
|
2012-03-16 22:45:52 -06:00
|
|
|
{
|
|
|
|
assert(best_noble[i] >= 0);
|
|
|
|
assert(best_noble[i] < n_dwarfs);
|
|
|
|
|
|
|
|
dwarf_info[best_noble[i]].is_best_noble = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate a base penalty for using each dwarf for a task he isn't good at.
|
|
|
|
|
|
|
|
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
|
|
|
|
{
|
|
|
|
dwarf_info[dwarf].mastery_penalty -= 40 * dwarf_info[dwarf].highest_skill;
|
|
|
|
dwarf_info[dwarf].mastery_penalty -= 10 * dwarf_info[dwarf].total_skill;
|
|
|
|
if (dwarf_info[dwarf].is_best_noble)
|
|
|
|
dwarf_info[dwarf].mastery_penalty -= 250;
|
|
|
|
|
|
|
|
for (int labor = ENUM_FIRST_ITEM(unit_labor); labor <= ENUM_LAST_ITEM(unit_labor); labor++)
|
|
|
|
{
|
|
|
|
if (labor == df::enums::unit_labor::NONE)
|
|
|
|
continue;
|
|
|
|
|
2012-03-29 12:18:14 -06:00
|
|
|
/*
|
2012-03-16 22:45:52 -06:00
|
|
|
assert(labor >= 0);
|
2012-03-23 18:33:29 -06:00
|
|
|
assert(labor < ARRAY_COUNT(labor_infos));
|
2012-03-29 12:18:14 -06:00
|
|
|
*/
|
2012-03-16 22:45:52 -06:00
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
if (labor_infos[labor].is_exclusive && dwarfs[dwarf]->status.labors[labor])
|
2012-03-16 22:45:52 -06:00
|
|
|
dwarf_info[dwarf].mastery_penalty -= 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the activity state for each dwarf. It's important to get this right - a dwarf who we think is IDLE but
|
|
|
|
// can't work will gum everything up. In the future I might add code to auto-detect slacker dwarves.
|
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
state_count.clear();
|
|
|
|
state_count.resize(NUM_STATE);
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
|
|
|
|
{
|
|
|
|
bool is_on_break = false;
|
|
|
|
|
|
|
|
for (auto p = dwarfs[dwarf]->status.misc_traits.begin(); p < dwarfs[dwarf]->status.misc_traits.end(); p++)
|
|
|
|
{
|
|
|
|
// 7 / 0x7 = Newly arrived migrant, will not work yet
|
|
|
|
// 17 / 0x11 = On break
|
|
|
|
if ((*p)->id == 0x07 || (*p)->id == 0x11)
|
|
|
|
is_on_break = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dwarfs[dwarf]->profession == df::enums::profession::BABY ||
|
|
|
|
dwarfs[dwarf]->profession == df::enums::profession::CHILD ||
|
|
|
|
dwarfs[dwarf]->profession == df::enums::profession::DRUNK)
|
|
|
|
{
|
|
|
|
dwarf_info[dwarf].state = CHILD;
|
|
|
|
}
|
2012-04-04 00:26:15 -06:00
|
|
|
else if (ENUM_ATTR(profession, military, dwarfs[dwarf]->profession))
|
|
|
|
dwarf_info[dwarf].state = MILITARY;
|
2012-03-16 22:45:52 -06:00
|
|
|
else if (dwarfs[dwarf]->job.current_job == NULL)
|
|
|
|
{
|
2012-04-04 00:26:15 -06:00
|
|
|
if (is_on_break)
|
2012-03-16 22:45:52 -06:00
|
|
|
dwarf_info[dwarf].state = OTHER;
|
|
|
|
else if (dwarfs[dwarf]->meetings.size() > 0)
|
|
|
|
dwarf_info[dwarf].state = OTHER;
|
|
|
|
else
|
|
|
|
dwarf_info[dwarf].state = IDLE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int job = dwarfs[dwarf]->job.current_job->job_type;
|
|
|
|
|
2012-03-29 12:18:14 -06:00
|
|
|
/*
|
2012-03-16 22:45:52 -06:00
|
|
|
assert(job >= 0);
|
2012-03-23 18:33:29 -06:00
|
|
|
assert(job < ARRAY_COUNT(dwarf_states));
|
2012-03-29 12:18:14 -06:00
|
|
|
*/
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
dwarf_info[dwarf].state = dwarf_states[job];
|
|
|
|
}
|
|
|
|
|
|
|
|
state_count[dwarf_info[dwarf].state]++;
|
2012-04-02 21:55:02 -06:00
|
|
|
|
|
|
|
if (print_debug)
|
|
|
|
out.print("Dwarf %i \"%s\": penalty %i, state %s\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), dwarf_info[dwarf].mastery_penalty, state_names[dwarf_info[dwarf].state]);
|
2012-03-16 22:45:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate labor -> skill mapping
|
|
|
|
|
|
|
|
df::job_skill labor_to_skill[ENUM_LAST_ITEM(unit_labor) + 1];
|
|
|
|
for (int i = 0; i <= ENUM_LAST_ITEM(unit_labor); i++)
|
|
|
|
labor_to_skill[i] = df::enums::job_skill::NONE;
|
|
|
|
|
|
|
|
FOR_ENUM_ITEMS(job_skill, skill)
|
|
|
|
{
|
|
|
|
int labor = ENUM_ATTR(job_skill, labor, skill);
|
|
|
|
if (labor != df::enums::unit_labor::NONE)
|
|
|
|
{
|
2012-03-29 12:18:14 -06:00
|
|
|
/*
|
2012-03-16 22:45:52 -06:00
|
|
|
assert(labor >= 0);
|
2012-03-23 18:33:29 -06:00
|
|
|
assert(labor < ARRAY_COUNT(labor_to_skill));
|
2012-03-29 12:18:14 -06:00
|
|
|
*/
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
labor_to_skill[labor] = skill;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
std::vector<df::unit_labor> labors;
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
FOR_ENUM_ITEMS(unit_labor, labor)
|
|
|
|
{
|
|
|
|
if (labor == df::enums::unit_labor::NONE)
|
|
|
|
continue;
|
|
|
|
|
2012-03-29 12:18:14 -06:00
|
|
|
/*
|
2012-03-16 22:45:52 -06:00
|
|
|
assert(labor >= 0);
|
2012-03-23 18:33:29 -06:00
|
|
|
assert(labor < ARRAY_COUNT(labor_infos));
|
2012-03-29 12:18:14 -06:00
|
|
|
*/
|
2012-03-22 01:33:18 -06:00
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
labor_infos[labor].active_dwarfs = 0;
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
labors.push_back(labor);
|
|
|
|
}
|
2012-03-29 12:00:26 -06:00
|
|
|
laborinfo_sorter lasorter;
|
|
|
|
std::sort(labors.begin(), labors.end(), lasorter);
|
2012-03-22 01:33:18 -06:00
|
|
|
|
2012-04-02 21:55:02 -06:00
|
|
|
// Handle DISABLED skills (just bookkeeping)
|
|
|
|
for (auto lp = labors.begin(); lp != labors.end(); ++lp)
|
|
|
|
{
|
|
|
|
auto labor = *lp;
|
|
|
|
|
|
|
|
if (labor_infos[labor].mode != DISABLE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
|
|
|
|
{
|
|
|
|
if (dwarfs[dwarf]->status.labors[labor])
|
|
|
|
{
|
|
|
|
if (labor_infos[labor].is_exclusive)
|
|
|
|
dwarf_info[dwarf].has_exclusive_labor = true;
|
|
|
|
|
|
|
|
dwarf_info[dwarf].assigned_jobs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
// Handle all skills except those marked HAULERS
|
|
|
|
|
|
|
|
for (auto lp = labors.begin(); lp != labors.end(); ++lp)
|
|
|
|
{
|
|
|
|
auto labor = *lp;
|
|
|
|
|
2012-03-29 12:18:14 -06:00
|
|
|
/*
|
2012-03-22 01:33:18 -06:00
|
|
|
assert(labor >= 0);
|
2012-03-23 18:33:29 -06:00
|
|
|
assert(labor < ARRAY_COUNT(labor_infos));
|
2012-03-29 12:18:14 -06:00
|
|
|
*/
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
df::job_skill skill = labor_to_skill[labor];
|
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
if (labor_infos[labor].mode != AUTOMATIC)
|
2012-04-02 21:55:02 -06:00
|
|
|
continue;
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
int best_dwarf = 0;
|
|
|
|
int best_value = -10000;
|
|
|
|
|
|
|
|
std::vector<int> values(n_dwarfs);
|
|
|
|
std::vector<int> candidates;
|
2012-03-22 01:33:18 -06:00
|
|
|
std::map<int, int> dwarf_skill;
|
2012-04-03 01:12:13 -06:00
|
|
|
std::vector<bool> previously_enabled(n_dwarfs);
|
2012-03-22 01:33:18 -06:00
|
|
|
|
|
|
|
auto mode = labor_infos[labor].mode;
|
2012-04-02 21:55:02 -06:00
|
|
|
|
2012-04-04 00:26:15 -06:00
|
|
|
// Find candidate dwarfs, and calculate a preference value for each dwarf
|
2012-03-16 22:45:52 -06:00
|
|
|
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
|
|
|
|
{
|
2012-04-02 21:55:02 -06:00
|
|
|
if (dwarf_info[dwarf].state == CHILD)
|
|
|
|
continue;
|
|
|
|
if (dwarf_info[dwarf].state == MILITARY)
|
2012-03-16 22:45:52 -06:00
|
|
|
continue;
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
if (labor_infos[labor].is_exclusive && dwarf_info[dwarf].has_exclusive_labor)
|
2012-03-16 22:45:52 -06:00
|
|
|
continue;
|
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
int value = dwarf_info[dwarf].mastery_penalty;
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
if (skill != df::enums::job_skill::NONE)
|
|
|
|
{
|
|
|
|
int skill_level = 0;
|
|
|
|
int skill_experience = 0;
|
|
|
|
|
|
|
|
for (auto s = dwarfs[dwarf]->status.souls[0]->skills.begin(); s < dwarfs[dwarf]->status.souls[0]->skills.end(); s++)
|
|
|
|
{
|
|
|
|
if ((*s)->id == skill)
|
|
|
|
{
|
|
|
|
skill_level = (*s)->rating;
|
|
|
|
skill_experience = (*s)->experience;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
dwarf_skill[dwarf] = skill_level;
|
|
|
|
|
2012-03-16 22:45:52 -06:00
|
|
|
value += skill_level * 100;
|
|
|
|
value += skill_experience / 20;
|
|
|
|
if (skill_level > 0 || skill_experience > 0)
|
|
|
|
value += 200;
|
|
|
|
if (skill_level >= 15)
|
|
|
|
value += 1000 * (skill_level - 14);
|
|
|
|
}
|
2012-03-22 01:33:18 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
dwarf_skill[dwarf] = 0;
|
|
|
|
}
|
2012-03-16 22:45:52 -06:00
|
|
|
|
|
|
|
if (dwarfs[dwarf]->status.labors[labor])
|
|
|
|
{
|
|
|
|
value += 5;
|
2012-03-22 01:33:18 -06:00
|
|
|
if (labor_infos[labor].is_exclusive)
|
2012-03-16 22:45:52 -06:00
|
|
|
value += 350;
|
|
|
|
}
|
|
|
|
|
|
|
|
values[dwarf] = value;
|
2012-03-22 01:33:18 -06:00
|
|
|
|
2012-04-02 21:55:02 -06:00
|
|
|
candidates.push_back(dwarf);
|
2012-03-22 01:33:18 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-04-04 00:26:15 -06:00
|
|
|
// Sort candidates by preference value
|
2012-04-02 23:12:06 -06:00
|
|
|
values_sorter ivs(values);
|
|
|
|
std::sort(candidates.begin(), candidates.end(), ivs);
|
2012-04-02 21:55:02 -06:00
|
|
|
|
2012-04-04 00:26:15 -06:00
|
|
|
// Disable the labor on everyone
|
2012-03-16 22:45:52 -06:00
|
|
|
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
|
|
|
|
{
|
2012-04-02 21:55:02 -06:00
|
|
|
if (dwarf_info[dwarf].state == CHILD)
|
|
|
|
continue;
|
2012-03-22 01:33:18 -06:00
|
|
|
|
2012-04-03 01:12:13 -06:00
|
|
|
previously_enabled[dwarf] = dwarfs[dwarf]->status.labors[labor];
|
2012-04-02 21:55:02 -06:00
|
|
|
dwarfs[dwarf]->status.labors[labor] = false;
|
2012-03-16 22:45:52 -06:00
|
|
|
}
|
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
int min_dwarfs = labor_infos[labor].minimum_dwarfs;
|
|
|
|
int max_dwarfs = labor_infos[labor].maximum_dwarfs;
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
// Special - don't assign hunt without a butchers, or fish without a fishery
|
|
|
|
if (df::enums::unit_labor::HUNT == labor && !has_butchers)
|
2012-04-02 21:55:02 -06:00
|
|
|
min_dwarfs = max_dwarfs = 0;
|
2012-03-22 01:33:18 -06:00
|
|
|
if (df::enums::unit_labor::FISH == labor && !has_fishery)
|
2012-04-02 21:55:02 -06:00
|
|
|
min_dwarfs = max_dwarfs = 0;
|
2012-03-22 01:33:18 -06:00
|
|
|
|
2012-04-03 00:40:56 -06:00
|
|
|
bool want_idle_dwarf = true;
|
|
|
|
if (state_count[IDLE] < 2)
|
|
|
|
want_idle_dwarf = false;
|
|
|
|
|
2012-04-04 00:26:15 -06:00
|
|
|
/*
|
|
|
|
* Assign dwarfs to this labor. We assign at least the minimum number of dwarfs, in
|
|
|
|
* order of preference, and then assign additional dwarfs that meet any of these conditions:
|
|
|
|
* - The dwarf is idle and there are no idle dwarves assigned to this labor
|
|
|
|
* - The dwarf has nonzero skill associated with the labor
|
|
|
|
* - The labor is mining, hunting, or woodcutting and the dwarf currently has it enabled.
|
|
|
|
* We stop assigning dwarfs when we reach the maximum allowed.
|
|
|
|
* Note that only idle and busy dwarfs count towards the number of dwarfs. "Other" dwarfs
|
|
|
|
* (sleeping, eating, on break, etc.) will have labors assigned, but will not be counted.
|
|
|
|
* Military and children/nobles will not have labors assigned.
|
|
|
|
*/
|
2012-04-03 01:12:13 -06:00
|
|
|
for (int i = 0; i < candidates.size() && labor_infos[labor].active_dwarfs < max_dwarfs; i++)
|
2012-03-16 22:45:52 -06:00
|
|
|
{
|
|
|
|
int dwarf = candidates[i];
|
|
|
|
|
|
|
|
assert(dwarf >= 0);
|
|
|
|
assert(dwarf < n_dwarfs);
|
|
|
|
|
2012-04-03 01:12:13 -06:00
|
|
|
bool preferred_dwarf = false;
|
|
|
|
if (want_idle_dwarf && dwarf_info[dwarf].state == IDLE)
|
|
|
|
preferred_dwarf = true;
|
|
|
|
if (dwarf_skill[dwarf] > 0)
|
|
|
|
preferred_dwarf = true;
|
|
|
|
if (previously_enabled[dwarf] && labor_infos[labor].is_exclusive)
|
|
|
|
preferred_dwarf = true;
|
|
|
|
|
|
|
|
if (labor_infos[labor].active_dwarfs >= min_dwarfs && !preferred_dwarf)
|
2012-04-02 23:47:26 -06:00
|
|
|
continue;
|
|
|
|
|
2012-03-16 22:45:52 -06:00
|
|
|
if (!dwarfs[dwarf]->status.labors[labor])
|
|
|
|
dwarf_info[dwarf].assigned_jobs++;
|
|
|
|
|
|
|
|
dwarfs[dwarf]->status.labors[labor] = true;
|
|
|
|
|
2012-03-28 20:57:08 -06:00
|
|
|
if (labor_infos[labor].is_exclusive)
|
|
|
|
{
|
2012-03-16 22:45:52 -06:00
|
|
|
dwarf_info[dwarf].has_exclusive_labor = true;
|
2012-03-28 20:57:08 -06:00
|
|
|
// all the exclusive labors require equipment so this should force the dorf to reequip if needed
|
|
|
|
dwarfs[dwarf]->military.pickup_flags.bits.update = 1;
|
|
|
|
}
|
2012-04-02 21:55:02 -06:00
|
|
|
|
|
|
|
if (print_debug)
|
|
|
|
out.print("Dwarf %i \"%s\" assigned %s: value %i\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, labor).c_str(), values[dwarf]);
|
|
|
|
|
|
|
|
if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY)
|
2012-04-02 23:12:06 -06:00
|
|
|
labor_infos[labor].active_dwarfs++;
|
2012-04-02 21:55:02 -06:00
|
|
|
|
2012-04-03 01:12:13 -06:00
|
|
|
if (dwarf_info[dwarf].state == IDLE)
|
2012-04-03 00:40:56 -06:00
|
|
|
want_idle_dwarf = false;
|
2012-03-16 22:45:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
// Set about 1/3 of the dwarfs as haulers. The haulers have all HAULER labors enabled. Having a lot of haulers helps
|
|
|
|
// make sure that hauling jobs are handled quickly rather than building up.
|
|
|
|
|
|
|
|
int num_haulers = state_count[IDLE] + state_count[BUSY] / 3;
|
|
|
|
if (num_haulers < 1)
|
|
|
|
num_haulers = 1;
|
|
|
|
|
|
|
|
std::vector<int> hauler_ids;
|
|
|
|
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
|
|
|
|
{
|
|
|
|
if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY)
|
|
|
|
hauler_ids.push_back(dwarf);
|
|
|
|
}
|
2012-03-29 12:00:26 -06:00
|
|
|
dwarfinfo_sorter sorter(dwarf_info);
|
2012-03-22 01:33:18 -06:00
|
|
|
// Idle dwarves come first, then we sort from least-skilled to most-skilled.
|
2012-03-29 12:00:26 -06:00
|
|
|
std::sort(hauler_ids.begin(), hauler_ids.end(), sorter);
|
2012-03-22 01:33:18 -06:00
|
|
|
|
2012-03-27 20:39:06 -06:00
|
|
|
// don't set any haulers if everyone is off drinking or something
|
|
|
|
if (hauler_ids.size() == 0) {
|
|
|
|
num_haulers = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-22 01:33:18 -06:00
|
|
|
FOR_ENUM_ITEMS(unit_labor, labor)
|
|
|
|
{
|
|
|
|
if (labor == df::enums::unit_labor::NONE)
|
|
|
|
continue;
|
|
|
|
|
2012-03-29 12:18:14 -06:00
|
|
|
/*
|
2012-03-22 01:33:18 -06:00
|
|
|
assert(labor >= 0);
|
2012-03-23 18:33:29 -06:00
|
|
|
assert(labor < ARRAY_COUNT(labor_infos));
|
2012-03-29 12:18:14 -06:00
|
|
|
*/
|
2012-03-22 01:33:18 -06:00
|
|
|
|
|
|
|
if (labor_infos[labor].mode != HAULERS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int i = 0; i < num_haulers; i++)
|
|
|
|
{
|
|
|
|
assert(i < hauler_ids.size());
|
|
|
|
|
|
|
|
int dwarf = hauler_ids[i];
|
|
|
|
|
|
|
|
assert(dwarf >= 0);
|
|
|
|
assert(dwarf < n_dwarfs);
|
|
|
|
dwarfs[dwarf]->status.labors[labor] = true;
|
|
|
|
dwarf_info[dwarf].assigned_jobs++;
|
2012-04-02 21:55:02 -06:00
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY)
|
|
|
|
labor_infos[labor].active_dwarfs++;
|
|
|
|
|
2012-04-02 21:55:02 -06:00
|
|
|
if (print_debug)
|
|
|
|
out.print("Dwarf %i \"%s\" assigned %s: hauler\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, labor).c_str());
|
2012-03-22 01:33:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = num_haulers; i < hauler_ids.size(); i++)
|
|
|
|
{
|
|
|
|
assert(i < hauler_ids.size());
|
|
|
|
|
|
|
|
int dwarf = hauler_ids[i];
|
|
|
|
|
|
|
|
assert(dwarf >= 0);
|
|
|
|
assert(dwarf < n_dwarfs);
|
|
|
|
|
|
|
|
dwarfs[dwarf]->status.labors[labor] = false;
|
|
|
|
}
|
|
|
|
}
|
2012-04-02 21:55:02 -06:00
|
|
|
|
|
|
|
print_debug = 0;
|
|
|
|
|
2012-03-16 22:45:52 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-02 23:12:06 -06:00
|
|
|
void print_labor (df::enums::unit_labor::unit_labor labor, color_ostream &out)
|
|
|
|
{
|
|
|
|
string labor_name = ENUM_KEY_STR(unit_labor, labor);
|
|
|
|
out << labor_name << ": ";
|
|
|
|
for (int i = 0; i < 20 - (int)labor_name.length(); i++)
|
|
|
|
out << ' ';
|
|
|
|
if (labor_infos[labor].mode == DISABLE)
|
|
|
|
out << "disabled" << endl;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (labor_infos[labor].mode == HAULERS)
|
|
|
|
out << "haulers";
|
|
|
|
else
|
|
|
|
out << "minimum " << labor_infos[labor].minimum_dwarfs << ", maximum " << labor_infos[labor].maximum_dwarfs;
|
|
|
|
out << ", currently " << labor_infos[labor].active_dwarfs << " dwarfs" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-16 22:45:52 -06:00
|
|
|
command_result autolabor (color_ostream &out, std::vector <std::string> & parameters)
|
|
|
|
{
|
2012-03-28 09:25:36 -06:00
|
|
|
if (parameters.size() == 1 &&
|
|
|
|
(parameters[0] == "0" || parameters[0] == "enable" ||
|
|
|
|
parameters[0] == "1" || parameters[0] == "disable"))
|
2012-03-16 22:45:52 -06:00
|
|
|
{
|
2012-03-28 09:25:36 -06:00
|
|
|
if (parameters[0] == "0" || parameters[0] == "disable")
|
2012-03-16 22:45:52 -06:00
|
|
|
enable_autolabor = 0;
|
|
|
|
else
|
|
|
|
enable_autolabor = 1;
|
|
|
|
out.print("autolabor %sactivated.\n", (enable_autolabor ? "" : "de"));
|
|
|
|
}
|
2012-04-02 23:12:06 -06:00
|
|
|
else if (parameters.size() == 2 || parameters.size() == 3) {
|
|
|
|
df::enums::unit_labor::unit_labor labor = df::enums::unit_labor::NONE;
|
|
|
|
|
|
|
|
FOR_ENUM_ITEMS(unit_labor, test_labor)
|
|
|
|
{
|
|
|
|
if (parameters[0] == ENUM_KEY_STR(unit_labor, test_labor))
|
|
|
|
labor = test_labor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (labor == df::enums::unit_labor::NONE)
|
|
|
|
{
|
|
|
|
out.printerr("Could not find labor %s.", parameters[0].c_str());
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters[1] == "haulers")
|
|
|
|
{
|
|
|
|
labor_infos[labor].mode = HAULERS;
|
|
|
|
print_labor(labor, out);
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
if (parameters[1] == "disable")
|
|
|
|
{
|
|
|
|
labor_infos[labor].mode = DISABLE;
|
|
|
|
print_labor(labor, out);
|
|
|
|
return CR_OK;
|
2012-03-28 09:25:36 -06:00
|
|
|
}
|
2012-04-02 23:12:06 -06:00
|
|
|
|
|
|
|
int minimum = atoi (parameters[1].c_str());
|
|
|
|
int maximum = 200;
|
|
|
|
if (parameters.size() == 3)
|
|
|
|
maximum = atoi (parameters[2].c_str());
|
|
|
|
|
|
|
|
if (maximum < minimum || maximum < 0 || minimum < 0)
|
|
|
|
{
|
|
|
|
out.printerr("Syntax: autolabor <labor> <minimum> [<maximum>]\n", maximum, minimum);
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
labor_infos[labor].minimum_dwarfs = minimum;
|
|
|
|
labor_infos[labor].maximum_dwarfs = maximum;
|
|
|
|
labor_infos[labor].mode = AUTOMATIC;
|
|
|
|
print_labor(labor, out);
|
2012-04-02 21:55:02 -06:00
|
|
|
}
|
2012-04-02 23:12:06 -06:00
|
|
|
else if (parameters.size() == 1 && parameters[0] == "list") {
|
|
|
|
if (!enable_autolabor)
|
|
|
|
{
|
|
|
|
out << "autolabor not activated." << endl;
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_comma = 0;
|
|
|
|
for (int i = 0; i < NUM_STATE; i++)
|
|
|
|
{
|
|
|
|
if (state_count[i] == 0)
|
|
|
|
continue;
|
|
|
|
if (need_comma)
|
|
|
|
out << ", ";
|
|
|
|
out << state_count[i] << ' ' << state_names[i];
|
|
|
|
need_comma = 1;
|
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
|
|
|
|
FOR_ENUM_ITEMS(unit_labor, labor)
|
|
|
|
{
|
|
|
|
if (labor == df::enums::unit_labor::NONE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
print_labor(labor, out);
|
|
|
|
}
|
|
|
|
}
|
2012-04-02 21:55:02 -06:00
|
|
|
else if (parameters.size() == 1 && parameters[0] == "debug") {
|
|
|
|
print_debug = 1;
|
|
|
|
}
|
|
|
|
else
|
2012-03-16 22:45:52 -06:00
|
|
|
{
|
|
|
|
out.print("Automatically assigns labors to dwarves.\n"
|
|
|
|
"Activate with 'autolabor 1', deactivate with 'autolabor 0'.\n"
|
|
|
|
"Current state: %d.\n", enable_autolabor);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|