Author Topic: Simple Python Scripts for Mapper  (Read 3952 times)

Offline Ganado

  • Moderator
  • Dishonest Abe
Simple Python Scripts for Mapper
« on: February 21, 2014, 09:43:20 pm »
Edit: See this instead: http://fodev.net/forum/index.php/topic,29557.0.html by Realism. It's 100% better :>

This is a script I made using Python (3.x version) for the mapper. I tested it in FOnline: 2238 Mapper, version 1.29.0.
I don't know if someone else has already made something like this, or how useful it is as opposed to just copy-pasting, but anyway...

Code: [Select]
#
# fomap_gen_file_desert.py
# for FOnline engine maps (.fomap)
# tested in FOnline: 2238 Mapper, version 1.29.0
#

from random import *


########################
##  Initial Settings  ##
########################
print("This program will write a .fomap to current directory.")
print("If the name you choose already exists, it will overwrite that file.\n")
filename = input("Enter name of map, not including extension: ")
ext = '.fomap'

MaxHexX = int(input("Enter X-length of map: "))
MaxHexY = int(input("Enter Y-length of map: "))

print("Writing map file...")
f = open(filename+ext, 'w')



####################
##  Makes Header  ##
####################
#(lines 1 to 15)
f.write("[Header]\n")
f.write("Version              4\n")
f.write("MaxHexX              "+str(MaxHexX)+'\n')
f.write("MaxHexY              "+str(MaxHexY)+'\n')
f.write("WorkHexX             "+str(MaxHexX//2)+'\n')
f.write("WorkHexY             "+str(MaxHexY//2)+'\n')
f.write("ScriptModule         map_encounter\n")
f.write("ScriptFunc           map_init\n")
f.write("NoLogOut             0\n")
f.write("Time                 -1\n")
f.write("DayTime              300  600  1140 1380\n")
f.write("DayColor0            18  18  53\n")
f.write("DayColor1            128 128 128\n")
f.write("DayColor2            103 95  86\n")
f.write("DayColor3            51  40  29\n\n")



###################
##  Makes Tiles  ##
###################
f.write("[Tiles]\n")

if (MaxHexX%2 == 0):
    MaxHexX += 1

x_col = 0 
y_col = 0
tile_frm_type = "edg" #desert tiles, randomly placed
tile_frm_num  = 5003  #desert tiles

while (y_col <= MaxHexY - 2):
    f.write("tile       ")
    f.write(str(x_col) + "    ");
    x_col += 2;
    f.write(str(y_col) + "              ")
   
    if (x_col >= MaxHexX - 2):
        y_col += 2
        x_col = 0

    tile_frm_num = randrange(5000, 5008)
    f.write("art\\tiles\\"+ tile_frm_type + str(tile_frm_num) + ".frm\n")



#####################
##  Makes Objects  ##
#####################
f.write("[Objects]\n")

#tree7 = 1820
#tree8 = 1821
#tree9 = 1822
#tall big tree is 9730
#less tall big tree is 9731
#apple tree is 9910
#weed01 is 2102
#weed02 is 2103
#weed03 is 2104
#weed04 is 2105
#weed24 is 2125
#weed26 is 2127


#Makes Trees:
MapObjType = 1
TREE_NUM = 100
for i in range(0, TREE_NUM):
    MapX_rand = randrange(MaxHexX)
    MapY_rand = randrange(MaxHexY)
    ProtoId   = randrange(1820, 1823) #currently trees, random
   
    f.write("MapObjType           " + str(MapObjType) + '\n' )
    f.write("ProtoId              " + str(ProtoId)    + '\n' )
    f.write("MapX                 " + str(MapX_rand)  + '\n' )
    f.write("MapY                 " + str(MapY_rand) +'\n\n' )




#Mob Spawn is 21001, MapObjType 1.
   
"""
MapObjType           1
ProtoId              1820
MapX                 83
MapY                 41
ScriptName           prod_tree_firewood
FuncName             _EncTree
"""


###########################
##  Map Border Creation  ##
###########################
""" # these are Scroll Blockers:
MapObjType           2
ProtoId              4012
MapX                 145
MapY                 108
""" #


x_hex = int(MaxHexX/2)
y_hex = 0
hex_counter = 0

#Left Vertical Boundary:
while (x_hex < MaxHexX - 2 ):
    f.write("MapObjType           "+"2\n"       )
    f.write("ProtoId              "+"4012\n"    )
    f.write("MapX                 "+str(x_hex)+'\n')
    f.write("MapY                 "+str(y_hex)+'\n\n')
    if   (hex_counter == 0):
        hex_counter += 1
        x_hex += 1
        y_hex += 1
    elif (hex_counter == 1):
        hex_counter += 1
        y_hex += 1
    elif (hex_counter == 2):
        hex_counter += 1
        x_hex += 1
    elif (hex_counter == 3):
        y_hex += 1
        hex_counter = 0

#note: upper right corner seems to be about at MaxHexY/3

       
#Top Horizontal Boundary:
x_hex = int(MaxHexX/2)
y_hex = 1
hex_counter = 0
while (x_hex > 0):
    f.write("MapObjType           "+"2\n"       )
    f.write("ProtoId              "+"4012\n"    )
    f.write("MapX                 "+str(x_hex)+'\n')
    f.write("MapY                 "+str(y_hex)+'\n\n')
    if   (hex_counter == 0):
        x_hex -= 1
        y_hex += 1
        hex_counter += 1
    elif (hex_counter == 1):
        x_hex -= 1
        hex_counter = 0

#Right Vertical Boundary:
hex_counter = 0
while (y_hex < MaxHexY - 2):
    f.write("MapObjType           "+"2\n"       )
    f.write("ProtoId              "+"4012\n"    )
    f.write("MapX                 "+str(x_hex)+'\n')
    f.write("MapY                 "+str(y_hex)+'\n\n')
    if   (hex_counter == 0):
        hex_counter += 1
        x_hex += 1
        y_hex += 1
    elif (hex_counter == 1):
        hex_counter += 1
        y_hex += 1
    elif (hex_counter == 2):
        hex_counter += 1
        x_hex += 1
    elif (hex_counter == 3):
        y_hex += 1
        hex_counter = 0

#Bottom Horizontal Boundary:
hex_counter = 0
while (x_hex < MaxHexX - 2):
    f.write("MapObjType           "+"2\n"       )
    f.write("ProtoId              "+"4012\n"    )
    f.write("MapX                 "+str(x_hex)+'\n')
    f.write("MapY                 "+str(y_hex)+'\n\n')
    if   (hex_counter == 0):
        x_hex += 1
        y_hex -= 1
        hex_counter += 1
    elif (hex_counter == 1):
        x_hex += 1
        hex_counter = 0

f.close()
print("Done. Press Enter to quit.", end=""); input();

###

The first part of the script makes random desert tiles at any HexMax x/y to start. Note that it currently doesn't look too "natural" and looks somewhat repetitive compared to, for example, e_desert1.

The second part of the script makes the exit border around the generated map.
It should be ready to view in the mapper after writing to file (first load might take longer before you save it using the mapper).

The script will write to file in the same directory that the Python script is in.

Feel free to use/improve the code for any purpose, it's not too convenient at the moment. If I have time, I'll finish/improve the parts of the script that make the exit border, and probably some more natural-looking random attributes. There's probably some off-by-one errors possible on the map borders and scroll blockers, depending on if the number is odd or even.
« Last Edit: March 11, 2014, 04:59:48 pm by Ganado »
Error while opening cfg file: spawnnpc.cfg
Shit! Damn admins! Always ruining my fun! I guess I'll talk to them. WITH MY FISTS!!!! No seriously, I will write them a nice email or make a thread on the forums or something. Thanks!

Offline Wipe

  • Rotator
  • Random is god
Re: Simple Python Scripts for Mapper
« Reply #1 on: February 22, 2014, 06:15:36 am »
Mmm, python... Is there any reason why not use AngelScript and make it regular mapper script? Didn't check mapper for some time, but i think it was still possible to make simple map from scripts.
Games are meant to be created, not played...

Offline Ganado

  • Moderator
  • Dishonest Abe
Re: Simple Python Scripts for Mapper
« Reply #2 on: February 24, 2014, 04:41:04 am »
Oh, that sounds like it should be possible. I just didn't know, guess I'll do it through that if I have time.
Error while opening cfg file: spawnnpc.cfg
Shit! Damn admins! Always ruining my fun! I guess I'll talk to them. WITH MY FISTS!!!! No seriously, I will write them a nice email or make a thread on the forums or something. Thanks!