• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.
[TFS 1.5] Player function - add magic level keeping progress

[TFS 1.5] Player function - add magic level keeping progress

VitoxMaster

Well-Known Member
Joined
Feb 6, 2023
Messages
118
Solutions
5
Reaction score
87
VitoxMaster submitted a new resource:

Player function - add magic level keeping progress - A clean, reliable +x magic level function that preserves your progress.

I wanted to give a great reward to players who complete the Ferumbras Quest on my server by granting them +1 magic level.
The problem is that the standard addMagicLevel function fills the entire progress bar to the next level.
Because of that, the reward is unbalanced — players with 0% progress get a full level, while players who already had 95% progress only gain that 5%.

Result:

Add code below to the file:
Code:
lib/core/player.lua
...

Read more about this resource...
 
You got gaslighted into thinking it's clean.
Did you at least read it? :(
 
Well, clean was exaggeration. I will drop newer version tomorrow, for now should be enough.
 
There is goto (never use goto), and there is some weird logic checking for non-existing functions it likely hallucinated the first time it generated the script.
Why don't you read what you generate before posting?
 
Thanks for pointing out, I updated resource. Not best time to share anything.
 
You picked the time yourself, the problem was your resource quality, but the bright side is you can still learn.

I will review what you did after I am back home - and keep in mind I am in no way trying to pick on you, we all started somewhere, I am just trying to help out.
 
No worries, thanks for interest. I was determined to finish whole quest's reward system yesterday and it lead to last poor scripts where I stucked annoyed. I was pretty sure that such kind of function should be in regular player script base that's why I posted it here.

I reviewed version, yep it hallucinated after debugging session. Lost context what's in TFS and what's not. Lesson learned.
 
You'r welcome! That is the attitude that will make you a great developer.
I managed to review the code you posted, and I have a couple of suggestions to make your code even cleaner now.

1. Create reusable code
Your code is over-specialised, specific to magic level, while it could increase any skill with a given argument.
Why not make it addSkill(SKILL_CRAFTING, true), first argument dictates which skill we increase, and later if we want to keep the current percentage.

2. Use in-built tools
When faced with a block like
LUA:
if pos < 0 then
    pos = 0
elseif pos > 1 then
    pos = 1
end
You can use math.max and math.min
It's not only cleaner, but does explicitly what you want, "clamps" the value between the arguments.
Unfortunately math.clamp comes in later Lua versions, thus you likely won't be able to use it without additional chores.

3. Use rvalues
In simple words, don't name variables redundantly.
For example, here
LUA:
local reqOld = voc:getRequiredManaSpent(baseMl + 1)
local pos = self:getManaSpent() / reqOld
reqOld is used only once, and the process of creating it as a named variable (lvalue) has some overhead.

After applying 2 and 3, that line should look something like
LUA:
local pos = math.min(1, math.max(0, self:getManaSpent() / voc:getRequiredManaSpent(baseMl + 1))

4. Avoid defensive programming
Although I assume this might be an AI artefact, I will mention it nonetheless.
LUA:
if delta > 0 then
    self:addManaSpent(delta)
elseif delta < 0 then
    self:removeManaSpent(-delta)
end
Assuming we addMagicLevelKeepPercent, this function should never reduce your mana spent.
Although it is wise to ensure that we are not passing any negative value to that function, as it casts it to uint, and a negative value here would result in a very big uint (and a lot of mlvl gain :D), we should ensure that implicitly, or raise an error when this condition is met.

5. Avoid excessive commenting
That is, I am sure due to AI, as it is a staple of generated code.
What's interesting (another proof that LLMs are not yet smart), that the same generator will run into issues because it reads those comments :D
Both because the comments will mislead it and because it will bloat its context.

Anyway, code should be self-documenting, it should read like a book - if it doesn't, you should work on your variable and method names, not add a comment.
Don't confuse that with docstrings, whose sole purpose is to document something in the code, but comments like
-- mana cost for the current band: ML -> ML + 1
are a bloat, and if the logic is not really correct - simply confusing.

Wall of text, but I hope I didn't bore you at any point :D
Feel free to ask any questions
 
Last edited:
Thank you very much for your statements. I appreciate time spent to share your view. I'm not very familiar with LUA so any tips are always welcome from more experienced lua devs.

AD 1
I like that idea, I would probably come back to that function when my chest should give other skills. I won't change whole post here but maybe in the future I will share improved version for other skill also.
AD 2 & 3
I'm not that fluent with fast processing such one-liners that's why I stick to separated lines with single responsibility. Maybe that's just me.
AD 4
In regular scenarios it shouldn't happen. It was to prevent unpredicted work when hand-made changes are done on database - but yeah most likely won't happen in production env.
AD 5
Commenting was straight from generated part, in most cases I agree with self-documenting code. However in shared resources I don't really mind if there are --comments (when resource is not that big to cut comments for my own use). Sometimes it can help others to understand what's going on and decide if they want to keep them.

I will keep these tips in mind for future development. Thanks!
 
Back
Top