Weapon code giving me a headache

I’m trying to make a game with different sci-fi energy weapons in it, each of which has slightly different effectiveness etc etc. Here is the code that gives a basic gun that charges up:

Earth is a room.

A gun is a kind of device. A gun has a number called the power level.

A gun has a number called the charge level.

A gun has a number called the max charge.

A gun has a number called the charge speed.

A gun has a number called the energy efficiency.

Every turn:
	repeat with X running through switched on guns:
		if the charge level of X is the max charge of X:
			next;
		let ch be (charge speed of X * energy efficiency of X) / 10;
		if the charge speed of X is greater than the power level of X:
			if ch is greater than the max charge of X - the charge level of X:
				let Y be max charge of X - the charge level of X;
				let Y be Y / (energy efficiency of X  * 10);
				decrease the power level of X by Y;
				now the charge level of X is the max charge of X;
			else:
				increase the charge level of X by (the power level of X * energy efficiency of X) / 10;
				now the power level of X is 0;
		else:
			if ch is greater than the max charge of X - the charge level of X:
				let Y be max charge of X - the charge level of X;
				let Y be (Y / energy efficiency of X) * 10;
				decrease the power level of X by Y;
				now the charge level of X is the max charge of X;
			else:
				increase the charge level of X by ch;
				decrease the power level of X by charge speed of X;
				
The player carries a laser pistol. The laser pistol is a gun. The power level is 400. The max charge is 145. The charge speed is 13. The energy efficiency is 12.

Carry out examining a gun:
	say "Weapon stats:[line break]Current shot charge level: [charge level of noun]/[max charge of noun][line break]Power level: [power level of noun][line break]Battery discharge speed: [charge speed of noun] level[s] per turn[line break]Energy efficiency: [energy efficiency of noun]%[line break]";
	now examine text printed is true.

In short, power level is the gun’s “battery,” charge level is the amount of damage that will be done when the gun is fired, max charge is the amount of energy that can be used in one shot, charge speed is the number of power level levels that are consumed per turn, and energy efficiency is the increase in charge level per 10 power levels consumed.

However this gives a truly annoying and slightly unbalanced response, the source of which I cannot figure out–obviously it is in the “every turn” rule, but I can’t see what the problem with it is.

>x pistol
Weapon stats:
Current shot charge level: 135/145
Power level: 283
Battery discharge speed: 13 levels per turn
Energy efficiency: 12%

The laser pistol is currently switched on.

>x pistol
Weapon stats:
Current shot charge level: 145/145
Power level: 283
Battery discharge speed: 13 levels per turn
Energy efficiency: 12%

The laser pistol is currently switched on.

This turn should have consumed several power levels (previously calculated to be 3 but that wasn’t reasonable, more like 8-9) but did not use any. Anyone know what’s wrong?

The problem is in this line:

let Y be (Y / energy efficiency of X) * 10;

Which is better off as…

let Y be (Y * 10) / energy efficiency of X;

It’s tripped me up, too. Basically Inform uses integers and not floats. So when the code branches here (if ch is greater than the max charge of X - the charge level of X:) e.g. when you reach 135/145 and don’t want to go to 150/145,

Y/energy efficiency of X = 0

So 0*10 is 0.

Switching this seems to give what you’d want. You often have to be extra careful with parentheses as Inform works left-to-right and doesn’t always use PEMDAS.

At any rate, welcome! It looks like you have a good grasp on coding in general & are taking on something pretty ambitious. Speak up if this wasn’t quite the question you want answered.

Thank you very much, this is what I needed! Previously I’d tried to make a new decimal number value but found that only confused Inform more.

I’ve been using Inform for about two years and yes, this is part of the first game I’d consider having any chance of being released–although, that’s what I thought two years ago…

For decimal numbers you’d probably want the Fixed Point Maths extension (though I’ve never used it).

I suppose it would work, but A) there’s already this system which works just about as well and B) I’m not sure there’s an easy way to print decimal numbers without the extra zeroes showing up, considering I would only need it to the 100ths place.