Page 1 of 1
[ACS] Generally useful code snippets
Posted: Sun Aug 13, 2023 10:18 pm
by TDRR
I'll be using this thread to post ACS code snippets that aren't quite large enough or too specific for a library but are still fairly useful.
Spoiler: Precise fixed point string format (Open)Normally, when printing (or converting into a string) a fixed point number with the f: format specifier, a lot of precision is lost.
This happens because internally Zandronum (and really just G/ZDoom in general) converts the fixed point number to a floating point number, losing precision in the process. This precision loss becomes more and more extreme the larger the number.
This function does a more accurate conversion that loses much less precision, and has an adjustable amount of decimal places. If you specify 0 for decPlaces, you'll get 4, which is the max, and default amount of decimal places.
Code: Select all
function str PreciseFixedFmt (int n, int decPlaces)
{
str retStr;
int integerPart = n >> 16;
int fractPart = (n & 65535) * 10000 / 65536;
if(fractPart < 1)
retStr = StrParam(s:"0000");
else if(fractPart < 10)
retStr = StrParam(s:"000", i:fractPart);
else if(fractPart < 100)
retStr = StrParam(s:"00", i:fractPart);
else if(fractPart < 1000)
retStr = StrParam(s:"0", i:fractPart);
else
retStr = StrParam(i:fractPart);
if( (decPlaces > 0) && (decPlaces < 4) )
{
retStr = StrLeft(retStr, decPlaces);
}
return StrParam(i:integerPart, c:'.', s:retStr);
}
Some testing results:
Code: Select all
Log(s:"== Normal fixed print ==");
Log(f:10.5);
Log(f:0.25);
Log(f:5.001);
Log(f:32767.8523);
Log(s:"== PreciseFixedFmt ==");
Log(s:PreciseFixedFmt(10.5, 0));
Log(s:PreciseFixedFmt(0.25, 0));
Log(s:PreciseFixedFmt(5.001, 0));
Log(s:PreciseFixedFmt(32767.8523, 0));
Re: [ACS] Generally useful code snippets
Posted: Wed Aug 30, 2023 8:26 pm
by Samuzero15tlh
Some other few codes that I found, and could be useful for someone.
Spoiler: StrTrim: removes all blank spaces between the start and the end of a string. Keeping the inner spaces intact. (Open)
Code: Select all
function str StrTrim(str string){
int len = StrLen(string);
str newstring;
int i;
int leftchar;
int rightchar;
for(i = 0; i < len; i++){
if(GetChar(string, i) != ' '){
leftchar = i;
break;
}
}
// ACS Utils version.
//newstring = StrSlice(string, leftchar, len);
// Vanilla version
newstring = StrMid(string, leftchar, len - leftchar);
len = StrLen(newstring);
for(i = len-1; i >= 0; i--){
if(GetChar(newstring, i) != ' '){
rightchar = i;
break;
}
}
// ACS Utils Version.
//newstring = StrSlice(newstring, 0, rightchar+1);
// Vanilla version
newstring = StrMid(newstring , 0, rightchar+1);
return newstring;
}
Also, this code made by Trillster
Spoiler: GiveActorReplacedInventory: Simply gives the item that it has been already repleaced through decorate (Open)
Code: Select all
function int GiveActorReplacedInventory(int tid, str item, int amt)
{
int tempTid = UniqueTID();
int ret = SpawnForced(item, GetActorX(tid), GetActorY(tid), GetActorZ(tid), tempTid, 0);
if(ret)
{
GiveActorInventory(tid, GetActorClass(tempTid), amt);
Thing_Remove(tempTid);
}
return ret;
}
Re: [ACS] Generally useful code snippets
Posted: Thu Aug 31, 2023 2:32 am
by TDRR
It would be more convenient if the StrTrim function didn't require ACSUtils just for StrSlice, especially considering how trivial a function that is.
Code: Select all
newstring = StrSlice(string, leftchar, len);
Would instead be
Code: Select all
newstring = StrMid(string, leftchar, len - leftchar);
And the other StrSlice occurence doesn't even require any changes, it'll just work if changed to StrMid.
Re: [ACS] Generally useful code snippets
Posted: Thu Aug 31, 2023 2:42 pm
by Samuzero15tlh
Alright, updated, StrTrim now does'nt really need ACS Utils to work out, but still, I kept the old code just in case.
Re: [ACS] Generally useful code snippets
Posted: Thu Aug 31, 2023 5:15 pm
by Samuzero15tlh
'nother snippet!
Spoiler: StrCharCount, Counts how many characters are inside a given string, useful for counting arguments in Lump Reading. (Open)
Code: Select all
function int StrCharCount(str string, int check){
int len = StrLen(string);
int res = 0;
for(int i = 0; i < len; i++){
if(GetChar(string, i) == check){
res++;
}
}
return res;
}