File: TriggerDialog.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (826 lines) | stat: -rw-r--r-- 25,647 bytes parent folder | download | duplicates (2)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* 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 3 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, see <http://www.gnu.org/licenses/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/Main/editor/TriggerDialog.cpp $
 * $Revision: 1.1.1.1 $
 * $Date: 2003-08-26 03:57:39 $
 * $Author: kevinb $
 *
 * Trigger keypad dialog.
 *
 * $Log: not supported by cvs2svn $
 *
 * 31    6/15/99 5:33p Matt
 * Fixed small stupid bug.
 *
 * 30    4/03/99 7:55p Matt
 * Use new keypad update system instead of timer to update.
 *
 * 29    4/03/99 3:41p Kevin
 * Put in code for "Edit Script" button
 *
 * 28    3/30/99 4:11p Matt
 * Set world changed flag when trigger deleted.
 *
 * 27    3/27/99 7:10p Matt
 * Strip leading & trailing spaces before check for duplicate names.
 *
 * 26    3/23/99 5:52p Matt
 * Added clutter activators for triggers
 *
 * 25    3/22/99 6:59p Matt
 * Fixed stupid bug:  was assigning UVs to floating trigger face before
 * computing normal.
 *
 * 24    3/01/99 10:39a Matt
 * Strip leading and trailing spaces from object, trigger, and room names
 * on level load and when the names are entered.
 *
 * 23    2/28/99 10:40p Matt
 * Check for name already in use when entering a name.
 *
 * 22    2/11/99 8:30p Matt
 *
 * 21    2/02/99 11:54p Luke
 * Fixed small bug (MattT on Luke's machine)
 *
 * 20    1/21/99 11:34a Matt
 * Got rid of portal triggers.  Since we don't have multi-face portals, a
 * face trigger works fine for a portal.  Also fixed a few editor/trigger
 * bugs.
 *
 * 19    1/15/99 10:51a Matt
 * Disable rename button if no current trigger
 *
 * 18    1/14/99 11:06a Matt
 * Added names to triggers
 *
 * 17    1/08/99 2:56p Samir
 * Ripped out OSIRIS1.
 *
 * 16    10/13/98 3:23p Matt
 * Fixed a problem with the add portal trigger button being enabled when
 * there was already a trigger on the portal's face.  Also, fixed and
 * added some asserts.
 *
 * 15    9/01/98 12:04p Matt
 * Ripped out multi-face portal code
 *
 * 14    6/23/98 2:43p Matt
 * Changed calls to OutrageMessageBox() & Debug_MessageBox() to deal with
 * int return value (instead of bool).
 *
 * 13    2/04/98 6:23p Matt
 * Changed object room number to indicate a terrain cell via a flag.  Got
 * rid of the object flag which used to indicate terrain.
 *
 * 12    1/23/98 5:54p Samir
 * Added new style script support to triggers.
 *
 * 11    1/20/98 4:12p Samir
 * New script housekeeping system.
 *
 * 10    1/15/98 7:34p Matt
 * Revamped error checking when computing face normals
 *
 * 9     9/24/97 6:18p Samir
 * Use script names instead of script id values to identify scripts.
 *
 * 8     9/24/97 2:58p Samir
 * New ScriptSelect.
 *
 * 7     9/15/97 11:51a Matt
 * Disable 'select by number' if no triggers
 *
 * 6     9/15/97 11:38a Matt
 * Added better trigger selection functions
 *
 * 5     9/14/97 11:07p Matt
 * Use new InputNumber() function
 *
 * 4     9/12/97 3:59p Matt
 * Lots of changes to trigger dialog
 *
 * 3     9/08/97 10:01a Matt
 * Ripped out old trigger code
 *
 * $NoKeywords: $
 */

#include <stdlib.h>

#include "stdafx.h"
#include "editor.h"
#include "TriggerDialog.h"
#include "trigger.h"
#include "EditLineDialog.h"
#include "render.h"
#include "trigger.h"
#include "HView.h"
#include "FindIntersection.h"
#include "erooms.h"
//@@#include "d3x.h"

#include "DallasMainDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int Current_trigger = -1; // current editing trigger

/////////////////////////////////////////////////////////////////////////////
// CTriggerDialog dialog

CTriggerDialog::CTriggerDialog(CWnd *pParent /*=NULL*/) : CKeypadDialog(CTriggerDialog::IDD, pParent) {
  //{{AFX_DATA_INIT(CTriggerDialog)
  // NOTE: the ClassWizard will add member initialization here
  //}}AFX_DATA_INIT
}

void CTriggerDialog::DoDataExchange(CDataExchange *pDX) {
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CTriggerDialog)
  // NOTE: the ClassWizard will add DDX and DDV calls here
  //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTriggerDialog, CDialog)
//{{AFX_MSG_MAP(CTriggerDialog)
ON_WM_SIZE()
ON_WM_VSCROLL()
ON_WM_HSCROLL()
ON_WM_PAINT()
ON_WM_HELPINFO()
ON_BN_CLICKED(IDC_TRIG_ADD_TO_CURFACE, OnTrigAddToCurface)
ON_BN_CLICKED(IDC_TRIG_GOTO, OnTrigGoto)
ON_BN_CLICKED(IDC_TRIG_DELETE, OnTrigDelete)
ON_BN_CLICKED(IDC_TRIG_ADD_TO_CURPORTAL, OnTrigAddToCurportal)
ON_BN_CLICKED(IDC_TRIG_ADD_FLOATING, OnTrigAddFloating)
ON_BN_CLICKED(IDC_TRIG_RENDER_FLOATING, OnTrigRenderFloating)
ON_BN_CLICKED(IDC_TRIG_ACTIV_PLAYER, OnTrigActivPlayer)
ON_BN_CLICKED(IDC_TRIG_ACTIV_PLAYER_WEAPONS, OnTrigActivPlayerWeapons)
ON_BN_CLICKED(IDC_TRIG_ACTIV_ROBOT_WEAPONS, OnTrigActivRobotWeapons)
ON_BN_CLICKED(IDC_TRIG_ACTIV_ROBOTS, OnTrigActivRobots)
ON_BN_CLICKED(IDC_TRIG_FLOAT_NARROWER, OnTrigFloatNarrower)
ON_BN_CLICKED(IDC_TRIG_FLOAT_SHORTER, OnTrigFloatShorter)
ON_BN_CLICKED(IDC_TRIG_FLOAT_TALLER, OnTrigFloatTaller)
ON_BN_CLICKED(IDC_TRIG_FLOAT_WIDER, OnTrigFloatWider)
ON_BN_CLICKED(IDC_TRIG_ONESHOT, OnTrigOneShot)
ON_BN_CLICKED(IDC_TRIG_NEXT_PORTAL, OnTrigNextPortal)
ON_BN_CLICKED(IDC_TRIG_NEXT_IN_MINE, OnTrigNextInMine)
ON_BN_CLICKED(IDC_TRIG_NEXT_IN_ROOM, OnTrigNextInRoom)
ON_BN_CLICKED(IDC_TRIG_PREV_IN_MINE, OnTrigPrevInMine)
ON_BN_CLICKED(IDC_TRIG_PREV_IN_ROOM, OnTrigPrevInRoom)
ON_BN_CLICKED(IDC_TRIG_EDIT_NAME, OnTrigEditName)
ON_BN_CLICKED(IDC_TRIG_EDIT_SCRIPT, OnTrigEditScript)
ON_BN_CLICKED(IDC_TRIG_ACTIV_CLUTTER, OnTrigActivClutter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTriggerDialog message handlers

void CTriggerDialog::OnSize(UINT nType, int cx, int cy) {
  CKeypadDialog::OnSize(nType, cx, cy);

  //	make sure we can see a few items in the combo box when we drop it down.
  RECT rect;

  CComboBox *box = (CComboBox *)GetDlgItem(IDC_TRIGGERTYPE);
  if (box) {
    box->GetClientRect(&rect);
    box->SetWindowPos(NULL, 0, 0, rect.right - rect.left, box->GetItemHeight(0) * 10, SWP_NOMOVE | SWP_NOZORDER);
  }
}

void CTriggerDialog::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar) {
  // TODO: Add your message handler code here and/or call default

  CKeypadDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CTriggerDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar) {
  // TODO: Add your message handler code here and/or call default

  CKeypadDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

trigger *FindTrigger(int roomnum, int facenum);

int FindNextTrigInRoom(int roomnum, int start) {
  if ((start == -1) || (Triggers[start].roomnum != roomnum))
    start = 0;
  else
    start++;

  for (int i = 0; i < Num_triggers; i++) {
    int n = (start + i) % Num_triggers;

    if (Triggers[n].roomnum == roomnum)
      return n;
  }

  // Couldn't find one
  return -1;
}

int FindPrevTrigInRoom(int roomnum, int start) {
  if (Triggers[start].roomnum != roomnum)
    start = 0;
  else
    start--;

  for (int i = Num_triggers; i > 0; i--) {
    int n = (start + i) % Num_triggers;

    if (Triggers[n].roomnum == roomnum)
      return n;
  }

  // Couldn't find one
  return -1;
}

void CTriggerDialog::UpdateDialog() {
  bool portal = 0, floating = 0;
  static int last_curroom = -1, last_curface = -1, last_curportal = -1;

  if (Current_trigger >= Num_triggers)
    Current_trigger--;

  // If current room/face/portal change, select current trigger from current room/face/portal
  if ((ROOMNUM(Curroomp) != last_curroom) || (Curface != last_curface)) {
    if (Curroomp->faces[Curface].flags & FF_HAS_TRIGGER)
      Current_trigger = (FindTrigger(ROOMNUM(Curroomp), Curface) - Triggers);
  } else if ((Curportal != -1) && (Curportal != last_curportal))
    if (Curroomp->portals[Curportal].flags & FF_HAS_TRIGGER)
      Current_trigger = (FindTrigger(ROOMNUM(Curroomp), Curroomp->portals[Curportal].portal_face) - Triggers);
  last_curroom = ROOMNUM(Curroomp);
  last_curface = Curface, last_curportal = Curportal;

  // Enable/Disable some buttons, etc.
  ((CButton *)GetDlgItem(IDC_TRIG_NEXT_IN_MINE))->EnableWindow(Num_triggers > 1);
  ((CButton *)GetDlgItem(IDC_TRIG_PREV_IN_MINE))->EnableWindow(Num_triggers > 1);
  ((CButton *)GetDlgItem(IDC_TRIG_GOTO))->EnableWindow(Num_triggers > 0);
  ((CButton *)GetDlgItem(IDC_TRIG_DELETE))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_EDIT_SCRIPT))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_EDIT_NAME))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_ADD_TO_CURFACE))->EnableWindow(!(Curroomp->faces[Curface].flags & FF_HAS_TRIGGER));
  ((CButton *)GetDlgItem(IDC_TRIG_ADD_TO_CURPORTAL))
      ->EnableWindow((Curportal != -1) &&
                     !(Curroomp->faces[Curroomp->portals[Curportal].portal_face].flags & FF_HAS_TRIGGER) &&
                     !(Curroomp->faces[Curroomp->portals[Curportal].portal_face].flags & FF_HAS_TRIGGER));
  ((CButton *)GetDlgItem(IDC_TRIG_CURRENT_NUM))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_CURRENT_TYPE))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_CURRENT_ROOM))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_CURRENT_FACE))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_CURRENT_FACE))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_CURRENT_TRIG_PORTAL))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_ACTIVATORS))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_PLAYER))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_PLAYER_WEAPONS))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_ROBOT_WEAPONS))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_ROBOTS))->EnableWindow(Current_trigger != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_ONESHOT))->EnableWindow(Current_trigger != -1);

  int n = FindNextTrigInRoom(ROOMNUM(Curroomp), Current_trigger);
  ((CButton *)GetDlgItem(IDC_TRIG_NEXT_IN_ROOM))->EnableWindow((n != -1) && (n != Current_trigger));
  ((CButton *)GetDlgItem(IDC_TRIG_PREV_IN_ROOM))->EnableWindow((n != -1) && (n != Current_trigger));

  // Update current trigger info
  if (Current_trigger > -1) {
    trigger *tp = &Triggers[Current_trigger];
    room *rp = &Rooms[tp->roomnum];
    face *fp = &rp->faces[tp->facenum];

    portal = (fp->portal_num != -1);
    floating = ((fp->flags & FF_FLOATING_TRIG) != 0);
    ASSERT(!(portal && floating)); // Shouldn't be both portal and floating

    PrintToDlgItem(this, IDC_TRIG_CURRENT_NAME, "Name:\t %s", tp->name[0] ? tp->name : "<none>");
    PrintToDlgItem(this, IDC_TRIG_CURRENT_NUM, "Number:\t %d", Current_trigger);
    PrintToDlgItem(this, IDC_TRIG_CURRENT_TYPE, "Type:\t %s", portal ? "PORTAL" : (floating ? "FLOATING" : "FACE"));
    PrintToDlgItem(this, IDC_TRIG_CURRENT_ROOM, "Room:\t%d", tp->roomnum);
    PrintToDlgItem(this, IDC_TRIG_CURRENT_FACE, "Face:\t%d", tp->facenum);
    PrintToDlgItem(this, IDC_TRIG_CURRENT_TRIG_PORTAL, (portal) ? "Portal:\t%d" : "", fp->portal_num);

    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_PLAYER))->SetCheck(tp->activator & AF_PLAYER);
    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_PLAYER_WEAPONS))->SetCheck(tp->activator & AF_PLAYER_WEAPON);
    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_ROBOT_WEAPONS))->SetCheck(tp->activator & AF_ROBOT_WEAPON);
    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_ROBOTS))->SetCheck(tp->activator & AF_ROBOT);
    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_CLUTTER))->SetCheck(tp->activator & AF_CLUTTER);

    ((CButton *)GetDlgItem(IDC_TRIG_ONESHOT))->SetCheck(tp->flags & TF_ONESHOT);
  } else {
    PrintToDlgItem(this, IDC_TRIG_CURRENT_NAME, "Name:");
    PrintToDlgItem(this, IDC_TRIG_CURRENT_NUM, "Number:");
    PrintToDlgItem(this, IDC_TRIG_CURRENT_TYPE, "Type:");
    PrintToDlgItem(this, IDC_TRIG_CURRENT_ROOM, "Room:");
    PrintToDlgItem(this, IDC_TRIG_CURRENT_FACE, "Face:");
    PrintToDlgItem(this, IDC_TRIG_CURRENT_TRIG_PORTAL, "");

    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_PLAYER))->SetCheck(0);
    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_PLAYER_WEAPONS))->SetCheck(0);
    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_ROBOT_WEAPONS))->SetCheck(0);
    ((CButton *)GetDlgItem(IDC_TRIG_ACTIV_ROBOTS))->SetCheck(0);

    ((CButton *)GetDlgItem(IDC_TRIG_ONESHOT))->SetCheck(0);
  }

  // Enable/disable floating trig items
  ((CButton *)GetDlgItem(IDC_TRIG_FLOAT_BORDER))->EnableWindow(floating);
  ((CButton *)GetDlgItem(IDC_TRIG_FLOAT_WIDER))->EnableWindow(floating);
  ((CButton *)GetDlgItem(IDC_TRIG_FLOAT_NARROWER))->EnableWindow(floating);
  ((CButton *)GetDlgItem(IDC_TRIG_FLOAT_TALLER))->EnableWindow(floating);
  ((CButton *)GetDlgItem(IDC_TRIG_FLOAT_SHORTER))->EnableWindow(floating);

  // Enable/disable the portal items
  ((CButton *)GetDlgItem(IDC_TRIG_CURRENT_PORTAL))->EnableWindow(Curportal != -1);
  ((CButton *)GetDlgItem(IDC_TRIG_NEXT_PORTAL))
      ->EnableWindow(((Curportal == -1) && (Curroomp->num_portals > 0)) || (Curroomp->num_portals > 1));

  // Update the portal number
  if (Curportal != -1)
    PrintToDlgItem(this, IDC_TRIG_CURRENT_PORTAL, "Current Portal:\t%d", Curportal);
  else
    PrintToDlgItem(this, IDC_TRIG_CURRENT_PORTAL, "Current Portal:");
}

