ID:189784
 
//Allows the embedding of variables during runtime. If you think about it that has tons of
//neat possibilities :). (I only thought of a couple due to a short attention span, but
//perhaps you could think of more)

proc/K_EmbedVars(T as text,/mob/src)
if(!src) src=usr
var/open ; var/close ; var/count ; var/lastfound

var/found=findtext(T,"\[")
var/workwithme
while(found)
open=findtext(T,"\[",lastfound+1)
close=findtext(T,"]",lastfound+1)
if(!workwithme) workwithme=open
if(!workwithme) break
if(close)
if(open)
if(close<open && count>0)
count--
lastfound=close
else if(count>0)
count--
lastfound=close
if(open)
if(close)
if(open<close)
count++
lastfound=open
else
count++
lastfound=open
if(!count)
if(close>workwithme)
T="[copytext(T,1,workwithme)][K_EMBED(copytext(T,workwithme+1,close),src)][copytext(T,close+1)]"
workwithme=0
return T



proc/K_EMBED(T as text,mob/src)
if(!src) src=usr
src=src
var/list/layers=new
var/pos
var/revert
var/savetext=T

pos=findtext(T,".")
while(pos)
layers+=copytext(T,1,pos)
T=copytext(T,pos+1)
pos=findtext(T,".")
layers+=T

var/datum/lookup=src
for(var/X=1,X<=length(layers),X++)
var/brack
var/text
pos=findtext(layers[X],"\[")
if(pos && findtext(layers[X],"\[",pos))
brack=copytext(layers[X],pos+1,findtext(layers[X],"]"))
text=copytext(layers[X],1,pos)
else text = layers[X]

for(var/cur in lookup:vars)
if(text=="[cur]")
if(X==length(layers))
if(islist(lookup:vars[cur]) && brack)
var/list/templist=lookup:vars[cur]
for(var/XX in templist)
if("[templist.Find(XX)]"==brack)
T=XX
else
T=lookup:vars[cur]
else
if(islist(lookup:vars[cur])) revert=1
else if(istext(lookup:vars[cur])) revert=1
else lookup=lookup:vars[cur]
break

if(revert) break
if(!T) revert=1
if(revert) T="\[[savetext]]"
return T




//This simple little one was created by me just for parsing down types.
//Example: K_ParseType(/mob/tank/M1Tank) returns M1Tank

proc/K_ParseType(text)
text="[text]"
var/slash_pos = findtext(text,"/")
while(slash_pos)
text=copytext(text,slash_pos+1)
slash_pos = findtext(text,"/")
return text

//The counterpart to K_ParseType. This proc takes an object type and returns it's parent. Useful
//if using a type path instead of something already defined with the parent_type variable.
proc/K_GetParent(text)
text="[text]"
var/slash_pos
var/last_slash
while(1)
last_slash= slash_pos
slash_pos = findtext(text,"/",last_slash+1)
if(slash_pos) last_slash=slash_pos
else return copytext(text,1,last_slash)



//This procedure allows a target to input from a list containing a given parent and it's
//children. Useful for automatically cutting down big type lists into a series of smaller lists.
//Is only helpful if you make use of subtypes appropriately.

proc/K_InputType(mob/source,basetype,title)
var/temptext
var/list/L=new
var/list/Ltext=new
var/temptype=basetype
var/lastype=basetype
while(1)
L.Cut() ; Ltext.Cut()
L.Add(temptype)
for(var/X in typesof(temptype)) if(K_GetParent(X)=="[temptype]") L.Add(X)
if(temptype!=basetype) L.Add(K_GetParent(temptype))
for(var/X in L) Ltext.Add(K_ParseType(X))

lastype=temptype
temptext=input(source,"Current type is: [temptype]",title) in Ltext
temptype=L[Ltext.Find(temptext)]

if(temptype==lastype) return temptype

//This procedure checks to see if the variable in question is a list, returning 1 if it is.
//It is simple, but Im sure a few could make use of it.

proc/islist(var/X)
if(X!="/list" && "[X]"=="/list") return 1



I hope you decide to add some/all of these. :) If not, I suppose I'll post them myself somewhere.

-Koshigia
If theres any bugs, btw, I am willing to work them out myself of course and resubmit ASAP.
Thanks!

Why not put those in a library of your own? They don't look reliant on the TextHandling library, or in quite the same subject area, but they should be useful.

A challenge:

Using the dd_text2list() proc, you should be able to do K_ParseType() and K_GetParent() in just a couple of lines of code...