Server Help

ASSS Questions - python region

Anonymous - Sun Jul 09, 2006 12:21 pm
Post subject: python region
How do I read the region chunks in python? The following does not do what I'd expect it to do:
Code: Show/Hide

# demo
from asss import *

# magic numbers
rTIL = 0x4C495472
rNAM = 0x4D414E72
rNAW = 0x57414E72
rBSE = 0x45534272
rAWP = 0x50574172

chat = get_interface(I_CHAT)
mapd = get_interface(I_MAPDATA)
def mm_attach(arena):
   arena.wbgoal = mapd.FindRegionByName(arena, "wbgoal")
   # i should have all of these
   arena.rAWP = mapd.RegionChunk(arena.wbgoal, rAWP)
   arena.rBSE = mapd.RegionChunk(arena.wbgoal, rBSE)
   arena.rNAM = mapd.RegionChunk(arena.wbgoal, rNAM)
   arena.rTIL = mapd.RegionChunk(arena.wbgoal, rTIL)
   # i shouldn't have any rNAW
   arena.rNAW = mapd.RegionChunk(arena.wbgoal, rNAW)
   # let's see what we get
   chat.SendArenaMessage(arena, "rAWP" + str(type(arena.rAWP)))
   chat.SendArenaMessage(arena, "rBSE" + str(type(arena.rBSE)))
   chat.SendArenaMessage(arena, "rNAM" + str(type(arena.rNAM)))
   chat.SendArenaMessage(arena, "rTIL" + str(type(arena.rTIL)))
   chat.SendArenaMessage(arena, "rNAW" + str(type(arena.rNAW)))


Here's the output:
Code: Show/Hide

         Plareplane> ?|attmod -d demo|rmmod demo|insmod <py> demo|attmod demo
Module demo detached.
Module demo unloaded successfully
Module <py> demo loaded successfully
rAWP<type 'str'>
rBSE<type 'str'>
rNAM<type 'NoneType'>
rTIL<type 'NoneType'>
rNAW<type 'NoneType'>
Module demo attached.
         Plareplane> ?py print(a.rAWP)
E <pymod> error in command handler for 'py'
         Plareplane> ?py print(a.rBSE)
         Plareplane> ?py print(a.rNAM)
None
         Plareplane> ?py print(a.rTIL)
None
         Plareplane> ?py print(a.rNAW)
None


The error in print(a.rAWP) is:
Code: Show/Hide

Traceback (most recent call last):
  File "/home/plareplane/software/asss/bin/exec.py", line 46, in c_py
    chat.SendMessage(player, l)
TypeError: argument 2 must be string without null bytes, not str


Shouldn't I be getting some sort of tuple for rAWP, rNAM, and rTIL? (Or a list of bytes would be nice.) Why do I get None for rNAM and rTIL?

Also, what is required to make a callback in c available in python? I see that the available python callbacks have a /* pycb ... */ in the c code, but I'm not sure what else there is to it. Specifically, I'd like to use the CB_REGION callback, but the guide on the wiki seems to say that it is not currently available.
Anonymous - Sun Jul 09, 2006 12:59 pm
Post subject:
Also, I only started learning python a couple days ago from reading the python module guide coupled with occasional glances at the api on python.org, so if something is outrageously bad in the above learning example, let me know.
Smong - Sun Jul 09, 2006 2:00 pm
Post subject:
You should probably check if arena.wbgoal is None before you try to use it.

Why the other stuff doesn't work I don't know. You can edit game.h line 93 if you want to play with CB_REGION, although I don't know if that will actually work since it must be disabled for a reason (increased server load maybe).
Anonymous - Sun Jul 09, 2006 2:21 pm
Post subject:
I didn't bother checking wbgoal because I just editted a map specifically with a region named wbgoal just to test how things work. Can FindRegionByName return None in this case?
Grelminar - Sun Jul 09, 2006 3:59 pm
Post subject:
The type error is pretty clear: your chunk has nul bytes in it. Strings are the most efficient way to pass around binary data in python, and so RegionChunk returns strings (or None, if the chunk isn't there). Chunks can contain binary data, but strings used for messages can't (actually, that's the first time I heard of that restriction, but it makes sense).

Adding a pycb thing for CB_REGION should be enough. I think the reason it's not there is because it gets called synchronously in the unreliable thread, and having a callback do a lot of work there would be bad. Feel free to add it, just be careful about doing stuff that could take too long.
Smong - Sun Jul 09, 2006 6:42 pm
Post subject:
Code: Show/Hide
   int (*RegionChunk)(Region *rgn, u32 ctype, const void **datap, int *sizep);
   /* pyint: region, int, bufp out, int buflen out -> void */
Since it says 'out' twice, why doesn't it return a tuple with 2 elements?
Grelminar - Sun Jul 09, 2006 7:00 pm
Post subject:
Because bufp/buflen parameters are handled specially. I suppose I could change it so the second "out" isn't required, but conceptually, it is an "out" parameter, so it makes sense to have it there.
Anonymous - Sun Jul 09, 2006 8:01 pm
Post subject:
Quote:

The type error is pretty clear: your chunk has nul bytes in it. Strings are the most efficient way to pass around binary data in python, and so RegionChunk returns strings (or None, if the chunk isn't there).


Ok, so rAWP, rBSE, and rNAW were exactly what they were expected to be (4 bytes corresponding to a coord, 0 bytes, and None, respectively), but why do I get None for rTIL and rNAM? I thought maybe I got my magic numbers wrong, but I used the same method (writing r??? in hex) for each one, and it clearly worked for rAWP and rBSE.

Quote:

Chunks can contain binary data, but strings used for messages can't (actually, that's the first time I heard of that restriction, but it makes sense).


I'm not sure if I understand exactly what you mean.

As a side note, is the rPYC chunk type implemented?
Smong - Sun Jul 09, 2006 8:36 pm
Post subject:
I don't think rPYC is implemented. Although it would be extremely cool if it was. It would save having to make custom region types for things like object toggling, ship changing and prizing.
Plareplane - Mon Jul 10, 2006 3:26 am
Post subject:
Changed a line in src/include/game.h and added a few lines to scripts/pymod-process.py. CB_REGION seems to work now (at least for my simple test of flying over a region and having a module call out the name of the region).
Plareplane - Wed Jul 12, 2006 3:39 am
Post subject:
I don't think too many people will be interested, but I wrote/translated a py version of autowarp. It seems to work after some minutes of testing.
Smong - Wed Jul 12, 2006 11:01 am
Post subject:
Hmm. When you unpack I think you can do something like this:
Code: Show/Hide
x, y, name = unpack("hh16s", aw)

That would help to make it more readable.
Plareplane - Wed Jul 12, 2006 11:42 am
Post subject:
Newbie pythoner me didn't know that. icon_redface.gif Suggestion taken.

Now, if only I can figure out why RegionChunk still can't find chunk type rNAM and rTIL
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group