void CTriggerDialog::OnPaint() {
  CPaintDC dc(this); // device context for painting

  UpdateDialog();
}

BOOL CTriggerDialog::OnInitDialog() {
  CDialog::OnInitDialog();

  // Check current trigger
  if (Current_trigger >= Num_triggers)
    Current_trigger = Num_triggers - 1;

  // Update global checkboxes
  (((CButton *)GetDlgItem(IDC_TRIG_RENDER_FLOATING))->SetCheck(Render_floating_triggers));

  return TRUE; // return TRUE unless you set the focus to a control
               // EXCEPTION: OCX Property Pages should return FALSE
}

void CTriggerDialog::OnOK() {
  CWnd *focus = CWnd::GetFocus();
  if (focus)
    focus->GetNextWindow()->SetFocus();
}

BOOL CTriggerDialog::OnHelpInfo(HELPINFO *pHelpInfo) {
  // TODO: Add your message handler code here and/or call default
  WinHelp(HID_TRIGGERTAB, HELP_CONTEXT);
  return TRUE;
  // return CDialog::OnHelpInfo(pHelpInfo);
}

void CTriggerDialog::OnTrigAddToCurface() {
  ASSERT(!(Curroomp->faces[Curface].flags & FF_HAS_TRIGGER));

  char name[TRIG_NAME_LEN + 1] = "";

  if (!InputString(name, sizeof(name), "Trigger Name", "Enter a name for this trigger"))
    return;

  int activator = (Curroomp->faces[Curface].portal_num != -1) ? AF_PLAYER : AF_PLAYER_WEAPON;

  Current_trigger = AddTrigger(name, ROOMNUM(Curroomp), Curface, activator, NULL);
  UpdateDialog();
}

