Angelscript in Source

Discussion in 'Coding' started by Solokiller, Nov 15, 2016.

  1. Solokiller

    Solokiller Member

    Messages:
    4,861
    Likes Received:
    7
    Trophy Points:
    0
    Like i said yesterday, i was going to get Angelscript implemented in Source today so i could show how it would work. Well, here it is:
    https://github.com/SamVanheer/source-sdk-2013/tree/angelscript

    You'll have to excuse any compiler errors there might be, i spent a while trying to get the SDK to compile with VS2015, which ultimately failed so i had to backport my utility code to work with VS2013.

    Anyway, this is a minimal implementation in Source 2013 that lets you load any number of scripts on the server for any map. When a map is started, a file called maps/<mapname>.txt is loaded and checked for a list of scripts to load. If any are found, it loads them all, and builds the module. I added a dummy API to show what it would look like.

    This is the format used by the mapname Keyvalues text file:
    Code:
    "MapConfig"
    {
       "Angelscript"
       {
         "scripts"
         {
           "script" "test.as"
         }
       }
    }
    
    test.as is the script i'm using to test the implementation, as shown here:
    Code:
    class RunOnBuild
    {
       RunOnBuild()
       {
         Empires.GetTeam( TEAM_NF ).GetResearch().ResearchItem( "Foobar" );
       }
    }
    
    RunOnBuild g_RunOnBuild;
    
    I haven't implemented functions that get called on startup yet, so i'm abusing the behavior of global constructors to run my code. This calls into the dummy API, which then outputs to the console. Console output looks like this:
    Code:
    Map script compilation started
    Researching item: Foobar
    Map script compilation succeeded
    
    So basically, you can load an arbitrary number of scripts on the server side and do whatever you want with it. Once you add in an API to access the important stuff you can use it to control events in the game at will, like triggering the execution of a function that then accesses various entities, changes game rules, etc.
    With Angelscript you can also make custom entities, which means custom vehicles too.

    Once you get used to Angelscript's syntax (both the C++ and script sides of things) you can do some amazing stuff. I implemented this in Sven Co-op and it's being used to make custom weapons, custom entities, custom monsters, and script various things.

    For Empires you could have an API that lets you precisely control when items are researched, even control how long it takes to research by specifying modifiers for the research speed. You can add a callback that is invoked whenever a commander starts research so you can manipulate the research settings, etc.

    I've only ever implemented Angelscript in GoldSource before so i don't know what it will take to network it to the client, but it should be relatively easy to load scripts on the client as well and manipulate the UI with it. Then you can basically script addons to the UI for individual maps, add map-specific commander control buttons for things like gates, etc. If you can imagine it, it's possible.

    That said, this is just an example to show how it would work. It's by no means a guarantee that it will work, and i haven't spent any time to investigate just what is and what isn't possible in Source.
    Also keep in mind that i'm using my own utility code to implement this, so what you're seeing in the implementation code isn't the regular Angelscript library code. Angelscript's types all start with as*, mine start with IAS and CAS.

    Here's the build that you can use to try it out. Place it in steamapps/sourcemods, restart Steam and run it.
    It should work, but i might have missed something. If so, just let me know.

    https://dl.dropboxusercontent.com/u/46048979/SourceSDK_HL2MP_AngelscriptTest.rar
     
  2. PwnedYoAss

    PwnedYoAss Member

    Messages:
    1,088
    Likes Received:
    39
    Trophy Points:
    0
    Hell it'd be pretty amazing if we had an actual scripting engine in the game as opposed to the over glorified key-value store that are the current scripts. Thing is, is anyone on the dev team willing to refactor nearly the entirety of the weapons and vehicles?

    It'd be damned cool if Empires could have the weapons and vehicles decoupled and shoved into a scripting language like how most modern games handle it; it would allow for some rapid prototyping of new mechanics and vehicle balance...

    How's AngelScript in terms of performance? Did you notice any significant drops in Sven Co-op? I mean I know at this point everyone can run Empires at well past 60 fps so it's probably not a big deal.
     
  3. Solokiller

    Solokiller Member

    Messages:
    4,861
    Likes Received:
    7
    Trophy Points:
    0
    I've never experienced any measurable performance issues when using Angelscript. It's more efficient than Lua due to being a compiled language, ran through a virtual machine. It calls C++ code directly so there is less indirection involved with accessing the game.
    In SC it's been used for custom vehicles a la Counter-Strike, which are thinking every frame and performing crude physics calculations. If that doesn't cause performance issues then i doubt anything you throw at it in Empires will, since the CPU heavy stuff is being handled by the engine.
     
  4. PwnedYoAss

    PwnedYoAss Member

    Messages:
    1,088
    Likes Received:
    39
    Trophy Points:
    0
    Well shit I'm sold, let's get this into Empires.
     
  5. Solokiller

    Solokiller Member

    Messages:
    4,861
    Likes Received:
    7
    Trophy Points:
    0
    That all depends on whether the devs want that kind of access to internal game state, so you'll have to ask them for their thoughts on the matter. While you can restrict scripts to being used on a per-map basis, it's not very hard to turn them into pseudo-plugins that alter game mechanics into something unrecognizable.
     
  6. PwnedYoAss

    PwnedYoAss Member

    Messages:
    1,088
    Likes Received:
    39
    Trophy Points:
    0
    Admittedly I'm pretty sure that's the very reason why there isn't a scripting engine already.
     

Share This Page