[ACS] Generally useful code snippets

Looking for Resources for your mod/project? Want to share some of your creations so others can use them? Post them here!
Post Reply
User avatar
TDRR
Forum Regular
Posts: 220
Joined: Thu Jun 28, 2018 9:13 pm
Location: Venezuela
Contact:

[ACS] Generally useful code snippets

#1

Post by TDRR » Sun Aug 13, 2023 10:18 pm

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));
Image
One thing I have asked from the Lord, that I shall seek: That I may dwell in the house of the Lord all the days of my life, to behold the beauty of the Lord and to meditate in His temple. (Psalm 27:4, NASB)
My Discord tag is @tdrr, and it's my preferred contact method. I also check PMs here from time to time.

Samuzero15tlh
Forum Regular
Posts: 256
Joined: Wed Sep 09, 2015 2:21 pm
Location: In home, sweet Home
Clan Tag: <skr>

Re: [ACS] Generally useful code snippets

#2

Post by Samuzero15tlh » Wed Aug 30, 2023 8:26 pm

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;
}
Last edited by Samuzero15tlh on Thu Aug 31, 2023 2:41 pm, edited 1 time in total.
Everyone wants happiness without pain, but you cant have a rainbow without a rain.

I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.

Spoiler: My Other zandro stuff! (Open)


Samuzero15tlh

User avatar
TDRR
Forum Regular
Posts: 220
Joined: Thu Jun 28, 2018 9:13 pm
Location: Venezuela
Contact:

Re: [ACS] Generally useful code snippets

#3

Post by TDRR » Thu Aug 31, 2023 2:32 am

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.
One thing I have asked from the Lord, that I shall seek: That I may dwell in the house of the Lord all the days of my life, to behold the beauty of the Lord and to meditate in His temple. (Psalm 27:4, NASB)
My Discord tag is @tdrr, and it's my preferred contact method. I also check PMs here from time to time.

Samuzero15tlh
Forum Regular
Posts: 256
Joined: Wed Sep 09, 2015 2:21 pm
Location: In home, sweet Home
Clan Tag: <skr>

Re: [ACS] Generally useful code snippets

#4

Post by Samuzero15tlh » Thu Aug 31, 2023 2:42 pm

Alright, updated, StrTrim now does'nt really need ACS Utils to work out, but still, I kept the old code just in case.
Everyone wants happiness without pain, but you cant have a rainbow without a rain.

I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.

Spoiler: My Other zandro stuff! (Open)


Samuzero15tlh

Samuzero15tlh
Forum Regular
Posts: 256
Joined: Wed Sep 09, 2015 2:21 pm
Location: In home, sweet Home
Clan Tag: <skr>

Re: [ACS] Generally useful code snippets

#5

Post by Samuzero15tlh » Thu Aug 31, 2023 5:15 pm

'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;
}
Everyone wants happiness without pain, but you cant have a rainbow without a rain.

I'm working in the Shotgun Frenzy Plus mod in my free time.
Yes, I have a pet that helps me to build doom projects, the Pack-O-Daemon, ain't it cute?.

Spoiler: My Other zandro stuff! (Open)


Samuzero15tlh

Post Reply