void SetCurroomFromTrigger() {
  if (ROOMNUM(Curroomp) != Triggers[Current_trigger].roomnum) {

    Curroomp = &Rooms[Triggers[Current_trigger].roomnum];

    Curface = Curedge = Curvert = 0;
    Curportal = -1;
  }

  Curface = Triggers[Current_trigger].facenum;

  if (Curroomp->faces[Curface].portal_num != -1)
    Curportal = Curroomp->faces[Curface].portal_num;

  State_changed = 1;
}

void CTriggerDialog::OnTrigGoto() {
  int n;

  if (InputNumber(&n, "Select Trigger", "Enter trigger number to select", this)) {

    if (n >= Num_triggers) {
      OutrageMessageBox("Invalid trigger number.");
      return;
    }

    Current_trigger = n;

    SetCurroomFromTrigger();

    UpdateDialog();
  }
}

void CTriggerDialog::OnTrigDelete() {
  ASSERT(Current_trigger != -1);

  if (OutrageMessageBox(MBOX_YESNO, "Are you sure you want to delete the current trigger?") != IDYES)
    return;

  trigger *tp = &Triggers[Current_trigger];
  room *rp = &Rooms[tp->roomnum];
  int facenum = tp->facenum;

  // Delete the trigger
  DeleteTrigger(Current_trigger);

  // Delete face if this was a floating trigger
  if (rp->faces[facenum].flags & FF_FLOATING_TRIG) {
    DeleteRoomFace(rp, facenum);
    if (Curface == facenum)
      Curface = (facenum + 1) % rp->num_faces;
  }

  if (Current_trigger == Num_triggers)
    Current_trigger--;

  UpdateDialog();

  World_changed = 1;
}

