Raised This Month: $51 Target: $400
 12% 

Solved Stock Algorithm problems


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 09-27-2017 , 14:10   Stock Algorithm problems
Reply With Quote #1

This topic has been solved by a Blacky Rose,
here
I already know how to do this.
__________________

Last edited by Relaxing; 03-30-2018 at 19:01.
Relaxing is offline
Old 09-27-2017, 14:24
HamletEagle
This message has been deleted by HamletEagle. Reason: Nevermind, I read too fast
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 09-27-2017 , 15:02   Re: Stock Algorithm problems
Reply With Quote #2

Use
Code:
new data[64]; get_time_length(id, get_user_time(id), timeunit_seconds, data, charsmax(data))
This is stock included in AMXX. Also your stock is terrible....
If you want to use your stock, you need to do something like:
Code:
stock get_user_itime(id, data[], len) {     new secs = get_user_time(id), mins, hours;         while(secs >= 60)     {         secs -= 60;         mins++;             }         while(mins >= 60)     {         mins -= 60;         hours++;             }         formatex(data, len, "%i hours, %i minutes and %i seconds", hours, mins, secs); }

Last edited by KiLLeR.; 09-27-2017 at 15:10.
KiLLeR. is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 09-27-2017 , 15:55   Re: Stock Algorithm problems
Reply With Quote #3

Hours = Minutes / 60
Minutes = Seconds / 60
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 09-27-2017 , 16:42   Re: Stock Algorithm problems
Reply With Quote #4

These things are pretty funny to write.
Also, this hurts my eyes

First is your requested function with an explanation. Allthough, I would probably use get_time_length() because it's just so much easier.
At the bottom is a more raw version which is more like get_time_length().

Code:
#define DAYS(%0)        ( %0 / 86400 ) #define HOURS(%0)      ( %0 % 86400 / 3600 ) #define MINUTES(%0)  ( %0 % 3600 / 60 ) #define SECONDS(%0)  ( %0 % 60 ) #define BIT_SECONDS  1 // 0001 or (1<<0) #define BIT_MINUTES  2 // 0010 or (1<<1) #define BIT_HOURS      4 // 0100 or (1<<2) #define BIT_DAYS        8 // 1000 or (1<<3) stock get_user_time_s(id, output[], len) {         new iTime = get_user_time(id); // Getting the user time in seconds, easy so far.     // To avoid a huge nesting of if/elseif/else to find out if there should be any comma or "and" and if so, where should they be.     // This will probably be easiest with a bitfield. Using binary numbers along with bitmasking since that will help us predict how the string will play out before we start formatting.     new TimeBitfield = ( DAYS(iTime) ? BIT_DAYS : 0 ) \     + ( HOURS(iTime) ? BIT_HOURS : 0 ) \     + ( MINUTES(iTime) ? BIT_MINUTES : 0 ) \     + ( SECONDS(iTime) ? BIT_SECONDS : 0 );     /*     This bitmask will now basically consist of a binary number with four digits. In decimal these are represented by 1, 2, 4 and 8.     0  0  0  0     |  |  |  Seconds?     |  |  Minutes?     |  Hours?     Days?     If any of these are set to 1 instead of 0 we know this string will contain that element.     For example, if our result is 0101 we know it will contain hours and seconds but no days or minutes.     If you don't understand binary and bitmasking there are great tutorials on it out there.     Now, to make it even more easier later we can even find out how many of these bits are set.     There are a number of ways to do it but this is what I find easiest, looping through the different bits and checking them one by one to see if they're set.     */     new BitCount;     for ( new i = 1 ; i <= BIT_DAYS ; i *= 2 ) {         if ( TimeBitfield & i )             BitCount++;     }     // So now we have these two simple variables that contains exactly the information we need to format the string.     /*     When using formatex() the function will return the number of characters written to the string. This enables us to append to a message very easily.         First you set up a variable to catch the return and write to the string:     new CurLen = formatex(Output, MaxLen, "Hello");     Next, you do the same thing but this time you give the output string an offset ( Output[X] ). If this is not done, the string will start from the beginning, overwriting the old one.     Also tell the function there's less cells to write to ( MaxLen - CurLen ), otherwise you might end up with index out of bounds.     And finally don't forget to use += so the new offset will be added to the old one. If you miss this, the string will not take the previous length into account and you will lose part of the string when you format the next time.     CurLen += formatex(Output[CurLen], MaxLen - CurLen, "!");     Output = "Hello!"     */     new templen;         if ( TimeBitfield & BIT_DAYS ) { // Does the integer contain days?         templen = formatex(output, len, "%d day%s", DAYS(iTime), DAYS(iTime) == 1 ? "" : "s"); // If so, add them to the string. Also check if it's one or more days and add nothing or "s" at the end.         if ( --BitCount ) // Remove one before checking if we have more elements to print out. If we have we need to add the divider.             templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and "); // And depending on how many elements we have left to print we add a comma or the word "and".     }     if ( TimeBitfield & BIT_HOURS ) { // Repeat for hours...         templen += formatex(output[templen], len - templen, "%d hour%s", HOURS(iTime), HOURS(iTime) == 1 ? "" : "s");         if ( --BitCount )             templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and ");     }     if ( TimeBitfield & BIT_MINUTES ) { // ... minutes...         templen += formatex(output[templen], len - templen, "%d minute%s", MINUTES(iTime), MINUTES(iTime) == 1 ? "" : "s");         if ( --BitCount )             templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and ");     }     if ( TimeBitfield & BIT_SECONDS ) // ... and seconds.         templen += formatex(output[templen], len - templen, "%d second%s", SECONDS(iTime), SECONDS(iTime) == 1 ? "" : "s");     // No need to check if there's more elements coming because there can't be.     // Done. No need to return anything. Checking if there's any time should be done prior to this function being called so we don't need any error handling because we can't really expect any errors. }
Could easily be edited to ML by using the same dictionary as get_time_length().





