]> icculus.org git repositories - btb/d2x.git/blob - main/editor/eswitch.c
add remove_trigger_num
[btb/d2x.git] / main / editor / eswitch.c
1 /* $Id: eswitch.c,v 1.5 2005-02-26 06:25:37 chris Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Editor switch functions.
18  *
19  */
20
21 #ifdef RCS
22 static char rcsid[] = "$Id: eswitch.c,v 1.5 2005-02-26 06:25:37 chris Exp $";
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #include "conf.h"
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <string.h>
33
34 #include "inferno.h"
35 #include "editor.h"
36 #include "eswitch.h"
37 #include "segment.h"
38 #include "error.h"
39 #include "gameseg.h"
40 #include "mono.h"
41 #include "wall.h"
42 #include "medwall.h"
43
44 #include "screens.h"
45
46 #include "textures.h"
47 #include "texmerge.h"
48 #include "medrobot.h"
49 #include "timer.h"
50 #include "key.h"
51 #include "ehostage.h"
52 #include "centers.h"
53 #include "piggy.h"
54
55 //-------------------------------------------------------------------------
56 // Variables for this module...
57 //-------------------------------------------------------------------------
58 #define NUM_TRIGGER_FLAGS 10
59
60 static UI_WINDOW                                *MainWindow = NULL;
61 static UI_GADGET_USERBOX        *WallViewBox;
62 static UI_GADGET_BUTTON         *QuitButton;
63 static UI_GADGET_CHECKBOX       *TriggerFlag[NUM_TRIGGER_FLAGS];
64
65 static int old_trigger_num;
66
67 //-----------------------------------------------------------------
68 // Adds a trigger to wall, and returns the trigger number. 
69 // If there is a trigger already present, it returns the trigger number. (To be replaced)
70 int add_trigger(segment *seg, short side)
71 {
72         int trigger_num = Num_triggers;
73         int wall_num = seg->sides[side].wall_num;
74
75         Assert(trigger_num < MAX_TRIGGERS);
76         if (trigger_num>=MAX_TRIGGERS) return -1;
77
78         if (wall_num == -1) {
79                 wall_add_to_markedside(WALL_OPEN);
80                 wall_num = seg->sides[side].wall_num;
81                 Walls[wall_num].trigger = trigger_num;
82                 
83                 // Set default values first time trigger is added
84                 Triggers[trigger_num].flags = 0;
85                 Triggers[trigger_num].value = F1_0*5;
86                 Triggers[trigger_num].num_links = 0;
87                 Triggers[trigger_num].flags &= TRIGGER_ON;              
88
89                 Num_triggers++;
90                 return trigger_num;
91         } else {
92                 if (Walls[wall_num].trigger != -1)
93                         return Walls[wall_num].trigger;
94
95                 // Create new trigger.
96                 Walls[wall_num].trigger = trigger_num;
97
98                 // Set default values first time trigger is added
99                 Triggers[trigger_num].flags = 0;
100                 Triggers[trigger_num].value = F1_0*5;
101                 Triggers[trigger_num].num_links = 0;
102                 Triggers[trigger_num].flags &= TRIGGER_ON;
103
104                 Num_triggers++;
105                 return trigger_num;
106         }
107 }               
108
109 //-----------------------------------------------------------------
110 // Adds a specific trigger flag to Markedsegp/Markedside if it is possible.
111 // Automatically adds flag to Connectside if possible unless it is a control trigger.
112 // Returns 1 if trigger flag added.
113 // Returns 0 if trigger flag cannot be added.
114 int trigger_add_to_Markedside(short flag) {
115         int trigger_num; //, ctrigger_num;
116
117         if (!Markedsegp) {
118                 editor_status("No Markedside.");
119                 return 0;
120         }
121
122         // If no child on Markedside return
123         if (!IS_CHILD(Markedsegp->children[Markedside])) return 0;
124
125         trigger_num = add_trigger(Markedsegp, Markedside);
126
127         if (trigger_num == -1) {
128                 editor_status("Cannot add trigger at Markedside.");
129                 return 0;
130         }
131
132         Triggers[trigger_num].flags |= flag;
133
134         return 1;
135 }
136
137 int trigger_remove_flag_from_Markedside(short flag) {
138         int trigger_num; //, ctrigger_num;
139         int wall_num;
140         
141         if (!Markedsegp) {
142                 editor_status("No Markedside.");
143                 return 0;
144         }
145
146         // If no child on Markedside return
147         if (!IS_CHILD(Markedsegp->children[Markedside])) return 0;
148
149         // If no wall just return
150         wall_num = Markedsegp->sides[Markedside].wall_num;
151         if (wall_num == -1) return 0;
152
153         trigger_num = Walls[wall_num].trigger;
154
155         // If flag is already cleared, then don't change anything.
156         if ( trigger_num == -1 ) {
157                 editor_status("No trigger at Markedside.");
158                 return 0;
159         }
160
161         if (!Triggers[trigger_num].flags & flag)
162                 return 1;
163
164         Triggers[trigger_num].flags &= ~flag;
165
166         return 1;
167 }
168
169
170 int bind_matcen_to_trigger() {
171
172         int wall_num, trigger_num, link_num;
173         int i;
174
175         if (!Markedsegp) {
176                 editor_status("No marked segment.");
177                 return 0;
178         }
179
180         wall_num = Markedsegp->sides[Markedside].wall_num;
181         if (wall_num == -1) {
182                 editor_status("No wall at Markedside.");
183                 return 0;
184         }
185
186         trigger_num = Walls[wall_num].trigger;  
187
188         if (trigger_num == -1) {
189                 editor_status("No trigger at Markedside.");
190                 return 0;
191         }
192
193         if (!(Curseg2p->special & SEGMENT_IS_ROBOTMAKER))
194         {
195                 editor_status("No Matcen at Cursegp.");
196                 return 0;
197         }
198
199         link_num = Triggers[trigger_num].num_links;
200         for (i=0;i<link_num;i++)
201                 if (Cursegp-Segments == Triggers[trigger_num].seg[i]) {
202                         editor_status("Matcen already bound to Markedside.");
203                         return 0;
204                 }
205
206         // Error checking completed, actual binding begins
207         Triggers[trigger_num].seg[link_num] = Cursegp - Segments;
208         Triggers[trigger_num].num_links++;
209
210         mprintf((0, "seg %d linked to link_num %d\n",
211                                 Triggers[trigger_num].seg[link_num], link_num)); 
212
213         editor_status("Matcen linked to trigger");
214
215         return 1;
216 }
217
218
219 int bind_wall_to_trigger() {
220
221         int wall_num, trigger_num, link_num;
222         int i;
223
224         if (!Markedsegp) {
225                 editor_status("No marked segment.");
226                 return 0;
227         }
228
229         wall_num = Markedsegp->sides[Markedside].wall_num;
230         if (wall_num == -1) {
231                 editor_status("No wall at Markedside.");
232                 return 0;
233         }
234
235         trigger_num = Walls[wall_num].trigger;  
236
237         if (trigger_num == -1) {
238                 editor_status("No trigger at Markedside.");
239                 return 0;
240         }
241
242         if (Cursegp->sides[Curside].wall_num == -1) {
243                 editor_status("No wall at Curside.");
244                 return 0;
245         }
246
247         if ((Cursegp==Markedsegp) && (Curside==Markedside)) {
248                 editor_status("Cannot bind wall to itself.");
249                 return 0;
250         }
251
252         link_num = Triggers[trigger_num].num_links;
253         for (i=0;i<link_num;i++)
254                 if ((Cursegp-Segments == Triggers[trigger_num].seg[i]) && (Curside == Triggers[trigger_num].side[i])) {
255                         editor_status("Curside already bound to Markedside.");
256                         return 0;
257                 }
258
259         // Error checking completed, actual binding begins
260         Triggers[trigger_num].seg[link_num] = Cursegp - Segments;
261         Triggers[trigger_num].side[link_num] = Curside;
262         Triggers[trigger_num].num_links++;
263
264         mprintf((0, "seg %d:side %d linked to link_num %d\n",
265                                 Triggers[trigger_num].seg[link_num], Triggers[trigger_num].side[link_num], link_num)); 
266
267         editor_status("Wall linked to trigger");
268
269         return 1;
270 }
271
272 int remove_trigger_num(int trigger_num)
273 {
274         if (trigger_num != -1)
275         {
276                 int t, w;
277         
278                 Num_triggers--;
279                 for (t = trigger_num; t < Num_triggers; t++)
280                         Triggers[t] = Triggers[t + 1];
281         
282                 for (w = 0; w < Num_walls; w++)
283                 {
284                         if (Walls[w].trigger == trigger_num)
285                                 Walls[w].trigger = -1;  // a trigger can be shared by multiple walls
286                         else if (Walls[w].trigger > trigger_num) 
287                                 Walls[w].trigger--;
288                 }
289
290                 return 1;
291         }
292
293         editor_status("No trigger to remove");
294         return 0;
295 }
296
297 int remove_trigger(segment *seg, short side)
298 {       
299         if (seg->sides[side].wall_num == -1)
300         {
301                 mprintf((0, "Can't remove trigger from wall_num -1\n"));        
302                 return 0;
303         }
304
305         return remove_trigger_num(Walls[seg->sides[side].wall_num].trigger);
306 }
307
308
309 int add_trigger_control()
310 {
311         trigger_add_to_Markedside(TRIGGER_CONTROL_DOORS);
312         Update_flags = UF_WORLD_CHANGED;
313         return 1;
314 }
315
316 int trigger_remove()
317 {
318         remove_trigger(Markedsegp, Markedside);
319         Update_flags = UF_WORLD_CHANGED;
320         return 1;
321 }
322
323 int trigger_turn_all_ON()
324 {
325         int t;
326
327         for (t=0;t<Num_triggers;t++)
328                 Triggers[t].flags &= TRIGGER_ON;
329         return 1;
330 }
331
332 //-------------------------------------------------------------------------
333 // Called from the editor... does one instance of the trigger dialog box
334 //-------------------------------------------------------------------------
335 int do_trigger_dialog()
336 {
337         int i;
338
339         if (!Markedsegp) {
340                 editor_status("Trigger requires Marked Segment & Side.");
341                 return 0;
342         }
343
344         // Only open 1 instance of this window...
345         if ( MainWindow != NULL ) return 0;
346
347         // Close other windows. 
348         robot_close_window();
349         close_wall_window();
350         close_centers_window();
351         hostage_close_window();
352
353         // Open a window with a quit button
354         MainWindow = ui_open_window( TMAPBOX_X+20, TMAPBOX_Y+20, 765-TMAPBOX_X, 545-TMAPBOX_Y, WIN_DIALOG );
355
356         // These are the checkboxes for each door flag.
357         i = 44;
358         TriggerFlag[0] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Door Control" );        i+=22;
359         TriggerFlag[1] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Shield damage" );       i+=22;
360         TriggerFlag[2] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Energy drain" );                i+=22;
361         TriggerFlag[3] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Exit" );                                        i+=22;
362         TriggerFlag[4] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "One-shot" );                    i+=22;
363         TriggerFlag[5] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Illusion ON" );         i+=22;
364         TriggerFlag[6] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Illusion OFF" );                i+=22;
365         TriggerFlag[7] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Trigger ON" );                  i+=22;
366         TriggerFlag[8] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Matcen Trigger" );      i+=22;
367         TriggerFlag[9] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Secret Exit" );                 i+=22;
368
369         QuitButton = ui_add_gadget_button( MainWindow, 20, i, 48, 40, "Done", NULL );
370                                                                                                                                                                  
371         // The little box the wall will appear in.
372         WallViewBox = ui_add_gadget_userbox( MainWindow, 155, 5, 64, 64 );
373
374         // A bunch of buttons...
375         i = 80;
376 //      ui_add_gadget_button( MainWindow,155,i,140, 26, "Add Door Control", add_trigger_control ); i += 29;
377         ui_add_gadget_button( MainWindow,155,i,140, 26, "Remove Trigger", trigger_remove ); i += 29;
378         ui_add_gadget_button( MainWindow,155,i,140, 26, "Bind Wall", bind_wall_to_trigger ); i += 29;
379         ui_add_gadget_button( MainWindow,155,i,140, 26, "Bind Matcen", bind_matcen_to_trigger ); i += 29;
380         ui_add_gadget_button( MainWindow,155,i,140, 26, "All Triggers ON", trigger_turn_all_ON ); i += 29;
381
382         old_trigger_num = -2;           // Set to some dummy value so everything works ok on the first frame.
383
384         return 1;
385 }
386
387 void close_trigger_window()
388 {
389         if ( MainWindow!=NULL ) {
390                 ui_close_window( MainWindow );
391                 MainWindow = NULL;
392         }
393 }
394
395 void do_trigger_window()
396 {
397         int i;
398         short Markedwall, trigger_num;
399
400         if ( MainWindow == NULL ) return;
401         if (!Markedsegp) {
402                 close_trigger_window();
403                 return;
404         }
405
406         //------------------------------------------------------------
407         // Call the ui code..
408         //------------------------------------------------------------
409         ui_button_any_drawn = 0;
410         ui_window_do_gadgets(MainWindow);
411         
412         //------------------------------------------------------------
413         // If we change walls, we need to reset the ui code for all
414         // of the checkboxes that control the wall flags.  
415         //------------------------------------------------------------
416         Markedwall = Markedsegp->sides[Markedside].wall_num;
417         if (Markedwall != -1)
418                 trigger_num = Walls[Markedwall].trigger;
419         else trigger_num = -1;
420
421         if (old_trigger_num != trigger_num ) {
422                 for (   i=0; i < NUM_TRIGGER_FLAGS; i++ )       {
423                         TriggerFlag[i]->flag = 0;                               // Tells ui that this button isn't checked
424                         TriggerFlag[i]->status = 1;                             // Tells ui to redraw button
425                 }
426
427                 if (trigger_num != -1) {
428                         if (Triggers[trigger_num].flags & TRIGGER_CONTROL_DOORS)
429                                 TriggerFlag[0]->flag = 1;
430                         if (Triggers[trigger_num].flags & TRIGGER_SHIELD_DAMAGE)
431                                 TriggerFlag[1]->flag = 1;
432                         if (Triggers[trigger_num].flags & TRIGGER_ENERGY_DRAIN)
433                                 TriggerFlag[2]->flag = 1;
434                         if (Triggers[trigger_num].flags & TRIGGER_EXIT)
435                                 TriggerFlag[3]->flag = 1;
436                         if (Triggers[trigger_num].flags & TRIGGER_ONE_SHOT)
437                                 TriggerFlag[4]->flag = 1;
438                         if (Triggers[trigger_num].flags & TRIGGER_ILLUSION_ON)
439                                 TriggerFlag[5]->flag = 1;
440                         if (Triggers[trigger_num].flags & TRIGGER_ILLUSION_OFF)
441                                 TriggerFlag[6]->flag = 1;
442                         if (Triggers[trigger_num].flags & TRIGGER_ON)
443                                 TriggerFlag[7]->flag = 1;
444                         if (Triggers[trigger_num].flags & TRIGGER_MATCEN)
445                                 TriggerFlag[8]->flag = 1;
446                         if (Triggers[trigger_num].flags & TRIGGER_SECRET_EXIT)
447                                 TriggerFlag[9]->flag = 1;
448                 }
449         }
450         
451         //------------------------------------------------------------
452         // If any of the checkboxes that control the wallflags are set, then
453         // update the cooresponding wall flag.
454         //------------------------------------------------------------
455         if (IS_CHILD(Markedsegp->children[Markedside])) {
456                 if (TriggerFlag[0]->flag == 1) 
457                         trigger_add_to_Markedside(TRIGGER_CONTROL_DOORS); 
458                 else
459                         trigger_remove_flag_from_Markedside(TRIGGER_CONTROL_DOORS);
460                 if (TriggerFlag[1]->flag == 1)
461                         trigger_add_to_Markedside(TRIGGER_SHIELD_DAMAGE); 
462                 else
463                         trigger_remove_flag_from_Markedside(TRIGGER_SHIELD_DAMAGE);
464                 if (TriggerFlag[2]->flag == 1)
465                         trigger_add_to_Markedside(TRIGGER_ENERGY_DRAIN); 
466                 else
467                         trigger_remove_flag_from_Markedside(TRIGGER_ENERGY_DRAIN);
468                 if (TriggerFlag[3]->flag == 1)
469                         trigger_add_to_Markedside(TRIGGER_EXIT); 
470                 else
471                         trigger_remove_flag_from_Markedside(TRIGGER_EXIT);
472                 if (TriggerFlag[4]->flag == 1)
473                         trigger_add_to_Markedside(TRIGGER_ONE_SHOT); 
474                 else
475                         trigger_remove_flag_from_Markedside(TRIGGER_ONE_SHOT);
476                 if (TriggerFlag[5]->flag == 1)
477                         trigger_add_to_Markedside(TRIGGER_ILLUSION_ON); 
478                 else
479                         trigger_remove_flag_from_Markedside(TRIGGER_ILLUSION_ON);
480                 if (TriggerFlag[6]->flag == 1)
481                         trigger_add_to_Markedside(TRIGGER_ILLUSION_OFF);
482                 else
483                         trigger_remove_flag_from_Markedside(TRIGGER_ILLUSION_OFF);
484                 if (TriggerFlag[7]->flag == 1)
485                         trigger_add_to_Markedside(TRIGGER_ON);
486                 else
487                         trigger_remove_flag_from_Markedside(TRIGGER_ON);
488
489                 if (TriggerFlag[8]->flag == 1) 
490                         trigger_add_to_Markedside(TRIGGER_MATCEN);
491                 else
492                         trigger_remove_flag_from_Markedside(TRIGGER_MATCEN);
493
494                 if (TriggerFlag[9]->flag == 1) 
495                         trigger_add_to_Markedside(TRIGGER_SECRET_EXIT);
496                 else
497                         trigger_remove_flag_from_Markedside(TRIGGER_SECRET_EXIT);
498
499         } else
500                 for (   i=0; i < NUM_TRIGGER_FLAGS; i++ )
501                         if (TriggerFlag[i]->flag == 1) { 
502                                 TriggerFlag[i]->flag = 0;                                       // Tells ui that this button isn't checked
503                                 TriggerFlag[i]->status = 1;                             // Tells ui to redraw button
504                         }
505         
506         //------------------------------------------------------------
507         // Draw the wall in the little 64x64 box
508         //------------------------------------------------------------
509         gr_set_current_canvas( WallViewBox->canvas );
510
511         if ((Markedsegp->sides[Markedside].wall_num == -1) || (Walls[Markedsegp->sides[Markedside].wall_num].trigger) == -1)
512                 gr_clear_canvas( CBLACK );
513         else {
514                 if (Markedsegp->sides[Markedside].tmap_num2 > 0)  {
515                         gr_ubitmap(0,0, texmerge_get_cached_bitmap( Markedsegp->sides[Markedside].tmap_num, Markedsegp->sides[Markedside].tmap_num2));
516                 } else {
517                         if (Markedsegp->sides[Markedside].tmap_num > 0) {
518                                 PIGGY_PAGE_IN(Textures[Markedsegp->sides[Markedside].tmap_num]);
519                                 gr_ubitmap(0,0, &GameBitmaps[Textures[Markedsegp->sides[Markedside].tmap_num].index]);
520                         } else
521                                 gr_clear_canvas( CGREY );
522                 }
523         }
524
525         //------------------------------------------------------------
526         // If anything changes in the ui system, redraw all the text that
527         // identifies this robot.
528         //------------------------------------------------------------
529         if (ui_button_any_drawn || (old_trigger_num != trigger_num) ) {
530                 if ( Markedsegp->sides[Markedside].wall_num > -1 )      {
531                         ui_wprintf_at( MainWindow, 12, 6, "Trigger: %d    ", trigger_num);
532                 }       else {
533                         ui_wprintf_at( MainWindow, 12, 6, "Trigger: none ");
534                 }
535                 Update_flags |= UF_WORLD_CHANGED;
536         }
537
538         if ( QuitButton->pressed || (last_keypress==KEY_ESC))   {
539                 close_trigger_window();
540                 return;
541         }               
542
543         old_trigger_num = trigger_num;
544 }
545
546
547
548
549