| 1 |
// |
| 2 |
// Copyright(C) 2022 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Tracker |
| 16 |
// |
| 17 |
|
| 18 |
#include "doomstat.h" |
| 19 |
#include "p_tick.h" |
| 20 |
#include "r_state.h" |
| 21 |
|
| 22 |
#include "dsda/args.h" |
| 23 |
#include "dsda/features.h" |
| 24 |
#include "dsda/settings.h" |
| 25 |
#include "dsda/tracker.h" |
| 26 |
|
| 27 |
#include "tracker.h" |
| 28 |
|
| 29 |
dsda_tracker_t dsda_tracker[TRACKER_LIMIT]; |
| 30 |
|
| 31 |
static int tracker_map; |
| 32 |
static int tracker_episode; |
| 33 |
|
| 34 |
static int dsda_FindTracker(int type, int id) { |
| 35 |
int i; |
| 36 |
|
| 37 |
for (i = 0; i < TRACKER_LIMIT; ++i) |
| 38 |
if (dsda_tracker[i].type == type && dsda_tracker[i].id == id) |
| 39 |
return i; |
| 40 |
|
| 41 |
return -1; |
| 42 |
} |
| 43 |
|
| 44 |
mobj_t* dsda_FindMobj(int id) { |
| 45 |
thinker_t* th; |
| 46 |
mobj_t* mobj; |
| 47 |
|
| 48 |
for (th = thinkercap.next; th != &thinkercap; th = th->next) { |
| 49 |
if (th->function != P_MobjThinker) |
| 50 |
continue; |
| 51 |
|
| 52 |
mobj = (mobj_t*) th; |
| 53 |
|
| 54 |
if (mobj->index == id) |
| 55 |
return mobj; |
| 56 |
} |
| 57 |
|
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
static void dsda_WipeTracker(int i) { |
| 62 |
dsda_tracker[i].type = dsda_tracker_nothing; |
| 63 |
dsda_tracker[i].id = 0; |
| 64 |
dsda_tracker[i].mobj = NULL; |
| 65 |
} |
| 66 |
|
| 67 |
void dsda_WipeTrackers(void) { |
| 68 |
int i; |
| 69 |
|
| 70 |
for (i = 0; i < TRACKER_LIMIT; ++i) |
| 71 |
if (dsda_tracker[i].type != dsda_tracker_player) |
| 72 |
dsda_WipeTracker(i); |
| 73 |
} |
| 74 |
|
| 75 |
static void dsda_ConsolidateTrackers(void) { |
| 76 |
int i; |
| 77 |
|
| 78 |
for (i = 0; i < TRACKER_LIMIT; ++i) |
| 79 |
if (dsda_tracker[i].type == dsda_tracker_nothing) { |
| 80 |
int j; |
| 81 |
|
| 82 |
for (j = i + 1; j < TRACKER_LIMIT; ++j) |
| 83 |
if (dsda_tracker[j].type != dsda_tracker_nothing) { |
| 84 |
dsda_tracker[i] = dsda_tracker[j]; |
| 85 |
dsda_WipeTracker(j); |
| 86 |
break; |
| 87 |
} |
| 88 |
|
| 89 |
if (j == TRACKER_LIMIT) |
| 90 |
return; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static void dsda_RefreshTrackers(void) { |
| 95 |
int i; |
| 96 |
|
| 97 |
for (i = 0; i < TRACKER_LIMIT; ++i) { |
| 98 |
switch (dsda_tracker[i].type) { |
| 99 |
default: |
| 100 |
break; |
| 101 |
case dsda_tracker_mobj: |
| 102 |
dsda_tracker[i].mobj = dsda_FindMobj(dsda_tracker[i].id); |
| 103 |
if (!dsda_tracker[i].mobj) |
| 104 |
dsda_WipeTracker(i); |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
if (dsda_tracker[i].type != dsda_tracker_nothing) |
| 109 |
dsda_TrackFeature(uf_tracker); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
static void dsda_ParseCommandlineTrackers(int arg_id, dboolean (*track)(int)) { |
| 114 |
dsda_arg_t* arg; |
| 115 |
|
| 116 |
arg = dsda_Arg(arg_id); |
| 117 |
if (arg->found) { |
| 118 |
int i; |
| 119 |
|
| 120 |
for (i = 0; i < arg->count; ++i) |
| 121 |
track(arg->value.v_int_array[i]); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void dsda_ResetTrackers(void) { |
| 126 |
static dboolean first_time = true; |
| 127 |
|
| 128 |
if (first_time) { |
| 129 |
first_time = false; |
| 130 |
|
| 131 |
dsda_ParseCommandlineTrackers(dsda_arg_track_line, dsda_TrackLine); |
| 132 |
dsda_ParseCommandlineTrackers(dsda_arg_track_line_distance, dsda_TrackLineDistance); |
| 133 |
dsda_ParseCommandlineTrackers(dsda_arg_track_sector, dsda_TrackSector); |
| 134 |
dsda_ParseCommandlineTrackers(dsda_arg_track_mobj, dsda_TrackMobj); |
| 135 |
|
| 136 |
if (dsda_Flag(dsda_arg_track_player)) |
| 137 |
dsda_TrackPlayer(0); |
| 138 |
|
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
if (gamemap != tracker_map || gameepisode != tracker_episode) |
| 143 |
dsda_WipeTrackers(); |
| 144 |
else |
| 145 |
dsda_RefreshTrackers(); |
| 146 |
|
| 147 |
dsda_ConsolidateTrackers(); |
| 148 |
} |
| 149 |
|
| 150 |
static dboolean dsda_AddTracker(int type, int id, mobj_t* mobj) { |
| 151 |
int i; |
| 152 |
|
| 153 |
tracker_map = gamemap; |
| 154 |
tracker_episode = gameepisode; |
| 155 |
|
| 156 |
if (dsda_FindTracker(type, id) >= 0) |
| 157 |
return false; |
| 158 |
|
| 159 |
if ((i = dsda_FindTracker(dsda_tracker_nothing, 0)) >= 0) { |
| 160 |
dsda_TrackFeature(uf_tracker); |
| 161 |
|
| 162 |
dsda_tracker[i].type = type; |
| 163 |
dsda_tracker[i].id = id; |
| 164 |
dsda_tracker[i].mobj = mobj; |
| 165 |
|
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
static dboolean dsda_RemoveTracker(int type, int id) { |
| 173 |
int i; |
| 174 |
|
| 175 |
if (dsda_StrictMode()) |
| 176 |
return false; |
| 177 |
|
| 178 |
if ((i = dsda_FindTracker(type, id)) >= 0) { |
| 179 |
dsda_WipeTracker(i); |
| 180 |
dsda_ConsolidateTrackers(); |
| 181 |
|
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
dboolean dsda_TrackLine(int id) { |
| 189 |
if (dsda_StrictMode()) |
| 190 |
return false; |
| 191 |
|
| 192 |
if (id >= numlines || id < 0) |
| 193 |
return false; |
| 194 |
|
| 195 |
return dsda_AddTracker(dsda_tracker_line, id, NULL); |
| 196 |
} |
| 197 |
|
| 198 |
dboolean dsda_UntrackLine(int id) { |
| 199 |
return dsda_RemoveTracker(dsda_tracker_line, id); |
| 200 |
} |
| 201 |
|
| 202 |
dboolean dsda_TrackLineDistance(int id) { |
| 203 |
if (dsda_StrictMode()) |
| 204 |
return false; |
| 205 |
|
| 206 |
if (id >= numlines || id < 0) |
| 207 |
return false; |
| 208 |
|
| 209 |
return dsda_AddTracker(dsda_tracker_line_distance, id, NULL); |
| 210 |
} |
| 211 |
|
| 212 |
dboolean dsda_UntrackLineDistance(int id) { |
| 213 |
return dsda_RemoveTracker(dsda_tracker_line_distance, id); |
| 214 |
} |
| 215 |
|
| 216 |
dboolean dsda_TrackSector(int id) { |
| 217 |
if (dsda_StrictMode()) |
| 218 |
return false; |
| 219 |
|
| 220 |
if (id >= numsectors || id < 0) |
| 221 |
return false; |
| 222 |
|
| 223 |
return dsda_AddTracker(dsda_tracker_sector, id, NULL); |
| 224 |
} |
| 225 |
|
| 226 |
dboolean dsda_UntrackSector(int id) { |
| 227 |
return dsda_RemoveTracker(dsda_tracker_sector, id); |
| 228 |
} |
| 229 |
|
| 230 |
dboolean dsda_TrackMobj(int id) { |
| 231 |
mobj_t* mobj = NULL; |
| 232 |
|
| 233 |
if (dsda_StrictMode()) |
| 234 |
return false; |
| 235 |
|
| 236 |
mobj = dsda_FindMobj(id); |
| 237 |
|
| 238 |
if (!mobj) |
| 239 |
return false; |
| 240 |
|
| 241 |
{ |
| 242 |
mobj_t* target = NULL; |
| 243 |
|
| 244 |
// While a mobj is targeted, its address is preserved |
| 245 |
P_SetTarget(&target, mobj); |
| 246 |
} |
| 247 |
|
| 248 |
return dsda_AddTracker(dsda_tracker_mobj, id, mobj); |
| 249 |
} |
| 250 |
|
| 251 |
dboolean dsda_UntrackMobj(int id) { |
| 252 |
return dsda_RemoveTracker(dsda_tracker_mobj, id); |
| 253 |
} |
| 254 |
|
| 255 |
dboolean dsda_TrackPlayer(int id) { |
| 256 |
if (dsda_StrictMode()) |
| 257 |
return false; |
| 258 |
|
| 259 |
return dsda_AddTracker(dsda_tracker_player, id, NULL); |
| 260 |
} |
| 261 |
|
| 262 |
dboolean dsda_UntrackPlayer(int id) { |
| 263 |
return dsda_RemoveTracker(dsda_tracker_player, id); |
| 264 |
} |