Just the basic function of the code. Not bound to user time.
Code:
#define DAYS(%0)        ( %0 / 86400 ) #define HOURS(%0)      ( %0 % 86400 / 3600 ) #define MINUTES(%0)  ( %0 % 3600 / 60 ) #define SECONDS(%0)  ( %0 % 60 ) stock FormatTime(iTime, output[], len) {     new TimeBitfield = ( DAYS(iTime) ? (1<<3) : 0 ) \     + ( HOURS(iTime) ? (1<<2) : 0 ) \     + ( MINUTES(iTime) ? (1<<1) : 0 ) \     + ( SECONDS(iTime) ? (1<<0) : 0 );     new BitCount;     for ( new i = 1 ; i <= (1<<3) ; i *= 2 ) {         if ( TimeBitfield & i )             BitCount++;     }     new templen;         if ( TimeBitfield & (1<<3) ) {         templen = formatex(output, len, "%d day%s", DAYS(iTime), DAYS(iTime) == 1 ? "" : "s");         if ( --BitCount )             templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and ");     }     if ( TimeBitfield & (1<<2) ) {         templen += formatex(output[templen], len - templen, "%d hour%s", HOURS(iTime), HOURS(iTime) == 1 ? "" : "s");         if ( --BitCount )             templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and ");     }     if ( TimeBitfield & (1<<1) ) {         templen += formatex(output[templen], len - templen, "%d minute%s", MINUTES(iTime), MINUTES(iTime) == 1 ? "" : "s");         if ( --BitCount )             templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and ");     }     if ( TimeBitfield & (1<<0) )         templen += formatex(output[templen], len - templen, "%d second%s", SECONDS(iTime), SECONDS(iTime) == 1 ? "" : "s"); }

Last edited by Black Rose; 09-27-2017 at 17:01.
Black Rose is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 09-27-2017 , 17:34   Re: Stock Algorithm problems
Reply With Quote #5

return thank you very much;
__________________
Relaxing is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:41.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode