1 //
2 // Copyright(C) 2005-2014 Simon Howard
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 //
15 // Parses "Frame" sections in dehacked files
16 //
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include "doomtype.h"
22 #include "d_items.h"
23 #include "info.h"
24 
25 #include "deh_defs.h"
26 #include "deh_io.h"
27 #include "deh_main.h"
28 #include "deh_mapping.h"
29 
DEH_BEGIN_MAPPING(state_mapping,state_t)30 DEH_BEGIN_MAPPING(state_mapping, state_t)
31   DEH_MAPPING("Sprite number",    sprite)
32   DEH_MAPPING("Sprite subnumber", frame)
33   DEH_MAPPING("Duration",         tics)
34   DEH_MAPPING("Next frame",       nextstate)
35   DEH_UNSUPPORTED_MAPPING("Action pointer")
36 DEH_END_MAPPING
37 
38 static void *DEH_FrameStart(deh_context_t *context, char *line)
39 {
40     int frame_number = 0;
41     state_t *state;
42 
43     if (sscanf(line, "Frame %i", &frame_number) != 1)
44     {
45         DEH_Warning(context, "Parse error on section start");
46         return NULL;
47     }
48 
49     if (frame_number < 0 || frame_number >= NUMSTATES)
50     {
51         DEH_Warning(context, "Invalid frame number: %i", frame_number);
52         return NULL;
53     }
54 
55     if (frame_number >= DEH_VANILLA_NUMSTATES)
56     {
57         DEH_Warning(context, "Attempt to modify frame %i: this will cause "
58                              "problems in Vanilla dehacked.", frame_number);
59     }
60 
61     state = &states[frame_number];
62 
63     return state;
64 }
65 
66 // Simulate a frame overflow: Doom has 967 frames in the states[] array, but
67 // DOS dehacked internally only allocates memory for 966.  As a result,
68 // attempts to set frame 966 (the last frame) will overflow the dehacked
69 // array and overwrite the weaponinfo[] array instead.
70 //
71 // This is noticable in Batman Doom where it is impossible to switch weapons
72 // away from the fist once selected.
73 
DEH_FrameOverflow(deh_context_t * context,char * varname,int value)74 static void DEH_FrameOverflow(deh_context_t *context, char *varname, int value)
75 {
76     if (!strcasecmp(varname, "Duration"))
77     {
78         weaponinfo[0].ammo = value;
79     }
80     else if (!strcasecmp(varname, "Codep frame"))
81     {
82         weaponinfo[0].upstate = value;
83     }
84     else if (!strcasecmp(varname, "Next frame"))
85     {
86         weaponinfo[0].downstate = value;
87     }
88     else if (!strcasecmp(varname, "Unknown 1"))
89     {
90         weaponinfo[0].readystate = value;
91     }
92     else if (!strcasecmp(varname, "Unknown 2"))
93     {
94         weaponinfo[0].atkstate = value;
95     }
96     else
97     {
98         DEH_Error(context, "Unable to simulate frame overflow: field '%s'",
99                   varname);
100     }
101 }
102 
DEH_FrameParseLine(deh_context_t * context,char * line,void * tag)103 static void DEH_FrameParseLine(deh_context_t *context, char *line, void *tag)
104 {
105     state_t *state;
106     char *variable_name, *value;
107     int ivalue;
108 
109     if (tag == NULL)
110        return;
111 
112     state = (state_t *) tag;
113 
114     // Parse the assignment
115 
116     if (!DEH_ParseAssignment(line, &variable_name, &value))
117     {
118         // Failed to parse
119 
120         DEH_Warning(context, "Failed to parse assignment");
121         return;
122     }
123 
124     // all values are integers
125 
126     ivalue = atoi(value);
127 
128     if (state == &states[NUMSTATES - 1])
129     {
130         DEH_FrameOverflow(context, variable_name, ivalue);
131     }
132     else
133     {
134         // set the appropriate field
135 
136         DEH_SetMapping(context, &state_mapping, state, variable_name, ivalue);
137     }
138 }
139 
DEH_FrameSHA1Sum(sha1_context_t * context)140 static void DEH_FrameSHA1Sum(sha1_context_t *context)
141 {
142     int i;
143 
144     for (i=0; i<NUMSTATES; ++i)
145     {
146         DEH_StructSHA1Sum(context, &state_mapping, &states[i]);
147     }
148 }
149 
150 deh_section_t deh_section_frame =
151 {
152     "Frame",
153     NULL,
154     DEH_FrameStart,
155     DEH_FrameParseLine,
156     NULL,
157     DEH_FrameSHA1Sum,
158 };
159 
160