intfiction.org

The Interactive Fiction Community Forum
It is currently Sun May 26, 2013 1:35 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Mar 30, 2012 7:32 pm 
Offline

Joined: Mon Jan 23, 2012 8:44 pm
Posts: 12
Location: US
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:

Code:
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.

Code:
>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?


Top
 Profile Send private message  
 
PostPosted: Fri Mar 30, 2012 8:37 pm 
Offline

Joined: Mon Oct 11, 2010 1:27 pm
Posts: 455
The problem is in this line:

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


Which is better off as...

Code:
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.


Top
 Profile Send private message  
 
PostPosted: Fri Mar 30, 2012 8:59 pm 
Offline

Joined: Mon Jan 23, 2012 8:44 pm
Posts: 12
Location: US
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.

Quote:
At any rate, welcome! It looks like you have a good grasp on coding in general & are taking on something pretty ambitious.


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...


Top
 Profile Send private message  
 
PostPosted: Fri Mar 30, 2012 9:28 pm 
Offline

Joined: Tue Mar 09, 2010 2:34 pm
Posts: 2129
Location: Burlington, VT
For decimal numbers you'd probably want the Fixed Point Maths extension (though I've never used it).


Top
 Profile Send private message  
 
PostPosted: Sat Mar 31, 2012 7:30 pm 
Offline

Joined: Mon Jan 23, 2012 8:44 pm
Posts: 12
Location: US
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.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group