Server Help

ASSS Questions - any python examples?

Anonymous - Sun Nov 14, 2004 5:28 pm
Post subject: any python examples?
Can you give some examples of how to do things in Python?
Here is what I know:
Code: Show/Hide
# callbacks
from asss import *

chat = get_interface(I_CHAT)

def goal(arena, p, bid, x, y):
    chat.SendArenaMessage(arena, "goal.")

cb1 = reg_callback(CB_GOAL, goal)

...

# commands
from asss import *

chat = get_interface(I_CHAT)

def c_mycmd(cmd, params, p, targ):
    """\
some help text
"""
    chat.SendMessage(p, "moo.")

cmd1 = add_command("t1", c_mycmd)

Here is a list of things I want to know how to do:
* attach modules
* looping players
* packet callbacks and how to read the data (like energy or weapon fired)

I'll probably add more to this thread when I think of it.
D1st0rt - Sun Nov 14, 2004 11:13 pm
Post subject:
how about adding python support in custom c modules?
Grelminar - Mon Nov 15, 2004 12:43 am
Post subject:
I just started adding some of this stuff to the developer's guide last week, but I can't publish stuff for now because of sscx server problems. Here are some quick answers, but I forgot how much of this is in 1.3.2 and how much has been added since then:

To attach/detach, define fuctions:

Code: Show/Hide
def mm_attach(arena):
    # do stuff with arena
def mm_detach(arena):
    # undo stuff


To do stuff to all players:
Code: Show/Hide
def blah(p):
    # do stuff to p
asss.for_each_player(blah)


asss.for_each_arena also exists. You currently can't play with raw packets from python, and there is no callback that gets called on every position packet. For most purposes, you can use CB_SAFEZONE and CB_REGION, and read the data in p.position, which is a tuple of (x, y, xspeed, yspeed, rotation, bounty, statusbits). Tell me what you want to do with position data and I'll try to figure out a good way to support it.
Smong - Mon Nov 15, 2004 6:36 am
Post subject:
Can you add python support to the objects module? This would be good for the rPYC segment or when CB_REGION support is added to python.

With the position thing I want to know when someone fires a weapon and what weapon it was. This is so I can convert my ammo module to python.

I noticed on windows with pymod loaded ctrl+c stops working.

@D1
You mean like some kind of proxy function to allow callbacks and stuff?
I think if you recompile pymod yourself it will scan your source and make the callbacks available. (I never got it to compile though).
D1st0rt - Mon Nov 15, 2004 4:59 pm
Post subject:
just an example of what I was getting at:

In the hyperspace package, theres an interface that deals with money, using addMoney(player,amount) or something along those lines. Is there anything you need to set up to be able to access that function in a python module?
Anonymous - Mon Nov 15, 2004 5:35 pm
Post subject:
Just look at the other header files. Looks something like:
Code: Show/Hide
typedef struct Imoney
{
...
   void (*addMoney)(Player *player, int amount);
   /* pyint: player, int -> void */
...
}


Something you can't do without net is sending shipreset, can you make an interface for that please?

And with the for for_each_player(func), do you use that nested magic to return stuff? Suppose I wanted to count the number of players that had a certain thing in their per player data.
Grelminar - Tue Nov 16, 2004 12:53 am
Post subject:
Ok, I just added support for objects, and support for mapdata/regions already exists, though I'm not sure when I'll be able to release a version with this stuff in it. And I'll try to write a little shipreset function soon.

You can use the nested stuff to return values, though it's a little messy. I think both of these will work:
Code: Show/Hide
# method 1
def count_players(condition):
    count = [0]
    def blah(p):
        if condition(p):
            count[0] += 1
    asss.for_each_player(blah)
    return count[0]

# method 2
class ConditionCounter:
    def __init__(me, condition):
        me.count = 0
        me.condition = me.condition
    def __call__(me, p):
        if me.condition(p):
            me.count += 1
c = ConditionCounter(condition)
asss.for_each_player(c)
# do stuff with c.count

In the first one, count has to be a list because you can't mutate variables in anything but the innermost scope. I have an idea for making this (and similar things) easier to do (using iterators), but I don't know when I'll get to it.

About adding python support to custom C modules: this isn't as easy as I'd like it to be, but it should be possible. You have to add your own declarations in comments, and then recompile pymod. You can use the existing modules as examples of the syntax. Or try to look at pymod-process.py, which is what interprets those declarations, but I wouldn't advise that. When recompiling, your header files at least should be in the same directory as the rest of the source, or (probably a better idea) you can edit the line in the Makefile to add the directory that contains your header files.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group