Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: RPG Leveling with Arrays - Looking for some crits  (Read 2064 times)

0 Members and 1 Guest are viewing this topic.
RPG Leveling with Arrays - Looking for some crit...
« on: February 20, 2010, 01:11:32 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Since I reset my pc, trying my best to not install Game Maker as I desire to move beyond RAD.
Been messing with XNA a bit and reading a C# book.

Came up with an idea based upon what I have read thus far.  Without using a database, I wanted to try to create a level up system.

Here's what I came up with at work today(probably more pseudo code than real code):

Code: [Select]
int [] expneeded = new int [100]; // sets up 99 levels of expneeded to use
int level = 1; //player's level
int G = 1; // random letter for the moment, you'll see in a sec
int exp = 0;  //player's exp
expneeded[1] = 30; // setting a base of exp needed for level 2, 0 = level 1


// setting up exp needed to level 99 - Initializing
while G < 99
{
        G = G + 1;
        
        expneeded[G] = ((expneeded[G - 1] / 2) + (expneeded[G - 1]));
}


//In-Game

if (exp > expneeded[level]) //checking to see if the exp is greater than expneeded
{
        level = level + 1; // adds one to the player's level
        exp = 0; // resets the player's exp to 0
}

Just wondering if this would actually work.  If it does, is there a more optimal way of coding it?  It could also be tweaked for the Initializing so that if G was greater than 10 but less than 20, it would multiply by 1.4, and so on.

EDIT: I see one error already.  Can't multiply by 1.5 since it is an integer and not a double or float.  Working on it again.

EDIT 2: I does work.  I tried it out in a console app with C# and displayed expneeded[G] in the while loop.  Now just gotta work out some kinks.
« Last Edit: February 20, 2010, 01:58:30 am by Theforeshadower »
Logged
  • Super Fan Gamers!
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #1 on: February 20, 2010, 03:12:13 am »
  • 笑い男
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 2124
Could just have a function to check if you levelled up after you receive exp (after a battle?) which pretty much contains the exact same calculation you have here:
Code: [Select]
// setting up exp needed to level 99 - Initializing
while G < 99
{
        G = G + 1;
       
        expneeded[G] = ((expneeded[G - 1] / 2) + (expneeded[G - 1]));
}
(Of course the calculation would need to be changed to not depend on an array or anything though.)
But G would be an argument of the function and would be currentLevel + 1 (I guess?) when you call the function.
You could also supply your current exp as another argument and the function could then check that vs the exp needed and if it's equal or greater would just return true (otherwise false). So in the end the function would just return if you should be levelled up or not. If you provide a reference to the currentLevel variable, you could work out G (as it's just currentlLevel + 1) and also do the levelling up in the function if it's going to return true (as you have a reference to the currentLevel variable, so just add 1 to it.)


Wouldn't then need to manually store an array holding every exp needed to level up for every level for every character or whatever.


But if you WERE to use an array like you have in your example, using a for loop would be better (as you can just keep the G variable in that loop, no need to have weird variables floating around in your code.)
eg:
Code: [Select]
for(int i=1; i<100; i++)
{
expneeded[i] = ((expneeded[i - 1] / 2) + (expneeded[i - 1]));
}
(Replaced G with "i" as that's the norm and made i start as 1 and end at 100. Normally it'd start as 0, as your array would start at 0, but the very first calculation would be 0-1, which is -1 and there is no -1 index in an array giving you an error.)



Sorry if this post sucks and doesn't help much, just ended up randomly typing it for some reason and going to hit the post button now >_<
Logged

この世に悪があるとすれば、それは人の心だ
  • .hack//The World

Dark-Hylian

Silence
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #2 on: February 20, 2010, 03:23:27 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 370
Code: [Select]
while G < 99
{
        G = G + 1;
       
        expneeded[G] = ((expneeded[G - 1] / 2) + (expneeded[G - 1]));
}

Code: [Select]
while G < 99
{
        G+=1;
        expneeded[G] = ((expneeded[.5G - .5]) + (expneeeded[G-1]));
}

If I'm not mistaken, that should work, and is a lot easier to read/shorter.



Code: [Select]
if (exp > expneeded[level]) //checking to see if the exp is greater than expneeded
{
        level = level + 1; // adds one to the player's level
        exp = 0; // resets the player's exp to 0
}


