Author |
Message |
Plareplane Guest
Offline
|
Posted: 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:
# 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:
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:
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.
|
|
Back to top |
|
 |
Plareplane Guest
Offline
|
Posted: 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.
|
|
Back to top |
|
 |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
Posted: 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).
|
|
Back to top |
|
 |
Plareplane Guest
Offline
|
Posted: 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?
|
|
Back to top |
|
 |
Grelminar Creator of Asss
Joined: Feb 26 2003 Posts: 378 Offline
|
Posted: 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.
|
|
Back to top |
|
 |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
|
Back to top |
|
 |
Grelminar Creator of Asss
Joined: Feb 26 2003 Posts: 378 Offline
|
Posted: 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.
|
|
Back to top |
|
 |
Plareplane Guest
Offline
|
Posted: 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?
|
|
Back to top |
|
 |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
Posted: 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.
|
|
Back to top |
|
 |
Plareplane Newbie
Joined: Jul 09 2006 Posts: 14 Offline
|
Posted: 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).
pymod-process.py.diff.txt - 0.49 KB
File downloaded or viewed 14 time(s)
game.h.diff.txt - 0.43 KB
File downloaded or viewed 9 time(s)
|
|
Back to top |
|
 |
Plareplane Newbie
Joined: Jul 09 2006 Posts: 14 Offline
|
Posted: 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.
autowarp.py - 0.87 KB
File downloaded or viewed 17 time(s)
|
|
Back to top |
|
 |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
|
Back to top |
|
 |
Plareplane Newbie
Joined: Jul 09 2006 Posts: 14 Offline
|
Posted: Wed Jul 12, 2006 11:42 am Post subject: |
 |
|
|
|
Newbie pythoner me didn't know that. Suggestion taken.
Now, if only I can figure out why RegionChunk still can't find chunk type rNAM and rTIL
autowarp.py - 0.88 KB
File downloaded or viewed 15 time(s)
|
|
Back to top |
|
 |
|