Angband.oook.cz
Angband.oook.cz
AboutDownloadVariantsLadderForumCompetitionSpoilersComicScreenshotsFunniesLinks

Go Back   Angband Forums > Angband > Development

Reply
 
Thread Tools Display Modes
Old March 9, 2012, 15:35   #1
buzzkill
Prophet
 
buzzkill's Avatar
 
Join Date: May 2008
Location: Indiana, USA
Posts: 2,904
Donated: $8
buzzkill is on a distinguished road
Help! Need a graf-foo.prf tool (again)

Anybody want to write a little code for me? It seems like I make this request about once a year.

Need a program that will read a typical graf-???.prf file and re-assign the hex values as specified by the user. Typically this program would be used to create a blank space in the tile sheet where I could then insert new tiles, while keeping them organized, near tiles of the same type. It's easy enough to move the tiles around. It's incredibly hard to make the prf changes necessitated by such a move.

The program will ask for a range of values to be altered, starting hex address : ending hex address, and then request a third hex address specifying where the values will 'end up'. Given the current prf file format, the program can make the assumption that first value on any given line is the vertical axis (y) and the second is the horizontal (x), not that it matters much as long as the values are handled as pairs.

Bare bones, it can assume that there is only one prf file present and therefore should read any prf file in it's own directory and exactly duplicate it line for line, substituting the altered values in place of the originals. It would output the results to a second file appended with -result.prf. Ideally, it will preform this task for each and every prf file present in it's directory.

Code:
SAMPLE OUTPUT:

GRAF-DVG.PRF FILE FOUND!

STARTING SELECTION : 0x85:0x95
ENDING SELECTION   : 0x85:0xFF

MOVE SELECTION TO : 0x86:0x80

GRAF-DVG-RESULT.PRF CREATED. FINISHED!
In the above example, I'm moving everything in row 0x85, beginning at column 0x95, and dropping it down to the first position in the next row. The program should begin by calculating the difference between the 'starting selection' and 'move selection to' values (in this case it's +1:-21 decimal). It should read through and duplicate the prf sequentially, one line at a time, altering in-range value pairs by the difference as it finds them.

Once again, bare bones, the programs doesn't need to check for user input error. If for some reason it can't complete it's task, it can just crash gracefully or output a file full of garbage, so long as it doesn't destroy the original file.

This program needs to run in windows. Something that can run in a DOS box will be sufficient. Written in C or C++ would be even better, and written as obviously/simply as possible so that I might be able to tweak it with my limited C skills (but anything that does the job as specified here would be just great).
__________________
www.mediafire.com/buzzkill - Get your 32x32 tiles here. UT32 now compatible Ironband and Quickband 9/6/2012.
My banding life on Buzzkill's ladder.
buzzkill is offline   Reply With Quote
Old March 10, 2012, 01:59   #2
Blue Baron
Adept
 
Join Date: Apr 2011
Posts: 103
Blue Baron is on a distinguished road
Several months ago I wrote some tools that might be close to what you want. one to split a tilesheet into individual tiles based on a pref file and another to build it back together based on a pref file.

They are in my github repository at github.com/blubaron. The tools would be in http://github.com/blubaron/tilesetpr.../tools/console.

I think for the tile image names, it only looks at the part of the pref line before the coordinates, but it has been several months since I looked at it.

Looking at your post again it seems like you want something that adjusts the pref file. The above tools affect the image file.

I don't know about entering the ranges while the program is running, but I can certainly make a console program that loads the ranges/destination from a text file. However, I don't know when I will get to it. Hopefully someone else will do it first. (The source of the above tools in my github repository, which may help.)
Blue Baron is offline   Reply With Quote
Old March 10, 2012, 06:50   #3
AnonymousHero
Swordsman
 
AnonymousHero's Avatar
 
Join Date: Jun 2007
Posts: 495
AnonymousHero is on a distinguished road
I'm not sure about the exact transformation of values that you want, but I can easily whip the processing bit up in a little Python program.

Give me a few minutes .
AnonymousHero is offline   Reply With Quote
Old March 10, 2012, 07:33   #4
AnonymousHero
Swordsman
 
AnonymousHero's Avatar
 
