/*  TDA C engine A&A simulator. (c).
    Version 1.03    By: The Dark Allies
    ANSI C standard.  Free for public domain.

    All modules bug-free.
    This engine has been tested in synch with various other
    simulators to ensure glitch-free processing.

*/

#include 
#include 
#include 
#include 

#define TRUE 1
#define FALSE 0
#define ON 1
#define OFF 0

int hb_tech, ss_tech, jet_tech, two_hits;

int a_inf, a_tanks, a_fighters, a_bombers;
int d_inf, d_tanks, d_fighters, d_bombers;

int a_trans, a_subs, a_carriers, a_bships;
int d_trans, d_subs, d_carriers, d_bships;


// ********** User Settings **********
int total_sims;      // Tota simulations
int land_battle;     // Land battle or Sea battle?


// **** Land variables
int attacking_inf, attacking_tanks, attacking_fighters, attacking_bombers;
int defending_inf, defending_tanks, defending_fighters, defending_bombers;

// *** Navy variables (excluding planes)
int attacking_trans, attacking_subs, attacking_carriers, attacking_bships;
int defending_trans, defending_subs, defending_carriers, defending_bships;
// ***********************************


// =====================================================
// ********** User Settings **********
total_sims = 1000;
land_battle = FALSE;

hb_tech = OFF;      // Heavy bombers
ss_tech = OFF;      // Super subs
jet_tech = OFF;     // Jets
two_hits = ON;     // Two hit bships

// **** Land variables
attacking_inf = 26;
attacking_tanks = 4;
attacking_fighters = 0;
attacking_bombers = 0;

defending_inf = 26;
defending_tanks =5;
defending_fighters = 0;
defending_bombers = 0;

// *** Navy variables (excluding planes)
attacking_trans = 1;
attacking_subs = 1;
attacking_carriers = 0;
attacking_bships = 0;

defending_trans = 0;
defending_subs = 0;
defending_carriers = 0;
defending_bships = 1;