void CTriggerDialog::OnTrigAddToCurportal() {
  ASSERT(Curportal != -1);
  ASSERT(!(Curroomp->faces[Curroomp->portals[Curportal].portal_face].flags & FF_HAS_TRIGGER));

  char name[TRIG_NAME_LEN + 1] = "";

  if (!InputString(name, sizeof(name), "Trigger Name", "Enter a name for this trigger"))
    return;

  Current_trigger = AddTrigger(name, ROOMNUM(Curroomp), Curroomp->portals[Curportal].portal_face, AF_PLAYER, NULL);
  UpdateDialog();
}

void CTriggerDialog::UpdateKeypad(int mask) {
  if (mask & (KUF_CURROOM_CHANGED + KUF_CURFACE_CHANGED + KUF_CURPORTAL_CHANGED))
    UpdateDialog();
}

#include "erooms.h"

#define TRIG_PLACE_DIST 5.0
#define TRIG_DEFAULT_SIZE 10.0

void CTriggerDialog::OnTrigAddFloating() {
  int facenum, vertnum;
  room *rp;
  face *fp;
  vector center;
  fvi_query fq;
  fvi_info hit_info;
  int fate;

  if (OBJECT_OUTSIDE(Viewer_object)) {
    OutrageMessageBox("Sorry, this operation does not yet work in the terrain.");
    return;
  }

  char name[TRIG_NAME_LEN + 1] = "";

  if (!InputString(name, sizeof(name), "Trigger Name", "Enter a name for this trigger"))
    return;

  // Get center point of new face
  center = Viewer_object->pos + (Viewer_object->orient.fvec * TRIG_PLACE_DIST);

  // Make sure the viewer in in the mine
  if (Viewer_object->flags & OF_OUTSIDE_MINE)
    goto bad_place;

  // Check if position ok
  fq.p0 = &Viewer_object->pos;
  fq.startroom = Viewer_object->roomnum;
  fq.p1 = &center;
  fq.rad = 0;
  fq.thisobjnum = -1;
  fq.ignore_obj_list = NULL;
  fq.flags = 0;
  fate = fvi_FindIntersection(&fq, &hit_info);

  // Bail if there's something in the way, or if the center point is outside
  if ((fate != HIT_NONE) || ROOMNUM_OUTSIDE(hit_info.hit_room)) {

  bad_place:;
    OutrageMessageBox("You cannot place a floating trigger here.");
    return;
  }

  // Get pointer to room the trigger is in
  rp = &Rooms[hit_info.hit_room];

  // Create a new face & new verts
  facenum = RoomAddFaces(rp, 1);
  vertnum = RoomAddVertices(rp, 4);
  fp = &rp->faces[facenum];
  InitRoomFace(fp, 4);

  // Set the vertices for the new face
  rp->verts[vertnum] =
      center - (Viewer_object->orient.rvec * TRIG_DEFAULT_SIZE) + (Viewer_object->orient.uvec * TRIG_DEFAULT_SIZE);
  rp->verts[vertnum + 1] =
      center + (Viewer_object->orient.rvec * TRIG_DEFAULT_SIZE) + (Viewer_object->orient.uvec * TRIG_DEFAULT_SIZE);
  rp->verts[vertnum + 2] =
      center + (Viewer_object->orient.rvec * TRIG_DEFAULT_SIZE) - (Viewer_object->orient.uvec * TRIG_DEFAULT_SIZE);
  rp->verts[vertnum + 3] =
      center - (Viewer_object->orient.rvec * TRIG_DEFAULT_SIZE) - (Viewer_object->orient.uvec * TRIG_DEFAULT_SIZE);

  // Set vertnums for new face
  for (int i = 0; i < 4; i++)
    fp->face_verts[i] = vertnum + i;

  // Assign UVs and normal to new face
  if (!ComputeFaceNormal(rp, facenum))
    Int3(); // Get Matt
  AssignDefaultUVsToRoomFace(rp, facenum);

  // Set floating trig flag
  fp->flags |= FF_FLOATING_TRIG;

  // Add the trigger for the new face
  Current_trigger = AddTrigger(name, ROOMNUM(rp), facenum, AF_PLAYER, NULL);

  // Set the current room:face to the new face
  SetCurroomFromTrigger();

  // We're done
  UpdateDialog();
}

