Skip to content

Commit

Permalink
feat(mechanics): Pit boarding attack against boarding defense (endles…
Browse files Browse the repository at this point in the history
…s-sky#10060)

Co-authored-by: Hurleveur <[email protected]>
  • Loading branch information
petervdmeer and Hurleveur authored Sep 23, 2024
1 parent 9ae3d64 commit 88f4e46
Showing 1 changed file with 56 additions and 20 deletions.
76 changes: 56 additions & 20 deletions source/BoardingPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,8 @@ bool BoardingPanel::KeyDown(SDL_Keycode key, Uint16 mod, const Command &command,
}
else
{
if(youAttack)
messages.push_back("You attack. ");
else if(enemyAttacks)
messages.push_back("You defend. ");
unsigned int yourCasualties = 0;
unsigned int enemyCasualties = 0;

// To speed things up, have multiple rounds of combat each time you
// click the button, if you started with a lot of crew.
Expand All @@ -333,26 +331,64 @@ bool BoardingPanel::KeyDown(SDL_Keycode key, Uint16 mod, const Command &command,
if(!yourCrew || !enemyCrew)
break;

// Your chance of winning this round is equal to the ratio of
// your power to the enemy's power.
double yourPower = (youAttack ?
attackOdds.AttackerPower(yourCrew) : defenseOdds.DefenderPower(yourCrew));
double enemyPower = (enemyAttacks ?
defenseOdds.AttackerPower(enemyCrew) : attackOdds.DefenderPower(enemyCrew));
if(youAttack)
{
// Your chance of winning this round is equal to the ratio of
// your power to the enemy's power.
double yourAttackPower = attackOdds.AttackerPower(yourCrew);
double total = yourAttackPower + attackOdds.DefenderPower(enemyCrew);

double total = yourPower + enemyPower;
if(!total)
break;
if(total)
{
if(Random::Real() * total >= yourAttackPower)
{
++yourCasualties;
you->AddCrew(-1);
if(you->Crew() <= 1)
break;
}
else
{
++enemyCasualties;
victim->AddCrew(-1);
if(!victim->Crew())
break;
}
}
}
if(enemyAttacks)
{
double yourDefensePower = defenseOdds.DefenderPower(yourCrew);
double total = defenseOdds.AttackerPower(enemyCrew) + yourDefensePower;

if(Random::Real() * total >= yourPower)
you->AddCrew(-1);
else
victim->AddCrew(-1);
if(total)
{
if(Random::Real() * total >= yourDefensePower)
{
++yourCasualties;
you->AddCrew(-1);
if(!you->Crew())
break;
}
else
{
++enemyCasualties;
victim->AddCrew(-1);
if(!victim->Crew())
break;
}
}
}
}

// Report how many casualties each side suffered.
int yourCasualties = yourStartCrew - you->Crew();
int enemyCasualties = enemyStartCrew - victim->Crew();
// Report what happened and how many casualties each side suffered.
if(youAttack && enemyAttacks)
messages.push_back("You both attack. ");
else if(youAttack)
messages.push_back("You attack. ");
else if(enemyAttacks)
messages.push_back("They attack. ");

if(yourCasualties && enemyCasualties)
messages.back() += "You lose " + to_string(yourCasualties)
+ " crew; they lose " + to_string(enemyCasualties) + ".";
Expand Down

0 comments on commit 88f4e46

Please sign in to comment.