Staredit Network > Forums > SC1 UMS Mapmaking Assistance > Topic: Example of using PyMS or other APIs to change/add location data
Example of using PyMS or other APIs to change/add location data
Mar 3 2019, 2:42 pm
By: sethmachine  

Mar 3 2019, 2:42 pm sethmachine Post #1



Hi,

I'm looking for an API that can be used programmatically (i.e. via Python) to change location data of a .scx map.

There would need to be functions that looks like this:

Code
class Location:
 name: string name of location
 x, y: coordinates of location
 length / width: dimensions of location (probably can be determined from x,y or something)
 properties: e.g. what flags are set (e..g. only detect ground units, etc.)

def add_location(mapfile, location: Location) -> bool:
 """Adds a location to the map file"""

def remove_location(mapfile, name):
 """Removes a location from the mapfile with the given name
 """

def get_locations(mapfile):
 """Returns a list of all locations in the map



I need these APIs, or examples of how to create them. Simply pointing me to a GitHub repo is not super helpful.



None.

Mar 3 2019, 5:15 pm MinuteMan Post #2



All of those functions are already available with EUD Editor 2. No separate API is needed.


Post has been edited 1 time(s), last time on Mar 3 2019, 5:26 pm by MinuteMan.




Mar 3 2019, 9:28 pm Ultraviolet Post #3



Quote from MinuteMan
All of those functions are already available with EUD Editor 2. No separate API is needed.

SetLocationToUnit sounds like it would be useful to me. Could you explain how this is used?




Mar 3 2019, 9:38 pm sethmachine Post #4



Quote from MinuteMan
All of those functions are already available with EUD Editor 2. No separate API is needed.

I am not looking for a GUI editor, but an actual library I can import into Python.

Could you clarify and explain how to use EUD Editor's API in Python?

Thanks



None.

Mar 4 2019, 12:20 am MinuteMan Post #5



Quote from Ultraviolet
SetLocationToUnit sounds like it would be useful to me. Could you explain how this is used?




Spawned extra vulture at start only to display that it's not just moving to any vulture, only whichever has the UNITPTR set to it.



Step 1: Load function from EUDE2
Step 2: Define UNITPTR
Step 3: Call Function with preferred arguments
Note: Will receive EUD error if attempting to call UNITPTR before it's been created. Recommend a conditional argument to prevent this.




Mar 4 2019, 12:27 am MinuteMan Post #6



Quote from sethmachine

I am not looking for a GUI editor, but an actual library I can import into Python.

Could you clarify and explain how to use EUD Editor's API in Python?

Thanks

I guess I still don't understand the reasoning for your python requirement, but you can always use these functions in your own custom python script and import it with EUDE2.





Mar 4 2019, 2:08 am sethmachine Post #7



Quote from MinuteMan

I guess I still don't understand the reasoning for your python requirement, but you can always use these functions in your own custom python script and import it with EUDE2.

Python is a requirement because how else would you manage a large codebase for a map? The editor GUI is really clunky and unusable, and I prefer to write it all in a modern IDE like PyCharm.

It'd be great if someone (or you) could write a tutorial that shows how to do EUDE2 in Python.

Not sure if these are the repos: https://github.com/phu54321/euddraft and https://github.com/phu54321/eudplib but they aren't documented well, and it's really confusing what's going on in any of the code (most objects seem to lack docstrings). There's also no readme explaining how to use them as libraries.



None.

Mar 4 2019, 2:25 am MinuteMan Post #8



Almost all "advanced" options are done with rawcode as-is; I fail to see how that is any less "clunky" than writing it yourself in a separate instance that then has no error-checking or resource compilation. Seems quite backwards to me personally.

Again though, just write your own script and import it as shown in my previous screenshot. You can place it in your EUDDraft/Libs folder for acquisition. Do it all in python if you wish, call upon it from EUDE2 as desired.




Mar 4 2019, 4:34 am Ultraviolet Post #9



Quote from MinuteMan
Quote from Ultraviolet
SetLocationToUnit sounds like it would be useful to me. Could you explain how this is used?




Spawned extra vulture at start only to display that it's not just moving to any vulture, only whichever has the UNITPTR set to it.



Step 1: Load function from EUDE2
Step 2: Define UNITPTR
Step 3: Call Function with preferred arguments
Note: Will receive EUD error if attempting to call UNITPTR before it's been created. Recommend a conditional argument to prevent this.

Thank you for this. If I was interested in cycling through all units of a certain type, would that be possible using this function (or any other)? My intended goal is to cycle through all infested kerrigans for a certain player and move a burrowed unit underneath them at an interval to reduce the movement speed of all infested kerrigan units. Giving the units away to another player for this cycling would be undesirable as it would interrupt the units' attacks each time.




Mar 4 2019, 5:04 am MinuteMan Post #10



If the only goal is to affect unit speed, it is probably easiest to just change movement control to flingy.dat and adjust it's speed from there.


Then you can adjust Speed/Acceleration/Turn Radius/etc as desired





Mar 4 2019, 5:07 am Ultraviolet Post #11



Yes, this seems much less clunky than the other approach. Thanks!




Mar 4 2019, 3:49 pm poiuy_qwert Post #12

PyMS and ProTRG developer

I think there is some confusion here. sethmachine is asking for a library to programmatically modify the map file (chk/scm/scx), not a way to modify in game values while the map is being played (EUD's).

PyMS does have the functionality to modify maps programmatically, though it may be slightly lower level than what you were hoping for. I was planning to make a "Map" class to help manage everything at a higher level, but have not gotten around to it. It is also not well documented, but I am happy to provide help if needed. Here is an example (note: I am writing this in the browser, so it might not be fully correct):
Code
from Libs.SFmpq import *
from Libs.CHK import *

# If you are modifying a .scm/.scx, its an MPQ which contains a .chk and .wav files
scmap = MpqOpenArchiveForUpdateEx(path_to_map)
if SFInvalidHandle(scmap):
    abort()

chkfile = SFileOpenFileEx(scmap, 'staredit\\scenario.chk')
if SFInvalidHandle(chkfile):
    MpqCloseUpdatedArchive(scmap)
    abort()
chkdata = SFileReadFile(chkfile)[0]
SFileCloseFile(chkfile)

chk = CHK()
# If you have the chk as an external file instead of in a .scm/.scx you can just use load_file/save_file and skip all mpq stuff
chk.load_data(chkdata)

# The MRGN section contains the locations
mrgn = chk.get_section(CHKSectionMRGN.NAME)

# Make all locations in the top left, and increasing in size
for (n,location) in enumerate(mrgn.locations):
    location.startX = 0
    location.startY = 0
    location.endX = 32 * (n+1)
    location.endY = 32 * (n+1)

chkdata = chk.save_data()
MpqAddFileFromBuffer(scmap, chkdata, 'staredit\\scenario.chk', MAFA_COMPRESS | MAFA_REPLACE_EXISTING)
MpqCloseUpdatedArchive(scmap)


If you need more help, or are having any issues, I would be glad to help out.

Edit: Also, this is probably the least used/tested part of PyMS, since it's primarily a toolset for modders, and most modders are not doing mapping related tasks. Though I have used it myself without any issues, I would recommend being careful with your maps, keeping a backup just in case of corruption.

Post has been edited 1 time(s), last time on Mar 4 2019, 4:13 pm by poiuy_qwert.




Mar 4 2019, 5:09 pm sethmachine Post #13



Thanks poiuy_qwert, that is exactly the example I've been looking for.

Quote
though it may be slightly lower level than what you were hoping for

Just out of curiousity, is every modder a masochist who doesn't like to write maintainable, reusable, and easy to understand code? It seems difficult to reuse most of the cool projects for more general purposes, which is too bad, because you guys do awesome work! But I guess since the primary "customer" is the player base and not other programmers, that may be less time is spent on readability of the code.




Mar 4 2019, 5:32 pm poiuy_qwert Post #14

PyMS and ProTRG developer

There are so few people who make content for starcraft, and even less of those people are programmers, and even less of those people combine both things. So people either build stuff just for themselves, or are targeting the people that actually use their tool, which are generally basic users that want a GUI. It's hard to put effort into something that seemingly noone cares about, and which will probably never get used.

Personally, I originally built PyMS with the intention of it being usable as a library, so other people could build tools and scripts with and contribute back. But that never really happened, so I focused more of my efforts on breadth of the tools, and more specifically on the tools which people actually used and provided feedback on. It also probably didn't help that I wrote all this code a long time ago when I was a much worse programmer. Luckily the CHK support was added much later than other stuff, so is generally a lot better than other parts of PyMS. I have tried to get people interested in the library side of PyMS, and specifically the .chk support, so that I would have more drive to support those things, but it just has not happened yet. Maybe you will change that :P




Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[12:46 pm]
razorback9423 -- :wob: KANDA FOREVER!
[2019-9-20. : 2:41 pm]
razorback9423 -- NudeRaider
NudeRaider shouted: MTiger156 power over:wob:ing?
No, POWER UNDER :wob: ing!
[2019-9-20. : 12:53 pm]
NudeRaider -- MTiger156
MTiger156 shouted: Power over:wob:ing
power over:wob:ing?
[2019-9-20. : 12:27 pm]
MTiger156 -- crap...
[2019-9-20. : 12:27 pm]
MTiger156 -- Power over:wob:ing
[2019-9-20. : 11:50 am]
UEDCommander -- WARNING: Unit Unplaceable! (u) (69, 69)
[2019-9-20. : 9:26 am]
lil-Inferno -- :wob:
[2019-9-20. : 8:25 am]
Black_Overseer -- razorback9423
razorback9423 shouted: WARNING: Unit Unplaceable! (Aldaris) (1656,2996)
You have no power here, Gandalf the Grey.
[2019-9-20. : 5:43 am]
razorback9423 -- WARNING: Unit Unplaceable! (Aldaris) (1656,2996)
[2019-9-20. : 5:27 am]
razorback9423 -- stop
Please log in to shout.


Members Online: Roy, luratosqori536, Neiv, suicosd, Wing Zero, MTiger156, UEDCommander, jun3hong