Join Date: Jun 2007
Posts: 495
AnonymousHero is on a distinguished road
Here's a first stab. You'll need to install Python 3.x -- If you don't already have it, you can get it from http://python.org/download/releases/3.2.2/

EDIT: Do you want all the graf-* files transformed identically once the coordinates have been provided? If so, I could easily add a "loop over all graf-*.prf files" bit of code. (I'd change the output file name format to 'result-X.prf', but hopefully that would be OK.)

Code:
#!/usr/bin/env python3
#
# Notes:
#
#   Always spits out upper-case hex digits; the current
#   graf-*.prf files seem to use inconsistent conventions for
#   this, so this would be non-trivial to work around.
#

import readline

def xform_input(prompt, transform):
    """Input values from user, apllying a transformation to the input."""
    while True:
        i = input(prompt)
        ti = transform(i)
        if ti is not None:
            return ti
        else:
            print("Could not understand input: %s" % (i,))

def xform_coordinate(s):
    fields = s.split(':')
    if len(fields) == 2:
        try:
            x = int(fields[0], 0)
            y = int(fields[1], 0)
        except ValueError as e:
            return None
        return (x,y)

def hexify(x):
    return ("0x%02X" % x).encode('ascii')

def transform_file(inf, outf, transform):
    with open(IN_FILE, "rb") as inf:
        with open(OUT_FILE, "wb") as outf:
            # transform
            for line in inf.readlines():
                if line.startswith(b"F:"):
                    fields = line.split(b':')
                    # transform coordinates
                    orig_x = int(fields[3], 0)
                    orig_y = int(fields[4], 0)
                    new_x,new_y = transform(orig_x, orig_y)
                    # build new line and output
                    new_fields = fields[0:3] + [hexify(new_x), hexify(new_y)]
                    outf.write(b":".join(new_fields))
                    outf.write(b"\n")
                else:
                    # output source line unchanged
                    outf.write(line)

# Here's the bit that actually does the transformation on
# "coordinates" in the "F:" line.
def transform(x,y):
    if ((x >= x1) and (x <= x2) and (y >= y1) and (y <= y2)):
        return (ox + (x - x1), oy + (y - y1))
    else:
        return (x,y)       # unchanged

# Main driver program
def main():
    x1,y1 = xform_input("Starting selection: ", xform_coordinate)
    x2,y2 = xform_input("Ending selection: ", xform_coordinate)
    ox,oy = xform_input("Move selection to: ", xform_coordinate)

    IN_FILE="graf-dvg.prf"
    OUT_FILE="graf-dvg-result.prf"
    transform_file(IN_FILE, OUT_FILE, transform)
(Be careful when copy/pasting this -- the whitespace must be preserved.)
AnonymousHero is offline   Reply With Quote
Old March 10, 2012, 15:24   #5
buzzkill
Prophet
 
buzzkill's Avatar
 
Join Date: May 2008
Location: Indiana, USA
Posts: 2,904
Donated: $8
buzzkill is on a distinguished road
Thanks guys. I probably won't get a chance to check this stuff out till Mondy.
__________________
www.mediafire.com/buzzkill - Get your 32x32 tiles here. UT32 now compatible Ironband and Quickband 9/6/2012.
My banding life on Buzzkill's ladder.
buzzkill is offline   Reply With Quote
Old March 12, 2012, 16:10   #6
buzzkill
Prophet
 
buzzkill's Avatar
 
Join Date: May 2008
Location: Indiana, USA
Posts: 2,904
Donated: $8
buzzkill is on a distinguished road
Quote:
Originally Posted by AnonymousHero View Post
Here's a first stab. You'll need to install Python 3.x -- If you don't already have it, you can get it from http://python.org/download/releases/3.2.2/

EDIT: Do you want all the graf-* files transformed identically once the coordinates have been provided? If so, I could easily add a "loop over all graf-*.prf files" bit of code. (I'd change the output file name format to 'result-X.prf', but hopefully that would be OK.)

(Be careful when copy/pasting this -- the whitespace must be preserved.)
Didn't work ... but I found found this...

Quote:
If you are using Windows, which also ships without GNU readline, you might want to consider using the pyreadline module instead, which is a readline replacement written in pure Python that interacts with the Windows clipboard.
... and installed this http://pypi.python.org/pypi/pyreadline

but it still doesn't work... I get this from Python IDLE

Code:
Traceback (most recent call last):
  File "C:\Users\Rob\Desktop\New folder\prf tool.py", line 10, in <module>
    import readline
  File "C:\Python32\lib\site-packages\readline.py", line 5, in <module>
    from pyreadline.rlmain import Readline
  File "C:\Python32\lib\site-packages\pyreadline\__init__.py", line 9, in <module>
    import unicode_helper, logger, clipboard, lineeditor, modes, console
ImportError: No module named unicode_helper
__init__.py reads...

Quote:
# -*- coding: utf-8 -*-
#************************************************* ****************************
# Copyright (C) 2003-2006 Gary Bishop.
# Copyright (C) 2006 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#************************************************* ****************************
import unicode_helper, logger, clipboard, lineeditor, modes, console
from rlmain import *
import rlmain
Using Windows 7 with a slight headache. Am I missing something painfully obvious?
__________________
www.mediafire.com/buzzkill - Get your 32x32 tiles here. UT32 now compatible Ironband and Quickband 9/6/2012.
My banding life on Buzzkill's ladder.
buzzkill is offline   Reply With Quote
Old March 12, 2012, 16:43   #7
ghengiz
Adept
 
ghengiz's Avatar
 
Join Date: Nov 2011
Location: Roaming in Terry Pratchett's Discworld
Posts: 178
ghengiz is on a distinguished road
Quote:
Originally Posted by buzzkill View Post
Using Windows 7 with a slight headache. Am I missing something painfully obvious?
but using windows?
more seriously, have you checked the library path of python? perhaps it has not been correctly set (strange but possible), or windows is not aware of its value (a reboot should fix this).
Or, the most obvious thing, are you sure unicode_helper is installed?
ghengiz is offline   Reply With Quote
Old March 12, 2012, 17:01   #8
buzzkill
Prophet
 
buzzkill's Avatar
 
Join Date: May 2008
Location: Indiana, USA
Posts: 2,904
Donated: $8
buzzkill is on a distinguished road
Quote:
Originally Posted by ghengiz View Post
but using windows?
more seriously, have you checked the library path of python? perhaps it has not been correctly set (strange but possible), or windows is not aware of its value (a reboot should fix this).
Or, the most obvious thing, are you sure unicode_helper is installed?
I'm not sure of anything. I was hoping that installing the Python IDE and executing the program would work. Then I hoped that installing pyreadline would work.

I fear that if I keep installing stuff without really knowing what the problem is, or what I'm doing, I'll get to a point where the actual solution will no longer work.

A quick search revealed that unicode_helper is indeed present in Python32/Lib/site-packages/pyreadline
__________________
www.mediafire.com/buzzkill - Get your 32x32 tiles here. UT32 now compatible Ironband and Quickband 9/6/2012.
My banding life on Buzzkill's ladder.
buzzkill is offline   Reply With Quote
Old March 12, 2012, 17:18   #9
Blue Baron
Adept
 
Join Date: Apr 2011
Posts: 103
Blue Baron is on a distinguished road
I just pushed a win32 C/C++ tool that I think will do what you want through files. It is at http://github.com/blubaron/tilesetpr...ws/adjust_pref. there is a binary there. If you want to compile it you will need the winmain.cpp file in that directory and many of the files in tilesetpref/tree/master/tools/console/tile_common.
Blue Baron is offline   Reply With Quote
Old March 12, 2012, 17:24   #10
AnonymousHero
Swordsman
 
AnonymousHero's Avatar
 
Join Date: Jun 2007
Posts: 495
AnonymousHero is on a distinguished road
You can just remove the "import readline" line.

It's not important.
AnonymousHero is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[3.3.x] Saving options/kemaps produces garbled prf file PowerWyrm Vanilla 1 November 28, 2011 10:20
x11-settings.prf not behaving like I would expect ulrichvonbek Vanilla 10 March 23, 2011 19:52
Tool-assisted Angband Derakon Vanilla 8 November 24, 2010 18:20
Saving PLAYER.prf *where* under Windows? TJA Vanilla 2 August 16, 2007 23:32


All times are GMT +1. The time now is 23:08.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.