1 /*
2  *	s_merge.cc
3  *	Merge sectors
4  *	AYM 1998-02-03
5  */
6 
7 
8 /*
9 This file is part of Yadex.
10 
11 Yadex incorporates code from DEU 5.21 that was put in the public domain in
12 1994 by Rapha�l Quinet and Brendon Wyber.
13 
14 The rest of Yadex is Copyright � 1997-2003 Andr� Majorel and others.
15 
16 This program is free software; you can redistribute it and/or modify it under
17 the terms of the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any later
19 version.
20 
21 This program is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License along with
26 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
27 Place, Suite 330, Boston, MA 02111-1307, USA.
28 */
29 
30 
31 #include "yadex.h"
32 #include "dialog.h"
33 #include "levels.h"
34 #include "objects.h"
35 #include "objid.h"
36 #include "selectn.h"
37 
38 
39 /*
40    merge two or more Sectors into one
41 */
42 
MergeSectors(SelPtr * slist)43 void MergeSectors (SelPtr *slist) /* SWAP! */
44 {
45 SelPtr cur;
46 int    n, olds, news;
47 
48 /* save the first Sector number */
49 news = (*slist)->objnum;
50 UnSelectObject (slist, news);
51 ObjectsNeeded (OBJ_SIDEDEFS, 0);
52 
53 /* change all SideDefs references to the other Sectors */
54 for (cur = *slist; cur; cur = cur->next)
55 {
56    olds = cur->objnum;
57    for (n = 0; n < NumSideDefs; n++)
58    {
59       if (SideDefs[n].sector == olds)
60 	 SideDefs[n].sector = news;
61    }
62 }
63 
64 /* delete the Sectors */
65 DeleteObjects (OBJ_SECTORS, slist);
66 
67 /* the returned list contains only the first Sector */
68 SelectObject (slist, news);
69 }
70 
71 
72 
73 /*
74    delete one or several two-sided LineDefs and join the two Sectors
75 */
76 
DeleteLineDefsJoinSectors(SelPtr * ldlist)77 void DeleteLineDefsJoinSectors (SelPtr *ldlist) /* SWAP! */
78 {
79 SelPtr cur, slist;
80 int    sd1, sd2;
81 int    s1, s2;
82 char   msg[80];
83 
84 /* first, do the tests for all LineDefs */
85 for (cur = *ldlist; cur; cur = cur->next)
86    {
87    ObjectsNeeded (OBJ_LINEDEFS, 0);
88    sd1 = LineDefs[cur->objnum].sidedef1;
89    sd2 = LineDefs[cur->objnum].sidedef2;
90    if (sd1 < 0 || sd2 < 0)
91       {
92       Beep ();
93       sprintf (msg, "ERROR: Linedef #%d has only one side", cur->objnum);
94       Notify (-1, -1, msg, NULL);
95       return;
96       }
97    ObjectsNeeded (OBJ_SIDEDEFS, 0);
98    s1 = SideDefs[sd1].sector;
99    s2 = SideDefs[sd2].sector;
100    if (s1 < 0 || s2 < 0)
101       {
102       Beep ();
103       sprintf (msg, "ERROR: Linedef #%d has two sides, but one", cur->objnum);
104       Notify (-1, -1, msg, "side is not bound to any sector");
105       return;
106       }
107    }
108 
109 /* then join the Sectors and delete the LineDefs */
110 for (cur = *ldlist; cur; cur = cur->next)
111    {
112    ObjectsNeeded (OBJ_LINEDEFS, 0);
113    sd1 = LineDefs[cur->objnum].sidedef1;
114    sd2 = LineDefs[cur->objnum].sidedef2;
115    ObjectsNeeded (OBJ_SIDEDEFS, 0);
116    s1 = SideDefs[sd1].sector;
117    s2 = SideDefs[sd2].sector;
118    slist = NULL;
119    SelectObject (&slist, s2);
120    SelectObject (&slist, s1);
121    MergeSectors (&slist);
122    ForgetSelection (&slist);
123    }
124 DeleteObjects (OBJ_LINEDEFS, ldlist);
125 }
126 
127 
128 
129