1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/// Some default stacks.
.entity verbs_idle;
.entity verbs_attack;
.entity verbs_move;
/// This global gets set to the verb in question each time the stack manager calls verb_call
entity verb;
/// Execure this verb
#define VCM_DO 0
/// Return the value of this verb. Return VS_CALL_REMOVE to delete it.
#define VCM_EVAL 1
/// This verb is beeing removed NOW (not sent when verb_call returns VS_CALL_REMOVE)
#define VCM_REMOVE 2
/// Verb callback
.float(float message) verb_call;
/// Points to this verb's stack.
.entity verbstack;
/// Static value of this verb
.float verb_static_value;
/// verb_call returns this when a verb in not doable
#define VS_CALL_NO 0
/// verb_call(VCM_DO) returns this when a verb is executing
#define VS_CALL_YES_DOING -1
/// verb_call(VCM_DO) returns this when a verb did execure and is done
#define VS_CALL_YES_DONE -2
/// verb_call(VCM_DO) returns this when a verb should be deleted by the stack manager
#define VS_CALL_REMOVE -3
/**
Push a new verb onto the specified stack. Set vrb_life to make it time-limited.
**/
entity verbstack_push(entity stack, float(float eval) vrb_call, float val_static, float vrb_life,entity verb_owner)
{
entity vrb;
if not(stack)
return world;
if not(vrb_call)
return world;
vrb = spawn();
vrb.owner = verb_owner;
vrb.verbstack = stack;
vrb.verb_call = vrb_call;
vrb.verb_static_value = val_static;
if(vrb_life)
{
vrb.think = SUB_Remove;
vrb.nextthink = time + vrb_life;
}
return vrb;
}
/**
Find the best verb in this stack and execurte it.
ALso remove any verbs returning VS_CALL_REMOVE on VCM_EVAL or VCM_DO
**/
float verbstack_pop(entity stack)
{
entity vrb, bestverb, oldself;
float value, bestvalue;
oldself = self;
vrb = findchainentity(verbstack,stack);
while(vrb)
{
verb = vrb;
vrb = vrb.chain;
self = verb.owner;
value = verb.verb_call(VCM_EVAL);
if(value < 0)
{
if(value == VS_CALL_REMOVE)
remove(verb);
}
else
{
if(value > bestvalue)
{
bestverb = verb;
bestvalue = value;
}
}
}
if(bestverb)
{
verb = bestverb;
self = verb.owner;
value = verb.verb_call(VCM_DO);
if(value == VS_CALL_REMOVE)
remove(bestverb);
}
self = oldself;
return value;
}
float verbstack_popfifo(entity stack)
{
entity oldself;
float ret;
oldself = self;
verb = findentity(stack,verbstack,stack);
if not (verb)
ret = 0;
else
{
self = verb.owner;
ret = verb.verb_call(VCM_DO);
if(ret == VS_CALL_REMOVE)
remove(verb);
}
self = oldself;
return ret;
}
/**
Find the best verb in this stack and return it.
ALso remove any verbs returning VS_CALL_REMOVE on VCM_EVAL.
**/
entity verbstack_pull(entity stack)
{
entity vrb;
entity bestverb, oldself;
float value, bestvalue;
oldself = self;
vrb = findchainentity(verbstack,stack);
while(vrb)
{
self = vrb.owner;
verb = vrb;
vrb = vrb.chain;
value = verb.verb_call(VCM_EVAL);
if(value > 0)
{
if(value == VS_CALL_REMOVE)
remove(verb);
}
else
{
if(value > bestvalue)
{
bestverb = verb;
bestvalue = value;
}
}
}
self = oldself;
return bestverb;
}
entity verbstack_pullfifo(entity stack)
{
return findentity(stack,verbstack,stack);
}
/**
Delete every verb on this stack, signaling them with VCM_REMOVE first.
**/
void verbstack_flush(entity stack)
{
entity vrb, oldself;
oldself = self;
vrb = findchainentity(verbstack,stack);
while(vrb)
{
self = vrb.owner;
verb = vrb;
vrb = vrb.chain;
verb.verb_call(VCM_REMOVE);
remove(verb);
}
self = oldself;
}
|