Prev: Re: [FTFB] Rules Clarification Next: Re: [FT] SMLs, Type 3 & Tactics

Re: Damage Program for many Batteries

From: Alan E & Carmel J Brain <aebrain@d...>
Date: Wed, 13 Jan 1999 19:30:04 +1000
Subject: Re: Damage Program for many Batteries

Charles Choukalos wrote:

> randomize timer
> a = 0
> while (a < 4000)
>   b=int(rnd*6)+1
>   if b=4 or b=5 then c=c+1
>   if b=6 then begin
>      c=c+2
>      a=a-1
>   end
> next a
> print c

OK....

Here's how to do it with a programming language that was purpose-built
for military simulations (and the real stuff too for that matter).

I've deliberately used the same "bulletproofing" and somewhat verbose
coding style that I'd use in real life. Trust me, it saves time in the
long run. Now you can see why Military systems
a) Cost so farnarckling much and
b) Are much more reliable than production COTS stuff, even before beta
testing.

The program may not be bug free BTW, I don't have FB1 to hand, so forget
how to handle Class II shields...

-- Package of Utilities to do damage Calculations for Full Thrust 3
-- Author  : A.E.Brain
-- Created : 19990113
-- History : Initial version
-- Notes   : Only Hits done in this version

package Full_Thrust_Damage_Calculation is

  Max_Number_Of_Dice : constant := 10_000;
  -- Max number of dice it makes sense to roll vs one target

  subtype Number_Of_Dice_Type is integer range 1..Max_Number_Of_Dice;
  -- Range of sensible values for number of dice to roll

  Minimum_Shield : constant := 0;
  Maximum_Shield : constant := 3;
  -- note it should be 2 for FT3...

  subtype ShieldType is integer range Minimum_Shield..Maximum_Shield;
  
  -- Function to determine number of hits from X dice
  function Hits ( Number_Of_Dice    : Number_Of_Dice_Type := 1;
		  ReRoll_6s	    : Boolean		  := True;
		  Effective_Shields : Shield_Type	  := 0

		) return integer;

  -- other functions, for say PDAFs vs Missiles etc go here

  Probable_MaxRolls_Error : exception;
  -- Probably an overflow error, ie the maximum number of dice is too
big

end Full_Thrust_Damage_Calculation;

with Mathematical_Functions;

-- Package of Utilities to do damage Calculations for Full Thrust 3
-- Author  : A.E.Brain
-- Created : 19990113
-- History : Initial version
-- Notes   : Only Hits done in this version

package body Full_Thrust_Damage_Calculation is

  Min_value_of_Die : constant := 1;
  Max_value_of_Die : constant := 6;
 
  subtype DieRoll_Type is integer
    range Min_value_of_Die..Max_value_of_Die;
  -- FT3 only uses D6s. This is hidden from the user.

  function Do_A_Single_Die
    (  ReRoll_6s	 : Boolean	       := True;
       Effective_Shields : Shield_Type	       := 0
    ) return integer is

  -- Note: All inputs have default values

    DieRoll	  : DieRoll_Type;	  -- The number rolled
    RoundUp	  : constant	  := 0.5; -- Needed to round values up
    ReturnedValue : integer	  := 0;   -- The final total, initially
0
      
  begin

    Mathematical_Functions.Random.Initialise;
    -- YMMV on this one, it depends on the exact specification
    -- of Mathematical Functions. Sometimes a seed is needed.

    DieRoll := integer
     ( ( Mathematical_Functions.Random.Get *  MaxValue_of_Die ) +
       RoundUp
     );

    case DieRoll is

      when 1..3 => null;

      when 4	=>
		   if Effective_Shields = 0 then
		     Returned_Value := 1;
		   end if;
      when 5	=>
		   if Effective_Shields = 0 or
		      Effective_Shields = 1 then
		     Returned_Value = 1;

      when 6	=> Returned_Value := 2;
		   if ReRoll_6s then
		     Returned_Value := Returned_Value + 
		       Hits ( Number_Of_Dice => Number_of_Dice,
			      Effective_Shields => 0,
			      ReRoll_6s => True
			    );
		   end if;
		   -- NOTE!!! Re-rolls ignore shields!	 

    end case;

    return Returned_Value;

  exception

    when others => raise Probable_MaxRolls_Error;

  end Do_A_Single_Die;

  -- Function to determine number of hits from X dice
  function Hits ( Number_Of_Dice    : Number_Of_Dice_Type := 1;
		  ReRoll_6s	    : Boolean		  := True;
		  Effective_Shields : Shield_Type	  := 0
		) return integer is

    Total : integer := 0;

  begin

    for i in 1..Number_Of_Dice loop

      Total := Total + Hits ( ReRoll_6s => ReRoll_6s,
			      Effective_Shields => Effective_Shields
			    );
    end loop;

    return Total;

  exception

    when others =>
      raise Probable_MaxRolls_Error;

  end Hits;


with Full_Thrust_Damage_Calculation;
with TEXT_IO;

-- Program to do 3000 Hits in FT3
-- Author  : A.E.Brain
-- Created : 19990113
-- History : Initial version
-- Notes   : Hits only printed out in this version;

procedure Do_3000_Hits is

  TotalDamage : integer := 0;
  -- the Damage

  package Hits_IO is new TEXT_IO.INTEGER_IO(integer);
  -- create an auxiliary package to do all inputs and outputs on
integers

begin

  TotalDamage := Hits( Number_Of_Dice => 3000 );
  -- Note, if shielded, just over-ride the defaults

  Hits_IO.put( TotalDamage );

exception

  when Probable_MaxRolls_Error =>
    TEXT_IO.Put
     ("Something went wrong, I'm afraid. Number of dice too large?");

  when others =>
    TEXT_IO.Put
      ("Probably Doze or Unix playing silly whatsits...");

end Do_3000_Hits;

If you've read this far, congratulations!

The language is Ada, and you can get FREE compilers (W95, W3.11, NT,
Linux. Mac etc etc) via http://www.adahome.com

-- 
aebrain@dynamite.com.au     <> <>    How doth the little Crocodile
| Alan & Carmel Brain|	    xxxxx	Improve his shining tail?
| Canberra Australia |	xxxxxHxHxxxxxx _MMMMMMMMM_MMMMMMMMM
 abrain@cs.adfa.oz.au  o OO*O^^^^O*OO o oo     oo oo	 oo  
		    By pulling MAERKLIN Wagons, in 1/220 Scale

Prev: Re: [FTFB] Rules Clarification Next: Re: [FT] SMLs, Type 3 & Tactics