ATTEMPTING TO CODE A BATTLE ENGINE

Posts

Pages: 1
So...This is some code that I tried to code on my own for a sort of side project where...I'm honestly too embarrassed by the idea to describe it here, so I'll just present my problem. The actual formulas aren't in there yet, but I was wondering if someone could tell me these three things:
1. How to have the buttons activate a different formulas depending on which one is pushed.
2. How to display the name, HP (health points), and MU (Munitions) for each of the characters.
3. How to set a picture to each of the characters.
4. Isn't as important now, but it is how to change the character. I assume I can set it to the buttons later, but I can do it later.

Here is the coding I have done:
<HTML>
<HEAD>
<SCRIPT>
var Entity = function (hp, mu, sp, pp, fp, re, de, sl) {
this.hp = hp;
this.pp = pp;
this.mu = mu;
this.re = re;
this.fp = fp;
this.de = de;
this.sp = sp;
this.sl = sl;
};
var Mina = new Entity(15, 15, 3, 3, 3, 4, 1, 3);
var RH_Whelp = new Entity(15, 15, 3, 3, 3, 4, 1, 3);
var BC_Whelp = new Entity(15, 15, 3, 3, 3, 4, 1, 3);
var SS_Whelp = new Entity(15, 15, 3, 3, 3, 4, 1, 3);

var names;

var randomnumber = Math.floor(Math.random(10)*10);

var selectedmonster;
if (randomnumber <= 3){
selectedmonster = RH_Whelp;
names = "Red Hand Whelp";
}
else if (randomnumber <= 6, > 3){
selectedmonster = BC_Whelp;
names = "Blue Chest Whelp";
}
else if (randomnumber > 6){
selectedmonster = SS_Whelp;
names = "Sand Stalker Whelp";
}

var battle = 1;
var monsterdead = 0;
var playerdead = 0;
var playerempty = 0;
var monsterempty = 0;

