summaryrefslogtreecommitdiff
path: root/editcrse.c
blob: c4f745f87067a07d8887c47d3a522b83dc9565da (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*

    viDOOM - level editor for DOOM

    Copyright (C) 2000  Ian Cowburn (ianc@noddybox.demon.co.uk)

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    -------------------------------------------------------------------------

    SECTOR interactive creation

*/
static const char rcs_id[]="$Id$";

#include "config.h"
#include "globals.h"
#include "editvar.h"
#include "sectors.h"

#include <ctype.h>


/* ---------------------------------------- SECTOR CREATION PRIVATE ROUTINES
*/
static void SwapTexture(DirName a, DirName b)
{
    DirName t;

    strcpy(t,a);
    strcpy(a,b);
    strcpy(b,t);
}


static List ReverseList(List l)
{
    Iterator i;
    List new;

    new=ListNew(sizeof(int));
    
    i=ListIterator(l);

    while(i)
        {
	ListInsert(new,IteratorData(i));
	i=IteratorNext(i);
	}

    return(new);
}


static List CopyList(List l)
{
    Iterator i;
    List new;

    new=ListNew(sizeof(int));
    
    i=ListIterator(l);

    while(i)
        {
	ListAppend(new,IteratorData(i));
	i=IteratorNext(i);
	}

    return(new);
}


/* ---------------------------------------- SECTOR CREATION ROUTINES
*/
int CreateUnboundSector(int special,int floor,int ceiling,int light,int tag,
					    DirName floor_t,DirName ceiling_t)
{
    Object o;
    EditSect *s;
    int sn;

    TRACE;

    sn=MapSize(sector);

    s=Grab(sizeof(EditSect));

    s->no=sn;
    s->s.special=special;
    s->s.floor=floor;
    s->s.ceiling=ceiling;
    s->s.light=light;
    strcpy(s->s.floor_t,floor_t);
    strcpy(s->s.ceiling_t,ceiling_t);
    s->v=ListNew(sizeof(Short));
    s->sr=ListNew(sizeof(EditLine *));
    s->sl=ListNew(sizeof(EditLine *));
    s->all=ListNew(sizeof(EditLine *));
    s->min_x=s->min_y=s->max_x=s->max_y=0;

    o.select=SELECT_NONE;
    o.data=s;
    MapAdd(sector,sn,&o);

    return(sn);
}

int CreateSector(List in_v)
{
    List v;
    Iterator i;
    int *f;
    int from;
    int to;
    int type;
    int flag;
    int two;
    int first;
    int sect;
    int wr,wl;
    int oxr,oxl;
    DirName sr_upper,sr_middle,sr_lower;
    DirName sl_upper,sl_middle,sl_lower;
    EditSect *s;
    EditVert *v1,*v2;
    int special,floor,ceiling,light;
    int in_floor,in_ceiling,in_light;
    DirName floor_t,ceiling_t;
    int in_sector,new_sec;
    int swap;
    int nl;
    int style_flag;
    List left_offset;

    TRACE;

    /* Check enough vertexes
    */
    if (ListSize(in_v)<3)
	{
	GuiInfoBox("ERROR","Need 3 or more vertices|"
				"to create a sector");
	FullRedraw();
	return(FALSE);
	}

    /* Check all points lie either inside the same sector or outside a sector
    */
    in_sector=-1;

    i=ListIterator(in_v);

    f=IteratorData(i);
    v1=GETVERT(*f);
    in_sector=SectorHoldingPoint(v1->v.x,v1->v.y);
    i=IteratorNext(i);

    /* Check to see if the vertices are all inside the same sector.  If some or
       all of the vertices lie outside the same sector then the sector is
       assumed to not be in another one
    */
    while(i)
    	{
	f=IteratorData(i);
	v1=GETVERT(*f);

	new_sec=SectorHoldingPoint(v1->v.x,v1->v.y);

	if ((in_sector!=-1)&&(new_sec!=in_sector))
	    in_sector=-1;

	i=IteratorNext(i);
	}

    if (in_sector!=-1)
	{
    	s=GETSECT(in_sector);
	light=in_light=s->s.light;
	floor=in_floor=s->s.floor;
	ceiling=in_ceiling=s->s.ceiling;
	}
    else
	{
    	s=NULL;
	light=in_light=default_light_level;
	floor=in_floor=default_floor_height;
	ceiling=in_ceiling=default_ceiling_height;
	}

    special=0;

    /* Now get the linedef and sector values
    */
    if (!GetSectorValues(&type,&flag,&two,
			 &floor,&ceiling,&light,
			 in_floor,in_ceiling,
			 &style_flag,
			 floor_t,ceiling_t,
			 sr_upper,sr_middle,sr_lower,
			 sl_upper,sl_middle,sl_lower))
	return(FALSE);

    /* See if the sector is inside another sector and see if the user wants
       the new sectors right sidedefs pointing out into the containing sector
    */
    if (in_sector!=-1)
	swap=YesNo("Sector in another sector. Make RIGHT SIDEDEF point out?");
    else
	swap=FALSE;


    /* Past here the user cannot cancel the create operation, so now create
       a copy of the vertex list (reversed if necessary) and reverse the
       textures as necessary
    */
    if (swap)
    	{
	if (two)
	    {
	    SwapTexture(sr_upper,sl_upper);
	    SwapTexture(sr_middle,sl_middle);
	    SwapTexture(sr_lower,sl_lower);
	    }

    	v=ReverseList(in_v);
	}
    else
    	v=CopyList(in_v);

    /* Add either lower/upper unpegged according to the sector style flags
    */
    if (style_flag&SSTYLE_UPPER_PEG)
    	flag|=upper_peg_mask;

    if (style_flag&SSTYLE_LOWER_PEG)
    	flag|=lower_peg_mask;

    /* Create list for left offsets if needed
    */
    if (two)
	left_offset=ListNew(sizeof(int));
    else
    	left_offset=NULL;

    /* Calc texture widths and get a new sector number
    */
    oxr=0;
    oxl=0;

    wr=CalcTextureWidth(sr_upper,sr_middle,sr_lower);

    if (two)
	wl=CalcTextureWidth
			(sl_upper,sl_middle,sl_lower);
    else
	wl=0;

    if (swap)
    	{
	sect=in_sector;
	in_sector=CreateUnboundSector
			(special,floor,ceiling,light,0,
			 floor_t,ceiling_t);
	}
    else
	sect=CreateUnboundSector
			(special,floor,ceiling,light,0,
			 floor_t,ceiling_t);

    i=ListIterator(v);

    memcpy(&to,IteratorData(i),sizeof(int));
    i=IteratorNext(i);
    first=to;

    while(i)
	{
	from=to;
	memcpy(&to,IteratorData(i),sizeof(int));
	i=IteratorNext(i);

	nl=CreateNewLinedef
		    (from,to,
		     flag,type,0,two,
		     oxr,0,sect,
		     sr_upper,sr_middle,sr_lower,
		     oxl,0,in_sector,
		     sl_upper,sl_middle,sl_lower);

	v1=GETVERT(from);
	v2=GETVERT(to);

	IntListUniqAdd(v1->l,nl);
	IntListUniqAdd(v2->l,nl);

	if (wr)
	    oxr=(oxr+Len(v1->v.x,v1->v.y,
			    v2->v.x,v2->v.y))%wr;

	/* The left sidedef has the offsets coming from the opposite direction
	    so have to be applied in reverse once all the lines have been
	    created.  Damn, wish I'd realised that earlier
	*/
	if (two)
	    ListInsert(left_offset,&nl);
	}

    /* Create the line to close the polygon
    */
    nl=CreateNewLinedef(to,first,
			flag,type,0,two,
			oxr,0,sect,
			sr_upper,sr_middle,sr_lower,
			oxl,0,in_sector,
			sl_upper,sl_middle,sl_lower);

    v1=GETVERT(to);
    v2=GETVERT(first);

    IntListUniqAdd(v1->l,nl);
    IntListUniqAdd(v2->l,nl);

    if (two)
	ListInsert(left_offset,&nl);

    /* Apply the left offsets
    */
    if (two)
	{
	EditLine *l;

	i=ListIterator(left_offset);

	while(i)
	    {
	    f=IteratorData(i);

	    l=GETLINE(*f);

	    if (wl)
		oxl=(oxl+Len(l->v[0]->v.x,l->v[0]->v.y,
				l->v[1]->v.x,l->v[1]->v.y))%wl;

	    l->sl->x=oxl;

	    i=IteratorNext(i);
	    }

	ListClear(left_offset);
	}

    /* Calc sector bounds
    */
    if (swap)
	s=GETSECT(in_sector);
    else
	s=GETSECT(sect);

    SectorCalcContainingAll();
    SectorCalcBounding(s);

    ListClear(v);

    /* Check for mergeable LINEDEFS
    */
    i=ListIterator(s->v);

    while(i)
    	{
	Short *s;

	s=IteratorData(i);
	CheckMergeLinedef(GETVERT(*s));
	i=IteratorNext(i);
	}

    /* Recalc containing in case LINEDEFS where merged
    */
    SectorCalcContainingAll();

    return(TRUE);
}


/* END OF FILE */