1 /*
2  *	s_lift.cc
3  *	Make lift from sector
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    turn a Sector into a lift: change the linedefs and sidedefs
41 */
42 
MakeLiftFromSector(int sector)43 void MakeLiftFromSector (int sector) /* SWAP! */
44 {
45 int    sd1, sd2;
46 int    n, s, tag;
47 SelPtr ldok, ldflip, ld1s;
48 SelPtr sect, curs;
49 int    minh, maxh;
50 
51 ldok = NULL;
52 ldflip = NULL;
53 ld1s = NULL;
54 sect = NULL;
55 /* build lists of linedefs that border the Sector */
56 for (n = 0; n < NumLineDefs; n++)
57 {
58    ObjectsNeeded (OBJ_LINEDEFS, 0);
59    sd1 = LineDefs[n].sidedef1;
60    sd2 = LineDefs[n].sidedef2;
61    if (sd1 >= 0 && sd2 >= 0)
62    {
63       ObjectsNeeded (OBJ_SIDEDEFS, 0);
64       if (SideDefs[sd2].sector == sector)
65       {
66 	 SelectObject (&ldok, n); /* already ok */
67 	 s = SideDefs[sd1].sector;
68 	 if (s != sector && !IsSelected (sect, s))
69 	    SelectObject (&sect, s);
70       }
71       if (SideDefs[sd1].sector == sector)
72       {
73 	 SelectObject (&ldflip, n); /* will be flipped */
74 	 s = SideDefs[sd2].sector;
75 	 if (s != sector && !IsSelected (sect, s))
76 	    SelectObject (&sect, s);
77       }
78    }
79    else if (sd1 >= 0 && sd2 < 0)
80    {
81       ObjectsNeeded (OBJ_SIDEDEFS, 0);
82       if (SideDefs[sd1].sector == sector)
83 	 SelectObject (&ld1s, n); /* wall (one-sided) */
84    }
85 }
86 /* there must be a way to go on the lift... */
87 if (sect == NULL)
88 {
89    Beep ();
90    Notify (-1, -1, "The lift must be connected to at least one other Sector.", NULL);
91    ForgetSelection (&ldok);
92    ForgetSelection (&ldflip);
93    ForgetSelection (&ld1s);
94    return;
95 }
96 /* flip the linedefs that have the wrong orientation */
97 if (ldflip != NULL)
98    FlipLineDefs (ldflip, 1);
99 /* merge the two selection lists */
100 while (ldflip != NULL)
101 {
102    if (!IsSelected (ldok, ldflip->objnum))
103       SelectObject (&ldok, ldflip->objnum);
104    UnSelectObject (&ldflip, ldflip->objnum);
105 }
106 
107 /* find a free tag number */
108 tag = FindFreeTag ();
109 
110 /* find the minimum and maximum altitudes */
111 ObjectsNeeded (OBJ_SECTORS, 0);
112 minh = 32767;
113 maxh = -32767;
114 for (curs = sect; curs; curs = curs->next)
115    {
116    if (Sectors[curs->objnum].floorh < minh)
117       minh = Sectors[curs->objnum].floorh;
118    if (Sectors[curs->objnum].floorh > maxh)
119       maxh = Sectors[curs->objnum].floorh;
120    }
121 ForgetSelection (&sect);
122 
123 /* change the lift's floor height if necessary */
124 if (Sectors[sector].floorh < maxh)
125    Sectors[sector].floorh = maxh;
126 
127 /* change the lift's ceiling height if necessary */
128 if (Sectors[sector].ceilh < maxh + DOOM_PLAYER_HEIGHT)
129    Sectors[sector].ceilh = maxh + DOOM_PLAYER_HEIGHT;
130 
131 /* assign the new tag number to the lift */
132 Sectors[sector].tag = tag;
133 
134 /* change the linedefs and sidedefs */
135 while (ldok != NULL)
136    {
137    /* give the "lower lift" type and flags to the linedef */
138    ObjectsNeeded (OBJ_LINEDEFS, 0);
139    n = ldok->objnum;
140    LineDefs[n].type = 62; /* lower lift (switch) */
141    LineDefs[n].flags = 0x04;
142    LineDefs[n].tag = tag;
143    sd1 = LineDefs[n].sidedef1; /* outside */
144    sd2 = LineDefs[n].sidedef2; /* inside */
145    /* adjust the textures for the sidedef visible from the outside */
146    ObjectsNeeded (OBJ_SIDEDEFS, 0);
147    if (strncmp (SideDefs[sd1].tex3, "-", WAD_TEX_NAME))
148       {
149       if (!strncmp (SideDefs[sd1].tex2, "-", WAD_TEX_NAME))
150 	 strncpy (SideDefs[sd1].tex2, SideDefs[sd1].tex3, WAD_TEX_NAME);
151       strncpy (SideDefs[sd1].tex3, "-", WAD_TEX_NAME);
152       }
153    if (!strncmp (SideDefs[sd1].tex2, "-", WAD_TEX_NAME))
154       strncpy (SideDefs[sd1].tex2, "SHAWN2", WAD_TEX_NAME);
155    /* adjust the textures for the sidedef visible from the lift */
156    strncpy (SideDefs[sd2].tex3, "-", WAD_TEX_NAME);
157    s = SideDefs[sd1].sector;
158    ObjectsNeeded (OBJ_SECTORS, 0);
159    if (Sectors[s].floorh > minh)
160       {
161       ObjectsNeeded (OBJ_SIDEDEFS, 0);
162       if (strncmp (SideDefs[sd2].tex3, "-", WAD_TEX_NAME))
163       {
164 	 if (!strncmp (SideDefs[sd2].tex2, "-", WAD_TEX_NAME))
165 	    strncpy (SideDefs[sd2].tex2, SideDefs[sd1].tex3, WAD_TEX_NAME);
166 	 strncpy (SideDefs[sd2].tex3, "-", WAD_TEX_NAME);
167       }
168       if (!strncmp (SideDefs[sd2].tex2, "-", WAD_TEX_NAME))
169 	 strncpy (SideDefs[sd2].tex2, "SHAWN2", WAD_TEX_NAME);
170       }
171    else
172       {
173       ObjectsNeeded (OBJ_SIDEDEFS, 0);
174       strncpy (SideDefs[sd2].tex2, "-", WAD_TEX_NAME);
175       }
176    strncpy (SideDefs[sd2].tex3, "-", WAD_TEX_NAME);
177    ObjectsNeeded (OBJ_SECTORS, 0);
178    /* if the ceiling of the sector is lower than that of the lift */
179    if (Sectors[s].ceilh < Sectors[sector].ceilh)
180       {
181       ObjectsNeeded (OBJ_SIDEDEFS, 0);
182       if (strncmp (SideDefs[sd2].tex1, "-", WAD_TEX_NAME))
183 	 strncpy (SideDefs[sd2].tex1, default_upper_texture, WAD_TEX_NAME);
184       }
185    ObjectsNeeded (OBJ_SECTORS, 0);
186    /* if the floor of the sector is above the lift */
187    if (Sectors[s].floorh >= Sectors[sector].floorh)
188       {
189       ObjectsNeeded (OBJ_LINEDEFS, 0);
190       LineDefs[n].type = 88; /* lower lift (walk through) */
191       /* flip it, just for fun */
192       curs = NULL;
193       SelectObject (&curs, n);
194       FlipLineDefs (curs, 1);
195       ForgetSelection (&curs);
196       }
197    /* done with this linedef */
198    UnSelectObject (&ldok, n);
199    }
200 
201 while (ld1s != NULL)
202    {
203    /* these are the lift walls (one-sided) */
204    ObjectsNeeded (OBJ_LINEDEFS, 0);
205    n = ld1s->objnum;
206    LineDefs[n].flags = 0x01;
207    sd1 = LineDefs[n].sidedef1;
208    /* adjust the textures for the sidedef */
209    ObjectsNeeded (OBJ_SIDEDEFS, 0);
210    if (!strncmp (SideDefs[sd1].tex3, "-", WAD_TEX_NAME))
211       strncpy (SideDefs[sd1].tex3, default_middle_texture, WAD_TEX_NAME);
212    strncpy (SideDefs[sd1].tex1, "-", WAD_TEX_NAME);
213    strncpy (SideDefs[sd1].tex2, "-", WAD_TEX_NAME);
214    UnSelectObject (&ld1s, n);
215    }
216 }
217 
218 
219 
220