Created
September 6, 2013 17:32
-
-
Save pleonex/6467030 to your computer and use it in GitHub Desktop.
Plug-in to create an interactive world map of "The Legend of Zelda" game for NES device.
It works under Gimp 2.8.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# MAP CREATOR | |
# by pleoNeX -- Version 1.2 -- 29/07/2013 | |
from gimpfu import * | |
import os | |
# Constants | |
SCREENSHOT_PATH = '/var/run/media/benito/DATA/Juegos/Zelda/virtuanes097e/snapshot/' | |
OUTPUT_PATH = '/home/benito/' | |
OUTPUT_NAME = 'map' | |
NUM_X = 16 # Number of horizontal regions | |
NUM_Y = 8 # Number of vertical regions | |
WIDTH = 256 # Region width | |
HEIGHT = 176 # Region height | |
OFFSET = { "L": [ -WIDTH, 0 ], "R": [ WIDTH, 0 ], "U": [0, -HEIGHT ], "D": [0, HEIGHT] } | |
def plugin_main(*args): | |
print '' | |
print '' | |
print "Initialazing map creator... (by pleonex)" | |
# Load or create a map and start the loop | |
if os.path.exists( os.path.join(OUTPUT_PATH, OUTPUT_NAME) + ".xcf" ): | |
print 'Map detected' | |
mapzone, group, wp = load_map() | |
play_loop(mapzone, group, wp) | |
else: | |
mapzone, group = create_map() | |
play_loop(mapzone, group) | |
# Finally save | |
pdb.file_png_save_defaults(mapzone, group, os.path.join(OUTPUT_PATH, OUTPUT_NAME) + ".png", OUTPUT_NAME) | |
pdb.gimp_xcf_save(0, mapzone, group, os.path.join(OUTPUT_PATH, OUTPUT_NAME) + ".xcf", OUTPUT_NAME) | |
pdb.gimp_image_delete(mapzone) | |
print "Done!" | |
print '' | |
def create_map(): | |
mapzone = pdb.gimp_image_new(NUM_X * WIDTH, NUM_Y * HEIGHT, RGB) | |
# Layer group and background layer | |
group = pdb.gimp_layer_group_new(mapzone) | |
pdb.gimp_image_insert_layer(mapzone, group, None, 0) | |
mainlayer = pdb.gimp_layer_new(mapzone, 4096, 1472, RGB_IMAGE, "Main", 100, NORMAL_MODE) | |
pdb.gimp_image_insert_layer(mapzone, mainlayer, group, 0) | |
return mapzone, group | |
def load_map(): | |
mapzone = pdb.gimp_xcf_load(0, os.path.join(OUTPUT_PATH, OUTPUT_NAME) + ".xcf", OUTPUT_NAME) | |
group = mapzone.layers[0] # First layer is the group | |
walkPos = [] | |
for layer in group.children: | |
if layer.name == "Main": | |
continue | |
walkPos.append( layer.offsets ) | |
print layer.offsets, | |
print ' -> ' + layer.name | |
return mapzone, group, walkPos | |
def play_loop(image, group, walkedPos = []): | |
print '' | |
# Initial pos | |
initialPos = '' | |
while len(initialPos) != 3 and initialPos != 'S': | |
initialPos = raw_input("Initial region as 'X Y' ('S' to start region): ").upper() | |
print "" | |
if initialPos == 'S': | |
x = 7 * WIDTH | |
y = 7 * HEIGHT | |
else: | |
x = int(initialPos[0]) * WIDTH | |
y = int(initialPos[2]) * HEIGHT | |
# Loop | |
while True: | |
# Get position | |
if initialPos == None: | |
move = "" | |
while move not in OFFSET.keys() and move != 'Q' and move != 'S': | |
move = raw_input("Movement (L->left, R->right, U->up, D->down, Q->Quit, S->Start): ").upper() | |
if move == 'S': | |
initialPos = True | |
x = 7 * WIDTH | |
y = 7 * HEIGHT | |
continue | |
elif move == 'Q': | |
break | |
x += OFFSET[move][0] | |
y += OFFSET[move][1] | |
else: | |
initialPos = None | |
if (x, y) not in walkedPos: | |
fpath = get_lastFile() # Gets the last image name created | |
walkedPos.append( (x, y) ) | |
print "Adding image " + os.path.basename(fpath) + " to (" + str(x) + ", " + str(y) + ")" | |
add_region(image, group, fpath, x, y) | |
else: | |
print "(" + str(x) + ", " + str(y) + ") already saved." | |
print "" | |
def add_region(image, group, fpath, x, y): | |
region = pdb.gimp_file_load_layer(image, fpath) # Load image as layer | |
pdb.gimp_image_insert_layer(image, region, group, 0) # Add layer | |
pdb.gimp_layer_resize(region, WIDTH, HEIGHT, 0, -64) # Get only map part | |
pdb.gimp_layer_set_offsets(region, x, y) # Move it to right position | |
def get_lastFile(): | |
lastTime = None | |
fpath = None | |
for fname in os.listdir(SCREENSHOT_PATH): | |
currpath = os.path.join(SCREENSHOT_PATH, fname) | |
ftime = os.path.getmtime(currpath) | |
if lastTime == None or lastTime < ftime: | |
lastTime = ftime | |
fpath = currpath | |
return fpath | |
register( | |
"zelda_map_creator", | |
"Create a map from screenshots", | |
"Create a map from screenshots", | |
"pleoNeX", | |
"pleoNeX", | |
"2013", | |
"<Toolbox>/Xtns/Languages/Python-Fu/Plugin/Zelda", | |
"", | |
[], | |
[], | |
plugin_main) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment