wheybags / freeablo

[ARCHIVED] Modern reimplementation of the Diablo 1 game engine
GNU General Public License v3.0
2.16k stars 195 forks source link

Add scripting #466

Open vieiraa opened 4 years ago

vieiraa commented 4 years ago

Attempt to fix #22. I only gonna work on this in my spare time, so this should take a while.

wheybags commented 4 years ago

I'm not sure that now is the best time for this, and there are some issues to work out with whatever scripting language we use. The primary use case for it will be scripting in mods, and mods scripts will need to be deterministic just like the rest of the game code, in order for multiplayer to work. This means they will need to use our FixedPoint class for maths. Given that, I'm not sure if we shoudl try to hack FixedPoint into an existing scriping language, or if we should just make our own simplistic langauge. In particular, since FixedPoints can be a bit slow, it would be nice to use a language that distinguished between integer and real number types, so people could only use FixedPoint operations when they actually needed them.

wheybags commented 4 years ago

That said, if you want to hack around and experiment with integrating FixedPoint into lua / other languages, please go ahead, but given the issues, I don't think it will be merged until we are looking adding proper mod support, probably after 1.0.

vieiraa commented 4 years ago

I was planning to use this to implement the quest system. As for the FixedPoint class, it is fairly easy to integrate C++ classes into Lua, and Lua would execute C++ functions when we use the class' methods inside it.

wheybags commented 4 years ago

The thing is, we would want lua to use that class for all of its maths. So, if a script tries to add two numbers, it should use FixedPoints internally instead of floats, which is not as simple as just adding a native binding for the class.

vieiraa commented 4 years ago

Added FixedType registration for use in Lua. Also added unit tests to test it.

wheybags commented 4 years ago

http://lua.2524044.n2.nabble.com/Fixed-point-LUA-NUMBER-td7630153.html is a good thread on the topic, and is talking more abotu what I'm getting at. We would need to bundle our own copy of lua (in the extern/ folder), and redefine it's internal number type using the LUA_NUMBER #define. The maths module will break, so we can just remove it for now. Or we could go for the other more moderate approach listed there of redefinign LUA_NUMBER to int32_t and keeping the custom fixedpoint class you've added.

vieiraa commented 4 years ago

Lua 5.3 added support to 64 bit integers, so I don't think it it necessary anymore.

wheybags commented 4 years ago

It culd still be worth trying out defining LUA_NUMBER to FixedPoint, even in 5.3, see https://github.com/lua/lua/blob/v5.3.5/luaconf.h#L457, and https://github.com/lua/lua/blob/v5.3.5/luaconf.h#L127

This might be perfect actually, as the lua interpreter will try to use normal integer operations, but fall back to FixedPoint where necessary. Of course, we would also need to compile lua as c++, so that it could use the operator overloading of the FixedPoint class.

vieiraa commented 4 years ago

Added FixedPoint to lua. Now all floating point operations and math functions use FixedPoint. Still need to implement some missing math functions that are not present in FixedPoint (log, exp, ...), implement the modulo (%) operator, and fix printing to stdout.

wheybags commented 4 years ago

This is looking really great so far, thank you! If all goes well, we might be able to start using this sooner that I had originally planned.

vieiraa commented 4 years ago

Fixed printing the FixedPoint, and fixed a segfault when trying to stringify some numbers. I have two questions: 1) Do we still need the FixedPoint class registration? (Being able to create c++ FixedPoint objects and access its methods from Lua) 2) How can I use FreeMono in the console? The Diablo font is too ugly for that, and there is no small caps.

wheybags commented 4 years ago

Do we still need the FixedPoint class registration? (Being able to create c++ FixedPoint objects and access its methods from Lua)

I think we don't.

How can I use FreeMono in the console? The Diablo font is too ugly for that, and there is no small caps.

You can set the font using the nuklear styling system. See here for an example of styling, I don't have an example of font setting specifically to hand.

wheybags commented 4 years ago

https://github.com/Immediate-Mode-UI/Nuklear/blob/master/example/skinning.c#L420 Woops, forgot the link in my last post

vieiraa commented 4 years ago

Ok, have added FixedPoint support to Lua, created a simple console and redirected stdout to it... what should I do next?

vieiraa commented 4 years ago

I'm getting "ERROR INVALID SPRITE CACHE REQUEST 1" when I try to open the console since #429 got merged.

vieiraa commented 4 years ago

Wtf? PR just closed on its own.

khanduras commented 4 years ago

Got me all excited, it'll be fun to play with this once it's ready for an audience

wheybags commented 4 years ago

Just so you know, I think things are looking decent here, and I haven't forgotten about the PR, but for now it won't be merged. There will come a time in the implementation of the game when we will need a scripting engine, and at that point this will be pulled in. I'm not sure when that will be, maybe when we first start to add mods, or quests or some other point, but it will happen. For now though, there's no need to have it in the main tree before it's in use. Thanks again for all the work!

khanduras commented 4 years ago

Is scripting done? Is it working? I'm assuming it's using LUA?

wheybags commented 4 years ago

This is a modified version of lua which is mostly ready to be integrated. Scripting being "donw" would mean that ypu can actually do something with it (like add custom lua scripts into the game), which is not yet done, and won't be for a while, as the priority is getting the base game working properly.