diff --git a/dfhack/python/examples/attachtest.py b/dfhack/python/examples/attachtest.py new file mode 100644 index 000000000..2f04437bc --- /dev/null +++ b/dfhack/python/examples/attachtest.py @@ -0,0 +1,43 @@ +import time +import math +import pydfapi + +df = pydfapi.API("Memory.xml") + +def test_attach(): + if not df.Attach(): + print "Unable to attach!" + return False + elif not df.Detach(): + print "Unable to detach!" + return False + else: + return True + +def suspend_test(): + print "Testing suspend/resume" + + df.Attach() + + t1 = time.time() + + for i in xrange(1000): + df.Suspend() + + if i % 10 == 0: + print "%i%%" % (i / 10,) + + df.Resume() + + t2 = time.time() + + df.Detach() + + print "suspend tests done in %0.9f seconds" % (t2 - t1,) + +if __name__ == "__main__": + if test_attach(): + suspend_test() + + print "Done. Press any key to continue" + raw_input() \ No newline at end of file diff --git a/dfhack/python/examples/miscutils.py b/dfhack/python/examples/miscutils.py new file mode 100644 index 000000000..64ccf9031 --- /dev/null +++ b/dfhack/python/examples/miscutils.py @@ -0,0 +1,36 @@ +_splatter_dict = { 0 : "Rock", + 1 : "Amber", + 2 : "Coral", + 3 : "Green Glass", + 4 : "Clear Glass", + 5 : "Crystal Glass", + 6 : "Ice", + 7 : "Coal", + 8 : "Potash", + 9 : "Ash", + 10 : "Pearlash", + 11 : "Lye", + 12 : "Mud", + 13 : "Vomit", + 14 : "Salt", + 15 : "Filth", + 16 : "Frozen? Filth", + 18 : "Grime", + 0xF2 : "Very Specific Blood (references a named creature)" } + +def get_splatter_type(mat1, mat2, creature_types): + from cStringIO import StringIO + + if mat1 in _splatter_dict: + return _splatter_dict[mat1] + elif mat1 == 0x2A or mat1 == 0x2B: + splatter = StringIO() + + if mat2 != -1: + splatter.write(creature_types[mat2]["id"] + " ") + + splatter.write("Blood") + + return splatter.getvalue() + else: + return "Unknown" diff --git a/dfhack/python/examples/position.py b/dfhack/python/examples/position.py new file mode 100644 index 000000000..82aae5b01 --- /dev/null +++ b/dfhack/python/examples/position.py @@ -0,0 +1,22 @@ +import sys +import pydfapi + +df = pydfapi.API("Memory.xml") + +if not df.Attach(): + print "Unable to attach!" + print "Press any key to continue" + raw_input() + sys.exit(1) + +pos = df.position + +print "view coords: %s" % (pos.view_coords,) +print "cursor coords: %s" % (pos.cursor_coords,) +print "window size: %s" % (pos.window_size,) + +if not df.Detach(): + print "Unable to detach!" + +print "Done. Press any key to continue" +raw_input() diff --git a/dfhack/python/examples/settlementdump.py b/dfhack/python/examples/settlementdump.py new file mode 100644 index 000000000..b30538ac9 --- /dev/null +++ b/dfhack/python/examples/settlementdump.py @@ -0,0 +1,8 @@ +import sys +from cStringIO import StringIO +import pydfapi + +df = pydfapi.API("Memory.xml") + +def print_settlement(settlement, english_words, foreign_words): + s = StringIO() diff --git a/dfhack/python/examples/suspendtest.py b/dfhack/python/examples/suspendtest.py new file mode 100644 index 000000000..65f11bf53 --- /dev/null +++ b/dfhack/python/examples/suspendtest.py @@ -0,0 +1,38 @@ +import pydfapi + +if __name__ == "__main__": + df = pydfapi.API("Memory.xml") + + if not df.Attach(): + print "Unable to attach!" + return False + + print "Attached, DF should be suspended now" + raw_input() + + df.Resume() + + print "Resumed, DF should be running" + raw_input() + + df.Suspend() + + print "Suspended, DF should be suspended now" + raw_input() + + df.Resume() + + print "Resumed, testing ForceResume. Suspend using SysInternals Process Explorer" + raw_input() + + df.Force_Resume() + + print "ForceResumed. DF should be running." + raw_input() + + if not df.Detach(): + print "Can't detach from DF" + return False + + print "Detached, DF should be running again" + raw_input() \ No newline at end of file diff --git a/dfhack/python/examples/treedump.py b/dfhack/python/examples/treedump.py new file mode 100644 index 000000000..7b62dfe91 --- /dev/null +++ b/dfhack/python/examples/treedump.py @@ -0,0 +1,66 @@ +import sys +from cStringIO import StringIO +import pydfapi + +df = pydfapi.API("Memory.xml") + +if not df.Attach(): + print "Unable to attach!\nPress any key to continue" + raw_input() + sys.exit(1) + +pos = df.position +veg = df.vegetation +mat = df.materials + +organics = mat.Read_Organic_Materials() + +x, y, z = pos.cursor_coords + +num_vegs = veg.Start() + +if x == -30000: + print "----==== Trees ====----" + + for i in xrange(num_vegs): + tree = veg.Read(i) + + t_x, t_y, t_z = tree["position"] + + print "%f/%f/%f, %f:%f" % (t_x, t_y, t_z, tree["type"], tree["material"]) +else: + print "----==== Tree at %i/%i/%i" % (x, y, z) + + for i in xrange(num_vegs): + tree = veg.Read(i) + + t_x, t_y, t_z = tree["position"] + t_type = tree["address"] + + if t_x == x and t_y == y and t_z == z: + s = StringIO() + + s.write("%f:%f = " % (tree["type"], tree["material"])) + + if t_type in (1, 3): + s.write("near-water ") + + s.write("%i " % (organics[tree["material"]]["id"]),) + + if t_type in (0, 1): + s.write("tree\n") + elif t_type in (2, 3): + s.write("shrub\n") + + print s.getvalue() + print "Address: 0x%x" % (tree["address"],) + + break + +veg.Finish() + +if not df.Detach(): + print "Unable to detach!" + +print "Done. Press any key to continue" +raw_input()