finished check_overlap and mark_block_as_used

This commit is contained in:
Timothy GFO 2023-10-30 12:26:42 -04:00
parent 723f8afe49
commit 350400d56f
3 changed files with 52 additions and 48 deletions

View file

@ -26,7 +26,8 @@ When in beta, you should be able to import as a module in another python program
If you find a bug, remember to create an issue about it. But don't do it right now, there is more bug than code. If you find a bug, remember to create an issue about it. But don't do it right now, there is more bug than code.
## Roadmap ## Roadmap
add get functions save changes to temporary file, then rename original to temp name, then rename the changed file to original name
add a get function for every set
add automatic room activation to set_tile add automatic room activation to set_tile
add ability to all functions to accept raw x, y values (the times 16 numbers in mmlv) and raw codes (like "a" instead of "active") add ability to all functions to accept raw x, y values (the times 16 numbers in mmlv) and raw codes (like "a" instead of "active")
complete set_tile, set_spike, set_ladder complete set_tile, set_spike, set_ladder

View file

@ -8,20 +8,20 @@ It could also work by calling a function, say place_tile(tilename, x, y) and the
try: try:
import configparser, os, definitions import configparser, os, definitions
import requests import requests
import csv
except ModuleNotFoundError: except ModuleNotFoundError:
print("module not found, please install configparser, requests, os and have the definitions.py in the same folder as mmlv_library.py") print("module not found, please install configparser, requests, os and have the definitions.py in the same folder as mmlv_library.py")
Exception(ModuleNotFoundError) Exception(ModuleNotFoundError)
### init global variables ### init global variables
section = "Level" section = "Level"
config = configparser.ConfigParser() config = configparser.ConfigParser(allow_no_value=True)
safe_config = configparser.SafeConfigParser() safe_config = configparser.SafeConfigParser()
alias = definitions.aliases alias = definitions.aliases
max_x = 799 max_x = 799
max_y = 279 max_y = 279
max_room_x = 49 max_room_x = 49
max_room_y = 19 max_room_y = 19
used_blocks = set()
### ###
# converts from multiples of 16 to normal x and y values. for use in reading # converts from multiples of 16 to normal x and y values. for use in reading
@ -52,10 +52,25 @@ def room_coordinates(roomX, roomY):
y = str(y) y = str(y)
return [x, y] return [x, y]
def mark_block_as_used(coord_tuple):
global used_blocks
if type(coord_tuple) is tuple:
if len(coord_tuple) == 2:
if all([isinstance(item, int) and item < max_x and item >= 0 for item in coord_tuple]):
if coord_tuple not in used_blocks:
print("worked")
used_blocks.add(coord_tuple)
else:
print("ERROR: mark_block_as_used did not recieve a tuple")
#done. writes the changes to file specified in global variable filename. #done. writes the changes to file specified in global variable filename.
def read_file(filename="blank.mmlv"): def read_file(filename="blank.mmlv"):
config.read(filename) config.read(filename)
for x in range(max_x+1):
for y in range(max_y+1):
if is_block_used(x, y):
mark_block_as_used((x,y))
def get_options(): def get_options():
options = config.options(section) options = config.options(section)
@ -139,9 +154,9 @@ def string_to_number(value):
return value return value
###----------------------------------------------------------------------------------------------------------------### ###----------------------------------------------------------------------------------------------------------------###
#I need to add the "mark_block_as_used" here
def set_values(key, x="", y="", value=""): def set_values(key, x=0, y=0, value=False):
if value != "": if value != False:
value = str(value) value = str(value)
keys_with_string_values = ("4a", "1a", "0v") keys_with_string_values = ("4a", "1a", "0v")
if key in value: if key in value:
@ -150,7 +165,9 @@ def set_values(key, x="", y="", value=""):
else: else:
Exception("no value specified in set_values. that is ironic.") Exception("no value specified in set_values. that is ironic.")
#def check_overlap(x,y): def check_overlap(x,y):
if (x,y) in used_blocks:
return true
## frozen ## frozen
@ -158,23 +175,27 @@ def optimize_mmlv(verbose=False):
for key in config[section]: for key in config[section]:
value_string = config[section][key] value_string = config[section][key]
try: try:
value = float(value_string.replace('"', "")) value = float(value_string.strip('"'))
floatified = float(value) if value.is_integer():
config[section][key] = str(floatified) value = int(value)
config[section][key] = str(value)
except ValueError: except ValueError:
if verbose == True: if verbose == True:
print("this was a float or a string",value_string) print("this was a float or a string",value_string)
else: else:
continue continue
#frozen
def write_changes(filename="sample.mmlv", optimize_enable=True): def write_changes(filename="sample.mmlv", optimize_enable=True, splash_message=True):
#Well well well... this for loop is here to make every thing work. I'm not sure for what the 1s, 1q,1r and 1p keys are, but if they are the same as a blank mmlv created by mmm, it just doesn't work. It's weird.' #Well well well... this for loop is here to make every thing work. I'm not sure for what the 1s, 1q,1r and 1p keys are, but if they are the same as a blank mmlv created by mmm, it just doesn't work. It's weird.'
for key,value in zip("srqp", [set_apos(4480), set_apos(0), set_apos(12800), set_apos(0)]): for key,value in zip("srqp", [4480, 0, 12800, 0]):
config.set(section,f"1{key}", value) config.set(section,f"1{key}", str(value))
if optimize_enable:
optimize_mmlv() optimize_mmlv()
if splash_message:
if not config.has_section("Py-MMLV"):
config.add_section("Py-MMLV")
config.set("Py-MMLV","; This level was modified using Py-MMLV by Timothy GFO (https://gitlab.com/gamefeveronline/py-mmlv)", None)
with open(filename, 'w') as configfile: with open(filename, 'w') as configfile:
config.write(configfile, space_around_delimiters=False) config.write(configfile, space_around_delimiters=False)
@ -207,10 +228,9 @@ def set_tile(x, y, tilename="mm1cuttile", tile_type="normal", tile_subtype=False
x16 = str(by16[0]) x16 = str(by16[0])
y16 = str(by16[1]) y16 = str(by16[1])
#if is_block_used(x, y): if (x,y) in used_blocks:
#this needs a check to see if the coordinates are already taken, and deal with object-tile conflicts accordingly. del_tile(x,y)
# opt = get_options()
#WIP
option = 0 option = 0
error = False error = False
tile_types = { tile_types = {
@ -267,7 +287,7 @@ def del_tile(x,y,times_16=False):
current = f"{letter}{x},{y}" current = f"{letter}{x},{y}"
config.remove_option(section, current) config.remove_option(section, current)
### should check if there is another "1t" (and maybe "r" and "e" idk) and delete everything on that tile. ## finally frozen
def set_player(x, y, player_type="mm",direction="right", allow_duplicates=False): def set_player(x, y, player_type="mm",direction="right", allow_duplicates=False):
by16 = conv_coordinates_x16(x, y) by16 = conv_coordinates_x16(x, y)
x16 = str(by16[0]) x16 = str(by16[0])
@ -328,7 +348,6 @@ def set_ver(version=False):
# inspected _____ frozen # inspected _____ frozen
#this needs the same calculations of the room active function
def connect_screen(h_or_v, roomX, roomY): def connect_screen(h_or_v, roomX, roomY):
coord = room_coordinates(roomX, roomY) coord = room_coordinates(roomX, roomY)
x = coord[0] x = coord[0]
@ -373,6 +392,3 @@ def set_background(roomX, roomY, bg_id):
def list_tiles < this will return a list of tiles already on the level as a list each one with every of the 5 values for the 5 letters def list_tiles < this will return a list of tiles already on the level as a list each one with every of the 5 values for the 5 letters
def set_weapons def set_weapons
""" """
# A problem that has to be fixed some day is that EVERY TILE WILL NOT APPEAR ON EDITOR TILL A PLAY TEST. It has something to do with the 1s, 1q,1p and 1r entries. I set the max value that works but on MMLV Editor the value changes if a tile is more torward the top left. Maybe an algorithm is needed to count every tile from the level and update those values accordingly.

View file

@ -20,30 +20,17 @@ mmlv.read_file("sample.mmlv")
#print(mmlv.get_block_contents(1,2)) #print(mmlv.get_block_contents(1,2))
#print(mmlv.is_block_used(1,2)) #print(mmlv.is_block_used(1,2))
n = mmlv.max_x * mmlv.max_y
t0 = time.time()
for i in range(n): mmlv.is_block_used(0,0,details=False, alt_algorithm=False)
# repeat = 0 # repeat = 0
# for x in range(mmlv.max_x+1): for x in range(mmlv.max_x+1):
# for y in range(mmlv.max_y+1): for y in range(mmlv.max_y+1):
# #if mmlv.is_block_used(x,y): #if mmlv.is_block_used(x,y):
# repeat = repeat + 1 # repeat = repeat + 1
# print(x,y) # print(x,y)
# mmlv.set_tile(x, y) mmlv.set_tile(x, y)
# #print(mmlv.get_block_contents(x, y)) #print(mmlv.get_block_contents(x, y))
t1 = time.time()
#
total_n = t1-t0
print(total_n)
n = mmlv.max_x * mmlv.max_y
t0 = time.time()
for i in range(n): mmlv.is_block_used(0,0,details=False, alt_algorithm=True)
t1 = time.time()
#
total_n = t1-t0
print(total_n, "alt")
# print(repeat) # print(repeat)
#mmlv.is_block_used(0,0,details=False,alt_algorithm=True) #mmlv.is_block_used(0,0,details=False,alt_algorithm=True)
#mmlv.write_changes("sample.mmlv") mmlv.write_changes("sample.mmlv")
#del_tile(1,1) #del_tile(1,1)