main() {
    int result;
    int ties =0;
    int attack_wins =0;
    int defend_wins =0;


    int dumy;

    ReSeed();   // Reseed Random Generator

    if (land_battle == TRUE) {
        // User wants land type battle
        for (dumy=0;dumy < total_sims;) {

            a_inf = attacking_inf;
            a_tanks = attacking_tanks;
            a_fighters = attacking_fighters;
            a_bombers = attacking_bombers;

            d_inf = defending_inf;
            d_tanks = defending_tanks;
            d_fighters = defending_fighters;
            d_bombers = defending_bombers;

            result = LandBattle();
            if (result == FALSE) {
                ties++;
            } else if (result == TRUE) {
                attack_wins++;
            } else {
                defend_wins++;
            }
            dumy++;
        }
    } else {
        // User wants navy type battle
        for (dumy=0; dumy", land_battle);
    printf("\n\nAttacking Inf = %d           Defending Inf = %d", attacking_inf, defending_inf);
    printf("\nAttacking Tanks = %d         Defending Tanks = %d", attacking_tanks, defending_tanks);
    printf("\n\nAttacking Fighters = %d       Defending Fighters = %d", attacking_fighters, defending_fighters);
    printf("\nAttacking Bombers = %d        Defending Bombers = %d", attacking_bombers, defending_bombers);
    printf("\n\nAttacking Transports = %d        Defending Transports = %d", attacking_trans, defending_trans);
    printf("\nAttacking Submarines = %d        Defending Submarines = %d", attacking_subs, defending_subs);
    printf("\nAttacking Carriers = %d        Defending Carriers = %d", attacking_carriers, defending_carriers);
    printf("\nAttacking Battle Ships = %d    Defending Battle Ships = %d", attacking_bships, defending_bships);
    printf("\n\nTotal # Simulations = %d", total_sims);
}

int LandBattle() {

    int attacker_lost = 0;
    int defender_lost = 0;
    int a_total_units = a_inf + a_tanks + a_fighters + a_bombers;
    int d_total_units = d_inf + d_tanks + d_fighters + d_bombers;


    while (1) {

        int attacker_hits = 0;
        int defender_hits = 0;

        int counter, counter2;    // variable for for loops
        int dice;

        counter = 0;
        // Infantry calculations
        for (; counter < a_inf;) {
            dice = Roll(1);
            if (dice == TRUE)
                attacker_hits++;
            ++counter;
        }
        counter= 0;
        // Tank calculations
        for (; counter < a_tanks;) {
            dice = Roll(3);
            if (dice == TRUE)
                attacker_hits++;
            ++counter;
        }
        // Fighter Calculations
        counter = 0;
        for (; counter < a_fighters;) {
            dice = Roll(3);
            if (dice == TRUE)
                attacker_hits++;
            ++counter;
        }
        // Bomber Calculations
        counter = 0;
        for (; counter < a_bombers;) {
            if (hb_tech == OFF) {
                dice = Roll(4);
                if (dice == TRUE)
                    attacker_hits++;

            } else {        // Heavy Bombers
                for (counter2=0; counter2<3; counter2++) {
                    dice = Roll(4);
                    if (dice == TRUE)
                        attacker_hits++;
                }
            }
            ++counter;
        }

        /**** Defending side does retals ****/

        // Infantry calculations
        counter = 0;
        for (; counter < d_inf;) {
            dice = Roll(2);
            if (dice == TRUE)
                defender_hits++;
            ++counter;
        }

        // Tank calculations
        counter= 0;
        for (; counter < d_tanks;) {
            dice = Roll(2);
            if (dice == TRUE)
                defender_hits++;
            ++counter;
        }

        // Fighter calculations
        counter = 0;
        for (; counter < d_fighters;){
            if (jet_tech == OFF) {
                dice = Roll(4);
                if (dice == TRUE)
                    defender_hits++;
            } else {
                dice = Roll(5);
                if (dice == TRUE)
                    defender_hits++;
            }
            ++counter;
        }

        // Bomber calculations
        counter = 0;
        for (; counter < d_bombers;){
            dice = Roll(1);
            if (dice == TRUE)
                defender_hits++;
            ++counter;
        }

        /*** Status ***/
        printf("\nAttacker hits = %d    Defender hits = %d", attacker_hits, defender_hits);

        /*** Cleanup... remove dead units ***/
        while (defender_hits > 0) {
            if (a_inf > 0) {
                a_inf--;
                a_total_units--;
            } else if (a_tanks > 0) {
                a_tanks--;
                a_total_units--;
            } else if (a_fighters >0) {
                a_fighters--;
                a_total_units--;
            } else if (a_bombers > 0) {
                a_bombers--;
                a_total_units--;
            } else {
                printf ("  Attacker loses");
                attacker_lost = 1;
                break;
            }
            --defender_hits;
        }

        // Defending bombers removed BEFORE fighters.
        while (attacker_hits > 0) {
            if(d_inf > 0) {
                d_inf--;
                d_total_units--;
            } else if (d_tanks > 0) {
                d_tanks--;
                d_total_units--;
            } else if (d_bombers > 0) {
                d_bombers--;
                d_total_units--;
            } else if (d_fighters > 0) {
                d_fighters--;
                d_total_units--;
            } else {
                printf ("  Defender loses");
                defender_lost = 1;
                break;
            }
            --attacker_hits;
        }

        if (d_total_units < 1) {
            defender_lost = 1;
        }
        if (a_total_units < 1) {
            attacker_lost = 1;
        }

        if (attacker_lost == 1 && defender_lost == 1) {
            printf("Tie.....");
            return 0;
        } else if (attacker_lost == 1) {
            printf("  Defending side wins!");
            return -1;
        } else if (defender_lost ==1) {
            printf("  Attacking side wins!");
            return 1;
        } else {
            printf("  Continue battle...");
        }
    }
}


int SeaBattle() {


    int attacker_lost = 0;
    int defender_lost = 0;
    int a_total_units = a_trans + a_subs + a_fighters + a_bombers + a_carriers + a_bships;
    int d_total_units = d_trans + d_subs + d_fighters + d_carriers + d_bships;

    // Two hit bships get extra absorb defence
    int a_bship_absorb = a_bships;
    int d_bship_absorb = d_bships;

    int dumy;   // Variable for for loops


    if (two_hits == OFF) {
        a_bship_absorb = 0;
        d_bship_absorb = 0;
    }


    while (TRUE) {

        int attacker_hits = 0;
        int defender_hits = 0;

        int counter = 0;    // Variable for for loops
        int dice;

        // Attacking sub hits to remove
        int sub_hits = 0;

        // Trannies calculations
        // Trannis cant roll on attack so no bother

        // Sub Calculations
        counter= 0;
        for (; counter < a_subs;) {
            if (ss_tech == OFF) {
                dice = Roll(2);
                if (dice == TRUE) {
                    sub_hits++;
                }
            } else {            // Super sub tech
                dice = Roll(3);
                if (dice == TRUE) {
                    sub_hits++;
                }
            }
            ++counter;
        }

        // Fighter Calculations
        counter = 0;
        for (; counter < a_fighters;) {
            dice = Roll(3);
            if (dice == TRUE)
                attacker_hits++;
            ++counter;
        }



        // Bomber Calculations
        counter = 0;
        for (; counter < a_bombers;) {
            dice = Roll(4);
            if (dice == TRUE)
                attacker_hits++;
            ++counter;
        }

        // Carrier Calculations
        counter = 0;
        for (; counter < a_carriers;) {
            dice = Roll(1);
            if (dice == TRUE)
                attacker_hits++;
            ++counter;
        }

        // Battle Ship Calculations
        counter = 0;
        for (; counter < a_bships;) {
            dice = Roll(4);
            if (dice == TRUE)
                attacker_hits++;
            ++counter;
        }

        //*******************************************************************
        //*** Remove any naval pieces hit by subs FIRST before defending is
        //*** allowed to use them to retaliate.
        for (dumy=0; dumy 0) {
                d_bship_absorb--;
            } else if (d_trans > 0) {
                d_trans--;
                d_total_units--;
            } else if (d_subs > 0) {
                d_subs--;
                d_total_units--;
            } else if (d_carriers > 0) {
                d_carriers--;
                d_total_units--;
            } else if (d_bships > 0) {
                d_bships--;
                d_total_units--;
            } else {
                // no more navy pieces to hit
                printf("  Attacking side wins!");
                return 1;
            }
            if (d_total_units < 1) {
                printf("  Attacking side wins!");
                // Defender has lost
                return 1;

            }
        }


        /**** Defending side does retals ****/

        // Transport Calculations
        counter = 0;
        for (; counter < d_trans;) {
            dice = Roll(1);
            if (dice == TRUE)
                defender_hits++;
            ++counter;
            printf("Counter");
        }

        // Sub Calculations
        counter= 0;
        for (; counter < d_subs;) {
            dice = Roll(2);
            if (dice == TRUE)
                defender_hits++;
            ++counter;
        }

        // Fighter Calculations
        counter = 0;
        for (; counter < d_fighters;){
            if (jet_tech == OFF) {
                dice = Roll(4);
                if (dice == TRUE)
                    defender_hits++;
            } else {
                dice = Roll(5);
                if (dice == TRUE)
                    defender_hits++;
            }
            ++counter;
        }


        // Bomber Calculations
        // Duh, can't have defending bombers

        // Carrier Calculations
        counter = 0;
        for (; counter < d_carriers;){
            dice = Roll(3);
            if (dice == TRUE)
                defender_hits++;
            ++counter;
        }

        // BattleShip Calculations
        counter = 0;
        for (; counter < d_bships;){
            dice = Roll(4);
            if (dice == TRUE)
                defender_hits++;
            ++counter;
        }




        /*** Status ***/
        printf("\nAttacker hits = %d    Defender hits = %d", attacker_hits, defender_hits);

        /*** Cleanup... remove dead units ***/
        while (defender_hits > 0) {
            // Check if bships can absorb a hit first
            if (a_bship_absorb > 0) {
                a_bship_absorb--;
            } else if (a_trans > 0) {
                a_trans--;
                a_total_units--;
            } else if (a_subs > 0) {
                a_subs--;
                a_total_units--;
            } else if (a_fighters >0) {
                a_fighters--;
                a_total_units--;
            } else if (a_bombers > 0) {
                a_bombers--;
                a_total_units--;
            } else if (a_carriers > 0) {
                a_carriers--;
                a_total_units--;
            } else if (a_bships > 0) {
                a_bships--;
                a_total_units--;
            } else {
                printf ("  Attacker loses");
                attacker_lost = TRUE;
                break;
            }
            --defender_hits;
        }

        while (attacker_hits > 0) {
            // Check if bships can absorb a hit first
            if (d_bship_absorb > 0) {
                d_bship_absorb--;
            } else if(d_trans > 0) {
                d_trans--;
                d_total_units--;
            } else if (d_subs > 0) {
                d_subs--;
                d_total_units--;
            } else if (d_fighters > 0) {
                d_fighters--;
                d_total_units--;
            } else if (d_carriers > 0) {
                d_carriers--;
                d_total_units--;
            } else if (d_bships > 0) {
                d_bships--;
                d_total_units--;
            } else {
                printf ("  Defender loses");
                defender_lost = TRUE;
                break;
            }
            --attacker_hits;
        }

        if (d_total_units < 1) {
            defender_lost = TRUE;
        }
        if (a_total_units < 1) {
            attacker_lost = TRUE;
        }

        if (attacker_lost == TRUE && defender_lost == TRUE) {
            printf("Tie.....");
            return 0;
        } else if (attacker_lost == TRUE) {
            printf("  Defending side wins!");
            return -1;
        } else if (defender_lost == TRUE) {
            printf("  Attacking side wins!");
            return 1;
        } else {
            printf("  Continue battle...");
        }
    }
}


// Roll at or below dice value
int Roll(int red_white) {
    if ((rand() %6 +1) <= red_white) {
        // printf("Hit!");
        return TRUE;
    } else {
        // printf("Miss!");
        return FALSE;
    }
}

// Reseed the random generator
void ReSeed() {
    struct time timebuf;
    gettime(&timebuf);
    srand(timebuf.ti_hund);
}

    Source: geocities.com/uboat101