Author Topic: Extending nwscript  (Read 1149 times)

Legacy_Valgav

  • Full Member
  • ***
  • Posts: 118
  • Karma: +0/-0
Extending nwscript
« on: November 18, 2015, 07:02:13 pm »


               

I'm looking for advice about extending nwscript. I'm developing nwnx on windows. Obvious candidates are nwnx_lua (not sure what are possibilities also i've never used lua before), nwnx_dotnet developed by Baaleos (if i remember correct) or using native c++ and compile methods into plugin itself, or finally writing plugin that call external files and load interpretor (ruby would be my first choice probably).


 


What's the fastest? I'm thinking mostly about some AI logic (i know it's possible like here ) and handle database connection. ODBC is great but trying to do some complex logic with database data in nwn is hard due to limitation of object types. No lists, tables, classes, just structures and variables.



               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Extending nwscript
« Reply #1 on: November 18, 2015, 10:24:30 pm »


               

Well since I made the solstice Lua library, I'll share my experience.  A lot will depend on your use cases, for example, I wanted to expose/replace internal engine functions with scripting so performance was paramount.  If you want something that can replace nwscripts and be more pleasant to work with I think anything could do performance wise.


 


Doing native c++ is surely the most performant and you could port something like the nwnx_cpp plugin I made.  With a bit of elbow grease/preprocessing you can convert/compile nwscript as c++, then replace things as you need.


 


For scripting languages I decided on Lua because it's easy to embed, the interpreter is faster than Ruby/Python.  It's obviously up to the task as it's the most used scripting language in gamedev.  Plus if you use LuaJIT it's one of the fastest dynamic languages around, matching or bettering V8, and has the nicest FFI you ever saw.  So you can easily call out to C for performance or expose internal game engine structures directly.  As a language it's not that fun to use, it's lacking in libraries and quite barebones.


 


If I had to do it all again, the only thing I personally would consider at the this point in time in lieu of Lua would be dotNet/Mono or maybe a new compiled language like D or Nim.



               
               

               
            

Legacy_Valgav

  • Full Member
  • ***
  • Posts: 118
  • Karma: +0/-0
Extending nwscript
« Reply #2 on: November 18, 2015, 11:14:34 pm »


               

I'm trying to use Terra's lua from here but I'm keep getting error:


 




NWN Extender V.2.7-beta4
(c) 2004 by Ingmar Stieger (Papillon) and Jeroen Broekhuizen
(c) 2007-2008 by virusman
visit us at http://www.nwnx.org


* Loading plugins...
* Plugin auth is loaded.
* Plugin chat is loaded.
* Plugin cool is loaded.
* Plugin dmactions is loaded.
* Plugin effects is loaded.
* Plugin events is loaded.
* Plugin fixes is loaded.
* Plugin funcs is loaded.
* An error occured while loading extension lua (126: The specified module could not be found. )
* Plugin magic is loaded.
* Plugin minipatch is loaded.
* Plugin names is loaded.
* Plugin odbc is loaded.
* Plugin palette is loaded.
* Plugin polymorph is loaded.
* Plugin profiler is loaded.
* Plugin random is loaded.
* Plugin resman is loaded.
* NWNX2 activated.


 


Also have no idea how to run functions. I assume that a can call functions from external *.lua files?


 


Mostly I'm thinking about some simple AI that would handly daily routine of npcs also maybe comat AI if it's fast enough.


 


By the way will that library be compatible with windows version?



               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Extending nwscript
« Reply #3 on: November 19, 2015, 12:05:20 am »


               

I'm not super familiar with nwnx_lua on windows, so I'm not sure what that error is.  Do you have lua52.dll in your nwnx/NWN directory?


 


It look like it's similar to nwnx_ruby where the nwscript->nwnx bridge is something like:



SetLocalString(object, "NWNX!LUA!sexec", "some string with lua code to execute");

where object becomes OBJECT_SELF in Lua scripts.


 


so like



SetLocalString(object, "NWNX!LUA!sexec", "print(OBJECT_SELF)");

That's how you'd call the Lua print function.  The string value could be replaced by any lua script tho.  Think of the string value as the script tag in html with an OBJECT_SELF.


 


The library is linux only so far, but with ScholarMidnight's new unified NWNX infrastructure it might be possible to port. I took a significantly different approach to things because I wanted Lua to be and feel first class.


 


So in my system ExecuteScript("script_thing", object) maps directly to the Lua



function script_thing(object)
...
end


               
               

               
            

Baaleos

  • Administrator
  • Hero Member
  • *****
  • Posts: 1916
  • Karma: +0/-0
Extending nwscript
« Reply #4 on: November 19, 2015, 02:48:38 pm »


               

BTW- 


I think someone else made the first version of nwnx_dotnet - I merely added the ability to execute from un-compiled class files.


'<img'>


 


Original version made by someone called : ronchese


 


I am looking at trying my hand at upskilling into c++ for linux.


So much of the functionality I have in windows, I am going to try to port to Linux


 


Im also looking into doing more nwnx stuff in Java - since it is multiplatform.


Code once - run on windows or linux etc



               
               

               
            

Legacy_Valgav

  • Full Member
  • ***
  • Posts: 118
  • Karma: +0/-0
Extending nwscript
« Reply #5 on: November 19, 2015, 07:43:33 pm »


               


 


The library is linux only so far, but with ScholarMidnight's new unified NWNX infrastructure it might be possible to port. I took a significantly different approach to things because I wanted Lua to be and feel first class.




 


Unified infrastructure? Thath means same logic of plugins on linux and windows?


 


Also fixed missing library, but still not sure how to call lua from external file instead of methods as inline strings.


 




Im also looking into doing more nwnx stuff in Java - since it is multiplatform.


Code once - run on windows or linux etc




 


I really don't like java but I'm thinking of some simple port of nwnx_ruby on windows (I know that people used to say that it's rubish on windows but i find it quite useful fore same small tasks). I'm thinking mostly about database queries, some logic for them and parser for posting results to nwn. Some sort of ELC system that way maybe?


 


Also to both of you. Using external language like lua or ruby for handle entire AI or even some parts of it sounds like massive project. It would need of rewrite entire battle logic probably.


 


And for something completely different. Is there any way to override tlk lines and icons references in 2da on fly? Some bridge that will post files in override folder?



               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Extending nwscript
« Reply #6 on: November 19, 2015, 08:02:20 pm »


               

You can find some details about the unified infrastructure and the github repo here: http://www.nwnx.org/...opic.php?t=2472  Hopefully in a lot of cases plugins will be able to be compiled on both platforms.


 


I don't think with nwnx_lua on windows you can call into Lua directly.  I think that's how the other Linux language plugins worked too.


 


If you want your external language to really feel like it's integrated with the engine and not just nwscript in some other language then, yea, there is a lot of work to be done.


 


Niv has a on the fly client TLK entry changer.  He's also working on some stuff that could push files to the client.



               
               

               
            

Legacy_Valgav

  • Full Member
  • ***
  • Posts: 118
  • Karma: +0/-0
Extending nwscript
« Reply #7 on: November 21, 2015, 01:15:54 am »


               

So finally I've been able to run server and do some tests.


 


I assume that if i want to use and write lua externally not inline i should create some sort of library with functions, but I'm not sure:


 


Should i load all of them at module load or call them only when they're necessery? How slow loadfile(xx)() will be? Or huge systems will be to heavy to keep them in memory?



               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Extending nwscript
« Reply #8 on: November 21, 2015, 10:30:21 am »


               

I preload everything.  Even with all that's in Solstice it adds maybe 5-10mb of memory to NWserver process, so it's pretty insignificant.


 


loadfile will require everything to be re-parsed, compiled, etc.  If you're using require to load 'modules', on the Lua side those will be cached and only loaded once.  I could see loadfile being useful for testing tho.



               
               

               
            

Legacy_Valgav

  • Full Member
  • ***
  • Posts: 118
  • Karma: +0/-0
Extending nwscript
« Reply #9 on: November 21, 2015, 02:45:27 pm »


               

I could probably reuse some of your logic from Solstice. ALso what's the most effective of running it? Do you use some wrappers? I'm thinking about overriding nwscript.nss functions to execute lua code. This way I could inject lua logic of combat into the standard combat AI without need of writing whole system from scratch.


 


Not sure if this is good way to do that. But definitly there's a need of some wrappers to get missing things from other nwnx_plugins and standard nwn values. Also I'm not sure what's the purpose of:



void MainLoopHook( function ) - hooks nwn's mainloop and runs the function every frame. void func()

               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Extending nwscript
« Reply #10 on: November 22, 2015, 12:18:14 pm »


               

You could hook any of the ExecuteCommand... functions to replace nwscript internal script functions.  In that case I'd probably do something along these lines: https://github.com/j...TouchAttack.cpp where the hook wraps the call to Lua.  If you switched the plugin over to use LuaJIT you could set things up to hook functions from Lua directly like my Hook module. However, the former way is probably safer.


 


From the looks of it, it would allow you to register a Lua function (by name) that would be called every main loop tick.  Not sure what I would use it for.  Some people use that kind of a hook to poll an external data source (Redis, network service).


 


I noticed one thing that might be a problem, I'm not sure since I've not used this windows version, but whoever made it replaced SetLocalString/Object with internal versions, meaning convert some NWNX nwscript includes that use the SetLocalString(obj, "NWNX!PLUGIN!FUCNTION", "params") to call NWNX it mostly likely will not work.


 


I also noticed that it looks like from nwscript you can do SetLocalString(obj, "NWNX!LUA!(lua function name)", "params") to call into Lua.


 


Edit:


 


Before you get too far along on this... I took a bit more time to look at nwnx_lua for windows and frankly it's terrible, quite limited (no direct access to any nwscript functions), and shows a lack of understanding regarding how the Lua API works.  No offense to the person that made it, but I would not feel comfortable recommending anyone use it.



               
               

               
            

Legacy_Valgav

  • Full Member
  • ***
  • Posts: 118
  • Karma: +0/-0
Extending nwscript
« Reply #11 on: November 22, 2015, 07:02:52 pm »


               

That's bad '<img'>


 


So if I'm stuck on windows my only way to do something more than standard nwscript is to write my own plugin? Any advices where to start, how to get it better than that?



               
               

               
            

Baaleos

  • Administrator
  • Hero Member
  • *****
  • Posts: 1916
  • Karma: +0/-0
Extending nwscript
« Reply #12 on: November 22, 2015, 08:42:20 pm »


               

If you want to do something in windows - you can use nwnx_dotnet


This is very rough around the edges, but yes - with .net you can invoke nwn internal functions.


 


 


https://github.com/Baaleos/NWNDotNet


 


For instance: the GetIsBlind function in nwscript -  in .Net 


https://github.com/B...ies/Creature.cs


 


Note - the project was just created as a POC


If anyone wants to continue it on, and make a more fleshed out version, by all means.


There are probably better ways handling the offsets etc


 


Note: This was made originally to be used with my nwnx_dotnet plugin which would allow invoking of external dlls.


But any .Net injector would work.



               
               

               
            

Legacy_Valgav

  • Full Member
  • ***
  • Posts: 118
  • Karma: +0/-0
Extending nwscript
« Reply #13 on: November 23, 2015, 12:04:56 am »


               

I know, but it's C#. It's not dynamic typed, has more classic syntax of code. I love C# but it's not so great step as switching to python or lua. 


 


Of course it's great and you can write effective, huge systems using it. But i'm looking for something more simple so I'm still thinking of using that lua plugin. It's crap, but it's a simple crap.


 


I hope you know what i mean '<img'>



               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Extending nwscript
« Reply #14 on: November 24, 2015, 08:05:05 pm »


               

If you're comfortable building plugins and such it wouldn't be hard to at least get NWScript functions exposed by copying from nwnx_lua on Linux.