Discussion in   Coding Corner   started     10 years ago   September 24, 2013, 09:20:08 AM   by   Keldon

Health based damage overrides

Keldon
Offline
223 Posts
Topic :   Health based damage overrides
10 years ago  September 24, 2013, 09:20:08 AM

I was looking at using a mobs % health left to allow the mob to do different things.  I am using an int variable, the OnDamage routine, and the AlterMeleeDamage routine.


The problem is no matter the health % is nothing changes.  It seems like the OnDamage routine is either not changing the number for my healthbasedamage variable.  I have even let the mob beat me down to see if it was checking my health instead of the mobs health and no luck there either.


Is this something that is possible or is this bound by the limitations of the server?


Code: [Select]

        int healthbasedamage;


        public override void OnDamage(int amount, Mobile from, bool willKill)
        {
            int healthpercent = from.Hits / from.HitsMax;


            if (healthpercent > .8)
            {
                healthbasedamage = 0;
                return;
            }
            if (healthpercent > .6 && healthpercent <= .8)
            {
                healthbasedamage = 1;
                return;
            }
            if (healthpercent > .4 && healthpercent <= .6)
            {
                healthbasedamage = 2;
                return;
            }
            if (healthpercent > .2 && healthpercent <= .4)
            {
                healthbasedamage = 3;
                return;
            }
            if (healthpercent > 0 && healthpercent <= .2)
            {
                healthbasedamage = 4;
                return;
            }
        }


        public override void AlterMeleeDamageTo(Mobile to, ref int damage)
        {
            if (healthbasedamage < 0 || healthbasedamage > 4)
                return;


            if (healthbasedamage == 0)
            {
                damage *= 5;
                return;
            }
            if (healthbasedamage == 1)
            {
                if (.3 > Utility.RandomDouble())
                {
                    Mobile spawn;
                    switch (Utility.Random(3))
                    {
                        default:
                        case 0: spawn = new Balron(); break;
                        case 1: spawn = new Succubus(); break;
                        case 2: spawn = new Daemon(); break;
                    }
                    spawn.MoveToWorld(this.Location, this.Map);
                }
                return;
            }
            if (healthbasedamage == 2)
            {
                if (to is BaseCreature)
                    damage *= 100;
                return;
            }
            if (healthbasedamage == 3)
            {
                if (.9 > Utility.RandomDouble())
                {
                    Mobile spawn;
                    switch (Utility.Random(3))
                    {
                        default:
                        case 0: spawn = new Balron(); break;
                        case 1: spawn = new Succubus(); break;
                        case 2: spawn = new Daemon(); break;
                    }
                    spawn.MoveToWorld(this.Location, this.Map);
                }
                return;
            }
            if (healthbasedamage == 4)
            {
                damage *= 5;
                return;
            }
        }

Kyn
Offline
336 Posts
#1 Re :   Health based damage overrides
10 years ago  September 24, 2013, 11:54:30 AM

Health percent cannot be 0% or the creature is dead. So you are stating a null value. It is possible though and a good idea.

Keldon
Offline
223 Posts
#2 Re :   Health based damage overrides
10 years ago  September 24, 2013, 12:05:11 PM

just realized my copy/paste missed a few characters.


I am going to work with this some more... as soon as I figure out where I am throwing the null value at.

Keldon
Offline
223 Posts
#3 Re :   Health based damage overrides
10 years ago  September 24, 2013, 12:22:27 PM

I have changed healthpercent from int to double.  I am sure that is part of my problem.

Keldon
Offline
223 Posts
#4 Re :   Health based damage overrides
10 years ago  September 24, 2013, 01:00:11 PM

Ok, I am having a moment of complete retardation.  I made a couple changes and converted this system to an if/else fall through.  This basically proved that my calculation is producing the final case (changed the damage mod to prove this, using 5 for both start and end was a bad choice to start with).


Basically it looks like this is producing a 0

Code: [Select]
double healthpercent = this.Hits / this.HitsMax;


Say a mob is at 8,400 Hits out of 10,000 HitsMax shouldn't this give healthpercent a value of 0.84? 