void CTriggerDialog::OnTrigRenderFloating() {
  Render_floating_triggers = (((CButton *)GetDlgItem(IDC_TRIG_RENDER_FLOATING))->GetCheck() != 0);
  State_changed = 1;
}

void CTriggerDialog::OnTrigActivPlayer() {
  trigger *tp = &Triggers[Current_trigger];

  if (((CButton *)GetDlgItem(IDC_TRIG_ACTIV_PLAYER))->GetCheck())
    tp->activator |= AF_PLAYER;
  else
    tp->activator &= ~AF_PLAYER;

  World_changed = 1;
}

void CTriggerDialog::OnTrigActivPlayerWeapons() {
  trigger *tp = &Triggers[Current_trigger];

  if (((CButton *)GetDlgItem(IDC_TRIG_ACTIV_PLAYER_WEAPONS))->GetCheck())
    tp->activator |= AF_PLAYER_WEAPON;
  else
    tp->activator &= ~AF_PLAYER_WEAPON;

  World_changed = 1;
}

void CTriggerDialog::OnTrigActivRobotWeapons() {
  trigger *tp = &Triggers[Current_trigger];

  if (((CButton *)GetDlgItem(IDC_TRIG_ACTIV_ROBOT_WEAPONS))->GetCheck())
    tp->activator |= AF_ROBOT_WEAPON;
  else
    tp->activator &= ~AF_ROBOT_WEAPON;

  World_changed = 1;
}

