ID:1237503
 
I worked this up quite some time ago for someone but never actually seen it implemented. This is designed to change the value of items based on a players "Reputation". Perhaps someone could make use of it.

At 0 Rep. the value is doubled.
At 5,000 Rep. the value remains the same.
At 20,000 Rep. the value is halved.

The Snippet:
mob
var/Reputation=5000

proc
ChangeRep(_Val)
Reputation = min(max(Reputation + _Val, 0), 2e4)

RepCost(_Value)
var/tmp/Nval = 5000-Reputation

Nval *= Reputation <= 5e3 ? 0.02 : 0.01 / 3

return _Value + round((_Value/100)*Nval)


And to test it:
mob/verb
//Pass a negative value to subtract Rep.
Change_Rep(var/N as num)
ChangeRep(N)
src << "Reputation: [Reputation]"

CheckValue(var/N as num)
src << "\nReputation: [Reputation]"
src << "Original Value: [N]"
src << "New Value: [RepCost(N)]"


EDIT: Updated ChangeRep() proc with Kaiochao's ammendment, and RepCost() thanks to Albro.
You're doing much more than you need to in ChangeRep(), in my opinion.

ChangeRep(_Val)
Reputation = min(20000, Reputation + _Val)
Reputation = max(0, Reputation)


You don't need _Op because all you have to do is send a negative number as _Val.
As far as processing efficiency, I don't know which is better, so here's another method for that:
Reputation += _Val
Reputation = Reputation<0 ? 0 : Reputation
Reputation = Reputation>20000 ? 20000 : Reputation
In response to Albro1
Or just,
Reputation = min(max(Reputation + _Val, 0), 2e4)
In response to Kaiochao
Lol, I never liked doing min and max in the same line, simply because it takes my brain longer than it needs to to process what is happening. XD
In response to Albro1
Or just,
#define clamp(n, lower, upper) min(max(n, lower), upper)

Reputation = clamp(Reputation + _Val, 0, 2e4)
In response to Kaiochao
Just a random question, but are you typing 2e4 out of personal preference of shortening here on the forums or does 2e4 really work in DM? I would test it but its kind of unavailable to me at the moment.
In response to Albro1
It really works in DM. :)
In response to Albro1
mob/verb/testingsdfs()
world << text2num("2e4")
world << text2num("5e6") // Read 5e+006


Read out


Edit: I just figured out, the number after e is how many 0'z behind the first number. *dumb*
In response to Kaiochao
That's just...beautiful.
In response to Albro1
Albro1 wrote:
That's just...beautiful.
In response to Flysbad
It's E notation, or scientific notation using an e instead of *10^x. And you don't have to use text2num, I'm not sure why you tried that.

0x0123456789ABCDEF is also a number.
I updated the first post with Kaiochao's ammendment to the ChangeRep() proc. Thanks for that, shortening down code has never been a strong point of mine.
In response to Danny Roe
Well if shortening is now your focus, here's some more tips.
if(Reputation<5000) Nval = Nval*0.02

if(Reputation>5000) Nval = Nval*0.00333333333333

Nval = round((_Value/100)*Nval)
_Value += Nval

return _Value


For the two if statements, you can use the ? operator:
Nval = Reputation>=5000 ? Nval*0.02 : Nval*(1/3)


As for the last 3 lines, you are:
1. Setting a variable
2. Adding that variable to another variable
3. Returning the variable added to

So instead, you can do this:
return _Value + (round((_Value/100)*Nval))


Which should successfully put it down to a 3 line procedure.
And this is why I'd be hopeless in those competitions with a maximum size limit.

A small error, but I get the jist of what you meant. I should have been less than or equal to, and it's not one third. The more decimal places I add with 3, the more accurate it is when calculating higher values.
Nval = Reputation<=5000 ? Nval*0.02 : Nval*(0.003333333)
In response to Danny Roe
Another small simplification:
//  0.01 / 3 = 0.00333... with repeating 3s
Nval *= Reputation <= 5e3 ? 0.02 : 0.01 / 3

// unnecessary parentheses
return _Value + round(_Value / 100 * Nval)
I should have caught that first one, thanks. Well at least the only fixes I've had to make were to simplify my code. I'll definitely be using E Notation more often when I'm dealing with large numbers.