Basically filling these numbers into the forumula:
0.84 = 8400/10000


I think at this juncture I need to step away from the PC for a little bit and clear my mind.


what I currently have coded for the OnDamage:
Code: [Select]

public override void OnDamage(int amount, Mobile from, bool willKill)
        {
            double healthpercent = this.Hits / this.HitsMax;


            if (healthpercent > 0.8)
            {
                healthbasedamage = 0;
                return;
            }
            else
                if (healthpercent > 0.6 && healthpercent <= 0.8)
                {
                    healthbasedamage = 1;
                    return;
                }
                else
                    if (healthpercent > 0.4 && healthpercent <= 0.6)
                    {
                        healthbasedamage = 2;
                        return;
                    }
                    else
                        if (healthpercent > 0.2 && healthpercent <= 0.4)
                        {
                            healthbasedamage = 3;
                            return;
                        }
                        else
                            if (healthpercent <= 0.2)
                            {
                                healthbasedamage = 4;
                                return;
                            }
        }



Kyn
Offline
336 Posts
#5 Re :   Health based damage overrides
10 years ago  September 24, 2013, 02:06:03 PM

Just a thought, you may want to look at: BaseCreature.TransformMoveDelay


*** I believe it may provide some reference
 ;D

Keldon
Offline
223 Posts
#6 Re :   Health based damage overrides
10 years ago  September 24, 2013, 06:01:52 PM

I got a method to work.  Probably not the prettiest but it does work.

as a note my mob has a hard set 10k hp, reasoning for that being the divisor.

Code: [Select]

        int healthbasedamage;


        public override void OnGaveMeleeAttack(Mobile defender)
        {
            base.OnGaveMeleeAttack(defender);


            double healthpercent = (double)this.Hits / 10000;


            if (healthpercent > 0.8)
                healthbasedamage = 0;
            if (healthpercent > 0.6 && healthpercent <= 0.8)
                healthbasedamage = 1;
            if (healthpercent > 0.4 && healthpercent <= 0.6)
                healthbasedamage = 2;
            if (healthpercent > 0.2 && healthpercent <= 0.4)
                healthbasedamage = 3;
            if (healthpercent <= 0.2)
                healthbasedamage = 4;
        }


        public override void AlterMeleeDamageTo(Mobile to, ref int damage)
        {


            if (healthbasedamage == 0)
            {
                damage *= 5;
                return;
            }
            if (healthbasedamage == 1)
            {
                if (0.3 > Utility.RandomDouble())
                {
                    Mobile spawn;
                    switch (Utility.Random(3))
                    {
                        default:
                        case 0: spawn = new Balron(); break;
                        case 1: spawn = new Succubus(); break;
                        case 2: spawn = new Daemon(); break;
                    }
                    spawn.MoveToWorld(this.Location, this.Map);
                }
                return;
            }
            if (healthbasedamage == 2)
            {
                if (to is BaseCreature)
                    damage *= 100;
                return;
            }
            if (healthbasedamage == 3)
            {
                if (0.9 > Utility.RandomDouble())
                {
                    Mobile spawn;
                    switch (Utility.Random(3))
                    {
                        default:
                        case 0: spawn = new Balron(); break;
                        case 1: spawn = new Succubus(); break;
                        case 2: spawn = new Daemon(); break;
                    }
                    spawn.MoveToWorld(this.Location, this.Map);
                }
                return;
            }
            if (healthbasedamage == 4)
            {
                damage *= 2;
                return;
            }
        }

Kyn
Offline
336 Posts
#7 Re :   Health based damage overrides
10 years ago  September 24, 2013, 06:52:43 PM

Looks a little hot wired. :)

Keldon
Offline
223 Posts
#8 Re :   Health based damage overrides
10 years ago  September 24, 2013, 07:05:31 PM

Looks a little hot wired. :)


hehe, ya basically.  Hopefully next week when I have more time I can refine it a little bit more.  I am just happy that after finding about 4 dozen ways it wont work I finally found one that would work.