void CTriggerDialog::OnTrigActivRobots() {
  trigger *tp = &Triggers[Current_trigger];

  if (((CButton *)GetDlgItem(IDC_TRIG_ACTIV_ROBOTS))->GetCheck())
    tp->activator |= AF_ROBOT;
  else
    tp->activator &= ~AF_ROBOT;

  World_changed = 1;
}

void ScaleFloatingTrigEdge(int roomnum, int facenum, int v0, int v1, float delta) {
  room *rp = &Rooms[roomnum];
  face *fp = &rp->faces[facenum];
  vector *vv0, *vv1;
  vector deltav;
  float mag;

  // Get pointers to verts
  vv0 = &rp->verts[fp->face_verts[v0]];
  vv1 = &rp->verts[fp->face_verts[v1]];

  // Get delta vec
  deltav = *vv1 - *vv0;
  mag = vm_NormalizeVector(&deltav);

  // Check for min size
  if (mag <= delta)
    return;

  // Now add
  *vv0 -= deltav * delta;
  *vv1 += deltav * delta;
}

#define SCALE_DELTA 1.0

void CTriggerDialog::OnTrigFloatNarrower() {
  ASSERT(Current_trigger != -1);

  trigger *tp = &Triggers[Current_trigger];

  ScaleFloatingTrigEdge(tp->roomnum, tp->facenum, 0, 1, -SCALE_DELTA);
  ScaleFloatingTrigEdge(tp->roomnum, tp->facenum, 2, 3, -SCALE_DELTA);

  World_changed = 1;
}

