Search the Community
Showing results for tags 'scripting'.
-
Hi folks, and thanks so much to the devs & mappers for such a great game. After playing a bunch over Christmas week after many years gap, I got curious about how it all went together, and decided to learn by picking a challenge - specifically, when I looked at scripting, I wondered how hard it would be to add library calls, for functionality that would never be in core, in a not-completely-hacky-way. Attached is an example of a few rough scripts - one which runs a pluggable webserver, one which logs anything you pick up to a webpage, one which does text-to-speech and has a Phi2 LLM chatbot ("Borland, the angry archery instructor"). The last is gimmicky, and takes 20-90s to generate responses on my i7 CPU while TDM runs, but if you really wanted something like this, you could host it and just do API calls from the process. The Piper text-to-speech is much more potentially useful IMO. Thanks to snatcher whose Forward Lantern and Smart Objects mods helped me pull example scripts together. I had a few other ideas in mind, like custom AI path-finding algorithms that could not be fitted into scripts, math/data algorithms, statistical models, or video generation/processing, etc. but really interested if anyone has ideas for use-cases. TL;DR: the upshot was a proof-of-concept, where PK4s can load new DLLs at runtime, scripts can call them within and across PK4 using "header files", and TDM scripting was patched with some syntax to support discovery and making matching calls, with proper script-compile-time checking. Why? Mostly curiosity, but also because I wanted to see what would happen if scripts could use text-to-speech and dynamically-defined sound shaders. I also could see that simply hard-coding it into a fork would not be very constructive or enlightening, so tried to pick a paradigm that fits (mostly) with what is there. In short, I added a Library idClass (that definitely needs work) that will instantiate a child Library for each PK4-defined external lib, each holding an eventCallbacks function table of callbacks defined in the .so file. This almost follows the idClass::ProcessEventArgsPtr flow normally. As such, the so/DLL extensions mostly behave as sys event calls in scripting. Critically, while I have tried to limit function reference jumps and var copies to almost the same count as the comparable sys event calls, this is not intended for performance critical code - more things like text-to-speech that use third-party libraries and are slow enough to need their own (OS) thread. Why Rust? While I have coded for many years, I am not a gamedev or modder, so I am learning as I go on the subject in general - my assumption was that this is not already a supported approach due to stability and security. It seems clear that you could mod TDM in C++ by loading a DLL alongside and reaching into the vtable, and pulling strings, or do something like https://github.com/dhewm/dhewm3-sdk/ . However, while you can certainly kill a game with a script, it seems harder to compile something that will do bad things with pointers or accidentally shove a gigabyte of data into a string, corrupt disks, run bitcoin miners, etc. and if you want to do this in a modular way to load a bunch of such mods then that doesn't seem so great. So, I thought "what provides a lot of flexibility, but some protection against subtle memory bugs", and decided that a very basic Rust SDK would make it easy to define a library extension as something like: #[therustymod_lib(daemon=true)] mod mod_web_browser { use crate::http::launch; async fn __run() { print!("Launching rocket...\n"); launch().await } fn init_mod_web_browser() -> bool { log::add_to_log("init".to_string(), MODULE_NAME.to_string()).is_ok() } fn register_module(name: *const c_char, author: *const c_char, tags: *const c_char, link: *const c_char, description: *const c_char) -> c_int { ... and then Rust macros can handle mapping return types to ReturnFloat(...) calls, etc. at compile-time rather than having to add layers of function call indirection. Ironically, I did not take it as far as building in the unsafe wrapping/unwrapping of C/C++ types via the macro, so the addon-writer person then has to do write unsafe calls to take *const c_char to string and v.v.. However, once that's done, the events can then call out to methods on a singleton and do actual work in safe Rust. While these functions correspond to dynamically-generated TDM events, I do not let the idClass get explicitly leaked to Rust to avoid overexposing the C++ side, so they are class methods in the vtable only to fool the compiler and not break Callback.cpp. For the examples in Rust, I was moving fast to do a PoC, so they are not idiomatic Rust and there is little error handling, but like a script, when it fails, it fails explicitly, rather than (normally) in subtle user-defined C++ buffer overflow ways. Having an always-running async executor (tokio) lets actual computation get shipped off fast to a real system thread, and the TDM event calls return immediately, with the caller able to poll for results by calling a second Rust TDM event from an idThread. As an example of a (synchronous) Rust call in a script: extern mod_web_browser { void init_mod_web_browser(); boolean do_log_to_web_browser(int module_num, string log_line); int register_module(string name, string author, string tags, string link, string description); void register_page(int module_num, bytes page); void update_status(int module_num, string status_data); } void mod_grab_log_init() { boolean grabbed_check = false; entity grabbed_entity = $null_entity; float web_module_id = mod_web_browser::register_module( "mod_grab_log", "philtweir based on snatcher's work", "Event,Grab", "https://github.com/philtweir/therustymod/", "Logs to web every time the player grabs something." ); On the verifiability point, both as there are transpiled TDM headers and to mandate source code checkability, the SDK is AGPL. What state is it in? The code goes from early-stage but kinda (hopefully) logical - e.g. what's in my TDM fork - through to basic, what's in the SDK - through to rough - what's in the first couple examples - through to hacky - what's in the fun stretch-goal example, with an AI chatbot talking on a dynamically-loaded sound shader. (see below) The important bit is the first, the TDM approach, but I did not see much point in refining it too far without feedback or a proper demonstration of what this could enable. Note that the TDM approach does not assume Rust, I wanted that as a baseline neutral thing - it passes out a short set of allowed callbacks according to a .h file, so language than can produce dynamically-linkable objects should be able to hook in. What functionality would be essential but is missing? support for anything other than Linux x86 (but I use TDM's dlsym wrappers so should not be a huge issue, if the type sizes, etc. match up) ability to conditionally call an external library function (the dependencies can be loaded out of order and used from any script, but now every referenced callback needs to be in place with matching signatures by the time the main load sequence finishes or it will complain) packaging a .so+DLL into the PK4, with verification of source and checksum tidying up the Rust SDK to be less brittle and (optionally) transparently manage pre-Rustified input/output types some way of semantic-versioning the headers and (easily) maintaining backwards compatibility in the external libraries right now, a dedicated .script file has to be written to define the interface for each .so/DLL - this could be dynamic via an autogenerated SDK callback to avoid mistakes maintaining any non-disposable state in the library seems like an inherently bad idea, but perhaps Rust-side Save/Restore hooks any way to pass entities from a script, although I'm skeptical that this is desirable at all One of the most obvious architectural issues is that I added a bytes type (for uncopied char* pointers) in the scripting to be useful - not for the script to interact with directly but so, for instance, a lib can pass back a Decl definition (for example) that can be held in a variable until the script calls a subsequent (sys) event call to parse it straight from memory. That breaks a bunch of assumptions about event arguments, I think, and likely save/restore. Keen for suggestions - making indexed entries in a global event arg pointer lookup table, say, that the script can safely pass about? Adding CreateNewDeclFromMemory to the exposed ABI instead? While I know that there is no network play at the moment, I also saw somebody had experimented and did not want to make that harder, so also conscious that would need thought about. One maybe interesting idea for a two-player stealth mode could be a player-capturable companion to take across the map, like a capture-the-AI-flag, and pluggable libs might help with adding statistical models for logic and behaviour more easily than scripts, so I can see ways dynamic libraries and multiplayer would be complementary if the technical friction could be resolved. Why am I telling anybody? I know this would not remotely be mergeable, and everyone has bigger priorities, but I did wonder if the general direction was sensible. Then I thought, "hey, maybe I can get feedback from the core team if this concept is even desirable and, if so, see how long that journey would be". And here I am. [EDITED: for some reason I said "speech-to-text" instead of "text-to-speech" everywhere the first time, although tbh I thought both would be interesting]
- 25 replies
-
- 3
-
So, as everybody seems to have his own mapping thread, I thought about starting my own. I'm currently working on my map for the "unusual gameplay contest" and just set up a nice working elevator. I took a look one the multi-level elevator tutorial on the wiki and I think that my approach is much more easy to implement, so I thought I could describe it here. The elevator consists out of three parts: - a func_mover entity that will be our elevator-platform - the buttons that will control the elevator - a couple of waypoints, one for each floor what else do we need: a really short script, I'll go to explain further down The first step is to create an elevator-platform. This is your part . If your done with it change its classname to func mover (under entities/func/movers). Rename it to platform. (The names are for referance. Of course you can choose them as you like). The next step is to create some path_corner entities and place them, where you want your platform to stop. Be aware that the origin of the platform entity will move to the center of the bottom face of the pink block representing the path_corner. Let the platform target the path_corners. Make sure you start with "target0", then "target1" and so one. Start with the lowest path_corner and move upwards level by level. Create buttons for every floor on one of the levels. For the other you can just copy them around when we've set them up. On the buttons, you have to set three spawnargs. - target: let the buttons target the platform (so you have to insert its name here) - state_change_callback: set this to "movePlat". This is the name of the function we'll use to move the elevator - moveDir: set this to "0" for the lowest button, "1" for the next one and so one. This spawnarg controls which path_corner the platform should move to When you're done, copy the buttons to where else you'll need them. Ok, that's the setup in Dark Radiant. Now everything that's is needed is a script. And here it is. void movePlat(entity button,boolean bOpen,boolean bLocked,boolean bInterrupted) { entity mover = button.getEntityKey("target"); entity target = mover.getEntityKey("target"+button.getKey("moveDir")); mover.moveTo(target); } The "state_change_callback" is called everytime when the state of the specific object was changed, for example if a button is pressed. The function receives four arguments, of which we will only need the first one. The entity that we get here is the one who has called the script. In this case, the button we have pressed. Now what does the code do. The first line brings us the entity targeted by the button. This is our platform. We need to know this as we want to make it move The second line gives us the path_corner the platform should move to. Here you can see why you should start targeting them from 0 upwards and what the "moveDir" spawnarg is used for. An example: If you push a button whichs "moveDir" spawnarg is "1", than the platform targeted by the button will move to the path_corner targeted by the platform via the spawnarg "target1". The last line is quite self-explaining. It tells the platform to move to the designated path_corner. That's it. Pro's: - short code that is used for every elevator in the map (in fact you can use it for everything that translates) - fast setup Con's: - As I didn't tested it very much I don't know any if you find some please report here What is missing: - there are no sounds set up yet (will add them as soos as possible) - AI's should not yet be able to use the elevator (dido)
-
Some games fade in player's view (from black) on spawn. I assume it's done to prevent player seeing certain things being spawns and such. So there is a delay between being spawned and seeing the world. I have a few things, particles in particular, that begin emitting at t he same time player is spawned. It doesn't look appropriate as I'd like for particles to be at full capacity when player is spawned. So I figured if I have a delay and the gradual fade in from black on the view, then the issue would be resolved. Is it something engine/game already provides (Doom 3) ? How can I achieve such effect ? Thanks.
-
Doom 3 question (probably applicable to TDM too) is below. I am not sure offhand, but can I spawn AI hidden? (via entity's property) Can I hide AI via script right after it spawns ? If yes, the question is if that AI is dormant and just hibernates until it's revealed via trigger or script ? Thanks.
-
Not sure where else to write since TheDarkMod forums is the only remaining outpost for Doom 3 modding. I need to make a mover to move along a spline, when triggered. How do I set it up? Can a mover tilt, following spline's curvature ? Is there a way to attach player to that mover and drop player if he presses a key ? Thanks!
- 3 replies
-
- scripting
- level design
-
(and 1 more)
Tagged with: