Jump to content

Noob in need.


Monk3h

Recommended Posts

I am currently working on a text based rpg and I need some help with the battle system.

 

Each unit has dmg points and health points. Each player can have many different units. The part im stuck with is how would I create a script that does damage across a range of units.

 

Eg, say I send 10 Ninjas to attack 10 rifleman, the ninjas would attack first and do there dmg, how would I create a script to calculate how many rifleman have been killed?

 

Any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/131110-noob-in-need/
Share on other sites

Usually it works in a simplified manner. All units in a stack have their HPs added to get 'stack HP'.

 

10 soldiers * 10HP = 100HP

 

Then all damage dealt to a stack is substracted from this sum.

 

10 ninjas deal 45 damage to a stack

100HP - 45HP = 55HP

 

55HP is enough for five full HP soldiers and one wounded. 4 soldiers have been killed.

 

As I said, this is the simplest way, and not overly realistic.

 

Link to comment
https://forums.phpfreaks.com/topic/131110-noob-in-need/#findComment-680715
Share on other sites

if you use levels for the units (example: peasants = lvl 0, footmen = lvl1, ninjas = lvl5...):

expansion of Mchl's suggestion - calculate total dmg dealt by attacking army (ninja dmg + ultimate avengers dmg + dinosaur dmg ;D). the defending army's HP will be contained in an array ordered by lvl. so using my exmaple, your array would be:

$defense[0] = 100; //50 peasants * 2HP

$defense[1] = 75; //25 footmen * 5HP

$defense[5] = 200//20 ninjas * 10HP

 

decide what percentage of the dmg you want each lvl to receive. i will decide that i want:

-lvl 0 take minimum 40% dmg + 6 points for leftover dmg (i will explain this later).

-lvl 1 take minimum 25% dmg + 5 points for leftover dmg

-lvl 2 take minimum 15% dmg + 4 points for leftover dmg

-lvl 3 take minimum 10% dmg + 3 points for leftover dmg

-lvl 4 take minimum 7% dmg + 2 points for leftover dmg

-lvl 5 take minimum 3% dmg + 1 points for leftover dmg

 

the attackers do 350 dmg (ultimate avengers felt gay in their spandex suits, and ran home to mommy  ;D otherwise it would be 5000 dmg).

 

since we only have lvl's 0,1,5, we only adress 68% of total dmg. here's what we do with the extra 32% = 112 dmg:

we take a weighted average of the dmg, by using the points:

there are a total of 12 points - 6 for lvl 0, 5 for lvl 1, 1 for lvl 5.

so lvl 0 get 6/12 = 50% of leftover dmg = 56 dmg.

lvl 1 get 5/12 =~ 42% of leftover dmg = (use round function) 47 dmg.

lvl 5 get 1/12 =~ 8% of leftover dmg = (use round function) 9 dmg.

 

so the attack works like this:

 

-peasants take 350*40/100 + 56 = 196 dmg. but, whoops, they only have 100HP. so the extra dmg (96 dmg) overflows to the next lvl up.

-footmen take 350/4 + 47 + 96 = (use round function) 231 dmg. but they only have 75HP. so 156 dmg overflows to next lvl.

-lvl 2 through 4 are empty, the the 156 keep overflowing upwards.

-ninjas take 350*3/100 + 9 + 156 = (use round function) 176 dmg. they have 200HP, so defense is left with 2.5 ninjas.

 

i know this is kinda clumsy and rough around the edges (and somewhat in the middle), but i think it's not bad for something i made up as i went along :D

 

hope this helps. you have my full permission to steal this idea. in about 1.5-2 years, when business is booming, i will sue you for copyright infringement and make a hefty sum ;D

Link to comment
https://forums.phpfreaks.com/topic/131110-noob-in-need/#findComment-680741
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.