Text based combat system trial (Java)

Hey JGO ^_^, haven’t been on for a while :smiley: (Few months i believe)
Nice to be back ::slight_smile:

I (tonight) decided to try to make a fair combat system which will include:

  • Weapons (Accuracy Modifiers)
  • Level System (Attack, Strength, Defence, Health)
  • Simple Combat System (Melee)

I’ll be attempting to add the following to the combat system (this is where I need a little help):

  • [color=red]A chance to hit formula
  • A chance to block formula
  • A damage formula

To be honest I haven’t gotten too far yet lol.

I’ve slowly been developing on it for the past half our or so, but I figured I’d ask for help before I begin furiously ‘tweaking’ with the percentages to in some UN-profound way :stuck_out_tongue:

The reason why I’m posting this is I’d like to get some information / tips on how to go about calculating the accuracy percentage, the blocking chance and damage dealt etc.

Side information:
Accuracy (Tier/Level 1 weapons) = 1.0
All levels will be level 1 @ first (ATK/STR/DEF) and the health will be 10, all having 99 as maximum.

Any feedback would be great guys thanks :slight_smile:

A combat system I’ve been interested in implementing, but haven’t had the time, goes kind-of like this:

Weapons have an attack stat
Creatures have a base defence stat.
Armour etc. adds to the defence.
Whenever a creature is attacked, you use random.nextInt(defence-attack) (obviously if attack >= defence you don’t do that check)
If the result is 0, or attack >= defence, target creature is killed/unconscious/whatever.

Just an idea.

I hope I don’t derail you thread with posting random ideas about attack systems, but … :slight_smile:

While playing Baldursgate a while back I really liked what effect it has that your weapons uses 1 or more dice calculate the attack damage.

The idea is this that you have a weapon which can give betwen x-y damage (i.e. 10-20), but the random distribution can be different.

one weapon can have damage between 20-25 and each valule has the same chance to be picked
and the other weapon has 15 - 30, but a higher chance that a lower value is picked

so in essence I like it when the player has a real choice between different weapons, and there isn’t only one correct to choose because it is most effective

Probability distributions are interesting (I promise!). A reasonable accessible overview of some basics is available here: http://www.redblobgames.com/articles/probability/damage-rolls.html

Thanks alot roquen seems like what I need, only skimmed over it as it is 2:40AM :stuck_out_tongue:
I’ll check it out tomorrow.

In the meantime here’s my attempt so far:
Only thing working so far is the accuracy / chance % to hit.


	public CombatTest() {
		int i = 0;
		double[] accuracyMarks = new double[100];

		while (i < 100) {
			/* Player */
			double combatLevel = 0;
			double attack = 99;
			double defence = 1;
			double strength = 1;
			double health = 10;
			double accuracy = 1;
			double weaponAccuracy = 1.0;
			
			/* Enemy */
			double enemyDefence = 77;
			double enemyHealth = 10;
			
			/* Formulas */
			combatLevel = getCombatLevel(new double[] { attack, defence, strength, health });
			attack = attack * (100 / enemyDefence);
			accuracy += attack * Math.random() ;
	
			if (accuracy > 100) { accuracy = 100; }
			accuracyMarks[i] = Math.floor(accuracy);
			
			int chance = (int) accuracyMarks[i++];

			System.out.println("Accuracy = "+chance+".");
		}
		debug(accuracyMarks);
	}

Snippet from the output:
(99 Attack Vs. 77 Defence)

[quote]Accuracy = 100.
Accuracy = 4.
Accuracy = 93.
Accuracy = 39.
Accuracy = 47.
Accuracy = 47.
Accuracy = 100.
Accuracy = 92.
Accuracy = 98.
Accuracy = 97.
Accuracy = 72.
Accuracy = 100.
Accuracy = 100.
Accuracy = 100.
Accuracy = 100.
Accuracy = 12.
Accuracy = 1.
Accuracy = 16.
Accuracy = 47.
Accuracy = 44.
Accuracy = 26.
Accuracy = 100.
Accuracy = 17.
Accuracy = 48.
Accuracy = 100.
Accuracy = 16.
Accuracy = 31.
Accuracy = 95.
Accuracy = 100.
Accuracy = 100.
Accuracy = 4.
Accuracy = 14.
Accuracy = 68.
Accuracy = 75.
Maximum accuracy: 100.0.
Minimum Accuracy: 1.0.
Most Common Accuracy: 100.0.
[/quote]
And this will be goodnight, looking forward to criticism on the above code :slight_smile:
I know it’s coming xD

You don’t have to recreate the variables, I’d suggest that you do this;

public CombatTest() {
      int i = 0;
      double[] accuracyMarks = new double[100];

double combatLevel = 0;
         double attack = 99;
         double defence = 1;
         double strength = 1;
         double health = 10;
         double accuracy = 1;
         double weaponAccuracy = 1.0;
         
         /* Enemy */
         double enemyDefence = 77;
         double enemyHealth = 10;

      while (i < 100) {
         /* Player */
         combatLevel = 0;
         attack = 99;
         defence = 1;
         strength = 1;
         health = 10;
         accuracy = 1;
         weaponAccuracy = 1.0;
         
         /* Enemy */
         enemyDefence = 77;
         enemyHealth = 10;
         
         /* Formulas */
         combatLevel = getCombatLevel(new double[] { attack, defence, strength, health });
         attack = attack * (100 / enemyDefence);
         accuracy += attack * Math.random() ;
   
         if (accuracy > 100) { accuracy = 100; }
         accuracyMarks[i] = Math.floor(accuracy);
         
         int chance = (int) accuracyMarks[i++];

         System.out.println("Accuracy = "+chance+".");
      }
      debug(accuracyMarks);
   }

Yeah, recreating variables is a good way to clog your memory up and make your program slower… plus their scope is only within that for loop, so you cannot use them outside of it. I wonder, how did you not know this before now?

Lol guys.

“Assume” = Ass + U + Me.

How that happened:
“Hmm, variables aren’t being updated, lets just throw them in the while loop during my TRIAL.”

This is a 100% random trial that’s been scrapped numerous times lol, I really don’t need code performance critique regarding a 1 second trial.

(Literally a 1 second test)

Thanks though :slight_smile:

not sure if serious :emo:

You could have a system like this:
Creatures have base defense, attack, dodge, accuracy, health, luck
Weapons add to attack
Armour adds to defence
if accuracy + random number between 1 and luck is larger than dodge:
if attack + weapons is larger than defence + armour:
damage done = combined attack - combined defence
damage done is deducted from health.
If health is 0, creature dies/whatever

Always give variables the narrowest possible scope.
Have a little faith in compiler and JVM optimizations.
Primitive variables != garbage collectable objects.