1 /* $Id: eswitch.c,v 1.4 2004-12-24 05:17:09 btb Exp $ */
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.
17 * Editor switch functions.
22 static char rcsid[] = "$Id: eswitch.c,v 1.4 2004-12-24 05:17:09 btb Exp $";
55 //-------------------------------------------------------------------------
56 // Variables for this module...
57 //-------------------------------------------------------------------------
58 #define NUM_TRIGGER_FLAGS 10
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];
65 static int old_trigger_num;
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)
72 int trigger_num = Num_triggers;
73 int wall_num = seg->sides[side].wall_num;
75 Assert(trigger_num < MAX_TRIGGERS);
76 if (trigger_num>=MAX_TRIGGERS) return -1;
79 wall_add_to_markedside(WALL_OPEN);
80 wall_num = seg->sides[side].wall_num;
81 Walls[wall_num].trigger = trigger_num;
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;
92 if (Walls[wall_num].trigger != -1)
93 return Walls[wall_num].trigger;
95 // Create new trigger.
96 Walls[wall_num].trigger = trigger_num;
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;
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;
118 editor_status("No Markedside.");
122 // If no child on Markedside return
123 if (!IS_CHILD(Markedsegp->children[Markedside])) return 0;
125 trigger_num = add_trigger(Markedsegp, Markedside);
127 if (trigger_num == -1) {
128 editor_status("Cannot add trigger at Markedside.");
132 Triggers[trigger_num].flags |= flag;
137 int trigger_remove_flag_from_Markedside(short flag) {
138 int trigger_num; //, ctrigger_num;
142 editor_status("No Markedside.");
146 // If no child on Markedside return
147 if (!IS_CHILD(Markedsegp->children[Markedside])) return 0;
149 // If no wall just return
150 wall_num = Markedsegp->sides[Markedside].wall_num;
151 if (wall_num == -1) return 0;
153 trigger_num = Walls[wall_num].trigger;
155 // If flag is already cleared, then don't change anything.
156 if ( trigger_num == -1 ) {
157 editor_status("No trigger at Markedside.");
161 if (!Triggers[trigger_num].flags & flag)
164 Triggers[trigger_num].flags &= ~flag;
170 int bind_matcen_to_trigger() {
172 int wall_num, trigger_num, link_num;
176 editor_status("No marked segment.");
180 wall_num = Markedsegp->sides[Markedside].wall_num;
181 if (wall_num == -1) {
182 editor_status("No wall at Markedside.");
186 trigger_num = Walls[wall_num].trigger;
188 if (trigger_num == -1) {
189 editor_status("No trigger at Markedside.");
193 if (!(Curseg2p->special & SEGMENT_IS_ROBOTMAKER))
195 editor_status("No Matcen at Cursegp.");
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.");
206 // Error checking completed, actual binding begins
207 Triggers[trigger_num].seg[link_num] = Cursegp - Segments;
208 Triggers[trigger_num].num_links++;
210 mprintf((0, "seg %d linked to link_num %d\n",
211 Triggers[trigger_num].seg[link_num], link_num));
213 editor_status("Matcen linked to trigger");
219 int bind_wall_to_trigger() {
221 int wall_num, trigger_num, link_num;
225 editor_status("No marked segment.");
229 wall_num = Markedsegp->sides[Markedside].wall_num;
230 if (wall_num == -1) {
231 editor_status("No wall at Markedside.");
235 trigger_num = Walls[wall_num].trigger;
237 if (trigger_num == -1) {
238 editor_status("No trigger at Markedside.");
242 if (Cursegp->sides[Curside].wall_num == -1) {
243 editor_status("No wall at Curside.");
247 if ((Cursegp==Markedsegp) && (Curside==Markedside)) {
248 editor_status("Cannot bind wall to itself.");
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.");
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++;
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));
267 editor_status("Wall linked to trigger");
272 int remove_trigger(segment *seg, short side)
274 int trigger_num, t, w;
276 if (seg->sides[side].wall_num == -1) {
277 mprintf((0, "Can't remove trigger from wall_num -1\n"));
281 trigger_num = Walls[seg->sides[side].wall_num].trigger;
283 if (trigger_num != -1) {
284 Walls[seg->sides[side].wall_num].trigger = -1;
285 for (t=trigger_num;t<Num_triggers-1;t++)
286 Triggers[t] = Triggers[t+1];
288 for (w=0; w<Num_walls; w++) {
289 if (Walls[w].trigger > trigger_num)
294 for (t=0;t<Num_walls;t++)
295 if (Walls[seg->sides[side].wall_num].trigger > trigger_num)
296 Walls[seg->sides[side].wall_num].trigger--;
301 editor_status("No trigger to remove");
306 int add_trigger_control()
308 trigger_add_to_Markedside(TRIGGER_CONTROL_DOORS);
309 Update_flags = UF_WORLD_CHANGED;
315 remove_trigger(Markedsegp, Markedside);
316 Update_flags = UF_WORLD_CHANGED;
320 int trigger_turn_all_ON()
324 for (t=0;t<Num_triggers;t++)
325 Triggers[t].flags &= TRIGGER_ON;
329 //-------------------------------------------------------------------------
330 // Called from the editor... does one instance of the trigger dialog box
331 //-------------------------------------------------------------------------
332 int do_trigger_dialog()
337 editor_status("Trigger requires Marked Segment & Side.");
341 // Only open 1 instance of this window...
342 if ( MainWindow != NULL ) return 0;
344 // Close other windows.
345 robot_close_window();
347 close_centers_window();
348 hostage_close_window();
350 // Open a window with a quit button
351 MainWindow = ui_open_window( TMAPBOX_X+20, TMAPBOX_Y+20, 765-TMAPBOX_X, 545-TMAPBOX_Y, WIN_DIALOG );
353 // These are the checkboxes for each door flag.
355 TriggerFlag[0] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Door Control" ); i+=22;
356 TriggerFlag[1] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Shield damage" ); i+=22;
357 TriggerFlag[2] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Energy drain" ); i+=22;
358 TriggerFlag[3] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Exit" ); i+=22;
359 TriggerFlag[4] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "One-shot" ); i+=22;
360 TriggerFlag[5] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Illusion ON" ); i+=22;
361 TriggerFlag[6] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Illusion OFF" ); i+=22;
362 TriggerFlag[7] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Trigger ON" ); i+=22;
363 TriggerFlag[8] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Matcen Trigger" ); i+=22;
364 TriggerFlag[9] = ui_add_gadget_checkbox( MainWindow, 22, i, 16, 16, 0, "Secret Exit" ); i+=22;
366 QuitButton = ui_add_gadget_button( MainWindow, 20, i, 48, 40, "Done", NULL );
368 // The little box the wall will appear in.
369 WallViewBox = ui_add_gadget_userbox( MainWindow, 155, 5, 64, 64 );
371 // A bunch of buttons...
373 // ui_add_gadget_button( MainWindow,155,i,140, 26, "Add Door Control", add_trigger_control ); i += 29;
374 ui_add_gadget_button( MainWindow,155,i,140, 26, "Remove Trigger", trigger_remove ); i += 29;
375 ui_add_gadget_button( MainWindow,155,i,140, 26, "Bind Wall", bind_wall_to_trigger ); i += 29;
376 ui_add_gadget_button( MainWindow,155,i,140, 26, "Bind Matcen", bind_matcen_to_trigger ); i += 29;
377 ui_add_gadget_button( MainWindow,155,i,140, 26, "All Triggers ON", trigger_turn_all_ON ); i += 29;
379 old_trigger_num = -2; // Set to some dummy value so everything works ok on the first frame.
384 void close_trigger_window()
386 if ( MainWindow!=NULL ) {
387 ui_close_window( MainWindow );
392 void do_trigger_window()
395 short Markedwall, trigger_num;
397 if ( MainWindow == NULL ) return;
399 close_trigger_window();
403 //------------------------------------------------------------
404 // Call the ui code..
405 //------------------------------------------------------------
406 ui_button_any_drawn = 0;
407 ui_window_do_gadgets(MainWindow);
409 //------------------------------------------------------------
410 // If we change walls, we need to reset the ui code for all
411 // of the checkboxes that control the wall flags.
412 //------------------------------------------------------------
413 Markedwall = Markedsegp->sides[Markedside].wall_num;
414 if (Markedwall != -1)
415 trigger_num = Walls[Markedwall].trigger;
416 else trigger_num = -1;
418 if (old_trigger_num != trigger_num ) {
419 for ( i=0; i < NUM_TRIGGER_FLAGS; i++ ) {
420 TriggerFlag[i]->flag = 0; // Tells ui that this button isn't checked
421 TriggerFlag[i]->status = 1; // Tells ui to redraw button
424 if (trigger_num != -1) {
425 if (Triggers[trigger_num].flags & TRIGGER_CONTROL_DOORS)
426 TriggerFlag[0]->flag = 1;
427 if (Triggers[trigger_num].flags & TRIGGER_SHIELD_DAMAGE)
428 TriggerFlag[1]->flag = 1;
429 if (Triggers[trigger_num].flags & TRIGGER_ENERGY_DRAIN)
430 TriggerFlag[2]->flag = 1;
431 if (Triggers[trigger_num].flags & TRIGGER_EXIT)
432 TriggerFlag[3]->flag = 1;
433 if (Triggers[trigger_num].flags & TRIGGER_ONE_SHOT)
434 TriggerFlag[4]->flag = 1;
435 if (Triggers[trigger_num].flags & TRIGGER_ILLUSION_ON)
436 TriggerFlag[5]->flag = 1;
437 if (Triggers[trigger_num].flags & TRIGGER_ILLUSION_OFF)
438 TriggerFlag[6]->flag = 1;
439 if (Triggers[trigger_num].flags & TRIGGER_ON)
440 TriggerFlag[7]->flag = 1;
441 if (Triggers[trigger_num].flags & TRIGGER_MATCEN)
442 TriggerFlag[8]->flag = 1;
443 if (Triggers[trigger_num].flags & TRIGGER_SECRET_EXIT)
444 TriggerFlag[9]->flag = 1;
448 //------------------------------------------------------------
449 // If any of the checkboxes that control the wallflags are set, then
450 // update the cooresponding wall flag.
451 //------------------------------------------------------------
452 if (IS_CHILD(Markedsegp->children[Markedside])) {
453 if (TriggerFlag[0]->flag == 1)
454 trigger_add_to_Markedside(TRIGGER_CONTROL_DOORS);
456 trigger_remove_flag_from_Markedside(TRIGGER_CONTROL_DOORS);
457 if (TriggerFlag[1]->flag == 1)
458 trigger_add_to_Markedside(TRIGGER_SHIELD_DAMAGE);
460 trigger_remove_flag_from_Markedside(TRIGGER_SHIELD_DAMAGE);
461 if (TriggerFlag[2]->flag == 1)
462 trigger_add_to_Markedside(TRIGGER_ENERGY_DRAIN);
464 trigger_remove_flag_from_Markedside(TRIGGER_ENERGY_DRAIN);
465 if (TriggerFlag[3]->flag == 1)
466 trigger_add_to_Markedside(TRIGGER_EXIT);
468 trigger_remove_flag_from_Markedside(TRIGGER_EXIT);
469 if (TriggerFlag[4]->flag == 1)
470 trigger_add_to_Markedside(TRIGGER_ONE_SHOT);
472 trigger_remove_flag_from_Markedside(TRIGGER_ONE_SHOT);
473 if (TriggerFlag[5]->flag == 1)
474 trigger_add_to_Markedside(TRIGGER_ILLUSION_ON);
476 trigger_remove_flag_from_Markedside(TRIGGER_ILLUSION_ON);
477 if (TriggerFlag[6]->flag == 1)
478 trigger_add_to_Markedside(TRIGGER_ILLUSION_OFF);
480 trigger_remove_flag_from_Markedside(TRIGGER_ILLUSION_OFF);
481 if (TriggerFlag[7]->flag == 1)
482 trigger_add_to_Markedside(TRIGGER_ON);
484 trigger_remove_flag_from_Markedside(TRIGGER_ON);
486 if (TriggerFlag[8]->flag == 1)
487 trigger_add_to_Markedside(TRIGGER_MATCEN);
489 trigger_remove_flag_from_Markedside(TRIGGER_MATCEN);
491 if (TriggerFlag[9]->flag == 1)
492 trigger_add_to_Markedside(TRIGGER_SECRET_EXIT);
494 trigger_remove_flag_from_Markedside(TRIGGER_SECRET_EXIT);
497 for ( i=0; i < NUM_TRIGGER_FLAGS; i++ )
498 if (TriggerFlag[i]->flag == 1) {
499 TriggerFlag[i]->flag = 0; // Tells ui that this button isn't checked
500 TriggerFlag[i]->status = 1; // Tells ui to redraw button
503 //------------------------------------------------------------
504 // Draw the wall in the little 64x64 box
505 //------------------------------------------------------------
506 gr_set_current_canvas( WallViewBox->canvas );
508 if ((Markedsegp->sides[Markedside].wall_num == -1) || (Walls[Markedsegp->sides[Markedside].wall_num].trigger) == -1)
509 gr_clear_canvas( CBLACK );
511 if (Markedsegp->sides[Markedside].tmap_num2 > 0) {
512 gr_ubitmap(0,0, texmerge_get_cached_bitmap( Markedsegp->sides[Markedside].tmap_num, Markedsegp->sides[Markedside].tmap_num2));
514 if (Markedsegp->sides[Markedside].tmap_num > 0) {
515 PIGGY_PAGE_IN(Textures[Markedsegp->sides[Markedside].tmap_num]);
516 gr_ubitmap(0,0, &GameBitmaps[Textures[Markedsegp->sides[Markedside].tmap_num].index]);
518 gr_clear_canvas( CGREY );
522 //------------------------------------------------------------
523 // If anything changes in the ui system, redraw all the text that
524 // identifies this robot.
525 //------------------------------------------------------------
526 if (ui_button_any_drawn || (old_trigger_num != trigger_num) ) {
527 if ( Markedsegp->sides[Markedside].wall_num > -1 ) {
528 ui_wprintf_at( MainWindow, 12, 6, "Trigger: %d ", trigger_num);
530 ui_wprintf_at( MainWindow, 12, 6, "Trigger: none ");
532 Update_flags |= UF_WORLD_CHANGED;
535 if ( QuitButton->pressed || (last_keypress==KEY_ESC)) {
536 close_trigger_window();
540 old_trigger_num = trigger_num;