| 1 |
// |
| 2 |
// Copyright(C) 2020 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 Ghost |
| 16 |
// |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
#include "lprintf.h" |
| 21 |
#include "doomtype.h" |
| 22 |
#include "doomstat.h" |
| 23 |
#include "info.h" |
| 24 |
#include "m_file.h" |
| 25 |
#include "p_maputl.h" |
| 26 |
#include "p_tick.h" |
| 27 |
#include "sounds.h" |
| 28 |
#include "z_zone.h" |
| 29 |
#include "w_wad.h" |
| 30 |
|
| 31 |
#include "dsda/features.h" |
| 32 |
#include "dsda/settings.h" |
| 33 |
|
| 34 |
#include "ghost.h" |
| 35 |
|
| 36 |
#define DSDA_GHOST_MIN_VERSION 1 |
| 37 |
#define DSDA_GHOST_VERSION 2 |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
fixed_t x; |
| 41 |
fixed_t y; |
| 42 |
fixed_t z; |
| 43 |
angle_t angle; |
| 44 |
spritenum_t sprite; |
| 45 |
int frame; |
| 46 |
int map; |
| 47 |
int episode; |
| 48 |
int tic; |
| 49 |
} dsda_ghost_frame_t; |
| 50 |
|
| 51 |
typedef struct { |
| 52 |
dsda_ghost_frame_t frame; |
| 53 |
mobj_t* mobj; |
| 54 |
FILE* fstream; |
| 55 |
} dsda_ghost_t; |
| 56 |
|
| 57 |
typedef struct { |
| 58 |
thinker_t* thinker; |
| 59 |
dsda_ghost_t* ghosts; |
| 60 |
int count; |
| 61 |
} dsda_ghost_import_t; |
| 62 |
|
| 63 |
typedef struct { |
| 64 |
FILE* fstream; |
| 65 |
int version; |
| 66 |
int count; |
| 67 |
} dsda_ghost_file_t; |
| 68 |
|
| 69 |
mobjinfo_t dsda_ghost_info = { |
| 70 |
-1, // doomednum |
| 71 |
S_PLAY, // spawnstate |
| 72 |
0, // spawnhealth |
| 73 |
S_PLAY_RUN1, // seestate |
| 74 |
sfx_None, // seesound |
| 75 |
0, // reactiontime |
| 76 |
sfx_None, // attacksound |
| 77 |
S_PLAY_PAIN, // painstate |
| 78 |
0, // painchance |
| 79 |
sfx_None, // painsound |
| 80 |
S_NULL, // meleestate |
| 81 |
S_PLAY_ATK1, // missilestate |
| 82 |
S_PLAY_DIE1, // deathstate |
| 83 |
S_PLAY_XDIE1, // xdeathstate |
| 84 |
sfx_None, // deathsound |
| 85 |
0, // speed |
| 86 |
0, // radius |
| 87 |
0, // height |
| 88 |
0, // mass |
| 89 |
0, // damage |
| 90 |
sfx_None, // activesound |
| 91 |
MF_NOBLOCKMAP | MF_TRANSLUCENT, // flags |
| 92 |
S_NULL // raisestate |
| 93 |
}; |
| 94 |
|
| 95 |
FILE *dsda_ghost_export; |
| 96 |
dsda_ghost_import_t dsda_ghost_import; |
| 97 |
|
| 98 |
void dsda_InitGhostExport(const char* name) { |
| 99 |
int version; |
| 100 |
char* filename; |
| 101 |
filename = Z_Malloc(strlen(name) + 4 + 1); |
| 102 |
AddDefaultExtension(strcpy(filename, name), ".gst"); |
| 103 |
|
| 104 |
dsda_ghost_export = M_OpenFile(filename, "wb"); |
| 105 |
|
| 106 |
if (dsda_ghost_export == NULL) |
| 107 |
I_Error("dsda_InitGhostExport: failed to open %s", name); |
| 108 |
|
| 109 |
version = DSDA_GHOST_VERSION; |
| 110 |
fwrite(&version, sizeof(int), 1, dsda_ghost_export); |
| 111 |
|
| 112 |
Z_Free(filename); |
| 113 |
} |
| 114 |
|
| 115 |
static void dsda_OpenGhostFile(const char* ghost_name, dsda_ghost_file_t* ghost_file) { |
| 116 |
char* filename; |
| 117 |
int read_result; |
| 118 |
|
| 119 |
memset(ghost_file, 0, sizeof(dsda_ghost_file_t)); |
| 120 |
|
| 121 |
filename = Z_Malloc(strlen(ghost_name) + 4 + 1); |
| 122 |
AddDefaultExtension(strcpy(filename, ghost_name), ".gst"); |
| 123 |
|
| 124 |
ghost_file->fstream = M_OpenFile(filename, "rb"); |
| 125 |
|
| 126 |
if (ghost_file->fstream == NULL) |
| 127 |
I_Error("dsda_OpenGhostImport: failed to open %s", ghost_name); |
| 128 |
|
| 129 |
read_result = fread(&ghost_file->version, sizeof(int), 1, ghost_file->fstream); |
| 130 |
if (ghost_file->version < DSDA_GHOST_MIN_VERSION || |
| 131 |
ghost_file->version > DSDA_GHOST_VERSION || |
| 132 |
read_result != 1) |
| 133 |
I_Error("dsda_OpenGhostImport: unsupported ghost version %s", ghost_name); |
| 134 |
|
| 135 |
if (ghost_file->version == 1) |
| 136 |
ghost_file->count = 1; |
| 137 |
else { |
| 138 |
read_result = fread(&ghost_file->count, sizeof(int), 1, ghost_file->fstream); |
| 139 |
if (read_result != 1) |
| 140 |
I_Error("dsda_OpenGhostImport: error reading ghost count %s", ghost_name); |
| 141 |
} |
| 142 |
|
| 143 |
Z_Free(filename); |
| 144 |
} |
| 145 |
|
| 146 |
static int dsda_GhostCount(const char* ghost_name) { |
| 147 |
dsda_ghost_file_t ghost_file; |
| 148 |
|
| 149 |
dsda_OpenGhostFile(ghost_name, &ghost_file); |
| 150 |
|
| 151 |
fclose(ghost_file.fstream); |
| 152 |
|
| 153 |
return ghost_file.count; |
| 154 |
} |
| 155 |
|
| 156 |
void dsda_InitGhostImport(const char** ghost_names, int count) { |
| 157 |
int arg_i; |
| 158 |
int ghost_i; |
| 159 |
int i; |
| 160 |
dsda_ghost_file_t ghost_file; |
| 161 |
|
| 162 |
ghost_i = 0; |
| 163 |
|
| 164 |
for (arg_i = 0; arg_i < count; ++arg_i) |
| 165 |
dsda_ghost_import.count += dsda_GhostCount(ghost_names[arg_i]); |
| 166 |
|
| 167 |
dsda_ghost_import.ghosts = Z_Calloc(dsda_ghost_import.count, sizeof(dsda_ghost_t)); |
| 168 |
|
| 169 |
for (arg_i = 0; arg_i < count; ++arg_i) { |
| 170 |
dsda_OpenGhostFile(ghost_names[arg_i], &ghost_file); |
| 171 |
|
| 172 |
for (i = 0; i < ghost_file.count; ++i) { |
| 173 |
dsda_ghost_import.ghosts[ghost_i].fstream = ghost_file.fstream; |
| 174 |
++ghost_i; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
void dsda_ExportGhostFrame(void) { |
| 180 |
dsda_ghost_frame_t ghost_frame; |
| 181 |
mobj_t* player; |
| 182 |
int i; |
| 183 |
|
| 184 |
if (dsda_ghost_export == NULL) return; |
| 185 |
|
| 186 |
// just write the number of players on the zeroth tic |
| 187 |
if (gametic == 0) { |
| 188 |
int count = 0; |
| 189 |
|
| 190 |
for (i = 0; i < g_maxplayers; ++i) if (playeringame[i]) ++count; |
| 191 |
|
| 192 |
fwrite(&count, sizeof(int), 1, dsda_ghost_export); |
| 193 |
|
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
for (i = 0; i < g_maxplayers; ++i) { |
| 198 |
if (!playeringame[i]) break; |
| 199 |
|
| 200 |
player = players[i].mo; |
| 201 |
|
| 202 |
if (player == NULL) continue; |
| 203 |
|
| 204 |
ghost_frame.x = player->x; |
| 205 |
ghost_frame.y = player->y; |
| 206 |
ghost_frame.z = player->z; |
| 207 |
ghost_frame.angle = player->angle; |
| 208 |
ghost_frame.sprite = player->sprite; |
| 209 |
ghost_frame.frame = player->frame; |
| 210 |
ghost_frame.map = gamemap; |
| 211 |
ghost_frame.episode = gameepisode; |
| 212 |
ghost_frame.tic = gametic; |
| 213 |
|
| 214 |
fwrite(&ghost_frame, sizeof(dsda_ghost_frame_t), 1, dsda_ghost_export); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// Stripped down version of P_SpawnMobj |
| 219 |
void dsda_SpawnGhost(void) { |
| 220 |
mobj_t* mobj; |
| 221 |
state_t* ghost_state; |
| 222 |
int ghost_i; |
| 223 |
|
| 224 |
if (dsda_StrictMode()) |
| 225 |
return; |
| 226 |
|
| 227 |
for (ghost_i = 0; ghost_i < dsda_ghost_import.count; ++ghost_i) { |
| 228 |
if (dsda_ghost_import.ghosts[ghost_i].fstream == NULL) { |
| 229 |
dsda_ghost_import.ghosts[ghost_i].mobj = NULL; |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
mobj = Z_MallocLevel(sizeof(*mobj)); |
| 234 |
memset(mobj, 0, sizeof(*mobj)); |
| 235 |
mobj->type = MT_NULL; |
| 236 |
mobj->info = &dsda_ghost_info; |
| 237 |
mobj->flags = dsda_ghost_info.flags; |
| 238 |
|
| 239 |
if (dsda_CycleGhostColors()) { |
| 240 |
switch (ghost_i % 4) { |
| 241 |
case 0: |
| 242 |
break; |
| 243 |
case 1: |
| 244 |
mobj->flags |= MF_TRANSLATION1; |
| 245 |
break; |
| 246 |
case 2: |
| 247 |
mobj->flags |= MF_TRANSLATION2; |
| 248 |
break; |
| 249 |
case 3: |
| 250 |
mobj->flags |= MF_TRANSLATION; |
| 251 |
break; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
mobj->x = players[0].mo->x; |
| 256 |
mobj->y = players[0].mo->y; |
| 257 |
mobj->z = players[0].mo->z; |
| 258 |
mobj->angle = players[0].mo->angle; |
| 259 |
|
| 260 |
ghost_state = &states[dsda_ghost_info.spawnstate]; |
| 261 |
|
| 262 |
mobj->state = ghost_state; |
| 263 |
mobj->tics = ghost_state->tics; |
| 264 |
mobj->sprite = ghost_state->sprite; |
| 265 |
mobj->frame = ghost_state->frame; |
| 266 |
mobj->touching_sectorlist = NULL; |
| 267 |
|
| 268 |
P_SetThingPosition(mobj); |
| 269 |
|
| 270 |
mobj->dropoffz = |
| 271 |
mobj->floorz = mobj->subsector->sector->floorheight; |
| 272 |
mobj->ceilingz = mobj->subsector->sector->ceilingheight; |
| 273 |
|
| 274 |
mobj->PrevX = mobj->x; |
| 275 |
mobj->PrevY = mobj->y; |
| 276 |
mobj->PrevZ = mobj->z; |
| 277 |
|
| 278 |
mobj->friction = ORIG_FRICTION; |
| 279 |
mobj->index = -1; |
| 280 |
|
| 281 |
dsda_ghost_import.ghosts[ghost_i].mobj = mobj; |
| 282 |
} |
| 283 |
|
| 284 |
if (dsda_ghost_import.count > 0) { |
| 285 |
dsda_TrackFeature(uf_ghost); |
| 286 |
dsda_ghost_import.thinker = Z_MallocLevel(sizeof(*mobj)); |
| 287 |
memset(dsda_ghost_import.thinker, 0, sizeof(thinker_t)); |
| 288 |
dsda_ghost_import.thinker->function = dsda_UpdateGhosts; |
| 289 |
P_AddThinker(dsda_ghost_import.thinker); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
void dsda_UpdateGhosts(void* _void) { |
| 294 |
dsda_ghost_t* ghost; |
| 295 |
mobj_t* mobj; |
| 296 |
int ghost_i; |
| 297 |
int read_result; |
| 298 |
dboolean ghost_was_behind; |
| 299 |
|
| 300 |
for (ghost_i = 0; ghost_i < dsda_ghost_import.count; ++ghost_i) { |
| 301 |
ghost = &dsda_ghost_import.ghosts[ghost_i]; |
| 302 |
|
| 303 |
if (ghost->fstream == NULL) continue; |
| 304 |
|
| 305 |
mobj = ghost->mobj; |
| 306 |
|
| 307 |
// Ghost removed from map (finished map already) |
| 308 |
if (mobj->touching_sectorlist == NULL) continue; |
| 309 |
|
| 310 |
mobj->PrevX = mobj->x; |
| 311 |
mobj->PrevY = mobj->y; |
| 312 |
mobj->PrevZ = mobj->z; |
| 313 |
|
| 314 |
ghost_was_behind = ghost->frame.map != 0 && ghost->frame.map != gamemap; |
| 315 |
|
| 316 |
// if the ghost was left behind, catch it up |
| 317 |
do { |
| 318 |
read_result = fread(&ghost->frame, sizeof(dsda_ghost_frame_t), 1, ghost->fstream); |
| 319 |
|
| 320 |
if (read_result != 1) { |
| 321 |
fclose(ghost->fstream); |
| 322 |
ghost->fstream = NULL; |
| 323 |
break; |
| 324 |
} |
| 325 |
} while (ghost_was_behind && ghost->frame.map != gamemap); |
| 326 |
|
| 327 |
if (ghost->fstream == NULL) continue; |
| 328 |
|
| 329 |
P_UnsetThingPosition(mobj); |
| 330 |
|
| 331 |
// Roll back one frame and leave position unset until next map |
| 332 |
if (ghost->frame.map != gamemap) { |
| 333 |
fseek(ghost->fstream, -(int)sizeof(dsda_ghost_frame_t), SEEK_CUR); |
| 334 |
continue; |
| 335 |
} |
| 336 |
|
| 337 |
mobj->x = ghost->frame.x; |
| 338 |
mobj->y = ghost->frame.y; |
| 339 |
mobj->z = ghost->frame.z; |
| 340 |
mobj->angle = ghost->frame.angle; |
| 341 |
mobj->sprite = ghost->frame.sprite; |
| 342 |
mobj->frame = ghost->frame.frame; |
| 343 |
|
| 344 |
P_SetThingPosition(mobj); |
| 345 |
} |
| 346 |
} |