Skip to content

Instantly share code, notes, and snippets.

@ab9rf
Created December 1, 2012 03:56
Show Gist options
  • Save ab9rf/4180480 to your computer and use it in GitHub Desktop.
Save ab9rf/4180480 to your computer and use it in GitHub Desktop.
autolabor in ruby. haha.
class JobInfo
def get_bld (job)
id = job.references.find { |r| r.kind_of?(DFHack::GeneralRefBuildingHolderst) }
bld = df.world.buildings.all.binsearch(id.building_id) if id
bld
end
def self.const_labor (l)
lambda {|job| return l}
end
no_labor = const_labor(:NONE)
hauling_labor = lambda {|job|
item = job.items[0].item
case item.class
when DFHack::ItemRoughst.class then :HAUL_ITEM
else
puts "Hauling: #{item.class}"
nil # FIXME
end
}
construct_building = lambda {|job|
bld = get_bld(job)
case bld.class
when DFHack::BuildingTrapst.class then :MECHANIC
else
puts "ConstructBuilding: #{bld.class}"
nil # FIXME
end
}
destroy_building = lambda {|job|
nil # FIXME
}
def self.make_generator (metaltype)
lambda {|job|
bld = get_bld(job)
case bld.type
when :Craftsdwarfs
jobitem = job.job_items[0].item_type
case jobitem
when :BOULDER then :STONE_CRAFT
when :NONE
if job.material_category.bone then :BONE_CARVE
else
puts "make_generator: job item type #{jobitem}"
nil
end
else
puts "make_generator: job item type #{jobitem}"
nil
end
when :Masons then :MASON
when :Carpenters then :CARPENTER
when :Leatherworks then :LEATHER
when :Clothiers then :CLOTHESMAKING
when :MagmaForge, :MetalsmithForge then metaltype
when :MagmaGlassFurnace, :GlassFurnace then :GLASSMAKER
else
puts "make_generator: workshop type #{bld.type}"
nil
end
}
end
make_furniture = make_generator (:FORGE_FURNITURE)
make_object = make_generator (:METAL_CRAFT)
make_armor = make_generator (:FORGE_ARMOR)
make_weapon = make_generator (:FORGE_WEAPON)
custom_reaction = lambda {|job|
react = df.world.raws.reactions.find { |r| r.code == job.reaction_name }
skill = react.skill
labor = DFHack::JobSkill::Labor[skill]
labor
}
@@job_to_labor = Hash.new
@@job_to_labor[:CarveFortification] = const_labor(:DETAIL)
@@job_to_labor[:DetailWall] = const_labor(:DETAIL)
@@job_to_labor[:DetailFloor] = const_labor(:DETAIL)
@@job_to_labor[:Dig] = const_labor(:DIG)
@@job_to_labor[:CarveUpwardStaircase] = const_labor(:DIG)
@@job_to_labor[:CarveDownwardStaircase] = const_labor(:DIG)
@@job_to_labor[:CarveUpDownStaircase] = const_labor(:DIG)
@@job_to_labor[:CarveRamp] = const_labor(:DIG)
@@job_to_labor[:DigChannel] = const_labor(:DIG)
@@job_to_labor[:FellTree] = const_labor(:CUTWOOD)
@@job_to_labor[:GatherPlants] = const_labor(:HERBALIST)
@@job_to_labor[:RemoveConstruction] = no_labor
@@job_to_labor[:CollectWebs] = const_labor(:WEAVER)
@@job_to_labor[:BringItemToDepot] = no_labor
@@job_to_labor[:BringItemToShop] = no_labor
@@job_to_labor[:Eat] = no_labor
@@job_to_labor[:GetProvisions] = no_labor
@@job_to_labor[:Drink] = no_labor
@@job_to_labor[:Drink2] = no_labor
@@job_to_labor[:FillWaterskin] = no_labor
@@job_to_labor[:FillWaterskin2] = no_labor
@@job_to_labor[:Sleep] = no_labor
@@job_to_labor[:CollectSand] = const_labor(:GLASSMAKER)
@@job_to_labor[:Fish] = const_labor(:FISH)
@@job_to_labor[:Hunt] = const_labor(:HUNT)
@@job_to_labor[:HuntVermin] = no_labor
@@job_to_labor[:Kidnap] = no_labor
@@job_to_labor[:BeatCriminal] = no_labor
@@job_to_labor[:StartingFistFight] = no_labor
@@job_to_labor[:CollectTaxes] = no_labor
@@job_to_labor[:GuardTaxCollector] = no_labor
@@job_to_labor[:CatchLiveLandAnimal] = const_labor(:HUNT)
@@job_to_labor[:CatchLiveFish] = const_labor(:FISH)
@@job_to_labor[:ReturnKill] = no_labor
@@job_to_labor[:CheckChest] = no_labor
@@job_to_labor[:StoreOwnedItem] = no_labor
@@job_to_labor[:PlaceItemInTomb] = const_labor(:HAUL_BODY)
@@job_to_labor[:StoreItemInStockpile] = hauling_labor
@@job_to_labor[:StoreItemInBag] = hauling_labor
@@job_to_labor[:StoreItemInHospital] = hauling_labor
@@job_to_labor[:StoreItemInChest] = hauling_labor
@@job_to_labor[:StoreItemInCabinet] = hauling_labor
@@job_to_labor[:StoreWeapon] = hauling_labor
@@job_to_labor[:StoreArmor] = hauling_labor
@@job_to_labor[:StoreItemInBarrel] = hauling_labor
@@job_to_labor[:StoreItemInBin] = hauling_labor
@@job_to_labor[:SeekArtifact] = no_labor
@@job_to_labor[:SeekInfant] = no_labor
@@job_to_labor[:AttendParty] = no_labor
@@job_to_labor[:GoShopping] = no_labor
@@job_to_labor[:GoShopping2] = no_labor
@@job_to_labor[:Clean] = const_labor(:CLEAN)
@@job_to_labor[:Rest] = no_labor
@@job_to_labor[:PickupEquipment] = no_labor
@@job_to_labor[:DumpItem] = hauling_labor
@@job_to_labor[:StrangeMoodCrafter] = no_labor
@@job_to_labor[:StrangeMoodJeweller] = no_labor
@@job_to_labor[:StrangeMoodForge] = no_labor
@@job_to_labor[:StrangeMoodMagmaForge] = no_labor
@@job_to_labor[:StrangeMoodBrooding] = no_labor
@@job_to_labor[:StrangeMoodFell] = no_labor
@@job_to_labor[:StrangeMoodCarpenter] = no_labor
@@job_to_labor[:StrangeMoodMason] = no_labor
@@job_to_labor[:StrangeMoodBowyer] = no_labor
@@job_to_labor[:StrangeMoodTanner] = no_labor
@@job_to_labor[:StrangeMoodWeaver] = no_labor
@@job_to_labor[:StrangeMoodGlassmaker] = no_labor
@@job_to_labor[:StrangeMoodMechanics] = no_labor
@@job_to_labor[:ConstructBuilding] = construct_building
@@job_to_labor[:ConstructDoor] = make_furniture
@@job_to_labor[:ConstructFloodgate] = make_furniture
@@job_to_labor[:ConstructBed] = make_furniture
@@job_to_labor[:ConstructThrone] = make_furniture
@@job_to_labor[:ConstructCoffin] = make_furniture
@@job_to_labor[:ConstructTable] = make_furniture
@@job_to_labor[:ConstructChest] = make_furniture
@@job_to_labor[:ConstructBin] = make_furniture
@@job_to_labor[:ConstructArmorStand] = make_furniture
@@job_to_labor[:ConstructWeaponRack] = make_furniture
@@job_to_labor[:ConstructCabinet] = make_furniture
@@job_to_labor[:ConstructStatue] = make_furniture
@@job_to_labor[:ConstructBlocks] = make_furniture
@@job_to_labor[:MakeRawGlass] = const_labor(:GLASSMAKER)
@@job_to_labor[:MakeCrafts] = make_object
@@job_to_labor[:MintCoins] = const_labor(:METAL_CRAFT)
@@job_to_labor[:CutGems] = const_labor(:CUT_GEM)
@@job_to_labor[:CutGlass] = const_labor(:CUT_GEM)
@@job_to_labor[:EncrustWithGems] = const_labor(:ENCRUST_GEM)
@@job_to_labor[:EncrustWithGlass] = const_labor(:ENCRUST_GEM)
@@job_to_labor[:DestroyBuilding] = destroy_building
@@job_to_labor[:SmeltOre] = const_labor(:SMELT)
@@job_to_labor[:MeltMetalObject] = const_labor(:SMELT)
@@job_to_labor[:ExtractMetalStrands] = const_labor(:EXTRACT_STRAND)
@@job_to_labor[:PlantSeeds] = const_labor(:PLANT)
@@job_to_labor[:HarvestPlants] = const_labor(:PLANT)
@@job_to_labor[:TrainHuntingAnimal] = const_labor(:ANIMALTRAIN)
@@job_to_labor[:TrainWarAnimal] = const_labor(:ANIMALTRAIN)
@@job_to_labor[:MakeWeapon] = make_weapon
@@job_to_labor[:ForgeAnvil] = make_furniture
@@job_to_labor[:ConstructCatapultParts] = const_labor(:SIEGECRAFT)
@@job_to_labor[:ConstructBallistaParts] = const_labor(:SIEGECRAFT)
@@job_to_labor[:MakeArmor] = make_armor
@@job_to_labor[:MakeHelm] = make_armor
@@job_to_labor[:MakePants] = make_armor
@@job_to_labor[:StudWith] = make_object
@@job_to_labor[:ButcherAnimal] = const_labor(:BUTCHER)
@@job_to_labor[:PrepareRawFish] = const_labor(:CLEAN_FISH)
@@job_to_labor[:MillPlants] = const_labor(:MILLER)
@@job_to_labor[:BaitTrap] = const_labor(:TRAPPER)
@@job_to_labor[:MilkCreature] = const_labor(:MILK)
@@job_to_labor[:MakeCheese] = const_labor(:MAKE_CHEESE)
@@job_to_labor[:ProcessPlants] = const_labor(:PROCESS_PLANT)
@@job_to_labor[:ProcessPlantsBag] = const_labor(:PROCESS_PLANT)
@@job_to_labor[:ProcessPlantsVial] = const_labor(:PROCESS_PLANT)
@@job_to_labor[:ProcessPlantsBarrel] = const_labor(:PROCESS_PLANT)
@@job_to_labor[:PrepareMeal] = const_labor(:COOK)
@@job_to_labor[:WeaveCloth] = const_labor(:CLOTHESMAKER)
@@job_to_labor[:MakeGloves] = make_armor
@@job_to_labor[:MakeShoes] = make_armor
@@job_to_labor[:MakeShield] = make_armor
@@job_to_labor[:MakeCage] = make_furniture
@@job_to_labor[:MakeChain] = make_object
@@job_to_labor[:MakeFlask] = make_object
@@job_to_labor[:MakeGoblet] = make_object
@@job_to_labor[:MakeInstrument] = make_object
@@job_to_labor[:MakeToy] = make_object
@@job_to_labor[:MakeAnimalTrap] = const_labor(:TRAPPER)
@@job_to_labor[:MakeBarrel] = make_furniture
@@job_to_labor[:MakeBucket] = make_furniture
@@job_to_labor[:MakeWindow] = make_furniture
@@job_to_labor[:MakeTotem] = const_labor(:BONE_CARVE)
@@job_to_labor[:MakeAmmo] = make_weapon
@@job_to_labor[:DecorateWith] = make_object
@@job_to_labor[:MakeBackpack] = make_object
@@job_to_labor[:MakeQuiver] = make_armor
@@job_to_labor[:MakeBallistaArrowHead] = make_weapon
@@job_to_labor[:AssembleSiegeAmmo] = const_labor(:SIEGECRAFT)
@@job_to_labor[:LoadCatapult] = const_labor(:SIEGEOPERATE)
@@job_to_labor[:LoadBallista] = const_labor(:SIEGEOPERATE)
@@job_to_labor[:FireCatapult] = const_labor(:SIEGEOPERATE)
@@job_to_labor[:FireBallista] = const_labor(:SIEGEOPERATE)
@@job_to_labor[:ConstructMechanisms] = const_labor(:MECHANIC)
@@job_to_labor[:MakeTrapComponent] = make_weapon
@@job_to_labor[:LoadCageTrap] = const_labor(:MECHANIC)
@@job_to_labor[:LoadStoneTrap] = const_labor(:MECHANIC)
@@job_to_labor[:LoadWeaponTrap] = const_labor(:MECHANIC)
@@job_to_labor[:CleanTrap] = const_labor(:MECHANIC)
@@job_to_labor[:CastSpell] = no_labor
@@job_to_labor[:LinkBuildingToTrigger] = const_labor(:MECHANIC)
@@job_to_labor[:PullLever] = no_labor
@@job_to_labor[:BrewDrink] = const_labor(:BREWER)
@@job_to_labor[:ExtractFromPlants] = const_labor(:HERBALIST)
@@job_to_labor[:ExtractFromRawFish] = const_labor(:FISH_DISSECTOR)
@@job_to_labor[:ExtractFromLandAnimal] = const_labor(:DISSECT_VERMIN)
@@job_to_labor[:TameVermin] = const_labor(:ANIMALTRAIN)
@@job_to_labor[:TameAnimal] = const_labor(:ANIMALTRAIN)
@@job_to_labor[:ChainAnimal] = no_labor
@@job_to_labor[:UnchainAnimal] = no_labor
@@job_to_labor[:UnchainPet] = no_labor
@@job_to_labor[:ReleaseLargeCreature] = no_labor
@@job_to_labor[:ReleasePet] = no_labor
@@job_to_labor[:ReleaseSmallCreature] = no_labor
@@job_to_labor[:HandleSmallCreature] = no_labor
@@job_to_labor[:HandleLargeCreature] = no_labor
@@job_to_labor[:CageLargeCreature] = no_labor
@@job_to_labor[:CageSmallCreature] = no_labor
@@job_to_labor[:RecoverWounded] = const_labor(:RECOVER_WOUNDED)
@@job_to_labor[:DiagnosePatient] = const_labor(:DIAGNOSE)
@@job_to_labor[:ImmobilizeBreak] = const_labor(:BONE_SETTING)
@@job_to_labor[:DressWound] = const_labor(:DRESSING_WOUNDS)
@@job_to_labor[:CleanPatient] = const_labor(:CLEAN)
@@job_to_labor[:Surgery] = const_labor(:SURGERY)
@@job_to_labor[:Suture] = const_labor(:SUTURING)
@@job_to_labor[:SetBone] = const_labor(:BONE_SETTING)
@@job_to_labor[:PlaceInTraction] = const_labor(:BONE_SETTING)
@@job_to_labor[:DrainAquarium] = no_labor
@@job_to_labor[:FillAquarium] = no_labor
@@job_to_labor[:FillPond] = no_labor
@@job_to_labor[:GiveWater] = const_labor(:FEED_WATER_CIVILIANS)
@@job_to_labor[:GiveFood] = const_labor(:FEED_WATER_CIVILIANS)
@@job_to_labor[:GiveWater2] = no_labor
@@job_to_labor[:GiveFood2] = no_labor
@@job_to_labor[:RecoverPet] = no_labor
@@job_to_labor[:PitLargeAnimal] = no_labor
@@job_to_labor[:PitSmallAnimal] = no_labor
@@job_to_labor[:SlaughterAnimal] = const_labor(:BUTCHER)
@@job_to_labor[:MakeCharcoal] = const_labor(:BURN_WOOD)
@@job_to_labor[:MakeAsh] = const_labor(:BURN_WOOD)
@@job_to_labor[:MakeLye] = const_labor(:LYE_MAKING)
@@job_to_labor[:MakePotashFromLye] = const_labor(:POTASH_MAKING)
@@job_to_labor[:FertilizeField] = const_labor(:PLANT)
@@job_to_labor[:MakePotashFromAsh] = const_labor(:POTASH_MAKING)
@@job_to_labor[:DyeThread] = const_labor(:DYER)
@@job_to_labor[:DyeCloth] = const_labor(:DYER)
@@job_to_labor[:SewImage] = make_object
@@job_to_labor[:MakePipeSection] = make_furniture
@@job_to_labor[:OperatePump] = const_labor(:OPERATE_PUMP)
@@job_to_labor[:ManageWorkOrders] = no_labor
@@job_to_labor[:UpdateStockpileRecords] = no_labor
@@job_to_labor[:TradeAtDepot] = no_labor
@@job_to_labor[:ConstructHatchCover] = make_furniture
@@job_to_labor[:ConstructGrate] = make_furniture
@@job_to_labor[:RemoveStairs] = const_labor(:DIG)
@@job_to_labor[:ConstructQuern] = make_furniture
@@job_to_labor[:ConstructMillstone] = make_furniture
@@job_to_labor[:ConstructSplint] = make_object
@@job_to_labor[:ConstructCrutch] = make_object
@@job_to_labor[:ConstructTractionBench] = const_labor(:MECHANIC)
@@job_to_labor[:CleanSelf] = no_labor
@@job_to_labor[:BringCrutch] = no_labor
@@job_to_labor[:ApplyCast] = const_labor(:BONE_SETTING)
@@job_to_labor[:CustomReaction] = custom_reaction
@@job_to_labor[:ConstructSlab] = make_furniture
@@job_to_labor[:EngraveSlab] = const_labor(:STONE_CRAFT)
@@job_to_labor[:ShearCreature] = const_labor(:SHEARER)
@@job_to_labor[:SpinThread] = const_labor(:SPINNER)
@@job_to_labor[:PenLargeAnimal] = no_labor
@@job_to_labor[:PenSmallAnimal] = no_labor
@@job_to_labor[:MakeTool] = make_furniture
@@job_to_labor[:CollectClay] = const_labor(:POTTERY)
@@job_to_labor[:InstallColonyInHive] = const_labor(:BEEKEEPING)
@@job_to_labor[:CollectHiveProducts] = const_labor(:BEEKEEPING)
@@job_to_labor[:CauseTrouble] = no_labor
@@job_to_labor[:DrinkBlood] = no_labor
@@job_to_labor[:ReportCrime] = no_labor
@@job_to_labor[:ExecuteCriminal] = no_labor
@@job_to_labor[:TrainAnimal] = const_labor(:ANIMALTRAIN)
@@job_to_labor[:CarveTrack] = const_labor(:DETAIL)
@@job_to_labor[:PushTrackVehicle] = const_labor(:PUSH_HAUL_VEHICLE)
@@job_to_labor[:PlaceTrackVehicle] = const_labor(:PUSH_HAUL_VEHICLE)
@@job_to_labor[:StoreItemInVehicle] = const_labor(:PUSH_HAUL_VEHICLE)
@@labor_to_skill = Hash.new
DFHack::JobSkill::Labor.each { |s,l| @@labor_to_skill[l] = s }
def self.get_job_labor (job)
if (job.job_type == :CustomReaction)
react = df.world.raws.reactions.find { |r| r.code == job.reaction_name }
skill = react.skill
else
skill = DFHack::JobType::Skill[job.job_type]
end
labor = DFHack::JobSkill::Labor[skill] if skill and skill != :NONE
if not labor then
l = @@job_to_labor[job.job_type]
labor = l.call(job)
end
return labor
end
def self.laborskill (l)
@@labor_to_skill[l]
end
def laborskill (l)
JobInfo::laborskill (l)
end
def analyze_jobs
race = df.ui.race_id
civ = df.ui.civ_id
has_butchers = has_fishery = trader_requested = false
df.world.buildings.all.each { |b|
case b.getType()
when :Workshop
case b.getSubType()
when :Butchers then has_butchers = true
when :Fishery then has_fishery = true
end
when :TradeDepot
trader_requested = b.trade_flags.trader_request
end
}
dwarfs = Array.new
df.world.units.active.each { |cre|
if Units::unit_iscitizen(cre)
next if cre.burrows
dwarfs << cre
end
}
return unless dwarfs;
joblist = df.world.job_list.next
dwarfs = df.world.
labor_count = Hash.new
while joblist
job = joblist.item
joblist = joblist.next
next if job.flags.suspend
dwarfref = job.references.find {|r| r.kind_of? DFHack::GeneralRefUnitWorkerst}
next if dwarfref
jobtype = job.job_type
joblabor = JobInfo::get_job_labor(job)
if (not joblabor) then
puts "#{jobtype} #{job.inspect}"
df.pause_state = true
break
end
labor_count[joblabor] = labor_count[joblabor].to_i + 1
end
# labor_count.each {|k,v| puts "#{k} (#{laborskill(k)}): #{v}"}
false
end
def initialize
@onupdate_handles = Array.new
end
def start
@onupdate_handles << df.onupdate_register (100, 10) { analyze_jobs }
@running = true
end
def stop
@onupdate_handles.each { |h| df.onupdate_unregister(h) }
@running = false
end
def status
@running ? "Running." : "Loaded."
end
end
case $script_args[0]
when 'start'
$ai = JobInfo.new
$ai.start
when 'end', 'stop'
$ai.stop
else
if $ai
puts $ai.status
else
puts "AI not started"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment