#nop -------------------------------------------------------------------------
#nop Learn by example
#nop -------------------------------------------------------------------------

#nop -------------------------------------------------------------------------
#nop Loop through room 1 to 1000 and change the color of rooms with the
#nop static (16) flag to <168>.
#nop -------------------------------------------------------------------------

#loop 1 1000 vnum
{
	#map at $vnum
	{
		#map get roomflags result;
		#if {$result & 16}
		{
			#map set roomcolor <168>
		}
	}
}

#nop -------------------------------------------------------------------------
#nop Capture system information. #script stores the output as a list,
#nop hence the need to convert it into a normal variable.
#nop -------------------------------------------------------------------------

#script {dir} {pwd}
#var dir $dir[1]

#script {home} {echo $HOME}
#var home $home[1]

#nop -------------------------------------------------------------------------
#nop Automatically reconnect on disconnect.
#nop -------------------------------------------------------------------------

#event {SESSION CONNECTED}
{
	#event {SESSION DISCONNECTED}
	{
		#gts #delay 5 {#session %0 %1 %3}
	}
}

#nop -------------------------------------------------------------------------
#nop Execute a random social at random time intervals.
#nop -------------------------------------------------------------------------

#tick {randomsocial}
{
	#delay {1d180}
	{
		#switch {1d4}
		{
			#case {1} {cheer}
			#case {2} {greet all}
			#case {3} {smile}
			#case {4} {laugh self}
		}
	}
}
{200}

#nop -------------------------------------------------------------------------
#nop Maintain a friendlist. %i creates a case insensitive regex.
#nop -------------------------------------------------------------------------

#variable {friendlist}
{
	{bubba};{pamela};{cookie};{harry potter}
}

#function isfriend
{
	#return &friendlist[%i%0];
}

#act {%1 follows you.}
{
	#if {@isfriend{%1}}
	{
		group %1
	};
	#else
	{
		unfollow %1
	}
}

#alias {addfriend}
{
	#format name %l {%0};

	#var friendlist[$name] {};

	#showme $name has been added to your friendlist.
}

#alias {delfriend}
{
	#format name %l {%0};

	#if {@isfriend{$name}}
	{
		#unvar friendlist[$name];
		#showme $name has been deleted from your friendlist.
	};
	#else
	{
		#showme $name is not on your friendlist.
	}
}

#nop -------------------------------------------------------------------------
#nop Append a goto to your current room when saving a map. You can use
#nop #map return instead.
#nop -------------------------------------------------------------------------

#alias {savemap}
{
	#map write %0;
	#map get roomvnum room;
	#system echo '#map goto $room' >> %0
}

#nop -------------------------------------------------------------------------
#nop Log all text to a file with a timestamp with decisecond precision.
#nop -------------------------------------------------------------------------

#function {timestamp}
{
	#format utime {%U};

	#format result {%t.%m} {%Y-%m-%d %H:%M:%S} {$utime % 1000000 / 100000}
}

#event {RECEIVED LINE}
{
	#line log mylog.txt {<178>@timestamp{} \};
	#line log mylog.txt
}

#nop -------------------------------------------------------------------------
#nop Old school tick support.
#nop -------------------------------------------------------------------------

#tick {oldtick}
{
	#delay 50 #showme #10 SECONDS TO TICK!!;
	#showme #TICK!!!
}
{60}

#nop -------------------------------------------------------------------------
#nop Execute speedwalks with .
#nop -------------------------------------------------------------------------

#alias {.%0}
{
	#var cnt {};

	#parse {%0} {char}
	{
		#if {"$char" >= "0" && "$char" <= "9"}
		{
			#var cnt $cnt$char
		};
		#elseif {"$cnt" == ""}
		{
			#send $char
		};
		#else
		{
			#$cnt #send $char;
			#var cnt {}
		}
	}
}

#nop -------------------------------------------------------------------------
#nop Targetting script
#nop -------------------------------------------------------------------------

#var targets {}

#alias {target}
{
	#if {"%0" == ""}
	{
		#showme {Current targets: $targets[]}
	};
	#elseif {&targets[%0]}
	{
		#unvar targets[%0];
		#showme Target '%0' removed.
	};
	#else
	{
		#var targets[%0] {};
		#showme Target '%0' added.
	}
}

#act {%1 arrives}
{
	#if {&targets[%1]} {kill %1}
}