var playerturn = 1;
var monsterturn = 0;
while (battle == 1){

while (playerturn == 1){

<form action="game_unkown.html" method="get">
Choose your action:
<button name="R_A" type="submit" value="RA">Ranged Attack</button>
<button name="C_A" type="submit" value="CA">Close Attack</button>
<button name="R_E" type="reset" value="RT">Retreat</button>
if ((isset($_RA) {
}

playerturn = 0;
monsterturn = 1;
}

while (monsterturn == 1){

playerturn = 1;
monsterturn = 0;
}

if (selectedmonster.hp < 0){
monsterdead = 1;
battle = 0;
}


if (Mina.hp < 0){
playerdead = 1;
battle = 0;
}

if (Mina.mu < 0){
playerempty = 1
}

if (selectedmonster.mu < 0){
monsterempty = 1
}
</SCRIPT>
</HEAD>
</HTML>
Ummm, are you using your own engine, one of the rpgmakers, etc? Because we don't know what this is for, we can't help much.

Also, kinda looks like you're reinventing the wheel. Surely some of this is doable without all that.
author=bulmabriefs144
Ummm, are you using your own engine, one of the rpgmakers, etc? Because we don't know what this is for, we can't help much.

Sorry, I was trying to create my own engine here. I forgot to specify that part.
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
To have the buttons do different things you would attach onclick handlers to them via JavaScript. Displaying the values would depend on how you want them to appear, though I assume you'd simply have a document.write(variablename) where you want the values to be shown. Not entirely sure what you mean with setting a picture for the characters, but you'd basically do more or less the same thing as setting the variables, except you'd have the var value as the filename of the picture then use JavaScript to insert the filename into an <img> tag.
author=Trihan
To have the buttons do different things you would attach onclick handlers to them via JavaScript. Displaying the values would depend on how you want them to appear, though I assume you'd simply have a document.write(variablename) where you want the values to be shown. Not entirely sure what you mean with setting a picture for the characters, but you'd basically do more or less the same thing as setting the variables, except you'd have the var value as the filename of the picture then use JavaScript to insert the filename into an <img> tag.

Ok, that does help me out a little bit, but I was hoping for something a little bit more specific on the javascript side of things, as I'm not entirely sure how to set that part up.
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
Let me see what I can do.
author=Trihan
Let me see what I can do.

thanks. Do you want me to post the prototype battle engine I had here too?
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
Sure, if you think it'll help. :)
Here's the battle engine. Sorry it's so crude:
Var mina.closeattack = Math.ceil(Math.random(Mina.pp + mina.sp)*20);
Var mina.rangeattack = Math.ceil(Math.random(Mina.fp + mina.sp)*20);
Var selectedmonster.closeattack = Math.ceil(Math.random(selectedmonster.pp + selectedmonster.sp)*20);
Var selectedmonster.rangeattack = Math.ceil(Math.random(selectedmonster.fp + selectedmonster.sp)*20);
Var mina.resistance = Math.ceil(Math.random(Mina.re + mina.de)*20);
Var selectedmonster.resistance = Math.ceil(Math.random(selectedmonster.re + selectedmonster.de)*20);

var dodgeroll = Math.floor(Math.random(100)*100);

If selectedmonster.sp >= dodgeroll;
Console.log(names + "dodged the attack!");
Else if mina.closeattack <= selectedmonster.resistance;
Console.log( "The attack bounced harmlessly off" + names);
Else if mina.closeattack < selectedmonster.resistance;
Selectedmonster.hp = selectedmonster.hp - mina.closeattack + selectedmonster.resistance
Console.log( names + "was hit! He has" + selectedmonster.hp + "hp left!")

If selectedmonster.sp >= dodgeroll;
Console.log(names + "dodged the attack!");
If mina.mu <= 0;
console.log( "there is no ammo left!");
Else if mina.rangeattack <= selectedmonster.resistance;
mina.mu = mina.mu - 1;
Console.log( "The attack bounced harmlessly off" + names + "armor. Mina has " + mina.mu + "left.");
Else if mina.rangeattack < selectedmonster.resistance;
mina.mu = mina.mu - 1;
Selectedmonster.hp = selectedmonster.hp - mina.rangeattack + selectedmonster.resistance
Console.log( names + "was hit! He has" + selectedmonster.hp + "hp left! Mina has " + mina.mu + "left!")

If mina.sp >= dodgeroll;
Console.log("Mina dodged the attack!");
Else if selectedmonster.closeattack <= mina.resistance;
Console.log( "The attack bounced harmlessly off Mina's armor.");
Else if selectedmonster.closeattack < mina.resistance;
mina.hp = mina.hp - selectedmonster.closeattack + mina.resistance
Console.log( "Mina was hit! She has" + mina.hp + "hp left!")

If mina.sp >= dodgeroll;
Console.log("Mina dodged the attack!");
If mina.mu <= 0;
console.log( names + "has no more ammo!");
Else if selectedmonster.rangeattack <= mina.resistance;
selectedmonster.mu = selectedmonster.mu - 1;
Console.log( "The attack bounced harmlessly off Mina's armor.");
Else if selectedmonster.rangeattack < mina.resistance;
selectedmonster.mu = selectedmonster.mu - 1;
mina.hp = mina.hp - selectedmonster.rangeattack + mina.resistance
Console.log( "Mina was hit! She has" + mina.hp + "hp left!)
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
I'll have a look at this later today and see what I can do to help you.
author=Trihan
I'll have a look at this later today and see what I can do to help you.
Thanks. I might make some changes to the stats later on, but I don't really think that will effect much at this stage of things.
Well, just to be funny, I tried running the engine as is and got this message:
SyntaxError: syntax error game.html:28
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

I think that might have something to do with the buttons, I managed to get it to stop doing that, and hope that this edited code will help:
<!DOCTYPE html>
<HTML lang="en-US">
<HEAD>
<SCRIPT>
var Entity = function (hp, mu, sp, pp, fp, re, de, sl) {
this.hp = hp;
this.pp = pp;
this.mu = mu;
this.re = re;
this.fp = fp;
this.de = de;
this.sp = sp;
this.sl = sl;
};
var Mina = new Entity(15, 15, 3, 3, 3, 4, 1, 3);
var RH_Whelp = new Entity(15, 15, 3, 3, 3, 4, 1, 3);
var BC_Whelp = new Entity(15, 15, 3, 3, 3, 4, 1, 3);
var SS_Whelp = new Entity(15, 15, 3, 3, 3, 4, 1, 3);

var names;

var randomnumber = Math.floor(Math.random(10)*10);

var selectedmonster;
if (randomnumber <= 3){
selectedmonster = RH_Whelp;
names = "Red Hand Whelp";
}
else if (randomnumber <= 6, > 3){
selectedmonster = BC_Whelp;
names = "Blue Chest Whelp";
}
else if (randomnumber > 6){
selectedmonster = SS_Whelp;
names = "Sand Stalker Whelp";
}

var battle = 1;
var monsterdead = 0;
var playerdead = 0;
var minaempty = 0;
var monsterempty = 0;
var mina.closeattack = Math.ceil(Math.random(Mina.pp + mina.sp)*20);
var mina.rangeattack = Math.ceil(Math.random(Mina.fp + mina.sp)*20);
var selectedmonster.closeattack = Math.ceil(Math.random(selectedmonster.pp + selectedmonster.sp)*20);
var selectedmonster.rangeattack = Math.ceil(Math.random(selectedmonster.fp + selectedmonster.sp)*20);
var mina.resistance = Math.ceil(Math.random(Mina.re + mina.de)*20);
var selectedmonster.resistance = Math.ceil(Math.random(selectedmonster.re + selectedmonster.de)*20);
var dodgeroll = Math.floor(Math.random(100)*100);
var playerturn = 1;
var monsterturn = 0;
while (battle == 1){

while (playerturn == 1){

if (selectedmonster.sp >= dodgeroll){
<p>names + "dodged the attack!"</p>;
}
else if (mina.closeattack <= selectedmonster.resistance){
<p>"The attack bounced harmlessly off" + names</p>;
}
else if (mina.closeattack < selectedmonster.resistance){
Selectedmonster.hp = (selectedmonster.hp - mina.closeattack + selectedmonster.resistance);
<p>names + "was hit! He has" + selectedmonster.hp + "hp left!"</p>;
}

if (mina.mu <= 0){
<p>"there is no ammo left!"</p>;
}
else if (selectedmonster.sp >= dodgeroll){
mina.mu = mina.mu - 1;
<p>names + "dodged the attack! Mina has" + mina.mu + " shots left!"</p>;
}
else if mina.rangeattack <= selectedmonster.resistance{
mina.mu = mina.mu - 1;
<p>"The attack bounced harmlessly off" + names + "armor. Mina has " + mina.mu + "left."</p>;
}
else if (mina.rangeattack < selectedmonster.resistance){
mina.mu = mina.mu - 1;
Selectedmonster.hp = (selectedmonster.hp - mina.rangeattack + selectedmonster.resistance);
<p> names + "was hit! He has" + selectedmonster.hp + "hp left! Mina has " + mina.mu + "left!"</p>;
}
playerturn = 0;
monsterturn = 1;
}

while (monsterturn == 1){
if (mina.sp >= dodgeroll){
<p>"Mina dodged the attack!"</p>;
}
else if (selectedmonster.closeattack <= mina.resistance)
<p>"The attack bounced harmlessly off Mina's armor."</p>;
}
else if (selectedmonster.closeattack < mina.resistance){
mina.hp = mina.hp - selectedmonster.closeattack + mina.resistance;
<p>"Mina was hit! She has " + mina.hp + " hp left!"</p>;
}
if (selectedmonster.mu <= 0){
<p>names + "has no more ammo!"</P>
}
else if (mina.sp >= dodgeroll){
<p>"Mina dodged the attack!"</p>;
}
else if (selectedmonster.rangeattack <= mina.resistance){
selectedmonster.mu = selectedmonster.mu - 1;
<p>"The attack bounced harmlessly off Mina's armor."</p>;
}
else if (selectedmonster.rangeattack < mina.resistance){
selectedmonster.mu = selectedmonster.mu - 1;
mina.hp = mina.hp - selectedmonster.rangeattack + mina.resistance;
<p>"Mina was hit! She has " + mina.hp + " hp left!</p>;
}
playerturn = 1;
monsterturn = 0;
}

if (selectedmonster.hp < 0){
monsterdead = 1;
battle = 0;
}

if (Mina.hp < 0){
playerdead = 1;
battle = 0;
}

if (Mina.mu < 0){
playerempty = 1
}

if (selectedmonster.mu < 0){
monsterempty = 1
}
</SCRIPT>
</HEAD>
</HTML>

Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
Okay, I was going to just try and slot in the stuff you need with the code you have, but I honestly think this would be more effective if you used PHP and AJAX for the action processing. You mind if I tweak it a bit?
author=Trihan
Okay, I was going to just try and slot in the stuff you need with the code you have, but I honestly think this would be more effective if you used PHP and AJAX for the action processing. You mind if I tweak it a bit?

If you think that would help, then go ahead and be my guest.
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
It might be a bit more complex for you to maintain, but using an AJAX call you can have real-time updates to the page and its contents without having to have the user navigate to a different one.
author=Trihan
It might be a bit more complex for you to maintain, but using an AJAX call you can have real-time updates to the page and its contents without having to have the user navigate to a different one.

That's what I was trying to build up to, thanks. I'll post it here later, but I also forgot to put in the code for retreating, and the perimeters under which the enemy might retreat.
Okay, here's the running away code, and the parameters:
var selectedmonster.healthpercent = Math.ceil((selctedmonster.hp / selectedmonster.hp)*100);
var mina.healthpercent = Math.ceil((mina.hp / mina.hp)*100);
var runaway = Math.floor(mathrandom(1000)*1000)

If (selectedmonster.healthpercent >= 25){
<p>names + "is readying to fight!"</p>;
}
Else if (math.ceil(selectedmonster.sp * selectedmonster.healthpercent) < runaway){
<p>names + "tried to run away, but failed to do so."</p>;
}
Else if (math.ceil(selectedmonster.sp * selectedmonster.healthpercent) >= runaway){
<p>names + "managed to escape!"</p>;
battle = 0;
}
if (math.ceil(mina.sp * mina.healthpercent) < runaway){
<p>"Mina tried to run away, but failed to do so."</p>;
}
Else if (math.ceil(mina.sp * mina.healthpercent) >= runaway){
<p>" Mina managed to escape!"</p>;
battle = 0;
}

I had to type this part up on my tablet, so I apologize in advance for if anything is wrong, and that it isn't just in there.
Pages: 1