Xonotic Forums
A script engine written in QuakeC - Printable Version

+- Xonotic Forums (https://forums.xonotic.org)
+-- Forum: Creating & Contributing (https://forums.xonotic.org/forumdisplay.php?fid=10)
+--- Forum: Xonotic - Development (https://forums.xonotic.org/forumdisplay.php?fid=12)
+--- Thread: A script engine written in QuakeC (/showthread.php?tid=5121)



A script engine written in QuakeC - Melanosuchus - 10-11-2014

Yesterday I thought it would be fun to make a script interpreter in QuakeC and so I did.
I'm not sure if this is an interesting concept or just a stupid idea.
Anyway, I managed to make it. It currently supports the following features:
  • basic imperative control structures
  • scoped (untyped) variables
  • functions
  • basic numeric and string operators
  • some misc math and string functions
  • reading/writing cvars
  • executing console commands
  • handling entities

The interpreter is accessible via a command which can either run a file or a single line.
I've only tested it in server code but it should work fine in menu and client too.

Sources:
https://github.com/mbasaglia/esk-modpack/tree/script/qcsrc/common/command/script
https://github.com/mbasaglia/esk-modpack/blob/script/qcsrc/common/command/script.qc

It has a C-like syntax, I guess somewhat similar to JavaScript. Semicolons can be replaced by commas so it's easier to execute snippets from the console.
Follows a simple script which gives a basic idea of the syntax:
Code:
function pluralize(string,number)
{
    if ( number == 1 )
        return string;
    else
        return string+"s";
}

function buckets_of_oats(n)
{
    return n+" "+pluralize("bucket",n)+" of oats";
}

function verse(n)
{
    print ( buckets_of_oats(n) + " on the wall, " + buckets_of_oats(n) + "!" );
    wallstring = n == 1 ? "WAAAAALLLL!!!" : "wall";
    print ( "Take one down, pass it around, you got " + buckets_of_oats(n-1) +
        " on the "+ wallstring );
}

function sing(n = 10)
{
    while ( n > 0 )
    {
        verse(n);
        n -= 1;
    }
}

sing();

I made it just for fun, does anyone know if something of this kind can be of any use?


RE: A script engine written in QuakeC - Mr. Bougo - 10-11-2014

Looks fun. Blub\0 gave this a thumbs up for the effort on #gmqcc Smile

As for usefulness, I don't know. What kind of use do you envision? In servers you may as well code in QC directly. In CSQC, I don't know what clients would use it for really, except maybe rainbow text? Tongue


RE: A script engine written in QuakeC - Melanosuchus - 10-12-2014

Yeah, rainbow text script!

It's much faster and more efficient than the console alias hack (and more readable too Tongue)

Code:
function hsv(h,s,v)
{
    h *= 6;
    c = v*s;
    m = v-c;
        
    h1 = floor(h);
    f = h - h1;
        
    n = v - c * f;
    k = v - c * (1 - f);
        
    if ( h1 == 0 )
        return rgb_to_hexcolor(vector(v,k,m));
    if ( h1 == 1 )
        return rgb_to_hexcolor(vector(n,v,m));
    if ( h1 == 2 )
        return rgb_to_hexcolor(vector(m,v,k));
    if ( h1 == 3 )
        return rgb_to_hexcolor(vector(m,n,v));
    if ( h1 == 4 )
        return rgb_to_hexcolor(vector(k,m,v));
    return rgb_to_hexcolor(vector(v,m,n));
}

function rainbow(string)
{
    string = strdecolorize(string);
    recolored="";
    for ( i = 0; i < strlen(string); i++ )
        recolored += hsv(i/strlen(string),0.75,1) + substring(string,i,1);
    return recolored;
}


command=arg1+" ";

string=""
for ( i = 2; i < argc; i+=1 )
{
    string += get("arg"+i);
    if ( i+1 < argc )
        string += " ";
}
    
localcmd(command+rainbow(string));



RE: A script engine written in QuakeC - Mr. Bougo - 10-13-2014

Nice, now that's a proper benchmark against existing alternatives Smile

It's a bit sad that there is no real use for this. Can this be plugged into menu QC (without entity access then) to be useable by clients on any server?


RE: A script engine written in QuakeC - Melanosuchus - 10-13-2014

Yes, it required some minor changes (mostly due to different entity fields in menu) but it works fine.


RE: A script engine written in QuakeC - tZork - 10-13-2014

Cool stuff =)

And im sure theres possible uses for it, just need to find them!
Some ideas:
Scripted map events / entities (SP just got a little easier to accomplish)
BOT Scripts (we need to get Mirio under better control! ; )
Gamecode that refines itself live (its aliiiiive! :S)


RE: A script engine written in QuakeC - Mr. Bougo - 10-13-2014

(10-13-2014, 07:39 AM)tZork Wrote: Cool stuff =)

And im sure theres possible uses for it, just need to find them!
Some ideas:
Scripted map events / entities (SP just got a little easier to accomplish)
BOT Scripts (we need to get Mirio under better control! ; )
Gamecode that refines itself live (its aliiiiive! :S)

What's SP?

I didn't think of mapping, that's a great idea. Perhaps it should be sealed tight to avoid crashes though Smile We already have some nice "programmability" with target_spawn and the range of triggers though, I think the difficulty of handling it makes it more charming. But I'm not a mapper Big Grin

EDIT: Ah, SP is single player.


RE: A script engine written in QuakeC - Maddin - 10-13-2014

I remember back the day working on some Jedi Knight Outcast maps (uses modified Quake 3 Engine) which also had a scripting tool allowing for some really crazy stuff. You could essentially do everything with it, control every entity and even create camera sequences/ cutscenes. Thumbs up for your work! I´d like to see it in action, care to make an example video? Smile


RE: A script engine written in QuakeC - end user - 10-13-2014

(10-13-2014, 12:44 PM)Maddin Wrote: care to make an example video? Smile

OMFG were gonna see technolcolor pony porn Tongue


RE: A script engine written in QuakeC - Melanosuchus - 10-14-2014

(10-13-2014, 12:44 PM)Maddin Wrote: I´d like to see it in action, care to make an example video? Smile

Sure, but do you have an idea about what to show in it? So far all the scripts I've written for it were quick tests to make sure that all the features are working correctly.