#act {%1 is standing here}
{
	#if {&targets[%1]} {kill %1}
}

#action {%1 is dead! R.I.P.}
{
	#if {&targets[%1]} {target %1}
}

#nop -------------------------------------------------------------------------
#nop Show xterm 256 colors.
#nop -------------------------------------------------------------------------

#var temp {}

#foreach {0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15} {var1}
{
	#showme {$var1 \e[38;5;${var1}m}
}

#foreach {a;b;c;d;e;f} {var1}
{
	#foreach {a;b;c;d;e;f} {var2}
	{
		#foreach {a;b;c;d;e;f} {var3}
		{
			#var temp {$temp <$var1$var2$var3><<888>$var1$var2$var3>}
		};
		#showme $temp;
		#var temp {}
	}
}

#loop 0 23 cnt
{
	#format temp {$temp <g%+02s><<888>g%+02s} {$cnt} {$cnt};
}

#showme $temp

#nop -------------------------------------------------------------------------
#nop Draw a health bar.
#nop -------------------------------------------------------------------------

#alias {hpbar}
{
	#math {hp_percent}{100 * %1 / %2};
	#math {hpbars1}   {$hp_percent / 5};
	#math {hpbars2}   {20 - $hpbars1};

	#format {hpbar} {<011>%+${hpbars1}s<099><000>%+${hpbars2}s<099> };

	#showme [$hpbar]
}

#alias {test}
{
	hpbar 30 100
}

#nop -------------------------------------------------------------------------
#nop Syntax: sleep <seconds to delay> {commands}
#nop
#nop If there is already a pending sleep the delay will be stacked.
#nop -------------------------------------------------------------------------

#var sleeptime 0
#var sleepcurr 0

#alias {sleep %1 %2}
{
	#format sleeptime %U;

	#if {$sleeptime > $sleepcurr}
	{
		#math sleepcurr $sleeptime + (%1) * 1000000;

		#delay {%1} %2;
	};
	#else
	{
		#math sleepcurr $sleepcurr + (%1) * 1000000;
	
		#delay {($sleepcurr - $sleeptime) / 1000000.000} %2
	}
}

#nop -------------------------------------------------------------------------
#nop This function and substitution will highlight spelling errors as red.
#nop -------------------------------------------------------------------------

#function spellcheck
{
	#format result %S %1;
	#if {$result == 0}
	{
		#var result %1
	};
	#else
	{
		#var result <118>%1<278>
	}
}

#substitute {{\b[a-zA-Z]+\b}} {@spellcheck{%1}}

#nop -------------------------------------------------------------------------
#nop This function tests the random number engine
#nop -------------------------------------------------------------------------

#alias random
{
	#var random {};
	#loop 1 1000 cnt
	{
		#math tmp 1d1000000000 % 10;
		#math random[$tmp] $random[$tmp] + 1
	};
	#var random
}

#nop -------------------------------------------------------------------------
#nop This macro allows pasting multi-line code fragments on pressing ctrl-v
#nop -------------------------------------------------------------------------

#macro {\cv}
{
	#cursor {convert meta} on;
	#line oneshot #event {CATCH RECEIVED INPUT}
	{
		#line sub {esc} #var paste {%0};
		#replace paste {\\n\\n} {;};
		#replace paste {\\n} {};
		#replace paste {\\t} {};
		#replace paste {;;} {;};
		#1 {$paste}
	}
}

#nop -------------------------------------------------------------------------
#nop This macro allows pasting multi-line code fragments on pressing ctrl-v
#nop followed by pressing ctrl-enter
#nop -------------------------------------------------------------------------

#macro {\e[13;5u}
{
	#cursor get tmp;
	#cursor home;
	#cursor set {#line sub esc };
	#cursor enter
}

#nop -------------------------------------------------------------------------
#nop This event will cause tintin to always report your screen width as 80
#nop columns
#nop -------------------------------------------------------------------------

#event {CATCH IAC DO NAWS}
{
	#screen get rows ROWS;
	#format ROWS %a $ROWS;

	#send {\xFF\xFB\x1F\xFF\xFA\x1F\x50\x00${ROWS}\x00\xFF\xF0\}
}

#nop -------------------------------------------------------------------------
#nop Remove duplicate lines and add a counter. Does not work in gts because
#nop it uses named delays.
#nop -------------------------------------------------------------------------

