Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:23:49+00:00 2026-05-26T08:23:49+00:00

This will be hard to explain what I want, so I’ll try my best.

  • 0

This will be hard to explain what I want, so I’ll try my best.

I have a game, and the players in the game have a health counter (hp), and bodyguards as well. Each bodyguard has 100 hp, and once all a players bodyguards are gone, the damage comes off their hp.

So, these are the values I have:

$dmg // the damage
$bgs // the amount of bodyguards the player has
$bg_hp // the bodyguards health. Starts at 100, drains with $dmg fired at
$hp // the players health

When they’re shot, it needs to check if they have $bgs. This is a basic example.

if ($bgs > 0) {
  $bgs_hp = $bgs_hp - $dmg;
     if ($bg_hp <= 0) { $bg = $bg - 1; $bg_hp = 100; }
  } else {
  $hp = $hp - $dmg;
  }

Anyway, the part I need help with is this. I want the damage to overlap.
So say the $dmg is 200, that would kill 2 bodyguards (they have 100 hp each). Or someone could shoot all their bodyguards and a remainder of the $dmg comes off the $hp too.

The reason I need help is because im awful at maths, and theres problem some MOD function or something that I need to use.

Examples;

1) Player has 3 bodyguards. Someone shoots at him for 150 dmg. It would kill 1 bodyguard, and cause 50 damage to the next.

2) Player has 1 bodyguard that is half hp (50). Someone shoots him for 160, it’d kill the bodyguard (50hp), and the rest of that damage (110) would kill the player too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:23:50+00:00Added an answer on May 26, 2026 at 8:23 am

    I’d simply retain an array with the hps of each bodyguard, like this:

    $bgs = array(100); // one bg with 100 hp
    

    or

    $bgs = array(100, 300, 50); // one bg with 100 hp, then one with 300, etc
    

    If you want to know how many bodyguards there are, a simple count($bgs) tells you.

    This will make it much easier for you to expand your game in the future, and it also makes it much easier to resolve these types of calculations. You can now write:

    $hp = 100;
    $bgs = array(100, 100);
    $damage = 101; // for example
    
    // As long as there's still damage to take and bgs to take it:
    while($damage && !empty($bgs)) {
        $soak = min($bgs[0], $damage); // not more than the first bg can take
        $bgs[0] -= $soak; // remove hps from the first bg
        $damage -= $soak; // deduct from the amount of damage to tage
        if ($bgs[0] == 0) {
            // bodyguard dead, remove him from the array
            array_shift($bgs);
        }
    }
    
    // If there's any damage left over, it goes to hp
    $hp -= $damage;
    

    See it in action.

    Update: Here’s code that works with your current scheme. It’s not really simpler, and it is more restrictive on what you can do:

    $max_bg_health = 100;
    $hp = 100;
    $bgs = 2;
    $bg_hp = $max_bg_health; // so that makes 2 "full health" bgs
    $damage = 101; // for example
    if($bgs) {
        $bg_capacity = ($bgs - 1) * $max_bg_health + $bg_hp;
        $soak = min($bg_capacity, $damage);
        $bg_capacity -= $soak;
        $damage -= $soak;
    
        // update bodyguard status; this is extra tricky because $bgs is not
        // "the amount of full hp bgs" (which would make it a lot simpler)
        // but "the amount of bgs including the one which may not have full hp"
        $bgs = $bg_capacity ? intval($bg_capacity - 1 / $max_bg_health) + 1 : 0;
        $bg_hp = $bg_capacity % $max_bg_health;
    }
    
    // If there's any damage left over, it goes to hp
    $hp -= $damage;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a little hard to explain, but I will try. I want
This is going to be hard to explain but I'll try my best. I
I will try to explain as best as I can what I realy want
This problem is going to be hard for me to explain but I will
This will probably be obvious but I can't find the best way. I want
its hard for me to explain this one, but i hope some code will
This is kindy hard to explain, Am looking for an Algorithms that will take
So this is kind of hard to explain in text, I have attached an
This is going to be pretty hard to explain, so I'll try to make
It's hard to explain exactly what I want to do here, but I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.