Author Sticky: Post Working Scripts Here
Kraloth Posted: Saturday, 06 July 11:11AM
I have no idea how many other versions of this there are in the thread; I don't have time to read all squillion pages. I'll post this anyway, "just in case".

All the code was written by J.S.Labuschagne on 2002-07-06.

1) Name: Level handling utilities.

2) Purpose: Allows you to figure out what level a PC is, give them enough experience for an arbitrary level, or calculate the min/max xp possible at any given level.

3) Notes: Does not take upper XP limit into account. Also, when calling get_player_level() you should ignore the second
argument; it is used for internal purposes.

4)
NWScript:
int min_xp_at_level(int L){

    if (L < 1) return 0;

    return (L - 1)*1000 + min_xp_at_level(L - 1);

}

int max_xp_at_level(int L){

    return min_xp_at_level(L + 1) - 1;

}

int get_player_level(object pc, int L=1){

    if(GetXP(pc) > max_xp_at_level(L))

        return get_player_level(pc, L+1);

    return L;

}

void set_player_level(object pc, int L){

    SetXP(pc, min_xp_at_level(L));

}