~iddqd-mail/boomlightgen/trunk

« back to all changes in this revision

Viewing changes to src/lightgen.cpp

  • Committer: cybermind
  • Date: 2016-02-27 12:02:58 UTC
  • Revision ID: iddqd_mail@mail.ru-20160227120258-ovugrmtn5tsdccio
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "lightgen.h"
 
2
#include "util.h"
 
3
#include "wad.h"
 
4
 
 
5
 
 
6
 
 
7
std::vector<wSector_t *> AllDaySectors;
 
8
std::ofstream OutFile;
 
9
 
 
10
const int TagFor242 = 9999;
 
11
const int StartTag = 10000;
 
12
const int LightStartTag = 10000;
 
13
const int TransferStartTag = 15000;
 
14
const int StaticTag = 12000;
 
15
 
 
16
void LG_OpenOutFile()
 
17
{
 
18
        OutFile.open("output.wl", std::ios::out);
 
19
}
 
20
 
 
21
void LG_CloseOutFile()
 
22
{
 
23
        OutFile.close();
 
24
}
 
25
 
 
26
void LC_Exit(int code)
 
27
{
 
28
        exit(code);
 
29
}
 
30
 
 
31
void LG_ReadSectors(wFile_t *file, std::string mapname, std::vector<wSector_t *> &sects)
 
32
{
 
33
        const int SECTOR_SIZE = 26;
 
34
        // clear everything
 
35
        if (!sects.empty()) {
 
36
                for (std::vector<wSector_t *>::iterator it = sects.begin(); it != sects.end(); ++it) {
 
37
                        delete *it;
 
38
                        sects.erase(sects.begin());
 
39
                }
 
40
        }
 
41
 
 
42
        wEntry_t *sectors = Wad_FindEntry(file, "SECTORS",
 
43
                Wad_FindEntry(file, mapname.c_str(), NULL, false), false);
 
44
        if (sectors) {
 
45
                uint8_t *sectLump = (uint8_t *)malloc(sectors->size * sizeof(uint8_t)); // SECTORS's data
 
46
                fseek(file->file, sectors->offset, SEEK_SET);
 
47
                fread((void*)sectLump, 1, sectors->size, file->file); 
 
48
 
 
49
                for (size_t i = 0; i < sectors->size; i += SECTOR_SIZE) { //
 
50
                        wSector_t *sector = new wSector_t;
 
51
                        sector->FloorHeight = read2bytes_mem((void *)(sectLump + i));
 
52
                        sector->CeilingHeight = read2bytes_mem((void *)(sectLump + i + 0x02));
 
53
                        memcpy((void *)(sector->FloorTexture), (void *)(sectLump + i + 0x04), 8);
 
54
                        memcpy((void *)(sector->CeilingTexture), (void *)(sectLump + i + 0x0C), 8);
 
55
                        sector->Light = read2bytes_mem((void *)(sectLump + i + 0x14));
 
56
                        sector->Tag = read2bytes_mem((void *)(sectLump + i + 0x18));
 
57
                        if (sector->Tag >= StartTag) {
 
58
                                sects.push_back(sector);
 
59
                        } else {
 
60
                                delete sector;
 
61
                        }
 
62
                        
 
63
                }
 
64
                delete [] sectLump;
 
65
        }
 
66
}
 
67
 
 
68
void LG_GenerateGeometry(std::vector<wSector_t *> daySects)
 
