diff --git a/README.md b/README.md index 0758e47..4c32ae6 100644 --- a/README.md +++ b/README.md @@ -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. ## 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 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 diff --git a/mmlv_library.py b/mmlv_library.py index 8afd80d..711fbf7 100644 --- a/mmlv_library.py +++ b/mmlv_library.py @@ -8,20 +8,20 @@ It could also work by calling a function, say place_tile(tilename, x, y) and the try: import configparser, os, definitions import requests - import csv except ModuleNotFoundError: print("module not found, please install configparser, requests, os and have the definitions.py in the same folder as mmlv_library.py") Exception(ModuleNotFoundError) ### init global variables section = "Level" -config = configparser.ConfigParser() +config = configparser.ConfigParser(allow_no_value=True) safe_config = configparser.SafeConfigParser() alias = definitions.aliases max_x = 799 max_y = 279 max_room_x = 49 max_room_y = 19 +used_blocks = set() ### # 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) 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. def read_file(filename="blank.mmlv"): 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(): options = config.options(section) @@ -139,9 +154,9 @@ def string_to_number(value): return value ###----------------------------------------------------------------------------------------------------------------### - -def set_values(key, x="", y="", value=""): - if value != "": +#I need to add the "mark_block_as_used" here +def set_values(key, x=0, y=0, value=False): + if value != False: value = str(value) keys_with_string_values = ("4a", "1a", "0v") if key in value: @@ -150,7 +165,9 @@ def set_values(key, x="", y="", value=""): else: 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 @@ -158,23 +175,27 @@ def optimize_mmlv(verbose=False): for key in config[section]: value_string = config[section][key] try: - value = float(value_string.replace('"', "")) - floatified = float(value) - config[section][key] = str(floatified) + value = float(value_string.strip('"')) + if value.is_integer(): + value = int(value) + config[section][key] = str(value) except ValueError: if verbose == True: print("this was a float or a string",value_string) else: continue - -def write_changes(filename="sample.mmlv", optimize_enable=True): +#frozen +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.' - for key,value in zip("srqp", [set_apos(4480), set_apos(0), set_apos(12800), set_apos(0)]): - config.set(section,f"1{key}", value) - - optimize_mmlv() - + for key,value in zip("srqp", [4480, 0, 12800, 0]): + config.set(section,f"1{key}", str(value)) + if optimize_enable: + 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: 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]) y16 = str(by16[1]) - #if is_block_used(x, y): - #this needs a check to see if the coordinates are already taken, and deal with object-tile conflicts accordingly. - # opt = get_options() - #WIP + if (x,y) in used_blocks: + del_tile(x,y) + option = 0 error = False tile_types = { @@ -267,7 +287,7 @@ def del_tile(x,y,times_16=False): current = f"{letter}{x},{y}" 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): by16 = conv_coordinates_x16(x, y) x16 = str(by16[0]) @@ -328,7 +348,6 @@ def set_ver(version=False): # inspected _____ frozen -#this needs the same calculations of the room active function def connect_screen(h_or_v, roomX, roomY): coord = room_coordinates(roomX, roomY) 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 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. - diff --git a/testing_grounds.py b/testing_grounds.py index 2919f9f..4505654 100644 --- a/testing_grounds.py +++ b/testing_grounds.py @@ -20,30 +20,17 @@ mmlv.read_file("sample.mmlv") #print(mmlv.get_block_contents(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 -# for x in range(mmlv.max_x+1): -# for y in range(mmlv.max_y+1): -# #if mmlv.is_block_used(x,y): -# repeat = repeat + 1 -# print(x,y) -# mmlv.set_tile(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") +for x in range(mmlv.max_x+1): + for y in range(mmlv.max_y+1): + #if mmlv.is_block_used(x,y): + # repeat = repeat + 1 + # print(x,y) + mmlv.set_tile(x, y) + #print(mmlv.get_block_contents(x, y)) + # print(repeat) #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)