#var repeat[str] {}
#var repeat[cnt] 1

#act {~%+}
{
	#if {{%0} === {$repeat[str]}}
	{
		#math repeat[cnt] $repeat[cnt] + 1;
		#delay {repeat} {repeat_show} {0}
	};
	#else
	{
		repeat_show;
		#var repeat[str] {%0}
	};
	#line gag
}

#alias {repeat_check}
{
	#if {$repeat[cnt] <= 1}
	{
		#line ignore #showme {$repeat[str]}
	};
	#else
	{
		#line ignore #showme {($repeat[cnt]) %0}
	};
	#var repeat[str] {};
	#var repeat[cnt] 1
}

#nop -------------------------------------------------------------------------
#nop These macros will allow you to move around with the arrow keys while
#nop holding down the control key. You can move ne by pressing arrow up +
#nop right simultaniously. Move up by pressing arrow up + down simultaniously.
#nop Move down by pressing arrow left + right simultaniously.
#nop -------------------------------------------------------------------------

#macro {\e[1;5A} {#cursor preserve;#delay {move} {#cursor reset_macro;n} {0.05}}
#macro {\e[1;5C} {#cursor preserve;#delay {move} {#cursor reset_macro;e} {0.05}}
#macro {\e[1;5B} {#cursor preserve;#delay {move} {#cursor reset_macro;s} {0.05}}
#macro {\e[1;5D} {#cursor preserve;#delay {move} {#cursor reset_macro;w} {0.05}}

#macro {\e[1;5A\e[1;5A} {#undelay {move};#cursor reset_macro;n;n}
#macro {\e[1;5C\e[1;5C} {#undelay {move};#cursor reset_macro;e;e}
#macro {\e[1;5B\e[1;5B} {#undelay {move};#cursor reset_macro;s;s}
#macro {\e[1;5D\e[1;5D} {#undelay {move};#cursor reset_macro;w;w}

#macro {\e[1;5A\e[1;5B} {#undelay {move};#cursor reset_macro;u}
#macro {\e[1;5B\e[1;5A} {#undelay {move};#cursor reset_macro;u}

#macro {\e[1;5C\e[1;5D} {#undelay {move};#cursor reset_macro;d}
#macro {\e[1;5D\e[1;5C} {#undelay {move};#cursor reset_macro;d}

#macro {\e[1;5A\e[1;5C} {#undelay {move};#cursor reset_macro;ne}
#macro {\e[1;5C\e[1;5A} {#undelay {move};#cursor reset_macro;ne}

#macro {\e[1;5B\e[1;5C} {#undelay {move};#cursor reset_macro;se}
#macro {\e[1;5C\e[1;5B} {#undelay {move};#cursor reset_macro;se}

#macro {\e[1;5D\e[1;5B} {#undelay {move};#cursor reset_macro;sw}
#macro {\e[1;5B\e[1;5D} {#undelay {move};#cursor reset_macro;sw}

#macro {\e[1;5D\e[1;5A} {#undelay {move};#cursor reset_macro;nw}
#macro {\e[1;5A\e[1;5D} {#undelay {move};#cursor reset_macro;nw}

#nop -------------------------------------------------------------------------
#nop Place tells in the top 5 lines of the screen
#nop -------------------------------------------------------------------------

#VARIABLE {COMMS} {}

#ACTION {~%1 tells you %2}
{
	addtowin %1 tells you %2
}

#ACTION {~%1 chats %2}
{
	addtowin %1 chats %2
}

#ALIAS {addtowin}
{
	#format temp {%w} {%0};

	#loop {1} {&temp[]} {cnt}
	{
		#list COMMS ins -1 {$temp[$cnt]}
	};
	#while {&COMMS[] > 100}
	{
		#list COMMS del 1
	};
	showwin
}

#ALIAS {showwin}
{
	#screen clear square 1 1 5 -1;

	#list temp create $COMMS[-5..-1];

	#loop {1} {&temp[]} {cnt}
	{
		#regexp {$temp[$cnt]} {^$}
		{
			#nop
		};
		#else
		{
			#line ignore #showme {$temp[$cnt]} {$cnt} {1}
		}
	}
}

#ALIAS {test}
{
	#split 5 1;
	#showme <138>Bubba tells you 'hello';
	#showme <158>Pamela chats 'bye';
}
