Diamond Bridge Commander - Projectiles
Saturday, April 11, 2009 11:21:59 PM
I say projectile, because there are two types of projectiles, torpedoes and pulse projectiles, the former is sprite based, the latter (fixed, but stretchable) model based, those are just the projectiles. Then there are torpedo and pulse systems. The difference there is that for torpedoes there are at most 4 projectile types, each with a predefined maximum of "ammo", you define it once, then it is used for all torpedo tubes.
Pulse weapons use a "charge", each time you fire a pulse weapon some of that charge is deducted, but will restore over time. Each pulse weapon is configured individually. Torpedo and Pulse projectiles can be used interchangably, for BC there is no difference between a torpedo script and a pulse script.
So, BC crashes when a projectile module isn't found, or contains an error.
People usually notice it when they are in a battle, so it's kinda annoying when it suddenly happens.
It gets worse, to find out you have to remember all the ships present when the crash occurred. After that, you will have to try out each ship on its own, against another without projectiles, such as asteroids. When you have found the ship, you can be considerd lucky if it's a torpedo, because in that case, you can simply look it up in the hardpoint, where all torpedoes are listed toghether in one neat place. It is the only a question of going by each of them to check if they are present. If it's a pulse weapon, then you have to go by all the pulse weapon hardpoints and check all the mentioned projectiles. It get's worse when the person doing the checking has no background with programming (languages).
So, can we do anything to make this easier? We can indeed. People could send their installs (or just their scripts directory) to me and I go by all of the ships... Hang on, that does not sound like a good idea at all! While it has been (and still is) a really last resort for me to fix a problem, there has to be a better way.
And indeed there is, even if it is slightly ugly, it won't even involve all the modders out there creating ships to check if they have all their projectiles included, it even works on older already released ships (in fact, on all ships). It's a script, auto loading on mod load, 4709 bytes big (with comments). And it monkey patches the projectile "setting" and loading parts. Yes, I monkey patched them, a phrase which originated from the Ruby community, but of which the practise was already present in the Python community. Well, it was slightly frowned upon, since (as it has been in Ruby) it is a maintenance nightmare. But it honestly was my only choice here! The only proper (programmatically speaking) and legel one anyway (for those interested, the other was patching the executable, much more time consuming, harder, complexer and illegal through the EULA, this would be needed since the logic for loading projectiles is in the executable, programmed in C++, and we don't have the source).
So, what did I do? I tracked down all functions/methods involved with projectile configuration (for hardpoints, not the projectiles themselves) and creation of them. Of the first category there are four methods, of the latter there is one function. And I wrote a function that verifies a projectile module, on two criteria no less, first of all, the most important one, does the module exist? If not, print a warning in the console and replace it with a "safe" module. The other criterium is if it contains five specific functions, Create, GetName, GetPowerCost, GetLaunchSpeed and GetLaunchSound, those seem to be the required functions that BC (executable) calls from outside Python. If one of those functions is missing, then we print it in the console and replace it with a "safe" module.
So why did I put the word "safe" in quotation marks? It is honestly meant as sarcastic. The definition of a safe projectile module is one which complies to the aforementioned criteria. What is the biggest thread to this? That someone doesn't install (or include) the safe projectile, I could have gone with the stock Photon torpedo (in way, I have), but some bigger mods remove even that. The second criterium is infact harder to break, since it would mean someone has to actively alter the file to do so, and most people who would want to include the mod in their own mods (to be on the safe side) wouldn't mess with it (others would make sure they have everything included). So what is the only, truely safe projectile module we can create? The module that does the monkey patching itself. If that module isn't present, then the logic couldn't possibly (have) run. So I copied the stock Photon Torpedo functions. One thing that I have done is changed the name of the projectile to "ProjectileSubstitute".
So, with this little script, a user is safe against missing, or broken, projectiles, and BC is stronger for it! So, to go full circle, I plan to create a serie of mods for BC that will all make BC stronger/robuster, in the end, as hard as a diamond! Here by, the DiamondBC mod is born.
And the first out of N (I haven't yet thought of all fixes, could be just this one, could be 2 could be 10 or more) is the module called "Projectiles".
I should mention, there is another peculiar thing in BC, related to torpedoes. It is impossible to have more than four torpedo types, per ship that is. Infact, in stock BC (and early modding) torpedo types for player ships were limited to two in Quick Battle, and three in Singleplayer. The reason for the two in QuickBattle was a little bit of code, that removed the extra torpedo type(s). But it appears that BC is hardcoded to a maximum of four torpedo types, and will crash if you configure a ship to have more. There is a way around this, but it involves doing the job yourself instead of letting BC do it. And in the past, I have created such a mod, part of a Detail Fixing mod (very old, in BC time). And I suppose mostly broken. But it actually worked. And it too contained code that would prevent BC from crashing on missing torpedo scripts (only for the torpedo system). So why does this mod not contain the "fix" for more than four torpedo type? Well, the tool for hardpoints doesn't like more than four torpedoes either, and hardpoints aren't that easily editted for those without experience and if you were to distribute that hardpoint to others without the script then BC would crash for others. Yes, if you don't distribute the script nor the projectiles, then BC will crash as well. But it's easier to distribute the projectiles afterwards than it is to fix a hardpoint afterwards (different mental payload, and people tend to dump it in scripts/ships/ the wrong directory for it). Aside from that, I want the AI to use it as well, so I'm currently reworking the previous script into a FoundationTechnologies plugin, which will automatically use the script for DiamondBC if it's installed. And since it will be a FoundationTechnologies plugin, it would mean that people without the plugin or FTech would simply fall back to the normal torpedo system with a maximum of four torpedoes. But that is for the future and not for now.
I will leave you with the current, beta, version of the script (I need to check all cases, and I need to check the possibility of a missing sound and missing texture) and a screenshot of the console showing that a projectile is missing.

Now released: http://bridgecommander.filefront.com/file/DiamondBC_Projectiles;99219