void CTriggerDialog::OnTrigFloatShorter() {
  ASSERT(Current_trigger != -1);

  trigger *tp = &Triggers[Current_trigger];

  ScaleFloatingTrigEdge(tp->roomnum, tp->facenum, 1, 2, -SCALE_DELTA);
  ScaleFloatingTrigEdge(tp->roomnum, tp->facenum, 3, 0, -SCALE_DELTA);

  World_changed = 1;
}

void CTriggerDialog::OnTrigFloatTaller() {
  ASSERT(Current_trigger != -1);

  trigger *tp = &Triggers[Current_trigger];

  ScaleFloatingTrigEdge(tp->roomnum, tp->facenum, 1, 2, SCALE_DELTA);
  ScaleFloatingTrigEdge(tp->roomnum, tp->facenum, 3, 0, SCALE_DELTA);

  World_changed = 1;
}

void CTriggerDialog::OnTrigFloatWider() {
  ASSERT(Current_trigger != -1);

  trigger *tp = &Triggers[Current_trigger];

  ScaleFloatingTrigEdge(tp->roomnum, tp->facenum, 0, 1, SCALE_DELTA);
  ScaleFloatingTrigEdge(tp->roomnum, tp->facenum, 2, 3, SCALE_DELTA);

  World_changed = 1;
}

