-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
p_svtexarc.c
243 lines (198 loc) · 6.42 KB
/
p_svtexarc.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2008 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2008 Daniel Swanson <danij@dengine.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* p_svtexarc.c: Archived texture names (save games).
*/
// HEADER FILES ------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#if __WOLFTC__
# include "wolftc.h"
#elif __JDOOM__
# include "jdoom.h"
#elif __JDOOM64__
# include "jdoom64.h"
#elif __JHERETIC__
# include "jheretic.h"
#elif __JHEXEN__
# include "jhexen.h"
#elif __JSTRIFE__
# include "jstrife.h"
#endif
#include "dmu_lib.h"
#include "p_mapsetup.h"
#include "p_svtexarc.h"
#include "p_saveg.h"
// MACROS ------------------------------------------------------------------
#define MAX_ARCHIVED_MATERIALS 1024
#define BADTEXNAME "DD_BADTX" // string that will be written in the texture
// archives to denote a missing texture.
// TYPES -------------------------------------------------------------------
typedef struct {
char name[9];
materialgroup_t group;
} materialentry_t;
typedef struct {
//// \todo Remove fixed limit.
materialentry_t table[MAX_ARCHIVED_MATERIALS];
int count, version;
} materialarchive_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
materialarchive_t matArchive;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static int numFlats; // Used with older versions.
// CODE --------------------------------------------------------------------
/**
* Called for every material in the level before saving by
* Sv_InitTextureArchives.
*/
void SV_PrepareMaterial(materialnum_t num, materialarchive_t* arc)
{
int c;
char name[9];
materialinfo_t info;
// Get the name of the material.
if(R_MaterialGetInfo(num, &info))
strncpy(name, R_MaterialNameForNum(num), 8);
else
strncpy(name, BADTEXNAME, 8);
name[8] = 0;
// Has this already been registered?
for(c = 0; c < arc->count; c++)
{
if(arc->table[c].group == info.group &&
!stricmp(arc->table[c].name, name))
{
break;// Yes, skip it...
}
}
if(c == arc->count)
{
strcpy(arc->table[arc->count++].name, name);
arc->table[arc->count - 1].group = info.group;
}
}
/**
* Initializes the material archives (translation tables).
* Must be called before saving. The table is written before any world data
* is saved.
*/
void SV_InitMaterialArchives(void)
{
uint i;
matArchive.count = 0;
for(i = 0; i < numsectors; ++i)
{
SV_PrepareMaterial(P_GetInt(DMU_SECTOR, i, DMU_FLOOR_MATERIAL), &matArchive);
SV_PrepareMaterial(P_GetInt(DMU_SECTOR, i, DMU_CEILING_MATERIAL), &matArchive);
}
for(i = 0; i < numsides; ++i)
{
SV_PrepareMaterial(P_GetInt(DMU_SIDEDEF, i, DMU_MIDDLE_MATERIAL), &matArchive);
SV_PrepareMaterial(P_GetInt(DMU_SIDEDEF, i, DMU_TOP_MATERIAL), &matArchive);
SV_PrepareMaterial(P_GetInt(DMU_SIDEDEF, i, DMU_BOTTOM_MATERIAL), &matArchive);
}
}
unsigned short SV_SearchArchive(materialarchive_t *arc, char *name)
{
int i;
for(i = 0; i < arc->count; ++i)
if(!stricmp(arc->table[i].name, name))
return i;
// Not found?!!!
return 0;
}
/**
* @return The archive number of the given texture.
*/
unsigned short SV_MaterialArchiveNum(materialnum_t num)
{
char name[9];
if(R_MaterialNameForNum(num))
strncpy(name, R_MaterialNameForNum(num), 8);
else
strncpy(name, BADTEXNAME, 8);
name[8] = 0;
return SV_SearchArchive(&matArchive, name);
}
materialnum_t SV_GetArchiveMaterial(int archivenum, int group)
{
if(matArchive.version < 1 && group == 1)
{
archivenum += numFlats;
}
if(!(archivenum < matArchive.count))
{
#if _DEBUG
Con_Error("SV_GetArchiveMaterial: Bad archivenum %i.", archivenum);
#endif
return 0;
}
if(!strncmp(matArchive.table[archivenum].name, BADTEXNAME, 8))
return 0;
else
return R_MaterialNumForName(matArchive.table[archivenum].name,
matArchive.table[archivenum].group);
return 0;
}
void SV_WriteMaterialArchive(void)
{
int i;
SV_WriteShort(matArchive.count);
for(i = 0; i < matArchive.count; ++i)
{
SV_Write(matArchive.table[i].name, 8);
SV_WriteByte(matArchive.table[i].group);
}
}
static void readMatArchive(materialarchive_t* arc, materialgroup_t defaultGroup)
{
int i, num;
num = SV_ReadShort();
for(i = arc->count; i < arc->count + num; ++i)
{
SV_Read(arc->table[i].name, 8);
arc->table[i].name[8] = 0;
if(arc->version >= 1)
arc->table[i].group = SV_ReadByte();
else
arc->table[i].group = defaultGroup;
}
arc->count += num;
}
void SV_ReadMaterialArchive(int version)
{
matArchive.count = 0;
matArchive.version = version;
readMatArchive(&matArchive, MAT_FLAT);
if(matArchive.version == 0)
{ // The old format saved textures and flats in seperate blocks.
numFlats = matArchive.count;
readMatArchive(&matArchive, MAT_TEXTURE);
}
}