Code: [Select]
if (exp > expneeded[level]) //checking to see if the exp is greater than expneeded
{
        level = level + 1; // adds one to the player's level
        exp = 0; // resets the player's exp to 0
}


Code: [Select]
expneeded[1] = 30; // setting a base of exp needed for level 2, 0 = level 1for the increasing percent for levels, this should work... i think.
Code: [Select]
expneeded[1] = H; // setting a base of exp needed for level 2, 0 = level 1
H=30;
if level=2
{
H+=.5H;
}

I'm not entirely sure in my reasoning, but that should work. It looks alright, and seems to work well in a crudely and quickly made test engine. Hope that helps.
Logged
  • Dawning Hour
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #3 on: February 20, 2010, 03:35:18 am »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
In all seriousness, and I am really trying not to be a DICK here, but you need to improve your problem solving skills. Arrays are inefficient, take up space, and are a pain in the ass to work with for something like this because of the number of things that must be changed to make modifications.

The best way that you could do this would be to put your algorithm, as Luke said, into a function that returns the required experience for a level. Here is an example that calculates the experience required for any level in the Dungeons & Dragons 3.5 rules:

Code: C
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         for (int lvl = 1; lvl < 21; lvl++)
  6.         {
  7.             Console.WriteLine("Level {0}: {1} EXP", lvl, GetXPForLevel(lvl));
  8.         }
  9.  
  10.         Console.ReadKey();
  11.     }
  12.  
  13.     static int GetXPForLevel(int level)
  14.     {
  15.         if (level <= 1)
  16.             return 0;
  17.  
  18.         return (GetXPForLevel(level - 1) + ((level - 1) * 1000));
  19.     }
  20. }
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #4 on: February 20, 2010, 03:53:19 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
In all seriousness, and I am really trying not to be a DICK here, but you need to improve your problem solving skills. Arrays are inefficient, take up space, and are a pain in the ass to work with for something like this because of the number of things that must be changed to make modifications.

The best way that you could do this would be to put your algorithm, as Luke said, into a function that returns the required experience for a level. Here is an example that calculates the experience required for any level in the Dungeons & Dragons 3.5 rules:

Code: C
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         for (int lvl = 1; lvl < 21; lvl++)
  6.         {
  7.             Console.WriteLine("Level {0}: {1} EXP", lvl, GetXPForLevel(lvl));
  8.         }
  9.  
  10.         Console.ReadKey();
  11.     }
  12.  
  13.     static int GetXPForLevel(int level)
  14.     {
  15.         if (level <= 1)
  16.             return 0;
  17.  
  18.         return (GetXPForLevel(level - 1) + ((level - 1) * 1000));
  19.     }
  20. }

Ah, that is more simplified and better looking too.  The main factor as to why I never thought of it that way is that I have not gotten into functions yet.  That and I am still trying to wrap my head around classes with declaring and creating new classes.  The book I am reading describes as class very well but then throws it at you outside of how it looks in the entire projects code without showing you how it fits in.
Logged
  • Super Fan Gamers!
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #5 on: February 20, 2010, 03:54:44 am »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Ah, that is more simplified and better looking too.  The main factor as to why I never thought of it that way is that I have not gotten into functions yet.  That and I am still trying to wrap my head around classes with declaring and creating new classes.  The book I am reading describes as class very well but then throws it at you outside of how it looks in the entire projects code without showing you how it fits in.

...and you're learning classes before functions because...?
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #6 on: February 20, 2010, 03:55:42 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849

...and you're learning classes before functions because...?

That's how the book goes >.>.  'Bout the only answer I can give.
Logged
  • Super Fan Gamers!
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #7 on: February 20, 2010, 06:01:03 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
That's kind of dumb and I disagree with that completely >_>  Functions should come before classes.

As for your code..personally what I would do is contain a variable within the player class containing how much exp they need.  Then I would just use your calculation at level up to adjust it.
Logged



i love big weenies and i cannot lie

Mamoruanime

@Mamoruanime
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #8 on: February 20, 2010, 06:14:44 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
This will sound a little silly, but I'd suggest taking a peek at how the Pokemon games do their levelling. All in all they have some very nice algorithms for level progression that can be worked into almost any RPG application. Those games are so incredibly sophisticated in terms of stat progression, and would be very good to learn from.

http://bulbapedia.bulbagarden.net/wiki/Experience

