ID:195161
 
//Title: Chore-Based Brains
//Credit to: Gughunter
//Contributed by: Jtgibson

//STANDARD FRAMEWORK CODE FOR BRAINS AND CHORES.
mob
var/chore/brain/brain

proc
AddEvent(event/myEvent)
if(brain)
brain.AddEvent(myEvent)

AddChore(myType)
if(brain)
brain.AddChore(myType)
else
brain = new myType(src)

KillChore(myType)
if(brain)
brain.KillChore(myType)

Del()
if(brain)
del(brain)
..()

obj
var/chore/brain/brain

proc
AddEvent(event/myEvent)
if(brain)
brain.AddEvent(myEvent)

AddChore(myType)
if(brain)
brain.AddChore(myType)
else
brain = new myType(src)

KillChore(myType)
if(brain)
brain.KillChore(myType)

Del()
if(brain)
del(brain)
..()


chore
var
name
chore/brain/brain
chore/parent
list/choreList[0]
uniqueness = 1 //Default is to overwrite existing chore of same type

New(chore/brain/parentBrain, chore/parentChore)
..()
brain = parentBrain
parent = parentChore
if(brain)
name = "[type] of [brain.name]"
else
name = "[type]"

Del()
var/chore/deletableChore
for(deletableChore in choreList)
del(deletableChore)
if(parent)
parent.choreList -= src
..()

proc
Think()
if(!src)
return
if(choreList.len)
var/chore/curChore
for(curChore in choreList)
if(curChore)
curChore.Think()
else
choreList -= curChore
if(src)
DoAlways()

DoAlways()
//prototype is just a stub


AddChore(myType)
var/chore/currentChore = null

//Note: this assumes all chores of the same type will have the same
//"uniqueness" value!
for(currentChore in choreList)
if(istype(currentChore, myType))
if(currentChore.uniqueness == 1)
//Uniqueness 1 replaces an existing chore of the same type
del(currentChore)
else if(currentChore.uniqueness == 2)
//Uniqueness 2 defers to an existing chore of the same type
return

var/chore/myNewChore = new myType(brain, src)
choreList += myNewChore
return myNewChore

KillChore(myType)
var/chore/currentChore = null

for(currentChore in choreList)
if(istype(currentChore, myType))
del(currentChore)

brain
var
thinkDelay = 31
obj/owner
ticks = 1
list/currentEvents[0]
list/knowledgeBase[0]


Think()
if(currentEvents.len)
HandleEvents()
currentEvents.len = 0

..()

if(src)
ticks++
spawn(thinkDelay) Think()

proc
HandleEvents()
//This is just a stub for overriding; called by Think().

AddEvent(event/myEvent)
currentEvents += myEvent


New(O)
..()
owner = O
name = "[O]'s brain"
brain = src

spawn(rand (1, thinkDelay)) Think()


lock
var
timeObtained
duration

proc
CheckLock()
if(timeObtained == null)
return 1
else if(duration == 0)
return 0
else if(timeObtained + duration <= world.time)
timeObtained = null
return 1

ObtainLock(myDuration)
if(myDuration == -1)
timeObtained = null
else if(CheckLock())
timeObtained = world.time
duration = myDuration
return 1

event
var/action
var/actor
var/target
var/content
var/time

New(myActor, myAction, myTarget, myContent)
..()
action = myAction
actor = myActor
target = myTarget
content = myContent
time = world.time

proc
NewEvent(myActor, myAction, myTarget, myContent)
return new /event(myActor, myAction, myTarget, myContent)