var/global/automation_types = subtypesof(/datum/automation) #define AUTOM_RT_NULL 0 #define AUTOM_RT_NUM 1 #define AUTOM_RT_STRING 2 /datum/automation // Name of the Automation var/name="Base Automation" // For labelling what shit does on the AAC. var/label="Unnamed Script" var/desc ="No Description." var/obj/machinery/computer/general_air_control/atmos_automation/parent var/list/valid_child_returntypes=list() var/list/datum/automation/children=list() var/returntype=AUTOM_RT_NULL /datum/automation/New(var/obj/machinery/computer/general_air_control/atmos_automation/aa) parent=aa /datum/automation/proc/GetText() return "[type] doesn't override GetText()!" /datum/automation/proc/OnReset() return /datum/automation/proc/OnRemove() return /datum/automation/proc/process() return /datum/automation/proc/Evaluate() return 0 /datum/automation/proc/Export() var/list/R = list("type"=type) if(initial(label)!=label) R["label"]=label if(initial(desc)!=desc) R["desc"]=desc if(children.len>0) var/list/C=list() for(var/datum/automation/A in children) C += list(A.Export()) R["children"]=C return R /datum/automation/proc/unpackChild(var/list/cData) if(isnull(cData) || !("type" in cData)) return null var/Atype=text2path(cData["type"]) if(!(Atype in automation_types)) return null var/datum/automation/A = new Atype(parent) A.Import(cData) return A /datum/automation/proc/unpackChildren(var/list/childList) . = list() if(childList.len>0) for(var/list/cData in childList) if(isnull(cData) || !("type" in cData)) . += null continue var/Atype=text2path(cData["type"]) if(!(Atype in automation_types)) continue var/datum/automation/A = new Atype(parent) A.Import(cData) . += A /datum/automation/proc/packChildren(var/list/childList) . = list() if(childList.len>0) for(var/datum/automation/A in childList) if(isnull(A) || !istype(A)) . += null continue . += list(A.Export()) /datum/automation/proc/Import(var/list/json) if("label" in json) label = json["label"] if("desc" in json) desc = json["desc"] if("children" in json) children = unpackChildren(json["children"]) /datum/automation/proc/fmtString(var/str) if(str==null || str == "") return "-----" return str /datum/automation/Topic(href,href_list) if(parent.Topic("src=[parent.UID()]", list("src" = parent)))//dumb hack to check sanity, empty topic shouldn't trigger a 1 on anything but sanity checks return 1 if(href_list["add"]) var/new_child=selectValidChildFor(usr) if(!new_child) return 1 children += new_child parent.updateUsrDialog() return 1 if(href_list["remove"]) if(href_list["remove"]=="*") var/confirm=alert("Are you sure you want to remove ALL automations?","Automations","Yes","No") if(confirm == "No") return 0 for(var/datum/automation/A in children) A.OnRemove() children.Remove(A) else var/datum/automation/A=locate(href_list["remove"]) if(!A) return 1 var/confirm=alert("Are you sure you want to remove this automation?","Automations","Yes","No") if(confirm == "No") return 0 A.OnRemove() children.Remove(A) parent.updateUsrDialog() return 1 if(href_list["reset"]) if(href_list["reset"]=="*") for(var/datum/automation/A in children) A.OnReset() else var/datum/automation/A=locate(href_list["reset"]) if(!A) return 1 A.OnReset() parent.updateUsrDialog() return 1 return 0 // 1 if handled /datum/automation/proc/selectValidChildFor(var/mob/user, var/list/returntypes=valid_child_returntypes) return parent.selectValidChildFor(src, user, returntypes) /////////////////////////////////////////// // AND /////////////////////////////////////////// /datum/automation/and name = "AND statement" returntype=AUTOM_RT_NUM valid_child_returntypes=list(AUTOM_RT_NUM) Evaluate() if(children.len==0) return 0 for(var/datum/automation/stmt in children) if(!stmt.Evaluate()) return 0 return 1 GetText() var/out="AND (Add)" if(children.len>0) out += "
" else out += "No statements to evaluate." return out /////////////////////////////////////////// // OR /////////////////////////////////////////// /datum/automation/or name = "OR statement" returntype=AUTOM_RT_NUM valid_child_returntypes=list(AUTOM_RT_NUM) Evaluate() if(children.len==0) return 0 for(var/datum/automation/stmt in children) if(stmt.Evaluate()) return 1 return 0 GetText() var/out="OR (Add)" if(children.len>0) out += "" else out += "
No statements to evaluate." return out /////////////////////////////////////////// // if .. then /////////////////////////////////////////// /datum/automation/if_statement name = "IF statement" var/datum/automation/condition=null valid_child_returntypes=list(AUTOM_RT_NULL) var/list/valid_conditions=list(AUTOM_RT_NUM) var/list/children_then=list() var/list/children_else=list() Export() var/list/R = ..() if(children_then.len>0) R["then"]=packChildren(children_then) if(children_else.len>0) R["else"]=packChildren(children_else) if(condition) R["condition"]=condition.Export() return R Import(var/list/json) ..(json) if("then" in json) children_then = unpackChildren(json["then"]) if("else" in json) children_else = unpackChildren(json["else"]) if("condition" in json) condition = unpackChild(json["condition"]) GetText() var/out="IF (SET):
" if(condition) out += condition.GetText() else out += "Not set" out += "" out += "THEN: (Add)" if(children_then.len>0) out += "
(No statements to run)" out += "ELSE: (Add)" if(children_then.len>0) out += "
(No statements to run)" return out Topic(href,href_list) if(href_list["add"]) var/new_child=selectValidChildFor(usr) if(!new_child) return 1 switch(href_list["add"]) if("then") children_then += new_child if("else") children_else += new_child else warning("Unknown add value given to [type]/Topic():[__LINE__]: [href]") return 1 parent.updateUsrDialog() return 1 if(href_list["remove"]) if(href_list["remove"]=="*") var/confirm=input("Are you sure you want to remove ALL automations?","Automations","No") in list("Yes","No") if(confirm == "No") return 0 for(var/datum/automation/A in children_then) A.OnRemove() children_then.Remove(A) for(var/datum/automation/A in children_else) A.OnRemove() children_else.Remove(A) else var/datum/automation/A=locate(href_list["remove"]) if(!A) return 1 var/confirm=input("Are you sure you want to remove this automation?","Automations","No") in list("Yes","No") if(confirm == "No") return 0 A.OnRemove() switch(href_list["context"]) if("then") children_then.Remove(A) if("else") children_else.Remove(A) parent.updateUsrDialog() return 1 if(href_list["reset"]) if(href_list["reset"]=="*") for(var/datum/automation/A in children_then) A.OnReset() for(var/datum/automation/A in children_else) A.OnReset() else var/datum/automation/A=locate(href_list["reset"]) if(!A) return 1 A.OnReset() parent.updateUsrDialog() return 1 if(href_list["set_condition"]) var/new_condition = selectValidChildFor(usr,valid_conditions) testing("Selected condition: [new_condition]") if(!new_condition) return 1 condition = new_condition parent.updateUsrDialog() return 1 process() if(condition) if(condition.Evaluate()) for(var/datum/automation/stmt in children_then) stmt.process() else for(var/datum/automation/stmt in children_else) stmt.process() /////////////////////////////////////////// // compare /////////////////////////////////////////// /datum/automation/compare name = "comparison" var/comparator="Greater Than" returntype=AUTOM_RT_NUM valid_child_returntypes=list(AUTOM_RT_NUM) New(var/obj/machinery/computer/general_air_control/atmos_automation/aa) ..(aa) children=list(null,null) Export() var/list/json = ..() json["cmp"]=comparator return json Import(var/list/json) ..(json) comparator = json["cmp"] Evaluate() if(children.len<2) return 0 var/datum/automation/d_left =children[1] var/datum/automation/d_right=children[2] if(!d_left || !d_right) return 0 var/left=d_left.Evaluate() var/right=d_right.Evaluate() switch(comparator) if("Greater Than") return left>right if("Greater Than or Equal to") return left>=right if("Less Than") return left