Logged
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #9 on: February 20, 2010, 06:18:47 am »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
This will sound a little silly, but I'd suggest taking a peek at how the Pokemon games do their levelling. All in all they have some very nice algorithms for level progression that can be worked into almost any RPG application. Those games are so incredibly sophisticated in terms of stat progression, and would be very good to learn from.

http://bulbapedia.bulbagarden.net/wiki/Experience
Your mom's a pokemon game. So take that.
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #10 on: February 20, 2010, 07:13:08 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
I based mine on pokemans, since i was making a pokemans game.

Code: [Select]
        public int ExpRequiredForNextLevel()
        {
            int n = this.level + 1;

            switch (this.experienceType)
            {
                case ExperienceTypes.Erratic:
                    if (n <= 50)       return Math.Pow(n, 3) * (100 - n) / 50.0f;
                    else if (n <= 68)  return Math.Pow(n, 3) * (100 - n) / 100.0f;
                    else if (n <= 98)  return Math.Pow(n, 3) * ((1911 - 10 * n) / 3.0f) / 500.0f;
                    else               return Math.Pow(n, 3) * (160 - n) / 100.0f;
                case ExperienceTypes.Fast:
                    return 4 * Math.Pow(n, 3) / 5.0f;
                case ExperienceTypes.Fluctuating:
                    if (n <= 15)       return Math.Pow(n, 3) * ((24 + ((n + 1) / 3.0f)) / 50.0f);
                    else if (n <= 35)  return Math.Pow(n, 3) * ((14 + n) / 50.0f);
                    else               return Math.Pow(n, 3) * ((32 + (n / 2.0f)) / 50.0f);
                case ExperienceTypes.MediumFast:
                    return Math.Pow(n, 3);
                case ExperienceTypes.MediumSlow:
                    return (int)((6 * Math.Pow(n, 3) / 5.0f) - 15 * Math.Pow(n, 2) + 100 * n - 140);
                case ExperienceTypes.Slow:
                    return 5 * Math.Pow(n, 3) / 4.0f;
            }

            return 0;
        }

        public bool RequiresLevelingUp()
        {
            return this.experience >= ExpRequiredForNextLevel();
        }

        public void LevelUp()
        {
            while (this.level < 100 && RequiresLevelingUp())
                this.level++;
        }
Logged
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #11 on: February 20, 2010, 09:24:54 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Well, I guess most of it is said already. I wouldn't use an array either. Use a function that would calculate the new experience target.

What I have not seen any comments on is:

Code: [Select]
if (exp > expneeded[level]) //checking to see if the exp is greater than expneeded
{
        level = level + 1; // adds one to the player's level
-->> exp = 0; // resets the player's exp to 0
}

This means that experience points get lost once you level up. Take for example this case:
- You still need 1 exp to level up.
- The enemy you defeat gives you 1000 exp.
- Thus you lose 999 exp.

This does not matter much if your next level requires about 1,000,000 exp to level up, but if your next level requires only 300 points to level up, then you lose one or even more levels in experience. I would do the following in pseudo:

Code: [Select]
function levelUp( ){
   if( exp > expNeeded){
      level += 1
      exp -= expNeeded
      expNeeded = newExpNeeded(level, expNeeded)
      levelUp( )
   }
}




Code: [Select]
while G < 99
{
        G = G + 1;
       
        expneeded[G] = ((expneeded[G - 1] / 2) + (expneeded[G - 1]));
}

Code: [Select]
while G < 99
{
        G+=1;
        expneeded[G] = ((expneeded[.5G - .5]) + (expneeeded[G-1]));
}

If I'm not mistaken, that should work, and is a lot easier to read/shorter.
No Dark Hylian, it does not work, because of (expneeded[.5G - .5]). This does not do the same as (expneeded[G - 1] / 2).
Logged
Re: RPG Leveling with Arrays - Looking for some ...
« Reply #12 on: February 20, 2010, 04:13:32 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
About the experience formulae, I don't think you should directly copy pokemon.

Pokemon games use experience in the order of n^3 probably because the amount of experience the pokemon gains by fighting a pokemon of the same level is also of the order of n^3, and if that works so we could suppose that the "difficulty" of fighting a pokemon of 1 level above or 1 level below would also be of the order of n^3.

Anyway, that was meticulously calculated by the game creators, so you would have to copy all the exp/hp/stats system, or calculate your own system with your criteria.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.079 seconds with 64 queries.