| 1 |
// |
| 2 |
// Copyright(C) 2021 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 State |
| 16 |
// |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include "state.h" |
| 22 |
|
| 23 |
state_t* states; |
| 24 |
int num_states; |
| 25 |
byte* defined_codeptr_args; |
| 26 |
statenum_t* seenstate_tab; |
| 27 |
|
| 28 |
static actionf_t* deh_codeptr; |
| 29 |
|
| 30 |
static void dsda_ResetStates(int from, int to) { |
| 31 |
int i; |
| 32 |
|
| 33 |
for (i = from; i < to; ++i) { |
| 34 |
states[i].sprite = SPR_TNT1; |
| 35 |
states[i].tics = -1; |
| 36 |
states[i].nextstate = i; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static void dsda_PrepAllocation(void) { |
| 41 |
static int first_allocation = true; |
| 42 |
|
| 43 |
if (first_allocation) { |
| 44 |
state_t* source = states; |
| 45 |
|
| 46 |
first_allocation = false; |
| 47 |
states = malloc(num_states * sizeof(*states)); |
| 48 |
memcpy(states, source, num_states * sizeof(*states)); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static void dsda_EnsureCapacity(int limit) { |
| 53 |
while (limit >= num_states) { |
| 54 |
int old_num_states = num_states; |
| 55 |
|
| 56 |
dsda_PrepAllocation(); |
| 57 |
|
| 58 |
num_states *= 2; |
| 59 |
|
| 60 |
states = realloc(states, num_states * sizeof(*states)); |
| 61 |
memset(states + old_num_states, 0, (num_states - old_num_states) * sizeof(*states)); |
| 62 |
|
| 63 |
deh_codeptr = realloc(deh_codeptr, num_states * sizeof(*deh_codeptr)); |
| 64 |
memset(deh_codeptr + old_num_states, 0, (num_states - old_num_states) * sizeof(*deh_codeptr)); |
| 65 |
|
| 66 |
defined_codeptr_args = |
| 67 |
realloc(defined_codeptr_args, num_states * sizeof(*defined_codeptr_args)); |
| 68 |
memset(defined_codeptr_args + old_num_states, 0, |
| 69 |
(num_states - old_num_states) * sizeof(*defined_codeptr_args)); |
| 70 |
|
| 71 |
seenstate_tab = realloc(seenstate_tab, num_states * sizeof(*seenstate_tab)); |
| 72 |
memset(seenstate_tab + old_num_states, 0, |
| 73 |
(num_states - old_num_states) * sizeof(*seenstate_tab)); |
| 74 |
|
| 75 |
dsda_ResetStates(old_num_states, num_states); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
dsda_deh_state_t dsda_GetDehState(int index) { |
| 80 |
dsda_deh_state_t deh_state; |
| 81 |
|
| 82 |
dsda_EnsureCapacity(index); |
| 83 |
|
| 84 |
deh_state.state = &states[index]; |
| 85 |
deh_state.codeptr = &deh_codeptr[index]; |
| 86 |
deh_state.defined_codeptr_args = &defined_codeptr_args[index]; |
| 87 |
|
| 88 |
return deh_state; |
| 89 |
} |
| 90 |
|
| 91 |
void dsda_InitializeStates(state_t* source, int count) { |
| 92 |
int i; |
| 93 |
extern int raven; |
| 94 |
|
| 95 |
num_states = count; |
| 96 |
|
| 97 |
states = source; |
| 98 |
|
| 99 |
if (raven) return; |
| 100 |
|
| 101 |
seenstate_tab = calloc(num_states, sizeof(*seenstate_tab)); |
| 102 |
|
| 103 |
deh_codeptr = malloc(num_states * sizeof(*deh_codeptr)); |
| 104 |
for (i = 0; i < num_states; i++) |
| 105 |
deh_codeptr[i] = states[i].action; |
| 106 |
|
| 107 |
defined_codeptr_args = calloc(num_states, sizeof(*defined_codeptr_args)); |
| 108 |
} |
| 109 |
|
| 110 |
void dsda_FreeDehStates(void) { |
| 111 |
free(defined_codeptr_args); |
| 112 |
free(deh_codeptr); |
| 113 |
} |