69
{
 
70
        const int dayintics = 1024;
 
71
        const int numsect = daySects.size();
 
72
        const int daytonight[] = {240, 224, 208, 192, 176, 160, 144, 128, 112};
 
73
        
 
74
        LG_OpenOutFile();
 
75
        OutFile << "#\"standard.h\"" << std::endl;
 
76
        OutFile << "#\"spawns.h\"" << std::endl;
 
77
        
 
78
        // Voodoo controller
 
79
        
 
80
        OutFile << "Voodoo_Evening(tag)" << std::endl;
 
81
        OutFile << "{" << std::endl;
 
82
        // vBoxSize - horizontal size of box, multiple of 32, rounded upward 
 
83
        OutFile << "set(\"vBoxSize\", mul(add(div(" << numsect << ", 32), 1), 32))" << std::endl;
 
84
        // number of voodoo dools
 
85
        OutFile << "set(\"voodoos\", div(get(\"vBoxSize\"), 32))" << std::endl;
 
86
        // first light to minimum neighbor
 
87
        OutFile << "typeline(157,tag, step(-32, 0))" << std::endl;
 
88
        // just lines to the end
 
89
        OutFile << "for(2,get(\"voodoos\"),step(-32, 0))" << std::endl;
 
90
        // one mappixel further
 
91
        OutFile << "movestep(get(\"vBoxSize\"), -1)" << std::endl;
 
92
        // transfer light to minimum neighbor
 
93
        OutFile << "typeline(157,1099, step(-32, 0))" << std::endl;
 
94
        // just lines to the end
 
95
        OutFile << "for(2,get(\"voodoos\"),step(-32, 0))" << std::endl;
 
96
        // one mappixel further
 
97
        OutFile << "movestep(get(\"vBoxSize\"), -1)" << std::endl;
 
98
        
 
99
        // determine current light level
 
100
        OutFile << "{eq(tag, 1002) ? set(\"lightlevel\", 113) : \
 
101
                {eq(tag, 1003) ? set(\"lightlevel\", 129) : \
 
102
                {eq(tag, 1004) ? set(\"lightlevel\", 145) : \
 
103
                {eq(tag, 1005) ? set(\"lightlevel\", 161) : \
 
104
                {eq(tag, 1006) ? set(\"lightlevel\", 177) : \
 
105
                {eq(tag, 1007) ? set(\"lightlevel\", 193) : \
 
106
                {eq(tag, 1008) ? set(\"lightlevel\", 209) : \
 
107
                {eq(tag, 1009) ? set(\"lightlevel\", 225) : \
 
108
                {eq(tag, 1010) ? set(\"lightlevel\", 241) : set(\"lightlevel\", 0)}}}}}}}}}" << std::endl;
 
109
        
 
110
        int counter = TransferStartTag;
 
111
        for (int i = 0; i < daySects.size(); ++i) {
 
112
                wSector_t &daysector = *daySects[i];
 
113
 
 
114
                OutFile << "{lessthaneq(" << daysector.Light << ", get(\"lightlevel\")) ? \
 
115
                        movestep(-1, 0) : typeline(157," << counter++ << ", step(-1, 0))}" << std::endl; 
 
116
                         /*
 
117
                OutFile << "{lessthaneq(" << daysector.Light << ", get(\"lightlevel\")) ? \
 
118
                        movestep(-1, 0) : typeline(157," << counter++ << ", step(-1, 0))}" << std::endl;*/
 
119
        }
 
120
        // just lines to the end
 
121
        OutFile << "step(inv(sub(get(\"vBoxSize\"), " << numsect <<")), 0)" << std::endl;
 
122
        // end
 
123
        
 
124
        OutFile << "movestep(get(\"vBoxSize\"), -1)" << std::endl;
 
125
        // next light transfer
 
126
        OutFile << "typeline(157, " << TagFor242 << ", step(-32, 0))" << std::endl;
 
127
        // just lines to the end
 
128
        OutFile << "for(2,get(\"voodoos\"),step(-32, 0))" << std::endl;
 
129
        // one mappixel further
 
130
        OutFile << "movestep(get(\"vBoxSize\"), 30)" << std::endl;
 
131
        
 
132
        OutFile << "}" << std::endl;
 
133
        
 
134
        
 
135
        // Voodoo morning controller
 
136
        OutFile << "Voodoo_Morning(tag)" << std::endl;
 
137
        OutFile << "{" << std::endl;
 
138
        // vBoxSize - horizontal size of box, multiple of 32, rounded upward 
 
139
        OutFile << "set(\"vBoxSize\", mul(add(div(" << numsect << ", 32), 1), 32))" << std::endl;
 
140
        // number of voodoo dools
 
141
        OutFile << "set(\"voodoos\", div(get(\"vBoxSize\"), 32))" << std::endl;
 
142
        OutFile << "typeline(80,tag, step(-32, 0))" << std::endl;
 
143
        // just lines to the end
 
144
        OutFile << "for(2,get(\"voodoos\"),step(-32, 0))" << std::endl;
 
145
        // one mappixel further
 
146
        OutFile << "movestep(get(\"vBoxSize\"), -1)" << std::endl;
 
147
        // transfer light to minimum neighbor
 
148
        OutFile << "typeline(80,1099, step(-32, 0))" << std::endl;
 
149
        // just lines to the end
 
150
        OutFile << "for(2,get(\"voodoos\"),step(-32, 0))" << std::endl;
 
151
        // one mappixel further
 
152
        OutFile << "movestep(get(\"vBoxSize\"), -1)" << std::endl;
 
153
        
 
154
        // determine current light level
 
155
        OutFile << "{eq(tag, 1003) ? set(\"lightlevel\", 127) : \
 
156
                {eq(tag, 1004) ? set(\"lightlevel\", 143) : \
 
157
                {eq(tag, 1005) ? set(\"lightlevel\", 159) : \
 
158
                {eq(tag, 1006) ? set(\"lightlevel\", 175) : \
 
159
                {eq(tag, 1007) ? set(\"lightlevel\", 191) : \
 
160
                {eq(tag, 1008) ? set(\"lightlevel\", 207) : \
 
161
                {eq(tag, 1009) ? set(\"lightlevel\", 223) : \
 
162
                {eq(tag, 1010) ? set(\"lightlevel\", 239) : \
 
163
                {eq(tag, 1011) ? set(\"lightlevel\", 254) : set(\"lightlevel\", 0)}}}}}}}}}" << std::endl;
 
164
        
 
165
        counter = TransferStartTag;
 
166
        for (int i = 0; i < daySects.size(); ++i) {
 
167
                wSector_t &daysector = *daySects[i];
 
168
                OutFile << "{lessthaneq(" << daysector.Light << ", get(\"lightlevel\")) ? \
 
169
                        movestep(-1, 0) : typeline(80," << counter++ << ", step(-1, 0))}" << std::endl; 
 
170
        }
 
171
        // just lines to the end
 
172
        OutFile << "step(inv(sub(get(\"vBoxSize\"), " << numsect <<")), 0)" << std::endl;
 
173
        // end
 
174
        OutFile << "movestep(get(\"vBoxSize\"), -1)" << std::endl;
 
175
        // next light transfer
 
176
        OutFile << "typeline(80, " << TagFor242 << ", step(-32, 0))" << std::endl;
 
177
        // just lines to the end
 
178
        OutFile << "for(2,get(\"voodoos\"),step(-32, 0))" << std::endl;
 
179
        // one mappixel further
 
180
        OutFile << "movestep(get(\"vBoxSize\"), 30)" << std::endl;
 
181
        
 
182
        OutFile << "}" << std::endl;
 
183
        
 
184
        
 
185
        OutFile << "GenVoodooController" << std::endl;
 
186
        OutFile << "{" << std::endl;
 
187
        OutFile << "set(\"vBoxSize\", mul(add(div(" << daySects.size() << ", 32), 1), 32))" << std::endl;
 
188
        OutFile << "set(\"voodoos\", div(get(\"vBoxSize\"), 32))" << std::endl;
 
189
        OutFile << "movestep(0, " << dayintics <<")" << std::endl;
 
190
        OutFile << "movestep(-16, -64)" << std::endl;   
 
191
        
 
192
        OutFile << "Voodoo_Morning(1011)" << std::endl;
 
193
        OutFile << "Voodoo_Morning(1010)" << std::endl;
 
194
        OutFile << "Voodoo_Morning(1009)" << std::endl;
 
195
        OutFile << "Voodoo_Morning(1008)" << std::endl;
 
196
        OutFile << "Voodoo_Morning(1007)" << std::endl;
 
197
        OutFile << "Voodoo_Morning(1006)" << std::endl;
 
198
        OutFile << "Voodoo_Morning(1005)" << std::endl;
 
199
        OutFile << "Voodoo_Morning(1004)" << std::endl;
 
200
        for (int i = 0; i < daySects.size(); ++i) {
 
201
                wSector_t &daysector = *daySects[i];
 
202
                OutFile << "typeline(157,"<<TransferStartTag+i <<", step(-1, 0))" << std::endl;
 
203
        }
 
204
        OutFile << "step(inv(sub(get(\"vBoxSize\"), " << numsect <<")), 0)" << std::endl;
 
205
        OutFile << "movestep(get(\"vBoxSize\"), 10)" << std::endl;
 
206
        
 
207
        
 
208
        OutFile << "typeline(157,1000, step(-1, 0))" << std::endl;
 
209
        OutFile << "typeline(157,1001, step(-1, 0))"  << std::endl;
 
210
        OutFile << "typeline(157,1002, step(-1, 0))"  << std::endl;
 
211
        OutFile << "typeline(157,1003, step(-1, 0))"  << std::endl;
 
212
        OutFile << "typeline(157,1004, step(-1, 0))" << std::endl;
 
213
        OutFile << "typeline(157,1005, step(-1, 0))" << std::endl;
 
214
        OutFile << "typeline(157,1006, step(-1, 0))" << std::endl;
 
215
        OutFile << "typeline(157,1007, step(-1, 0))" << std::endl;
 
216
        OutFile << "typeline(157,1008, step(-1, 0))" << std::endl;
 
217
        OutFile << "typeline(157,1009, step(-1, 0))" << std::endl;
 
218
        OutFile << "typeline(157,1010, step(-1, 0))" << std::endl;
 
219
        OutFile << "typeline(157,1011, step(-1, 0))" << std::endl;
 
220
        OutFile << "typeline(157,1099, step(-1, 0))" << std::endl;
 
221
        OutFile << "typeline(157,9999, step(-1, 0))" << std::endl;
 
222
        OutFile << "movestep(inv(sub(get(\"vBoxSize\"), " << 14 <<")), 0)" << std::endl;
 
223
        // one mappixel further
 
224
        OutFile << "movestep(get(\"vBoxSize\"), 10)" << std::endl;
 
225
        
 
226
        
 
227
        OutFile << "Voodoo_Evening(1003)" << std::endl;
 
228
        OutFile << "Voodoo_Evening(1004)" << std::endl;
 
229
        OutFile << "Voodoo_Evening(1005)" << std::endl;
 
230
        OutFile << "Voodoo_Evening(1006)" << std::endl;
 
231
        OutFile << "Voodoo_Evening(1007)" << std::endl;
 
232
        OutFile << "Voodoo_Evening(1008)" << std::endl;
 
233
        OutFile << "Voodoo_Evening(1009)" << std::endl;
 
234
        OutFile << "Voodoo_Evening(1010)" << std::endl;
 
235
        for (int i = 0; i < daySects.size(); ++i) {
 
236
                wSector_t &daysector = *daySects[i];
 
237
                OutFile << "typeline(81,"<<TransferStartTag+i <<", step(-1, 0))" << std::endl;
 
238
        }
 
239
        OutFile << "step(inv(sub(get(\"vBoxSize\"), " << numsect <<")), 0)" << std::endl;
 
240
        OutFile << "movestep(get(\"vBoxSize\"), 10)" << std::endl;
 
241
        
 
242
        OutFile << "typeline(81,1000, step(-1, 0))" << std::endl;
 
243
        OutFile << "typeline(81,1001, step(-1, 0))"  << std::endl;
 
244
        OutFile << "typeline(81,1002, step(-1, 0))"  << std::endl;
 
245
        OutFile << "typeline(81,1003, step(-1, 0))"  << std::endl;
 
246
        OutFile << "typeline(81,1004, step(-1, 0))" << std::endl;
 
247
        OutFile << "typeline(81,1005, step(-1, 0))" << std::endl;
 
248
        OutFile << "typeline(81,1006, step(-1, 0))" << std::endl;
 
249
        OutFile << "typeline(81,1007, step(-1, 0))" << std::endl;
 
250
        OutFile << "typeline(81,1008, step(-1, 0))" << std::endl;
 
251
        OutFile << "typeline(81,1009, step(-1, 0))" << std::endl;
 
252
        OutFile << "typeline(81,1010, step(-1, 0))" << std::endl;
 
253
        OutFile << "typeline(81,1011, step(-1, 0))" << std::endl;
 
254
        OutFile << "movestep(inv(sub(get(\"vBoxSize\"), " << 12 <<")), 0)" << std::endl;
 
255
        // one mappixel further
 
256
        OutFile << "movestep(get(\"vBoxSize\"), 1)" << std::endl;
 
257
        OutFile << "movestep(0, 64)" << std::endl;
 
258
        
 
259
        
 
260
        
 
261
        OutFile << "movestep(0, inv(add(get(\"vBoxSize\"), 1)))" << std::endl;
 
262
        
 
263
        OutFile << "typesector(0, 1012, " << std::endl;
 
264
        OutFile << "step(add(get(\"vBoxSize\"), 2), 0)" << std::endl;
 
265
        OutFile << "step(0, "<< dayintics <<")" << std::endl;
 
266
        OutFile << "step(inv(add(get(\"vBoxSize\"), 2)), 0)" << std::endl;
 
267
        OutFile << "step(0, inv("<< dayintics <<"))" << std::endl;
 
268
        OutFile << "rightsector(0, 128, 255) )" << std::endl;
 
269
        OutFile << "movestep(0, "<< dayintics <<")" << std::endl;
 
270
        OutFile << "typeline(252,1012, step(0, -64))" << std::endl;
 
271
        OutFile << "player1start" << std::endl;
 
272
        OutFile << "movestep(17, 0)" << std::endl;
 
273
        OutFile << "for(1,get(\"voodoos\"), thing movestep(32, 0))" << std::endl;
 
274
        OutFile << "}" << std::endl;
 
275
        LG_CloseOutFile();
 
276
        
 
277
        int sectorcount = 0;
 
278
        int filecount = 0;
 
279
        int count = 0;
 
280
        const int MaxSects = 100;
 
281
        counter = TransferStartTag;
 
282
        for (std::vector<wSector_t *>::iterator it = daySects.begin(); it != daySects.end(); ++it) {
 
283
                if (count % MaxSects == 0) {
 
284
                        if (OutFile.is_open()) {
 
285
                                OutFile << "}" << std::endl;
 
286
                                OutFile << "main {" << std::endl;
 
287
                                OutFile << "rotright" << std::endl;
 
288
                                OutFile << "GetLightManager"<<filecount << std::endl;
 
289
                                OutFile << "}" << std::endl;
 
290
                                ++filecount;
 
291
                                
 
292
                                OutFile.close();
 
293
                        }
 
294
                        std::string str("sector");
 
295
                        str.append(std::to_string(filecount));
 
296
                        str.append(".wl");
 
297
                        OutFile.open(str, std::ios::out);
 
298
                        OutFile << "#\"standard.h\"" << std::endl;
 
299
                        OutFile << "#\"spawns.h\"" << std::endl;
 
300
                        OutFile << "GetLightManager" << filecount << std::endl;
 
301
                        OutFile << "{" << std::endl;
 
302
                        OutFile << "top(\"SKY1\")" << std::endl;
 
303
                        OutFile << "step(24, 0)" << std::endl;
 
304
                        OutFile << "step(0, " << 2*(MaxSects+50)+1 << ")" << std::endl;
 
305
                        OutFile << "step(-24, 0)" << std::endl;
 
306
                        OutFile << "step(0," << -(2*(MaxSects+50)+1) <<")" << std::endl;
 
307
                        OutFile << "typesector(0, 1099, rightsector(-10000, 10000, 255))" << std::endl;
 
308
                }
 
309
                
 
310
                
 
311
                wSector_t &sector = **it;
 
312
                OutFile << "step(-1, 0)" << std::endl;
 
313
                OutFile << "typeline(213, " << sector.Tag << ", step(1, 1))" << std::endl;
 
314
                OutFile << "step(0, -1)" << std::endl;
 
315
                OutFile << "typesector(0, " << (sector.Tag >= StaticTag ? 6666 : counter) << ", leftsector(-10000, 10000, 255))" << std::endl;
 
316
                OutFile << "movestep(-1, 0)" << std::endl;
 
317
                OutFile << "typeline(271, " << sector.Tag << ", step(-1, 1))" << std::endl;
 
318
                OutFile << "typeline(242, " << sector.Tag << ", step(1, 0))" << std::endl;
 
319
                OutFile << "typeline(261, " << sector.Tag << ", step(1, 0))" << std::endl;
 
320
                OutFile << "typeline(213, " << sector.Tag << ", step(-1, -1))" << std::endl;
 
321
                OutFile << "floor(\"";
 
322
                OutFile.write(sector.FloorTexture, std::min<int>(8, strlen(sector.FloorTexture)));
 
323
                OutFile << "\")" << std::endl;
 
324
                OutFile << "ceil(\"";
 
325
                OutFile.write(sector.CeilingTexture, std::min<int>(8, strlen(sector.CeilingTexture)));
 
326
                OutFile << "\")" << std::endl;
 
327
                OutFile << "typesector(0," << (sector.Tag >= StaticTag ? 0 : TagFor242) << ", leftsector("
 
328
                        << sector.FloorHeight + 1 << ", " << sector.FloorHeight + 1 << ", " << sector.Light<<"))" << std::endl;
 
329
                OutFile << "movestep(1, 2)" << std::endl;
 
330
                ++count;
 
331
                ++counter;
 
332
        }
 
333
 
 
334
        if (OutFile.is_open()) {
 
335
                OutFile << "}" << std::endl;
 
336
                OutFile << "main {" << std::endl;
 
337
                OutFile << "rotright" << std::endl;
 
338
                OutFile << "GetLightManager"<<filecount << std::endl;
 
339
                OutFile << "}" << std::endl;
 
340
                OutFile.close();
 
341
        }
 
342
        
 
343
}
 
344
 
 
345
static void Usage()
 
346
{
 
347
        fprintf(stdout, "Usage: boomlightgen [OPTIONS] mapname daywad\n");
 
348
        
 
349
        fprintf(stdout, "  mapname\t Map to use\n");
 
350
        fprintf(stdout, "  daywad\t Wad used for day light\n");
 
351
 
352
 
 
353
int LG_Main(int argc, char **argv)
 
354
{
 
355
        fprintf(stdout, "boomlightgen 0.1 by cybermind - a tool to generate realistic day/night cycle for Boom wads.\n");
 
356
        if (argc < 3) {
 
357
                Usage();
 
358
                system("pause");
 
359
                LC_Exit(1);
 
360
        }
 
361
        std::string mapname(argv[1]);
 
362
        std::string daywad(argv[2]);
 
363
        wFile_t *daywadfile = Wad_Open(daywad.c_str());
 
364
        if (!daywadfile) {
 
365
                LC_Exit(1);
 
366
        }
 
367
 
 
368
        LG_ReadSectors(daywadfile, mapname, AllDaySectors);
 
369
        fprintf(stdout, "daysectors: %d\n", AllDaySectors.size());      
 
370
        
 
371
        LG_GenerateGeometry(AllDaySectors);
 
372
        fprintf(stdout, "done\n");
 
373
        return 0;
 
374
 
375
 
 
376
int main(int argc, char **argv)
 
377
{
 
378
        return LG_Main(argc, argv);
 
379
}
 
 
b'\\ No newline at end of file'