| 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 |
#include <stdlib.h> |
| 51 |
#include <string.h> |
| 52 |
#include "triggers.h" |
| 53 |
#include "map.h" |
| 54 |
#include "net_util.h" |
| 55 |
#include "objs.h" |
| 56 |
#include "sounds.h" |
| 57 |
#include "utils.h" |
| 58 |
|
| 59 |
CArray gWatches; |
| 60 |
static int watchIndex = 1; |
| 61 |
|
| 62 |
|
| 63 |
#define CANNOT_ACTIVATE_LOCK 50 |
| 64 |
|
| 65 |
|
| 66 |
Trigger *TriggerNew(void) |
| 67 |
{ |
| 68 |
Trigger *t; |
| 69 |
CCALLOC(t, sizeof *t); |
| 70 |
t->isActive = 1; |
| 71 |
CArrayInit(&t->actions, sizeof(Action)); |
| 72 |
return t; |
| 73 |
} |
| 74 |
void TriggerTerminate(Trigger *t) |
| 75 |
{ |
| 76 |
CArrayTerminate(&t->actions); |
| 77 |
CFREE(t); |
| 78 |
} |
| 79 |
Action *TriggerAddAction(Trigger *t) |
| 80 |
{ |
| 81 |
Action a; |
| 82 |
memset(&a, 0, sizeof a); |
| 83 |
CArrayPushBack(&t->actions, &a); |
| 84 |
return CArrayGet(&t->actions, t->actions.size - 1); |
| 85 |
} |
| 86 |
|
| 87 |
TWatch *WatchNew(void) |
| 88 |
{ |
| 89 |
TWatch t; |
| 90 |
memset(&t, 0, sizeof(TWatch)); |
| 91 |
t.index = watchIndex++; |
| 92 |
CArrayInit(&t.actions, sizeof(Action)); |
| 93 |
CArrayInit(&t.conditions, sizeof(Condition)); |
| 94 |
t.active = false; |
| 95 |
CArrayPushBack(&gWatches, &t); |
| 96 |
return CArrayGet(&gWatches, gWatches.size - 1); |
| 97 |
} |
| 98 |
Condition *WatchAddCondition( |
| 99 |
TWatch *w, const ConditionType type, const int counterMax, |
| 100 |
const struct vec2i pos) |
| 101 |
{ |
| 102 |
Condition c; |
| 103 |
memset(&c, 0, sizeof c); |
| 104 |
c.Type = type; |
| 105 |
c.CounterMax = counterMax; |
| 106 |
c.Pos = pos; |
| 107 |
CArrayPushBack(&w->conditions, &c); |
| 108 |
return CArrayGet(&w->conditions, w->conditions.size - 1); |
| 109 |
} |
| 110 |
Action *WatchAddAction(TWatch *w) |
| 111 |
{ |
| 112 |
Action a; |
| 113 |
memset(&a, 0, sizeof a); |
| 114 |
CArrayPushBack(&w->actions, &a); |
| 115 |
return CArrayGet(&w->actions, w->actions.size - 1); |
| 116 |
} |
| 117 |
|
| 118 |
static void ActivateWatch(int idx) |
| 119 |
{ |
| 120 |
CA_FOREACH(TWatch, w, gWatches) |
| 121 |
if (w->index == idx) |
| 122 |
{ |
| 123 |
w->active = true; |
| 124 |
|
| 125 |
|
| 126 |
for (int j = 0; j < (int)w->conditions.size; j++) |
| 127 |
{ |
| 128 |
Condition *c = CArrayGet(&w->conditions, j); |
| 129 |
c->Counter = 0; |
| 130 |
} |
| 131 |
return; |
| 132 |
} |
| 133 |
CA_FOREACH_END() |
| 134 |
CASSERT(false, "Cannot find watch"); |
| 135 |
} |
| 136 |
|
| 137 |
static void DeactivateWatch(int idx) |
| 138 |
{ |
| 139 |
CA_FOREACH(TWatch, w, gWatches) |
| 140 |
if (w->index == idx) |
| 141 |
{ |
| 142 |
w->active = false; |
| 143 |
return; |
| 144 |
} |
| 145 |
CA_FOREACH_END() |
| 146 |
CASSERT(false, "Cannot find watch"); |
| 147 |
} |
| 148 |
|
| 149 |
void WatchesInit(void) |
| 150 |
{ |
| 151 |
CArrayInit(&gWatches, sizeof(TWatch)); |
| 152 |
} |
| 153 |
void WatchesTerminate(void) |
| 154 |
{ |
| 155 |
CA_FOREACH(TWatch, w, gWatches) |
| 156 |
CArrayTerminate(&w->conditions); |
| 157 |
CArrayTerminate(&w->actions); |
| 158 |
CA_FOREACH_END() |
| 159 |
CArrayTerminate(&gWatches); |
| 160 |
} |
| 161 |
|
| 162 |
static void ActionRun(Action *a, CArray *mapTriggers) |
| 163 |
{ |
| 164 |
switch (a->Type) |
| 165 |
{ |
| 166 |
case ACTION_NULL: |
| 167 |
return; |
| 168 |
|
| 169 |
case ACTION_SETTRIGGER: |
| 170 |
for (int i = 0; i < (int)mapTriggers->size; i++) |
| 171 |
{ |
| 172 |
Trigger *tr = *(Trigger **)CArrayGet(mapTriggers, i); |
| 173 |
if (tr->id == a->u.index) |
| 174 |
{ |
| 175 |
tr->isActive = 1; |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
break; |
| 180 |
|
| 181 |
case ACTION_CLEARTRIGGER: |
| 182 |
for (int i = 0; i < (int)mapTriggers->size; i++) |
| 183 |
{ |
| 184 |
Trigger *tr = *(Trigger **)CArrayGet(mapTriggers, i); |
| 185 |
if (tr->id == a->u.index) |
| 186 |
{ |
| 187 |
tr->isActive = 0; |
| 188 |
break; |
| 189 |
} |
| 190 |
} |
| 191 |
break; |
| 192 |
|
| 193 |
case ACTION_EVENT: |
| 194 |
GameEventsEnqueue(&gGameEvents, a->u.Event); |
| 195 |
break; |
| 196 |
|
| 197 |
case ACTION_ACTIVATEWATCH: |
| 198 |
ActivateWatch(a->u.index); |
| 199 |
break; |
| 200 |
|
| 201 |
case ACTION_DEACTIVATEWATCH: |
| 202 |
DeactivateWatch(a->u.index); |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
static bool ConditionsMet(CArray *conditions, const int ticks) |
| 208 |
{ |
| 209 |
bool allConditionsMet = true; |
| 210 |
for (int i = 0; i < (int)conditions->size; i++) |
| 211 |
{ |
| 212 |
Condition *c = CArrayGet(conditions, i); |
| 213 |
bool conditionMet = false; |
| 214 |
switch (c->Type) |
| 215 |
{ |
| 216 |
case CONDITION_TILECLEAR: |
| 217 |
conditionMet = TileIsClear(MapGetTile(&gMap, c->Pos)); |
| 218 |
break; |
| 219 |
} |
| 220 |
if (conditionMet) |
| 221 |
{ |
| 222 |
c->Counter += ticks; |
| 223 |
allConditionsMet = |
| 224 |
allConditionsMet && c->Counter >= c->CounterMax; |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
c->Counter = 0; |
| 229 |
allConditionsMet = false; |
| 230 |
} |
| 231 |
} |
| 232 |
return allConditionsMet; |
| 233 |
} |
| 234 |
|
| 235 |
bool TriggerTryActivate(Trigger *t, const int flags, const struct vec2i tilePos) |
| 236 |
{ |
| 237 |
const bool canActivate = |
| 238 |
t->isActive && (t->flags == 0 || (t->flags & flags)); |
| 239 |
if (canActivate) |
| 240 |
{ |
| 241 |
GameEvent e = GameEventNew(GAME_EVENT_TRIGGER); |
| 242 |
e.u.TriggerEvent.ID = t->id; |
| 243 |
e.u.TriggerEvent.Tile = Vec2i2Net(tilePos); |
| 244 |
GameEventsEnqueue(&gGameEvents, e); |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
if (t->cannotActivateLock > 0) |
| 249 |
{ |
| 250 |
t->cannotActivateLock--; |
| 251 |
} |
| 252 |
} |
| 253 |
return canActivate; |
| 254 |
} |
| 255 |
|
| 256 |
bool TriggerCannotActivate(const Trigger *t) |
| 257 |
{ |
| 258 |
return t->cannotActivateLock == 0; |
| 259 |
} |
| 260 |
|
| 261 |
void TriggerSetCannotActivate(Trigger *t) |
| 262 |
{ |
| 263 |
t->cannotActivateLock = CANNOT_ACTIVATE_LOCK; |
| 264 |
} |
| 265 |
|
| 266 |
void TriggerActivate(Trigger *t, CArray *mapTriggers) |
| 267 |
{ |
| 268 |
CA_FOREACH(Action, a, t->actions) |
| 269 |
ActionRun(a, mapTriggers); |
| 270 |
CA_FOREACH_END() |
| 271 |
} |
| 272 |
|
| 273 |
void UpdateWatches(CArray *mapTriggers, const int ticks) |
| 274 |
{ |
| 275 |
CA_FOREACH(TWatch, w, gWatches) |
| 276 |
if (!w->active) continue; |
| 277 |
if (ConditionsMet(&w->conditions, ticks)) |
| 278 |
{ |
| 279 |
for (int j = 0; j < (int)w->actions.size; j++) |
| 280 |
{ |
| 281 |
ActionRun(CArrayGet(&w->actions, j), mapTriggers); |
| 282 |
} |
| 283 |
} |
| 284 |
CA_FOREACH_END() |
| 285 |
} |