[Gambas-user] "wrapping" function based on recent Mod thread
Kevin Fishburne
kevinfishburne at ...1887...
Wed Oct 9 04:53:02 CEST 2013
On 10/08/2013 09:14 AM, Jussi Lahtinen wrote:
> http://stackoverflow.com/questions/707370/clean-efficient-algorithm-for-wrapping-integers-in-c
>> It's also possible that I mistranslated the C code in the above link, as
>> I don't know C.
>>
> Yes, because Gambas makes automatic conversions and thus the line where
> division is, gives sometimes
> different answers than in C. Example kX += range_size * 1.5 instead of 1
> like in C.
>
>
> int Wrap(int kX, int const kLowerBound, int const kUpperBound)
> {
> int range_size = kUpperBound - kLowerBound + 1;
>
> if (kX < kLowerBound)
> kX += range_size * ((kLowerBound - kX) / range_size + 1);
>
> return kLowerBound + (kX - kLowerBound) % range_size;
> }
>
>
> -->
>
> Private Function Wrap(iX As Integer, iLowerBound As Integer, iUpperBound As
> Integer) As Integer
>
> Dim iRange As Integer = iUpperBound - iLowerBound + 1
>
> If iX < iLowerBound Then
> iX += iRange * CInt((iLowerBound - iX) / iRange + 1)
> Endif
>
> Return iLowerBound + ((iX - iLowerBound) Mod iRange)
>
> End
>
Badass, that works brilliantly. The final procedure looks like this now:
Public Function Wrap_Short(Minimum As Short, Maximum As Short, Value As
Short) As Short
' Constrain value to specified range.
' General declarations.
Dim Spread As Short ' Range between minimum and maximum values.
' Check if value is already within specified range.
If Value >= Minimum And Value <= Maximum Then Return Value
' Calculate and return wrapped value.
Spread = Maximum - Minimum + 1
If Value < Minimum Then Value += Spread * CShort((Minimum - Value) /
Spread + 1)
Return Minimum + ((Value - Minimum) Mod Spread)
End
> How is your game progressing?
> Games like yours require a lot of work even with Gambas.
> It's interesting to see what you will come up to.
>
> Jussi
Very well, thanks. And yes, if anyone ever tells you (as a solo dev with
no budget) that they're going to program an MMO you may laugh in their
face with confidence, as it is a bit like tackling Mount Everest naked
and with no oxygen tank. Despite that, things are steadily coming
together. A minimum of known bugs, most features have been at least
primitively implemented, and the codebase is well formatted, commented,
organized and broken down into reusable procedures (no spaghetti code
yet). I also have spreadsheets to document things like asset lists,
network transactions and data file formats.
You can find a lot of information here (screenshots, YouTube channel,
Twitter feed, detailed progress reports):
http://eightvirtues.com/sanctimonia/media.html
I also had a presentation on the game on a Hearth of Britannia
Dragonsmeet (weekly Google Hangout for Ultima fans hosted by Rustic Dragon):
http://youtu.be/A8pnppjPH_c?t=6m10s
Anyway, thanks Jussi and all for your help. I'd add you to the credits,
but you're already in them! ;)
--
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271
More information about the User
mailing list