home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Igromania 2000 September
/
Igromania_09.iso
/
MatrixPlus
/
Shogun
/
shogunbattle.exe
/
MainShogun.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-07-01
|
12KB
|
395 lines
//---------------------------------------------------------------------------
/ Shogun Battlefield Calculator
// Istvan Albert, July 2000
// http://www.nd.edu/~ialbert
//
#include <vcl.h>
#pragma hdrstop
#include "MainShogun.h"
#include "ShogunUnits.h"
#include "ShogunAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cgauges"
#pragma resource "*.dfm"
TMainForm *MainForm;
// my own stuff
#include <stdio.h>
#include <stdlib.h>
#define VERSION "ver. 0.5 - July 2000" // July 2000
#define UNITNUM 9
#define PROB 5.0 // one factor point difference = 5% change in the chance to kill
// unit types
struct units{
char* name;
int attack;
int defense;
int morale;
int cavalry; // true or false
int spear; // true or false
} unitList[UNITNUM]={
{"Yari Ashigaru" ,-1,-1,-4, 0, 1},
{"Yari Samurai" , 0, 2, 2, 0, 1},
{"No-Dachi" , 5,-2, 8, 0, 0},
{"Warrior Monks" , 5, 2, 8, 0, 0},
{"Naginata" , 0, 6, 0, 0, 0},
{"Samurai Archers", 0, 0, 0, 0, 0},
{"Yari Cavalry" , 2, 3, 2, 1, 1},
{"Cavalry Archers", 1, 2, 0, 1, 0},
{"Heavy Cavalry" , 2, 6, 4, 1, 0}
};
int unit1Num = 120;
int unit2Num = 120;
// nominal stats
int unit1Attack = -1;
int unit1Defense = -1;
int unit2Attack = -1;
int unit2Defense = -1;
// modifiers
int elevation = 1;
int location = 0;
int paramInd = 0;
int index1 = 0, honor1 = 0, rank1 = 0, fatigue1 = 0, formation1 = 0, flanking1 = 0;
int index2 = 0, honor2 = 0, rank2 = 0, fatigue2 = 0, formation2 = 0, flanking2 = 0;
char buffer[512];
//---------------------------------------------------------------------------
void Init()
{ int i;
for(i=0; i<UNITNUM; i++){
MainForm->Unit1TypeBox->Items->AddObject(unitList[i].name, (TObject *)vsIcon);
MainForm->Unit2TypeBox->Items->AddObject(unitList[i].name, (TObject *)vsIcon);
}
MainForm->Unit1TypeBox->Text=unitList[1].name;
MainForm->Unit1TypeBox->ItemIndex = 0;
MainForm->Unit2TypeBox->ItemIndex = 0;
MainForm->StatusBar->SimpleText = VERSION;
}
//---------------------------------------------------------------------------
void ComputeResult()
{ double chance1, chance2;
double damage1, damage2, rounds;
int whoWins, whoLoses, whichOne, stillStanding;
double newNum, newHon, diff;
// basic
chance1 = (unit1Attack-unit2Defense);
chance2 = (unit2Attack-unit1Defense);
// honor
diff = honor1 - honor2;
chance1 += diff;
chance2 -= diff;
//rank
diff = rank1 - rank2;
chance1 += diff;
chance2 -= diff;
// elevation
diff = 1 - elevation;
chance1 += diff;
chance2 -= diff;
// fatigue, needs more study
chance1 += - fatigue1 + fatigue2;
chance2 += - fatigue2 + fatigue1;
// formation, needs more study
switch (formation1){
case 1: chance1 +=3; chance2+=3;
case 2: chance1 +=2; chance2+=2;
default: ;
}
switch (formation2){
case 1: chance1 +=3; chance2+=3;
case 2: chance1 +=2; chance2+=2;
default: ;
}
// flanking, estimated
chance1 += flanking1*4;
chance2 += flanking2*4;
//spear effects
if ((unitList[index1].cavalry) && (unitList[index2].spear)){
chance1 += -6;
chance2 += +6;
}
if ((unitList[index2].cavalry) && (unitList[index1].spear)){
chance1 += +6;
chance2 += -6;
}
// location
if ((unitList[index1].cavalry) && (!unitList[index2].cavalry) && (location)){
chance1 += -2;
chance2 += +2;
}
if ((unitList[index2].cavalry) && (!unitList[index1].cavalry) && (location)){
chance1 += +2;
chance2 += -2;
}
// a crude morale adjustment
diff = unitList[index1].morale - unitList[index2].morale;
diff/= 4.0;
chance1 += diff;
chance2 -= diff;
// done with the stats compute percentages
chance1 = 50.0 + chance1*PROB;
chance2 = 50.0 + chance2*PROB;
if (chance1<0) chance1 = 0;
if (chance1>100) chance1 = 100;
if (chance2<0) chance2 = 0;
if (chance2>100) chance2 = 100;
damage1 = unit1Num * chance1;
damage2 = unit2Num * chance2;
if (damage1>=damage2) {
whoWins = index1;
whoLoses = index2;
whichOne = 1;
rounds = unit2Num/chance1;
stillStanding = unit1Num - chance2*rounds;
}
else {
whoWins = index2;
whoLoses = index1;
whichOne = 2;
rounds = unit1Num/chance2;
stillStanding = unit2Num - chance1*rounds;
}
//display winner
sprintf(buffer,"%s (%d) ",unitList[whoWins].name, whichOne);
MainForm->WinningUnitLabel->Caption= buffer;
sprintf(buffer,"%d men.", stillStanding);
MainForm->WinningUnitNumber->Caption= buffer;
// display chances and damages
MainForm->Chance1CGauge->Progress=(int)chance1;
MainForm->Chance2CGauge->Progress=(int)chance2;
MainForm->Damage1CGauge->Progress=(int)damage1/(damage1+damage2)*100.0;
MainForm->Damage2CGauge->Progress=(int)damage2/(damage1+damage2)*100.0;
// dealing with the parameter box
if (damage1>=damage2){
newNum = damage1/chance2 - unit2Num;
if(unit2Num>0) newHon = damage1/unit2Num - chance2;
newHon /= 2*PROB;
} else {
newNum = damage2/chance1 - unit1Num;
if(unit1Num>0) newHon = damage2/unit1Num - chance1;
newHon /= 2*PROB;
};
if (paramInd==0){
sprintf(buffer,"%d ", (int) newNum);
MainForm->NumLabel->Caption= buffer;
} else {
sprintf(buffer,"%d ", (int) newHon);
MainForm->NumLabel->Caption= buffer;
}
whichOne = 3 - whichOne;
sprintf(buffer,"%s (%d) need", unitList[whoLoses].name, whichOne);
MainForm->LoserLabel->Caption= buffer;
}
//---------------------------------------------------------------------------
void MainLoop(){
unit1Attack = unitList[index1].attack;
unit1Defense = unitList[index1].defense;
unit2Attack = unitList[index2].attack;
unit2Defense = unitList[index2].defense;
sprintf(buffer,"Stats: %d/ %d/ %d", unitList[index1].attack, unitList[index1].defense, unitList[index1].morale);
MainForm->Unit1StatsLabel->Caption= buffer;
sprintf(buffer,"Stats: %d/ %d/ %d", unitList[index2].attack, unitList[index2].defense, unitList[index2].morale);
MainForm->Unit2StatsLabel->Caption= buffer;
ComputeResult();
}
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
Init();
MainLoop();
MainForm->StatusBar->SimpleText=VERSION;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit1NumBoxChange(TObject *Sender)
{
unit1Num = atoi(MainForm->Unit1NumBox->Text.c_str());
MainForm->Unit1NumBox->Text = unit1Num;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit2NumBoxChange(TObject *Sender)
{
unit2Num = atoi(MainForm->Unit2NumBox->Text.c_str());
MainForm->Unit2NumBox->Text = unit2Num;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit1TypeBoxChange(TObject *Sender)
{
index1 = MainForm->Unit1TypeBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit2TypeBoxChange(TObject *Sender)
{
index2 = MainForm->Unit2TypeBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit1HonorBoxChange(TObject *Sender)
{
honor1 = MainForm->Unit1HonorBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit2HonorBoxChange(TObject *Sender)
{
honor2 = MainForm->Unit2HonorBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit1RankBoxChange(TObject *Sender)
{
rank1 = MainForm->Unit1RankBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit2RankBoxChange(TObject *Sender)
{
rank2 = MainForm->Unit2RankBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit1FatigueBoxChange(TObject *Sender)
{
fatigue1 = MainForm->Unit1FatigueBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit2FatigueBoxChange(TObject *Sender)
{
fatigue2 = MainForm->Unit2FatigueBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit1FormationBoxChange(TObject *Sender)
{
formation1 = MainForm->Unit1FormationBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit2FormationBoxChange(TObject *Sender)
{
formation2 = MainForm->Unit2FormationBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ElevationRadioGroupClick(TObject *Sender)
{
elevation = MainForm->ElevationRadioGroup->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit1AttackRadioGroupClick(TObject *Sender)
{
flanking1 = MainForm->Unit1AttackRadioGroup->ItemIndex;
if(flanking1 != 0){
flanking2 = 0;
MainForm->Unit2AttackRadioGroup->ItemIndex = 0;
}
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Unit2AttackRadioGroupClick(TObject *Sender)
{
flanking2 = MainForm->Unit2AttackRadioGroup->ItemIndex;
if(flanking2 != 0){
flanking1 = 0 ;
MainForm->Unit1AttackRadioGroup->ItemIndex = 0;
}
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::LocationRadioGroupClick(TObject *Sender)
{
location = MainForm->LocationRadioGroup->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ParameterBoxChange(TObject *Sender)
{
paramInd = MainForm->ParameterBox->ItemIndex;
MainLoop();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button1Click(TObject *Sender)
{
UnitsForm->Show();
UnitsForm->RichEdit->Lines->LoadFromFile("Units.rtf");
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button2Click(TObject *Sender)
{
UnitsForm->Show();
UnitsForm->RichEdit->Lines->LoadFromFile("Buildings.rtf");
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button3Click(TObject *Sender)
{
UnitsForm->Show();
UnitsForm->RichEdit->Lines->LoadFromFile("Tips.rtf");
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button4Click(TObject *Sender)
{
AboutForm->VersionLabel->Caption=VERSION;
AboutForm->ShowModal();
}
//---------------------------------------------------------------------------