/* SOURCE FOR WARS OF THE WHEEL (using allegro) */

/* Check out www.allegro.cc for info on the allegro library */

/* Copyright 2004 Craig Harrison. All rights reserved. */

Site hosted by Angelfire.com: Build your free website today!
#include <allegro.h> // Allegro Header file
#include <cstdlib> // Include for Random
#include "data.h"

// Timer
volatile long speed_counter = 0;
void increment_speed_counter()
{
	speed_counter++;
}
END_OF_FUNCTION(increment_speed_counter);

int in_box(int x,int y, int xx, int yy);

int over_button(int x,int y, int xx, int yy);

int num_key();

int unit_cost(int unit);

// Main Function
int main(int argc, char *argv[])
{

	
	
	allegro_init(); // Initialize Allegro
	install_keyboard(); // Initialize keyboard routines
	install_timer(); // Initialize the timer routines
    install_mouse(); // Initialize the mouse routines
    install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT , 0); // Initialize the sound routines


	LOCK_VARIABLE(speed_counter);		   //Used to set the timers
	LOCK_FUNCTION(increment_speed_counter);
	install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));//Set our BPS

	set_color_depth(16); // Set the color depth
							
	set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 800,600,0,0);
    srand(5);
    text_mode(-1);

    DATAFILE *datafile;
    datafile = load_datafile("data.dat");
    FONT* font;
    FONT* sfont;
    font = (FONT *)datafile[myfont].dat;
    sfont = (FONT *)datafile[smallfont].dat;

    // Setup Buffer and Bitmaps
	BITMAP *buffer;
	buffer = create_bitmap(800,600);
 	BITMAP *slider;
	slider = create_bitmap(368,315);

    // Variables
    int music_playing = 1;
    int towns[3][9];
    int view_town = 0;
    int towns_owned = 3;
    int mouse_pressed = 0;
    int slidein;
    int slideinb;
    int townata = 0;
    int build_num = 0;
    int train_num = 5;
    int build_dir = 0;
    int cur_player = 0;
    int play_variables[3][6];
    int pcost = 0;
    int pcostunit = 0;
    int free_var = 0;
    int free_vark = 0;
    int carrot = 0;
    int number_array[4];
    int akey_pressed = 0;
    int units_to_build = 0;
    int clicked_unit_box = 0;
    int blink_cursor = 0;

    towns[0][3] = 0;
    towns[0][4] = 0;
    towns[1][3] = 1;
    towns[1][4] = 1;
    towns[2][3] = 1;
    towns[2][4] = 3;
    towns[1][0] = 1;

    number_array[0] = -1;
    number_array[1] = -1;
    number_array[2] = -1;
    number_array[3] = -1;

    for (int xxs = 0; xxs < 3; xxs++) // Clear Player Variables
    {
    play_variables[xxs][0] = 10000; // Starting Gold
    play_variables[xxs][1] = 0;
    play_variables[xxs][2] = 0;
    play_variables[xxs][3] = 0;
    play_variables[xxs][4] = 0;
    play_variables[xxs][5] = 0;
    }

    while (townata < 3)
     {
     towns[townata][5] = 0;
     towns[townata][6] = 0;
     towns[townata][7] = 0;
     towns[townata][8] = 0;
     townata++;
     }

    towns[2][5] = 1;
    towns[2][7] = 3;
    towns[0][8] = 2;
    towns[0][5] = 4;
    towns[0][6] = 5;

    // Start Midi
    play_midi((MIDI *)datafile[midi_1].dat, 1);

    /////////////////////////////////////////////////////////////////////////////////////////
	// Game Loop ////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////

	while(!key[KEY_ESC])//If the user hits escape, quit
	{
	
		while(speed_counter > 0)
		{
        // Game Stuff
        speed_counter --;

        // Midi Music Controls
        if (midi_pos < 0)
         {
         if (music_playing == 1) play_midi((MIDI *)datafile[midi_2].dat, 1);
         if (music_playing == 2) play_midi((MIDI *)datafile[midi_3].dat, 1);
         if (music_playing == 3) play_midi((MIDI *)datafile[midi_1].dat, 1);
         music_playing ++;
         if (music_playing == 4) music_playing = 1;
         }

        // Draw to Screen

		acquire_screen();// Get the screen

        draw_sprite(buffer, (BITMAP *)datafile[back].dat, 0, 0); // Draw Background
        textprintf(buffer, font, 67, 11, makecol(0,0,0), "%d", play_variables[cur_player][0]);


        // Draw Buttons (Def Build Upgrades)
        if (over_button(188, 81, 126, 38) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 188, 81, 188, 81, 126, 38);
         pcost = 2000;
         if ((mouse_b & 1) && mouse_pressed == 0)
          {
          mouse_pressed = 1;
          if (play_variables[cur_player][0] > 1999 && towns[view_town][3] < 1)
           {
           play_variables[cur_player][0] = play_variables[cur_player][0]-2000;
           towns[view_town][3] = towns[view_town][3]+1;
           if (slidein == 0) slidein = -1;
           }
          }
         }
        if (over_button(188, 122, 126, 38) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 188, 122, 188, 122, 126, 38);
         pcost = 1500;
         if ((mouse_b & 1) && mouse_pressed == 0)
          {
          mouse_pressed = 1;
          if (play_variables[cur_player][0] > 1499 && towns[view_town][4] < 3)
           {
           play_variables[cur_player][0] = play_variables[cur_player][0]-1500;
           towns[view_town][4]++;
           if (slidein == 0) slidein = -1;
           }
          }
         }

        // Level Buttons
        if (over_button(6, 97, 78, 20) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 6, 97, 6, 97, 106, 20);
         pcost = 1000;
         if ((mouse_b & 1) && mouse_pressed == 0)
          {
           mouse_pressed = 1;
           if (play_variables[cur_player][0] > 999)
            {
            play_variables[cur_player][0] = play_variables[cur_player][0]-1000;
            play_variables[cur_player][1]++;
            }
          }
         }
        if (over_button(6, 119, 78, 20) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 6, 119, 6, 119, 106, 20);
         pcost = 1000;
         if ((mouse_b & 1) && mouse_pressed == 0)
          {
           mouse_pressed = 1;
           if (play_variables[cur_player][0] > 999)
            {
            play_variables[cur_player][0] = play_variables[cur_player][0]-1000;
            play_variables[cur_player][2]++;
            }
          }
         }
        if (over_button(6, 141, 78, 20) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 6, 141, 6, 141, 106, 20);
         pcost = 1000;
         if ((mouse_b & 1) && mouse_pressed == 0)
          {
           mouse_pressed = 1;
           if (play_variables[cur_player][0] > 999)
            {
            play_variables[cur_player][0] = play_variables[cur_player][0]-1000;
            play_variables[cur_player][3]++;
            }
          }
         }
        if (over_button(6, 163, 78, 20) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 6, 163, 6, 163, 106, 20);
         pcost = 1000;
         if ((mouse_b & 1) && mouse_pressed == 0)
          {
           mouse_pressed = 1;
           if (play_variables[cur_player][0] > 999)
            {
            play_variables[cur_player][0] = play_variables[cur_player][0]-1000;
            play_variables[cur_player][4]++;
            }
          }
         }
        if (over_button(6, 184, 106, 20) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 6, 184, 6, 184, 106, 20);
         pcost = 1000;
         if ((mouse_b & 1) && mouse_pressed == 0)
          {
           mouse_pressed = 1;
           if (play_variables[cur_player][0] > 999)
            {
            play_variables[cur_player][0] = play_variables[cur_player][0]-1000;
            play_variables[cur_player][5]++;
            }
          }
         }

        // Build/Train Buttons
        if (over_button(38, 488, 55, 28) == 1) blit((BITMAP *)datafile[backh].dat, buffer, 38, 488, 38, 488, 55, 28);
        if (over_button(38, 488, 143, 28) == 1)
         {
         if (build_num == 0) pcost = 4000;
         if (build_num == 1) pcost = 6000;
         if (build_num == 2) pcost = 8000;
         if (build_num == 3) pcost = 10000;
         if (build_num == 4) pcost = 15000;
         }
        if (over_button(38, 531, 55, 28) == 1) blit((BITMAP *)datafile[backh].dat, buffer, 38, 531, 38, 531, 55, 28);

        // CLick in Unit Build Box
        if (over_button(99, 533, 77, 27) && mouse_b & 1)
         {
         clicked_unit_box = 1;
         blink_cursor = 0;
         number_array[0] = -1;
         number_array[1] = -1;
         number_array[2] = -1;
         number_array[3] = -1;
         }

        // Unit Cost
        if (over_button(38, 531, 185, 29))
         {
         pcost = unit_cost(train_num)*units_to_build;
         pcostunit = unit_cost(train_num);
         }

        //////////////////////////////////////////////////////////////////////////////////////////
        // Mouse Pressed Buttons /////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////

        if (mouse_b & 1)
        {
        // Unclick in Unit Build Box
        if (!over_button(99, 533, 77, 27)) clicked_unit_box = 0;

        // Build Building
        if (over_button(38, 488, 55, 28) == 1 && mouse_pressed == 0)
         {
         mouse_pressed = 1;
         if ((!towns[view_town][5+build_dir] > 0) && (pcost < play_variables[cur_player][0]-1))
          {
          towns[view_town][5+build_dir] = build_num + 1;
          play_variables[cur_player][0] = play_variables[cur_player][0]-pcost;
          if (slidein == 0) slidein = -1;
          }
         }
        // Scroll Between Castles Buttons
        if (over_button(295, 165, 35, 60) == 1)
         {
         if (mouse_pressed == 0)
          {
          blit((BITMAP *)datafile[backh].dat, screen, 295, 165, 295, 165, 35, 60);
          draw_sprite( screen, (BITMAP *)datafile[lcursor].dat, mouse_x, mouse_y);
          mouse_pressed = 1;
          view_town --;
          if (view_town < 0) view_town = towns_owned-1;
          if (view_town > towns_owned-1) view_town = 0;
          if (slideinb == 0) slideinb = 800;
          }
         }

        if (over_button(2, 486, 31, 29) == 1)
         {
          blit((BITMAP *)datafile[backh].dat, buffer, 2, 486, 2, 486, 31, 29);
         }

        if (over_button(715, 165, 35, 60) == 1)
         {
         if (mouse_pressed == 0)
          {
          blit((BITMAP *)datafile[backh].dat, screen, 715, 165, 715, 165, 35, 60);
          draw_sprite( screen, (BITMAP *)datafile[lcursor].dat, mouse_x, mouse_y);
          mouse_pressed = 1;
          view_town ++;
          if (view_town < 0) view_town = towns_owned-1;
          if (view_town > towns_owned-1) view_town = 0;
          if (slideinb == 0) slideinb = 800;
          }
         }

        // Destroy Building Button
        if (over_button(2, 486, 31, 31) == 1)
         {
         if (mouse_pressed == 0)
          {
          mouse_pressed = 1;
          blit((BITMAP *)datafile[backh].dat, screen, 2, 486, 2, 486, 31, 31);
          draw_sprite(screen, (BITMAP *)datafile[lcursor].dat, mouse_x, mouse_y);
          if (slideinb == 0 && !towns[view_town][5+build_dir] == 0) slideinb = 800;
          towns[view_town][5+build_dir] = build_num = 0;
          }
         }

        // Max Unit Build
        if (over_button(2, 529, 31, 31) == 1 || (clicked_unit_box == 0 && (unit_cost(train_num)*units_to_build) > play_variables[cur_player][0]))
         {
          blit((BITMAP *)datafile[backh].dat, buffer, 2, 529, 2, 529, 31, 31);
          units_to_build = int(play_variables[cur_player][0]/unit_cost(train_num));
         }

        // Build Direction Buttons
        if (over_button(330, 490, 20, 20) == 1 && mouse_pressed == 0)
         {
           mouse_pressed = 1;
           build_dir = 0;
          }
        if (over_button(348, 490, 20, 20) == 1 && mouse_pressed == 0)
         {
           mouse_pressed = 1;
           build_dir = 2;
          }
        if (over_button(362, 490, 20, 20) == 1 && mouse_pressed == 0)
         {
           mouse_pressed = 1;
           build_dir = 1;
          }
        if (over_button(383, 490, 20, 20) == 1 && mouse_pressed == 0)
         {
           mouse_pressed = 1;
           build_dir = 3;
          }

        // build Arrow Buttons
        if (over_button(100, 493, 16, 19) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 100, 493, 100, 493, 16, 19);
          if (mouse_pressed == 0)
           {
           mouse_pressed = 1;
           build_num ++;
           }
          }
        if (over_button(121, 493, 16, 19) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 121, 493, 121, 493, 16, 19);
          if (mouse_pressed == 0)
           {
           mouse_pressed = 1;
           build_num --;
           }
          }
        if (build_num < 0) build_num = 4;
        if (build_num > 4) build_num = 0;

        // Train Arrow Buttons
        if (towns[view_town][5] == 0 && towns[view_town][6] == 0 && towns[view_town][7] == 0 && towns[view_town][8] == 0) free_var = -1;
        else free_var = 0;

        if (over_button(180, 537, 16, 19) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 180, 537, 180, 537, 16, 19);
         if (mouse_pressed == 0)
           {
           mouse_pressed = 1;
           train_num ++;
           if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num++;
           if (train_num > 5) train_num = 0;
           if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num++;
           if (train_num > 5) train_num = 0;
           if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num++;
           if (train_num > 5) train_num = 0;
           if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num++;
           if (train_num > 5) train_num = 0;
           if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num++;
           }
          }

        if (over_button(200, 537, 16, 19) == 1)
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 200, 537, 200, 537, 16, 19);
         if (mouse_pressed == 0)
           {
           mouse_pressed = 1;
           train_num --;
           }
          }
        if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num--;
        if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num--;
        if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num--;
        if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num--;
        if (!(towns[view_town][5] == train_num+1 || towns[view_town][6] == train_num+1 || towns[view_town][7] == train_num+1 || towns[view_town][8] == train_num+1) && !(train_num == 5)) train_num--;

        if (free_var == -1) train_num = 5;
        else if (train_num < 0) train_num = 5;
        else if (train_num > 5) train_num = 0;
        }

        // End Upkeep Button
        if (over_button(661, 552, 126, 38) == 1) blit((BITMAP *)datafile[backh].dat, buffer, 661, 552, 661, 552, 126, 38);

        // Draw Castle Image (and slide in new)
        if (slidein == 0)
         {
         if (towns[view_town][3] == 0 && towns[view_town][4] == 0) blit((BITMAP *)datafile[a1].dat, slider, 0, 0, 0, 0, 368, 315);
         if (towns[view_town][3] == 1 && towns[view_town][4] == 0) blit((BITMAP *)datafile[b1].dat, slider, 0, 0, 0, 0, 368, 315);
         if (towns[view_town][3] == 0 && towns[view_town][4] == 1) blit((BITMAP *)datafile[a2].dat, slider, 0, 0, 0, 0, 368, 315);
         if (towns[view_town][3] == 1 && towns[view_town][4] == 1) blit((BITMAP *)datafile[b2].dat, slider, 0, 0, 0, 0, 368, 315);
         if (towns[view_town][3] == 0 && towns[view_town][4] == 2) blit((BITMAP *)datafile[a3].dat, slider, 0, 0, 0, 0, 368, 315);
         if (towns[view_town][3] == 1 && towns[view_town][4] == 2) blit((BITMAP *)datafile[b3].dat, slider, 0, 0, 0, 0, 368, 315);
         if (towns[view_town][3] == 0 && towns[view_town][4] == 3) blit((BITMAP *)datafile[a4].dat, slider, 0, 0, 0, 0, 368, 315);
         if (towns[view_town][3] == 1 && towns[view_town][4] == 3) blit((BITMAP *)datafile[b4].dat, slider, 0, 0, 0, 0, 368, 315);
         }
        else
         {
         if (towns[view_town][3] == 0 && towns[view_town][4] == 0) blit((BITMAP *)datafile[a1].dat, slider, 0, 0, 0, 315 + slidein, 368, 315);
         if (towns[view_town][3] == 1 && towns[view_town][4] == 0) blit((BITMAP *)datafile[b1].dat, slider, 0, 0, 0, 315 + slidein, 368, 315);
         if (towns[view_town][3] == 0 && towns[view_town][4] == 1) blit((BITMAP *)datafile[a2].dat, slider, 0, 0, 0, 315 + slidein, 368, 315);
         if (towns[view_town][3] == 1 && towns[view_town][4] == 1) blit((BITMAP *)datafile[b2].dat, slider, 0, 0, 0, 315 + slidein, 368, 315);
         if (towns[view_town][3] == 0 && towns[view_town][4] == 2) blit((BITMAP *)datafile[a3].dat, slider, 0, 0, 0, 315 + slidein, 368, 315);
         if (towns[view_town][3] == 1 && towns[view_town][4] == 2) blit((BITMAP *)datafile[b3].dat, slider, 0, 0, 0, 315 + slidein, 368, 315);
         if (towns[view_town][3] == 0 && towns[view_town][4] == 3) blit((BITMAP *)datafile[a4].dat, slider, 0, 0, 0, 315 + slidein, 368, 315);
         if (towns[view_town][3] == 1 && towns[view_town][4] == 3) blit((BITMAP *)datafile[b4].dat, slider, 0, 0, 0, 315 + slidein, 368, 315);
         slidein = slidein - 7;
         hline(slider, 0, 322 + slidein, 368, makecol( 0, 0, 0));
         if (slidein < -315) slidein = 0;
         }
        blit( slider, buffer, 0, 0, 340, 40, 368, 315);

        // Draw building image
        if (!(towns[view_town][5] == 0 && towns[view_town][6] == 0 && towns[view_town][7] == 0 && towns[view_town][8] == 0))
         {
         blit((BITMAP *)datafile[backh].dat, buffer, 382, 43, 382, 43, 44, 44);
         blit((BITMAP *)datafile[backh].dat, buffer, 343, 82, 343, 82, 44, 44);
         blit((BITMAP *)datafile[backh].dat, buffer, 421, 82, 421, 82, 44, 44);
         blit((BITMAP *)datafile[backh].dat, buffer, 383, 121, 383, 121, 44, 44);

         if (towns[view_town][5] == 1) {draw_sprite( buffer, (BITMAP *)datafile[bldbarrack].dat, 384, 45);}
         if (towns[view_town][5] == 2) {draw_sprite( buffer, (BITMAP *)datafile[bldarchery].dat, 384, 45);}
         if (towns[view_town][5] == 3) {draw_sprite( buffer, (BITMAP *)datafile[bldstable].dat, 384, 45);}
         if (towns[view_town][5] == 4) {draw_sprite( buffer, (BITMAP *)datafile[bldsiege].dat, 384, 45);}
         if (towns[view_town][5] == 5) {draw_sprite( buffer, (BITMAP *)datafile[bldonepower].dat, 384, 45);}

         if (towns[view_town][6] == 1) {draw_sprite( buffer, (BITMAP *)datafile[bldbarrack].dat, 423, 84);}
         if (towns[view_town][6] == 2) {draw_sprite( buffer, (BITMAP *)datafile[bldarchery].dat, 423, 84);}
         if (towns[view_town][6] == 3) {draw_sprite( buffer, (BITMAP *)datafile[bldstable].dat, 423, 84);}
         if (towns[view_town][6] == 4) {draw_sprite( buffer, (BITMAP *)datafile[bldsiege].dat, 423, 84);}
         if (towns[view_town][6] == 5) {draw_sprite( buffer, (BITMAP *)datafile[bldonepower].dat, 423, 84);}

         if (towns[view_town][7] == 1) {draw_sprite( buffer, (BITMAP *)datafile[bldbarrack].dat, 385, 123);}
         if (towns[view_town][7] == 2) {draw_sprite( buffer, (BITMAP *)datafile[bldarchery].dat, 385, 123);}
         if (towns[view_town][7] == 3) {draw_sprite( buffer, (BITMAP *)datafile[bldstable].dat, 385, 123);}
         if (towns[view_town][7] == 4) {draw_sprite( buffer, (BITMAP *)datafile[bldsiege].dat, 385, 123);}
         if (towns[view_town][7] == 5) {draw_sprite( buffer, (BITMAP *)datafile[bldonepower].dat, 385, 123);}

         if (towns[view_town][8] == 1) {draw_sprite( buffer, (BITMAP *)datafile[bldbarrack].dat, 345, 84);}
         if (towns[view_town][8] == 2) {draw_sprite( buffer, (BITMAP *)datafile[bldarchery].dat, 345, 84);}
         if (towns[view_town][8] == 3) {draw_sprite( buffer, (BITMAP *)datafile[bldstable].dat, 345, 84);}
         if (towns[view_town][8] == 4) {draw_sprite( buffer, (BITMAP *)datafile[bldsiege].dat, 345, 84);}
         if (towns[view_town][8] == 5) {draw_sprite( buffer, (BITMAP *)datafile[bldonepower].dat, 345, 84);}
         }

         // Build Buildings Blit Text Bitmaps
         if (build_dir == 0) blit((BITMAP *)datafile[backh].dat, buffer, 330, 490, 330, 490, 20, 20);
         if (build_dir == 2) blit((BITMAP *)datafile[backh].dat, buffer, 348, 490, 348, 490, 20, 20);
         if (build_dir == 1) blit((BITMAP *)datafile[backh].dat, buffer, 362, 490, 362, 490, 20, 20);
         if (build_dir == 3) blit((BITMAP *)datafile[backh].dat, buffer, 383, 490, 383, 490, 20, 20);
         blit((BITMAP *)datafile[bldnames].dat, buffer, 170*build_num, 0, 144, 487, 170, 32);
         blit((BITMAP *)datafile[bldunits].dat, buffer, 150*train_num, 0, 219, 531, 150, 32);

        // Draw Level Stuff
        textprintf(buffer, sfont, 86, 94, makecol(0,0,0), "%d", play_variables[cur_player][1]);
        textprintf(buffer, sfont, 53, 117, makecol(0,0,0), "%d", play_variables[cur_player][2]);
        textprintf(buffer, sfont, 70, 138, makecol(0,0,0), "%d", play_variables[cur_player][3]);
        textprintf(buffer, sfont, 62, 160, makecol(0,0,0), "%d", play_variables[cur_player][4]);
        textprintf(buffer, sfont, 112, 181, makecol(0,0,0), "%d", play_variables[cur_player][5]);

        if (pcostunit > 0)
         {
         textout(buffer, sfont, "Each unit costs: ", 16,315, makecol(50,50,50));
         textprintf(buffer, sfont, 175, 315, makecol(50,50,50), "%d", pcostunit);
         }

        // Draw Cost
        if (pcost > 0 || pcostunit > 0)
         {
         if (pcost < play_variables[cur_player][0]+1)
          {
          textout(buffer, sfont, "This would cost: ", 16,280, makecol(0,0,0));
          textprintf(buffer, sfont, 175,280, makecol(0,0,0), "%d", pcost);
          }
         else
          {
          textout(buffer, sfont, "This would cost: ", 16,280, makecol(255,0,0));
          textprintf(buffer, sfont, 175,280, makecol(255,0,0), "%d", pcost);
          }
         pcost = 0;
         pcostunit = 0;
         }

         // Type Number
         if (clicked_unit_box == 1)
         {
         if (number_array[0] < 1 && number_array[1] < 1 && number_array[2] < 1 && number_array[3] < 1)
          {
          number_array[0] = -1;
          number_array[1] = -1;
          number_array[2] = -1;
          number_array[3] = -1;
          carrot = 0;
          }
         if (free_vark == 10 || !(free_vark == num_key())) akey_pressed = 0;
         free_vark = num_key();
         if (!(free_vark == 10) && akey_pressed == 0)
         {
          blink_cursor = 1;
          akey_pressed = 1;
          carrot = 4;
          if (number_array[3] == -1) carrot = 3;
          if (number_array[2] == -1) carrot = 2;
          if (number_array[1] == -1) carrot = 1;
          if (number_array[0] == -1) carrot = 0;

          if (carrot < 4 && free_vark > -1) number_array[carrot] = free_vark;
          if (free_vark == -1 && carrot > 0)
           {
           number_array[carrot-1] = -1;
           carrot = carrot - 2;
           }
          }

          if (carrot == 0) units_to_build = number_array[0];
          else if (carrot == 1) units_to_build = number_array[0]*10 + number_array[1];
          else if (carrot == 2) units_to_build = number_array[0]*100 + number_array[1]*10 + number_array[2];
          else if (carrot > 2) units_to_build = number_array[0]*1000 + number_array[1]*100 + number_array[2]*10 + number_array[3];
          if (number_array[0] + number_array[1] + number_array[2] + number_array[3] == -4)  units_to_build = 0;
          free_var = carrot;
          if (free_var > 3) free_var = 3;
          if (free_var < 0) free_var = 0;
          if (blink_cursor < 60) vline(buffer, 120+(10*free_var), 535, 555, makecol( 0, 0, 0));
          // Blinking Cursor
          blink_cursor ++;
          if (blink_cursor > 120) blink_cursor = 0;
          }

          // Draw Number of Units to build
          textprintf(buffer, sfont, 108, 532, makecol(0,0,0), "%d", units_to_build);

        // Slide in All
        if (slideinb > 0)
           {
           slideinb = slideinb - 20;
           hline(buffer, 0, 599, 800, makecol( 0, 0, 0));
           hline(buffer, 0, 600, 800, makecol( 0, 0, 0));
           }
        else {slideinb = 0;}

        // Draw Mouse Cursor
        draw_sprite(buffer, (BITMAP *)datafile[lcursor].dat, mouse_x, mouse_y);

        // Screen and Buffer stuff
	    blit(buffer, screen, 0,0,0,0-slideinb,800,600);//Draw the buffer to the screen
        clear_to_color( buffer,makecol( 0, 0, 0) );// make black background

        // Mouse Pressed Variables
        if (!(mouse_b & 1)) mouse_pressed = 0;
		release_screen();// Release screen
        }
	}


    // Release the game data
	destroy_bitmap(buffer);// Release Buffer bitmap
    destroy_bitmap(slider);
    unload_datafile(datafile);

	return(0);// Exit with no errors
}
END_OF_MAIN();


// Functions

int in_box(int x, int y, int xx, int yy)
{
 if (mouse_x>x && mouse_y>y && mouse_x<xx && mouse_y<yy) return 1;
 else return 0;
}


int over_button(int x, int y, int xx, int yy)
{
 if (mouse_x>x && mouse_y>y && mouse_x<x+xx && mouse_y<y+yy) return 1;
  else return 0;
}

int num_key()
{
if (key[KEY_0]) return 0;
else if (key[KEY_1]) return 1;
else if (key[KEY_2]) return 2;
else if (key[KEY_3]) return 3;
else if (key[KEY_4]) return 4;
else if (key[KEY_5]) return 5;
else if (key[KEY_6]) return 6;
else if (key[KEY_7]) return 7;
else if (key[KEY_8]) return 8;
else if (key[KEY_9]) return 9;
else if (key[KEY_BACKSPACE]) return -1;
else return 10;
}

int unit_cost(int unit)
{
if (unit == 0) return 250;
if (unit == 1) return 500;
if (unit == 2) return 500;
if (unit == 3) return 700;
if (unit == 4) return 1000;
if (unit == 5) return 250;
}