ruby: buildings tweaks

develop
jj 2012-09-02 01:38:58 +02:00
parent acdf0ffb42
commit a4c028b3b7
2 changed files with 56 additions and 9 deletions

@ -211,15 +211,13 @@ module DFHack
# link bld into other rooms if it is inside their extents # link bld into other rooms if it is inside their extents
def building_linkrooms(bld) def building_linkrooms(bld)
didstuff = false
world.buildings.other[:ANY_FREE].each { |ob| world.buildings.other[:ANY_FREE].each { |ob|
next if !ob.is_room or ob.z != bld.z next if !ob.is_room or ob.z != bld.z
next if !ob.room.extents or !ob.isExtentShaped or ob.room.extents[ob.room.width*(bld.y1-ob.room.y)+(bld.x1-ob.room.x)] == 0 next if !ob.room.extents or !ob.isExtentShaped or ob.room.extents[ob.room.width*(bld.y1-ob.room.y)+(bld.x1-ob.room.x)] == 0
didstuff = true ui.equipment.update.buildings = true
ob.children << bld ob.children << bld
bld.parents << ob bld.parents << ob
} }
ui.equipment.update.buildings = true if didstuff
end end
# link the building into the world, set map data, link rooms, bld.id # link the building into the world, set map data, link rooms, bld.id
@ -276,6 +274,43 @@ module DFHack
building_createdesign(bld, rough) building_createdesign(bld, rough)
end end
# construct an abstract building (stockpile, farmplot, ...)
def building_construct_abstract(bld)
if bld.getType == :Stockpile
max = df.world.buildings.other[:STOCKPILE].map { |s| s.stockpile_number }.max
bld.stockpile_number = max.to_i + 1
end
building_link bld
if !bld.flags.exists
bld.flags.exists = true
bld.initFarmSeasons
end
end
def building_setowner(bld, unit)
return unless bld.is_room
return if bld.owner == unit
if bld.owner
if idx = bld.owner.owned_buildings.index { |ob| ob.id == bld.id }
bld.owner.owned_buildings.delete_at(idx)
end
if spouse = bld.owner.relations.spouse_tg and
idx = spouse.owned_buildings.index { |ob| ob.id == bld.id }
spouse.owned_buildings.delete_at(idx)
end
end
bld.owner = unit
if unit
unit.owned_buildings << bld
if spouse = bld.owner.relations.spouse_tg and
!spouse.owned_buildings.index { |ob| ob.id == bld.id } and
bld.canUseSpouseRoom
spouse.owned_buildings << bld
end
end
end
# creates a job to deconstruct the building # creates a job to deconstruct the building
def building_deconstruct(bld) def building_deconstruct(bld)
job = Job.cpp_new job = Job.cpp_new

@ -133,6 +133,8 @@ module DFHack
def _field_names ; _fields_ancestors.map { |n, o, s| n } ; end def _field_names ; _fields_ancestors.map { |n, o, s| n } ; end
def _rtti_classname ; self.class._rtti_classname ; end def _rtti_classname ; self.class._rtti_classname ; end
def _sizeof ; self.class._sizeof ; end def _sizeof ; self.class._sizeof ; end
def ==(o) ; o.kind_of?(Compound) and o._memaddr == _memaddr ; end
@@inspecting = {} # avoid infinite recursion on mutually-referenced objects @@inspecting = {} # avoid infinite recursion on mutually-referenced objects
def inspect def inspect
cn = self.class.name.sub(/^DFHack::/, '') cn = self.class.name.sub(/^DFHack::/, '')
@ -289,12 +291,12 @@ module DFHack
# XXX shaky... # XXX shaky...
def _set(v) def _set(v)
if v.kind_of?(Pointer) case v
DFHack.memory_write_int32(@_memaddr, v._getp) when Pointer; DFHack.memory_write_int32(@_memaddr, v._getp)
elsif v.kind_of?(MemStruct) when MemStruct; DFHack.memory_write_int32(@_memaddr, v._memaddr)
DFHack.memory_write_int32(@_memaddr, v._memaddr) when Integer; DFHack.memory_write_int32(@_memaddr, v)
else when nil; DFHack.memory_write_int32(@_memaddr, 0)
_get._set(v) else _get._set(v)
end end
end end
@ -328,6 +330,16 @@ module DFHack
self self
end end
def _set(v)
case v
when Pointer; DFHack.memory_write_int32(@_memaddr, v._getp)
when MemStruct; DFHack.memory_write_int32(@_memaddr, v._memaddr)
when Integer; DFHack.memory_write_int32(@_memaddr, v)
when nil; DFHack.memory_write_int32(@_memaddr, 0)
else raise "cannot PointerAry._set(#{v.inspect})"
end
end
def [](i) def [](i)
addr = _getp(i) addr = _getp(i)
return if addr == 0 return if addr == 0