ID:170194
 
mob/proc/battle()
var/R = rand(1,2)
if(!src.dead)
src<<"You engage in combat!"
if(R == 1)
start
var/enemyhp = rand(50,100)
if(src.dead)
return 0
if(enemyhp<1)
src<<"You killed the bat & leveled up!"
src.level++
sleep(10)
var/edamage = rand(5,1)
src.hp-=edamage
src.death()
src<<"You lose [edamage] health from the bat!"
sleep(10)
var/damage = rand(5,20)
enemyhp-=damage
src.death()
src<<"You hit the bat for [damage] damage!"
goto start
else
start
if(src.dead)
return 0
var/enemyhp = rand(50,100)
sleep(10)
var/edamage = rand(5,20)
src.hp-=edamage
src.death()
src<<"You lose [edamage] health from the Dog!"
sleep(10)
var/damage = rand(5,20)
enemyhp-=damage
src.death()
src<<"You hit the bat for [damage] damage!"
if(enemyhp<1)
src<<"You killed the Dog & leveled up!"
src.level++
else
goto start


Is there anyway this can be shortened dramatically and get rid of the evil goto?

~>Jiskuha
Jiskuha wrote:
Is there anyway this can be shortened dramatically and get rid of the evil goto?

Yes. while() and/or for()

[edit]
A real fast removel of goto... -.-'
if(R == 1)
while(enemyhp<0)
var/enemyhp = rand(50,100)
if(src.dead)
return 0
if(enemyhp<1)
src<<"You killed the bat & leveled up!"
src.level++
sleep(10)
var/edamage = rand(5,1)
src.hp-=edamage
src.death()
src<<"You lose [edamage] health from the bat!"
sleep(10)
var/damage = rand(5,20)
enemyhp-=damage
src.death()
src<<"You hit the bat for [damage] damage!"
else
while(enemyhp>0)
if(src.dead)
return 0
var/enemyhp = rand(50,100)
sleep(10)
var/edamage = rand(5,20)
src.hp-=edamage
src.death()
src<<"You lose [edamage] health from the Dog!"
sleep(10)
var/damage = rand(5,20)
enemyhp-=damage
src.death()
src<<"You hit the bat for [damage] damage!"
if(enemyhp<1)
src<<"You killed the Dog & leveled up!"
src.level++


-Ryan
Jiskuha wrote:
Is there anyway this can be shortened dramatically and get rid of the evil goto?

~>Jiskuha

Hey, goto isn't really evil, some say it makes "spaghetti" code, but some people prefer it.
In response to Hell Ramen
Hell Ramen wrote:
Hey, goto isn't really evil, some say it makes "spaghetti" code, but some people prefer it.

When misused, yes, it is. It makes code much, much harder-to-read/comprehensible. That, in itself, is evil.
Both instances of goto can be replaced with a while() loop. Also, instead of using rand() here, you should be using prob().
In response to Wizkidd0123
Wizkidd0123 wrote:
Hell Ramen wrote:
Hey, goto isn't really evil, some say it makes "spaghetti" code, but some people prefer it.

When misused, yes, it is. It makes code much, much harder-to-read/comprehensible. That, in itself, is evil.

Goto make sometimes make it a tad bit more understandable than while() and for() *gasp*. Atleast, for me. And, of course, I use it correctly.
In response to Hell Ramen
Hell Ramen wrote:
Goto make sometimes make it a tad bit more understandable than while() and for() *gasp*.

Very, very rarely.

Atleast, for me.

Certainly not for anybody else who happens to be reading your code.

And, of course, I use it correctly.

By using it correctly, I mean using it when while() or for() would either not apply, or would greatly obfuscate the code.
In response to Wizkidd0123
Wizkidd0123 wrote:
Hell Ramen wrote:
Goto make sometimes make it a tad bit more understandable than while() and for() *gasp*.

Very, very rarely.

Atleast, for me.

Certainly not for anybody else who happens to be reading your code.

And, of course, I use it correctly.

By using it correctly, I mean using it when while() or for() would either not apply, or would greatly obfuscate the code.

=/ I understand it, and that's all I care about. And, I really don't want anyone browsing my code...it's just, well, for me!
In response to Hell Ramen
Hell Ramen wrote:
=/ I understand it, and that's all I care about. And, I really don't want anyone browsing my code...it's just, well, for me!

What if you're writing a demo?

What if you need help with something in the developer forum? Nobody wants to help debug obfuscated code.

[edit]
Don't reply to this: I don't want to hijack Jiskuha's thread.
In response to Wizkidd0123
Well that code Ryne posted does not work. I believe its because of the while().

~>Jiskuha
This would probably work. I haven't tested it, but it compiles all nice.

mob/proc/battle()

var/R = rand(1,2)

if(!src.dead)
src<<"You engage in combat!"

var/enemyhp = rand(50,100)
var/monster = "Bat" // default monster is the Bat
var/minatck = 1 // minimum attack
var/maxatck = 5 // maximum attack

if(R==2) // Just add more if()s to define more monsters
monster = "Dog"
minatck = 5
maxatck = 20

do
if(src.dead)
return 0
sleep(10)

var/edamage = rand(minatck,maxatck)
src.hp-=edamage
src.death()
src<<"You lose [edamage] health from the [monster]!"
sleep(10)

var/damage = rand(5,20)
enemyhp-=damage
src.death()
src<<"You hit the [monster] for [damage] damage!"

while(enemyhp)

src<<"You killed the [monster] & leveled up!"
src.level++


~X
In response to Xooxer
That works and its alot shorter, just what i was looking to learn from, Thanks xoox!

~>Jiskuha
In response to Jiskuha
Jiskuha wrote:
That works and its alot shorter, just what i was looking to learn from, Thanks xoox!

~>Jiskuha

To REALLY shorten the proc, you could make the monsters mobs and make the battle() proc take mobs as an arg.
In response to Xooxer
There is just one problem..

        while(enemyhp)

src<<"You killed the [monster] & leveled up!"
src.level++


is faulty, i dont think it ever checks if i killed the enemy or not.

~>Jiskuha
In response to Jiskuha
Jiskuha wrote:
There is just one problem..

>         while(enemyhp)
>
> src<<"You killed the [monster] & leveled up!"
> src.level++
>

is faulty, i dont think it ever checks if i killed the enemy or not.

~>Jiskuha

Jissy, you should know better!
It should be while(enemyhp>0). :p
while(enemyhp) checks if the enemy's HP is not equal to null, and unless you get lucky and get it it to 0, it won't stop.