Jump to content
Sign in to follow this  
Rydygier

Problematic Scripts Museum presents an exhibit: Hectic Muse

Recommended Posts

Exhibit No. 00003/20141122/Ryd



HECTIC MUSE

1. The file

Package contains two .sqf files:

2. What this does?

Apparently user can paste both files into mission folder and then via init.sqf (included containing some example) pass into main function (
_new = [_string,_find,_replace,_wildcard] call RYD_HecticMuse
) any string, phrase to find in it and phrase, that should replace each found. Seems, code handles so called wildcard symbols and user can choose any symbol as wildcard following the rule, wildcard symbol must not be present in the input string. Hectic Muse returns changed string and also saves it in the clipboard. Exemplary results of conducted experiments:

_string = ":-(";	
_find = "(";
_replace = ")";
_wildcard = "?";

_new = [_string,_find,_replace,_wildcard] call RYD_HecticMuse;

//:-)

_string = "0A0 0B0 00";
_find = "0?0";
_replace = "X?X";
_wildcard = "?";

_new = [_string,_find,_replace,_wildcard] call RYD_HecticMuse;

//XAX XBX XX

_string = "ABCDE";
_find = "B?";
_replace = "?B";
_wildcard = "?";

_new = [_string,_find,_replace,_wildcard] call RYD_HecticMuse;

//ACDEB

_string = "my_fnc = 
{
private ['_arr1','_arr2','_arr3','_val1','_val2','_val3'];

_arr1 = _this select 0;
_arr2 = _this select 1;
_arr3 = _this select 2;

_val1 = 1;
_val2 = 2;
_val3 = 3;

_arr1 set [(count _arr1),_val1];
_arr2 set [(count _arr2),_val2];
_arr3 set [(count _arr3),_val3];

[_arr1,_arr2,_arr3]
};";

_find = "set [(count ?),";
_replace = "pushback ";
_wildcard = "?";

_new = [_string,_find,_replace,_wildcard] call RYD_HecticMuse;

_find = "pushback ?]";
_replace = "pushBack ?";

_new = [_new,_find,_replace,_wildcard] call RYD_HecticMuse;

/*my_fnc = 
{
private ['_arr1','_arr2','_arr3','_val1','_val2','_val3'];

_arr1 = _this select 0;
_arr2 = _this select 1;
_arr3 = _this select 2;

_val1 = 1;
_val2 = 2;
_val3 = 3;

_arr1 pushBack _val1;
_arr2 pushBack _val2;
_arr3 pushBack _val3;

[_arr1,_arr2,_arr3]
};*/

Artifact wasn't tested thoroughly enough to exclude risk of any unexpected results in some special cases - limited trust recommended, reports about such cases - appreciated. Apparently Hectic Muse is compatibile only with
"strings"
, while for
'strings'
. Because of that HC can't handle strings containing " symbols.

Content of the HM files:

init.sqf:

call compile preprocessFile "HM_fnc.sqf";//preparing

_string = "0A0 0B0 00";//any "string" not containing " nor wildcard symbols
_find = "0?0";//phrase to find
_replace = "X?X";//replacement phrase
_wildcard = "?";//chosen wildcard symbol;

_new = [_string,_find,_replace,_wildcard] call RYD_HecticMuse;//execution - result also copied into the clipboard to paste anywhere with ctrl + v

HM_fnc.sqf:

RYD_HM_FindCharIxs = 
{
private ["_string","_chars","_div","_cnt","_cntM","_cntCs","_toChange","_elToC","_bIx","_eIx","_cut","_toC","_toC2","_found","_oFound","_char","_cntC","_ix","_nIx","_stringN","_charN","_inBetween"];

_string = _this select 0;
_chars = _this select 1;
_div = _this select 2;

_cnt = count _string;
_cntM = _cnt;
_cntCs = count _chars;
_toChange = [];
_eIx = 0;
_cut = 0;

while {_cnt > 0} do
	{		
	_cnt0 = count _string;

	_eIx = 0;
	_bIx = true;
	_ix = -2;
	_toC = "";
	_toC2 = "";
	_found = 0;
	_oFound = 0;

		{
		if ((_foreachIndex == 0) or {(_found > _oFound)}) then
			{
			_oFound = _found;
			_char = _x;

			if not (_char isEqualTo _div) then
				{
				_cntC = count _char;

				_ix = _string find _char;

				if not (_ix < 0) then
					{
					_found = _found + 1;
					if (_bIx) then
						{
						_bIx = false;
						_eIx = _ix
						};

					_eIx = _eIx + _cntC;
					_toC2 = _toC2 + _char
					}
				}
			else
				{
				if not (_ix == -1) then
					{
					_found = _found + 1;
					_nIx = _cnt0;
					_stringN = _string select [_eIx];
					if (_foreachIndex < ((count _chars) - 1)) then
						{
						_charN = (_chars select (_foreachIndex + 1));

						_nIx = _stringN find _charN
						};

					_inBetween = _string select [_eIx,_nIx];

					_toC = _toC + _inBetween;
					_toC2 = _toC2 + _inBetween;
					_eIx = _eIx + (count _inBetween);
					}
				}
			}
		}
	foreach _chars;

	_string = _string select [_eIx];
	_cnt = count _string;
	_cut = _cntM - _cnt;

	if (_cnt0 == _cnt) exitWith {};

	if (_found == _cntCs) then
		{
		_toChange pushBack [_toC,_cut - (count _toC2)]
		};
	};

_toChange
};

RYD_HM_SubDivStr = 
{
private ["_str","_div","_subs","_sub","_cntF","_lastWild","_char"];

_str = _this select 0;
_div = _this select 1;

_subs = [];
_sub = "";
_cntF = count _str;
_lastWild = false;

for "_i" from 0 to (_cntF - 1) do
	{
	_char = _str select [_i,1];

	if not (_char isEqualTo _div) then
		{
		_sub  = _sub + _char;			
		_lastWild = false;
		}
	else
		{
		if not (_sub isEqualTo "") then
			{
			_subs pushBack _sub;
			};

		_subs pushBack _div;
		_sub = "";
		_lastWild = true;
		}		
	};

if not (_lastWild) then
	{
	_subs pushBack _sub
	};

_subs
};

RYD_HM_Replace = 
{
private ["_string","_rMap","_baseC","_replacement","_div","_newString","_bIx","_wc","_repIx","_repLng","_stringArr","_inject"];

_string = _this select 0;
_rMap = _this select 1;
_baseC = _this select 2;
_replacement = _this select 3;
_div = _this select 4;

_newString = "";

_bIx = 0;

	{
	_wc = _x select 0;

	_repIx = _x select 1;
	_repLng = _baseC + (count _wc);

	_stringB = _string select [_bIx,_repIx - _bIx];

	_bIx = _repIx + _repLng;

	_inject = "";

		{
		if not (_x isEqualTo _div) then
			{
			_inject = _inject + _x;
			}
		else
			{
			_inject = _inject + _wc;
			}
		}
	foreach _replacement;

	_newString = _newString + _stringB + _inject
	}
foreach _rMap;

_newString = _newString + (_string select [_bIx]);

_newString
};

RYD_HecticMuse = 
{	
private ["_string","_find","_replace","_spc","_fLng","_toReplace","_map"];

_string = _this select 0;
_find = _this select 1;
_replace = _this select 2;
_spc = _this select 3;

_find = [_find,_spc] call RYD_HM_SubDivStr;

_baseC = 0;

	{
	if not (_x isEqualTo _spc) then
		{
		_baseC = _baseC + (count _x)
		}
	}
foreach _find;

_replace = [_replace,_spc] call RYD_HM_SubDivStr;

_toReplace = [_string,_find,_spc] call RYD_HM_FindCharIxs;

_string = [_string,_toReplace,_baseC,_replace,_spc] call RYD_HM_Replace;

copyToClipboard _string;

_string
};

