home *** CD-ROM | disk | FTP | other *** search
/ Igromania 2000 September / Igromania_09.iso / MatrixPlus / Shogun / shogunbattle.exe / MainShogun.cpp < prev    next >
C/C++ Source or Header  |  2000-07-01  |  12KB  |  395 lines

  1. //---------------------------------------------------------------------------
  2. / Shogun Battlefield Calculator
  3. // Istvan Albert, July 2000
  4. // http://www.nd.edu/~ialbert
  5. //
  6. #include <vcl.h>
  7. #pragma hdrstop 
  8.  
  9. #include "MainShogun.h"
  10. #include "ShogunUnits.h"
  11. #include "ShogunAbout.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. #pragma link "cgauges"
  15. #pragma resource "*.dfm"
  16. TMainForm *MainForm;
  17.  
  18. // my own stuff
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. #define VERSION   "ver. 0.5 - July 2000" // July 2000
  24. #define UNITNUM   9
  25. #define PROB      5.0 // one factor point difference = 5% change in the chance to kill
  26.  
  27. // unit types
  28. struct units{
  29.     char* name;
  30.     int attack;
  31.     int defense;
  32.     int morale;
  33.     int cavalry; // true or false
  34.     int spear;   // true or false
  35. } unitList[UNITNUM]={
  36.     {"Yari Ashigaru"  ,-1,-1,-4, 0, 1},
  37.     {"Yari Samurai"   , 0, 2, 2, 0, 1},
  38.     {"No-Dachi"       , 5,-2, 8, 0, 0},
  39.     {"Warrior Monks"  , 5, 2, 8, 0, 0},
  40.     {"Naginata"       , 0, 6, 0, 0, 0},
  41.     {"Samurai Archers", 0, 0, 0, 0, 0},
  42.     {"Yari Cavalry"   , 2, 3, 2, 1, 1},
  43.     {"Cavalry Archers", 1, 2, 0, 1, 0},
  44.     {"Heavy Cavalry"  , 2, 6, 4, 1, 0}
  45. };
  46.  
  47. int unit1Num = 120;
  48. int unit2Num = 120;
  49.  
  50. // nominal stats
  51. int unit1Attack  = -1;
  52. int unit1Defense = -1;
  53. int unit2Attack  = -1;
  54. int unit2Defense = -1;
  55.  
  56. // modifiers
  57. int elevation   = 1;
  58. int location    = 0;
  59. int paramInd   = 0;
  60.  
  61. int index1 = 0, honor1 = 0, rank1 = 0, fatigue1 = 0, formation1 = 0, flanking1 = 0;
  62. int index2 = 0, honor2 = 0, rank2 = 0, fatigue2 = 0, formation2 = 0, flanking2 = 0;
  63.  
  64. char buffer[512];
  65. //---------------------------------------------------------------------------
  66. void Init()
  67. {   int i;
  68.     for(i=0; i<UNITNUM; i++){
  69.         MainForm->Unit1TypeBox->Items->AddObject(unitList[i].name, (TObject *)vsIcon);
  70.         MainForm->Unit2TypeBox->Items->AddObject(unitList[i].name, (TObject *)vsIcon);
  71.     }
  72.     MainForm->Unit1TypeBox->Text=unitList[1].name;
  73.  
  74.     MainForm->Unit1TypeBox->ItemIndex = 0;
  75.     MainForm->Unit2TypeBox->ItemIndex = 0;
  76.     MainForm->StatusBar->SimpleText = VERSION;
  77. }
  78. //---------------------------------------------------------------------------
  79. void ComputeResult()
  80. {   double chance1, chance2;
  81.     double damage1, damage2, rounds;
  82.     int whoWins, whoLoses, whichOne, stillStanding;
  83.     double newNum, newHon, diff;
  84.  
  85.     // basic
  86.  
  87.     chance1 = (unit1Attack-unit2Defense);
  88.     chance2 = (unit2Attack-unit1Defense);
  89.  
  90.     // honor
  91.  
  92.     diff = honor1 - honor2;
  93.     chance1 += diff;
  94.     chance2 -= diff;
  95.  
  96.     //rank
  97.  
  98.     diff = rank1 - rank2;
  99.     chance1 += diff;
  100.     chance2 -= diff;
  101.  
  102.     // elevation  
  103.  
  104.     diff = 1 - elevation;
  105.     chance1 += diff;
  106.     chance2 -= diff;
  107.  
  108.     // fatigue, needs more study
  109.  
  110.     chance1 +=  - fatigue1 + fatigue2;
  111.     chance2 +=  - fatigue2 + fatigue1;
  112.  
  113.     // formation, needs more study
  114.  
  115.     switch (formation1){
  116.         case  1: chance1 +=3; chance2+=3;
  117.         case  2: chance1 +=2; chance2+=2;
  118.         default: ;
  119.     }
  120.     switch (formation2){
  121.         case  1: chance1 +=3; chance2+=3;
  122.         case  2: chance1 +=2; chance2+=2;
  123.         default: ;
  124.     }
  125.  
  126.     // flanking, estimated
  127.  
  128.     chance1 +=  flanking1*4;
  129.     chance2 +=  flanking2*4;
  130.  
  131.     //spear effects
  132.  
  133.     if ((unitList[index1].cavalry) && (unitList[index2].spear)){
  134.         chance1 += -6;
  135.         chance2 += +6;
  136.     }
  137.     if ((unitList[index2].cavalry) && (unitList[index1].spear)){
  138.         chance1 += +6;
  139.         chance2 += -6;
  140.     }
  141.  
  142.     // location
  143.  
  144.     if ((unitList[index1].cavalry) && (!unitList[index2].cavalry) && (location)){
  145.         chance1 += -2;
  146.         chance2 += +2;
  147.     }
  148.     if ((unitList[index2].cavalry) && (!unitList[index1].cavalry) && (location)){
  149.         chance1 += +2;
  150.         chance2 += -2;
  151.     }
  152.  
  153.     // a crude morale adjustment
  154.  
  155.     diff = unitList[index1].morale - unitList[index2].morale;
  156.     diff/= 4.0;
  157.     chance1 += diff;
  158.     chance2 -= diff;
  159.  
  160.     // done with the stats compute percentages
  161.  
  162.     chance1 = 50.0 + chance1*PROB;
  163.     chance2 = 50.0 + chance2*PROB;
  164.  
  165.     if (chance1<0)   chance1 = 0;
  166.     if (chance1>100) chance1 = 100;
  167.  
  168.     if (chance2<0)   chance2 = 0;
  169.     if (chance2>100) chance2 = 100;
  170.  
  171.     damage1 = unit1Num * chance1;
  172.     damage2 = unit2Num * chance2;
  173.  
  174.     if (damage1>=damage2) {
  175.         whoWins  =  index1;
  176.         whoLoses =  index2;
  177.         whichOne = 1;
  178.         rounds   = unit2Num/chance1;
  179.         stillStanding = unit1Num - chance2*rounds;
  180.     }
  181.     else {
  182.         whoWins  = index2;
  183.         whoLoses = index1;
  184.         whichOne = 2;
  185.         rounds   = unit1Num/chance2;
  186.         stillStanding = unit2Num - chance1*rounds;
  187.     }
  188.  
  189.     //display winner
  190.  
  191.     sprintf(buffer,"%s (%d) ",unitList[whoWins].name, whichOne);
  192.     MainForm->WinningUnitLabel->Caption= buffer;
  193.  
  194.     sprintf(buffer,"%d  men.", stillStanding);
  195.     MainForm->WinningUnitNumber->Caption= buffer;
  196.  
  197.     // display chances and damages
  198.  
  199.     MainForm->Chance1CGauge->Progress=(int)chance1;
  200.     MainForm->Chance2CGauge->Progress=(int)chance2;
  201.  
  202.     MainForm->Damage1CGauge->Progress=(int)damage1/(damage1+damage2)*100.0;
  203.     MainForm->Damage2CGauge->Progress=(int)damage2/(damage1+damage2)*100.0;
  204.  
  205.     // dealing with the parameter box
  206.  
  207.     if (damage1>=damage2){
  208.         newNum = damage1/chance2 - unit2Num;
  209.         if(unit2Num>0) newHon = damage1/unit2Num - chance2;
  210.         newHon /= 2*PROB;
  211.     } else {
  212.         newNum =  damage2/chance1 - unit1Num;
  213.         if(unit1Num>0) newHon = damage2/unit1Num - chance1;
  214.         newHon /= 2*PROB;
  215.     };
  216.  
  217.     if (paramInd==0){
  218.         sprintf(buffer,"%d ", (int) newNum);
  219.         MainForm->NumLabel->Caption= buffer;
  220.     } else {
  221.         sprintf(buffer,"%d ", (int) newHon);
  222.         MainForm->NumLabel->Caption= buffer;
  223.     }
  224.  
  225.     whichOne = 3 - whichOne;
  226.     sprintf(buffer,"%s (%d) need", unitList[whoLoses].name, whichOne);
  227.     MainForm->LoserLabel->Caption= buffer;
  228. }
  229. //---------------------------------------------------------------------------
  230. void MainLoop(){
  231.  
  232.     unit1Attack  = unitList[index1].attack;
  233.     unit1Defense = unitList[index1].defense;
  234.  
  235.     unit2Attack  = unitList[index2].attack;
  236.     unit2Defense = unitList[index2].defense;
  237.  
  238.     sprintf(buffer,"Stats: %d/ %d/ %d", unitList[index1].attack, unitList[index1].defense, unitList[index1].morale);
  239.     MainForm->Unit1StatsLabel->Caption= buffer;
  240.  
  241.     sprintf(buffer,"Stats: %d/ %d/ %d", unitList[index2].attack, unitList[index2].defense, unitList[index2].morale);
  242.     MainForm->Unit2StatsLabel->Caption= buffer;
  243.  
  244.     ComputeResult();
  245. }
  246. //---------------------------------------------------------------------------
  247. __fastcall TMainForm::TMainForm(TComponent* Owner)
  248.     : TForm(Owner)
  249. {
  250. }
  251. //---------------------------------------------------------------------------
  252. void __fastcall TMainForm::FormCreate(TObject *Sender)
  253. {
  254.     Init();
  255.     MainLoop();
  256.     MainForm->StatusBar->SimpleText=VERSION;
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall TMainForm::Unit1NumBoxChange(TObject *Sender)
  260. {
  261.    unit1Num  = atoi(MainForm->Unit1NumBox->Text.c_str());
  262.    MainForm->Unit1NumBox->Text = unit1Num;
  263.    MainLoop();
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall TMainForm::Unit2NumBoxChange(TObject *Sender)
  267. {
  268.    unit2Num  = atoi(MainForm->Unit2NumBox->Text.c_str());
  269.    MainForm->Unit2NumBox->Text = unit2Num;
  270.    MainLoop();
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall TMainForm::Unit1TypeBoxChange(TObject *Sender)
  274. {
  275.     index1 = MainForm->Unit1TypeBox->ItemIndex;
  276.     MainLoop();
  277. }
  278. //---------------------------------------------------------------------------
  279. void __fastcall TMainForm::Unit2TypeBoxChange(TObject *Sender)
  280. {
  281.     index2 = MainForm->Unit2TypeBox->ItemIndex;
  282.     MainLoop();
  283. }
  284. //---------------------------------------------------------------------------
  285. void __fastcall TMainForm::Unit1HonorBoxChange(TObject *Sender)
  286. {
  287.     honor1  = MainForm->Unit1HonorBox->ItemIndex;
  288.     MainLoop();
  289. }
  290. //---------------------------------------------------------------------------
  291. void __fastcall TMainForm::Unit2HonorBoxChange(TObject *Sender)
  292. {
  293.     honor2  = MainForm->Unit2HonorBox->ItemIndex;
  294.     MainLoop();
  295. }
  296. //---------------------------------------------------------------------------
  297. void __fastcall TMainForm::Unit1RankBoxChange(TObject *Sender)
  298. {
  299.     rank1  = MainForm->Unit1RankBox->ItemIndex;
  300.     MainLoop();
  301. }
  302. //---------------------------------------------------------------------------
  303. void __fastcall TMainForm::Unit2RankBoxChange(TObject *Sender)
  304. {
  305.     rank2  = MainForm->Unit2RankBox->ItemIndex;
  306.     MainLoop();
  307. }
  308. //---------------------------------------------------------------------------
  309. void __fastcall TMainForm::Unit1FatigueBoxChange(TObject *Sender)
  310. {
  311.     fatigue1  = MainForm->Unit1FatigueBox->ItemIndex;
  312.     MainLoop();
  313. }
  314. //---------------------------------------------------------------------------
  315. void __fastcall TMainForm::Unit2FatigueBoxChange(TObject *Sender)
  316. {
  317.    fatigue2  = MainForm->Unit2FatigueBox->ItemIndex;
  318.    MainLoop();
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TMainForm::Unit1FormationBoxChange(TObject *Sender)
  322. {
  323.    formation1  = MainForm->Unit1FormationBox->ItemIndex;
  324.    MainLoop();
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TMainForm::Unit2FormationBoxChange(TObject *Sender)
  328. {
  329.    formation2  = MainForm->Unit2FormationBox->ItemIndex;
  330.    MainLoop();
  331. }
  332. //---------------------------------------------------------------------------
  333. void __fastcall TMainForm::ElevationRadioGroupClick(TObject *Sender)
  334. {
  335.    elevation  = MainForm->ElevationRadioGroup->ItemIndex;
  336.    MainLoop();
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TMainForm::Unit1AttackRadioGroupClick(TObject *Sender)
  340. {
  341.    flanking1  = MainForm->Unit1AttackRadioGroup->ItemIndex;
  342.    if(flanking1 != 0){
  343.         flanking2 = 0;
  344.         MainForm->Unit2AttackRadioGroup->ItemIndex = 0;
  345.    }
  346.    MainLoop();
  347. }
  348. //---------------------------------------------------------------------------
  349. void __fastcall TMainForm::Unit2AttackRadioGroupClick(TObject *Sender)
  350. {
  351.    flanking2  = MainForm->Unit2AttackRadioGroup->ItemIndex;
  352.    if(flanking2 != 0){
  353.         flanking1 = 0 ;
  354.         MainForm->Unit1AttackRadioGroup->ItemIndex = 0;
  355.    }
  356.    MainLoop();
  357. }
  358. //---------------------------------------------------------------------------
  359. void __fastcall TMainForm::LocationRadioGroupClick(TObject *Sender)
  360. {
  361.     location  = MainForm->LocationRadioGroup->ItemIndex;
  362.     MainLoop();
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall TMainForm::ParameterBoxChange(TObject *Sender)
  366. {
  367.     paramInd = MainForm->ParameterBox->ItemIndex;
  368.     MainLoop();
  369. }
  370. //---------------------------------------------------------------------------
  371. void __fastcall TMainForm::Button1Click(TObject *Sender)
  372. {
  373.     UnitsForm->Show();
  374.     UnitsForm->RichEdit->Lines->LoadFromFile("Units.rtf");
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TMainForm::Button2Click(TObject *Sender)
  378. {
  379.     UnitsForm->Show();
  380.     UnitsForm->RichEdit->Lines->LoadFromFile("Buildings.rtf");
  381. }
  382. //---------------------------------------------------------------------------
  383. void __fastcall TMainForm::Button3Click(TObject *Sender)
  384. {
  385.     UnitsForm->Show();
  386.     UnitsForm->RichEdit->Lines->LoadFromFile("Tips.rtf");
  387. }
  388. //---------------------------------------------------------------------------
  389. void __fastcall TMainForm::Button4Click(TObject *Sender)
  390. {
  391.     AboutForm->VersionLabel->Caption=VERSION;
  392.     AboutForm->ShowModal();
  393. }
  394. //---------------------------------------------------------------------------
  395.