void CTriggerDialog::OnTrigOneShot() {
  trigger *tp = &Triggers[Current_trigger];

  if (((CButton *)GetDlgItem(IDC_TRIG_ONESHOT))->GetCheck())
    tp->flags |= TF_ONESHOT;
  else
    tp->flags &= ~TF_ONESHOT;

  World_changed = 1;
}

void CTriggerDialog::OnTrigNextPortal() {
  if (Curroomp->num_portals) {
    Curportal = (Curportal + 1) % Curroomp->num_portals;
    State_changed = 1;
    UpdateDialog();
  }
}

void CTriggerDialog::OnTrigNextInMine() {
  ASSERT(Current_trigger != -1);

  Current_trigger = (Current_trigger + 1) % Num_triggers;

  SetCurroomFromTrigger();

  UpdateDialog();
}

void CTriggerDialog::OnTrigNextInRoom() {
  int n = FindNextTrigInRoom(ROOMNUM(Curroomp), Current_trigger);

  ASSERT(n != -1);

  Current_trigger = n;

  SetCurroomFromTrigger();
}

void CTriggerDialog::OnTrigPrevInMine() {
  ASSERT(Current_trigger != -1);

  if (--Current_trigger < 0)
    Current_trigger += Num_triggers;

  SetCurroomFromTrigger();

  UpdateDialog();
}

void CTriggerDialog::OnTrigPrevInRoom() {
  int n = FindPrevTrigInRoom(ROOMNUM(Curroomp), Current_trigger);

  ASSERT(n != -1);

  Current_trigger = n;

  SetCurroomFromTrigger();
}

#include "osiris_predefs.h"
extern bool StripLeadingTrailingSpaces(char *s);

void CTriggerDialog::OnTrigEditName() {
  ASSERT(Current_trigger != -1);

  trigger *tp = &Triggers[Current_trigger];

  char name[TRIG_NAME_LEN + 1];

  strcpy(name, tp->name);

try_again:;
  if (!InputString(name, sizeof(name), "Trigger Name", "Enter a new name:"))
    return;

  if (StripLeadingTrailingSpaces(name))
    EditorMessageBox("Note: Leading and/or trailing spaces have been removed from this name (\"%s\")", name);

  int n = osipf_FindTriggerName(name);
  if ((n != -1) && (n != Current_trigger)) {
    EditorMessageBox("Trigger %d already has this name.", n);
    goto try_again;
  }

  strcpy(tp->name, name);

  UpdateDialog();
}

void CTriggerDialog::OnTrigEditScript() {

  mprintf(0, "Edit script for trigger %d\n", Current_trigger);

  // Make sure Dallas is open
  if (theApp.m_DallasModelessDlgPtr == NULL) {
    theApp.m_DallasModelessDlgPtr = new CDallasMainDlg;
    theApp.m_DallasModelessDlgPtr->Create(IDD_DALLAS_MAIN_DIALOG, this);
    theApp.m_DallasModelessDlgPtr->ShowWindow(SW_SHOW);
  } else
    theApp.m_DallasModelessDlgPtr->ShowWindow(SW_RESTORE);

  // make sure a trigger was selected
  if (Current_trigger == -1)
    return;

  // Tell Dallas to add a new script with this trigger as the owner
  theApp.m_DallasModelessDlgPtr->m_ScriptOwnerType = TRIGGER_TYPE;
  theApp.m_DallasModelessDlgPtr->m_ScriptOwnerHandle = Current_trigger;
  theApp.m_DallasModelessDlgPtr->PostMessage(WM_HIGHLIGHT_SCRIPTS);
}

void CTriggerDialog::OnTrigActivClutter() {
  trigger *tp = &Triggers[Current_trigger];

  if (((CButton *)GetDlgItem(IDC_TRIG_ACTIV_CLUTTER))->GetCheck())
    tp->activator |= AF_CLUTTER;
  else
    tp->activator &= ~AF_CLUTTER;

  World_changed = 1;
}