3. Purpose

Although artifact undoubtfully was created by some lazy culture, since nowadays there are known to mankind
to easily achieve same result, Hectic Muse artifact usefulness is questionable.

This exhibition has been prepared by courtesy of:
Problematic Scripts Museum
. Exhibits so far:

Script you can freely modify, copy, "cannibalize", to use in your projects. It is released under APL-SA license. I'll be grateful for notification about each such usage.

Edited by Rydygier

Share this post


Link to post
Share on other sites

My brain cells are melting trying to understand the purpose of this script.

Love your other scripts, and pretty sure the same goes for this one, once figured out, heh.

Share this post


Link to post
Share on other sites

Interesting script. I suppose this is what you mentioned you would work on in the other thread? I'm going to test this out later today, if I find anything peculiar I'll report back :)

Share this post


Link to post
Share on other sites

Yes, that's what I mentioned. Thanks. :)

My brain cells are melting trying to understand the purpose of this script.

What can I say? Welcome to the Problematic Scripts Museum. :) Perfect place for scripts I consider anyhow interesting, maybe even inspring, thus worthy of showing, still of unclear use.

But seriously, although finding them may prove to be problematic, some uses are possible... possibly. If you for example have a big script with many similar changes to make, tedious to do manually, that simple "find&replace all" cannot handle for you, this may help, just like in the last example. As mentioned, other, better ways for that are possible though. But apart from that, this script may be used also in-game, assuming anyone would need such kind of string manipulation in-game. Perhaps even someone could think about utilizing it to change and then recompiling scripts by scripts if any sense in that.

Besides all that, for me it was good opportunity to test new string commands (appear pretty fast - HM was able to perform all changes in 5000 lines long sqf well below 0.2 sec.) and train kind of scripting, that although not big in resulting size and looking not so complex, always was difficult to me due to used kind of logic, that often somehow makes my poor mind lost in the looped maze.

Edited by Rydygier

Share this post


Link to post
Share on other sites
Guest

Thanks for the headsup about the release :cool:

Release frontpaged on the Armaholic homepage.

================================================

We have also "connected" these pages to your account on Armaholic.

This means soon you will be able to maintain these pages yourself if you wish to do so. Once this new feature is ready we will contact you about it and explain how things work and what options you have.

When you have any questions already feel free to PM or email me!

Share this post


Link to post
Share on other sites

I only did a quick test but this is pretty awesome. I'm a bit too wasted atm to come up with good examples but with a little ingenuity, you could make sort of a dynamic conversation bot with this if it returns the captured part along with altered string.

Just pop this in a mission init.sqf, toss in a player and stare at the creepy conversation :P

call compile preprocessFile "HM_fnc.sqf";//preparing

waitUntil {isPlayer Player};

_sleepTime = 3;

_string = "I'm looking for a place to stay";	//any "string" not containing " nor wildcard symbols
hintSilent _string;
sleep _sleepTime;

_find = "place?stay";//phrase to find
_replace = "reason?replace you";//replacement phrase
_wildcard = "?";//chosen wildcard symbol;
_new = [_string,_find,_replace,_wildcard] call RYD_HecticMuse;//execution - res

hintSilent _new;
sleep _sleepTime;

_string = "I'm telling you, I don't need replacement";	//any "string" not containing " nor wildcard symbols
hintSilent _string;
sleep _sleepTime;

_find = "I'm#you";//phrase to find
_replace = "You're#me? Seems to me like I'm#you";//replacement phrase
_wildcard = "#";//chosen wildcard symbol;
_new = [_string,_find,_replace,_wildcard] call RYD_HecticMuse;//execution - res

_find = "I don't#";
_replace = "you do#";
_new = [_new,_find,_replace,_wildcard] call RYD_HecticMuse;//execution - res

hintSilent _new;

Share this post


Link to post
Share on other sites

:D Nice.

Problematic or not, for some cool stuff may be used apparently.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×