allow the editor to be built with NDEBUG
[btb/d2x.git] / main / editor / med.c
1 /* $Id: med.c,v 1.10 2006-03-05 12:19:57 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 loop for Inferno
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 //#define DEMO 1
26
27 #define DIAGNOSTIC_MESSAGE_MAX                          90
28 #define EDITOR_STATUS_MESSAGE_DURATION  4               //      Shows for 3+..4 seconds
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <time.h>
35
36 #ifdef __MSDOS__
37 #include <process.h>
38 #endif
39
40
41 //#define INCLUDE_XLISP
42
43 #include "inferno.h"
44 #include "segment.h"
45 #include "gr.h"
46 #include "ui.h"
47 #include "editor.h"
48 //#include "gamemine.h"
49 #include "gamesave.h"
50 #include "gameseg.h"
51
52 #include "key.h"
53 #include "mono.h"
54 #include "error.h"
55 #include "kfuncs.h"
56 #include "macro.h"
57
58 #ifdef INCLUDE_XLISP
59 #include "medlisp.h"
60 #endif
61 #include "u_mem.h"
62 #include "render.h"
63 #include "game.h"
64 #include "slew.h"
65 #include "kdefs.h"
66 #include "func.h"
67 #include "textures.h"
68 #include "screens.h"
69 #include "texmap.h"
70 #include "object.h"
71 #include "effects.h"
72 #include "wall.h"
73 #include "info.h"
74 #include "ai.h"
75
76 #include "texpage.h"            // Textue selection paging stuff
77 #include "objpage.h"            // Object selection paging stuff
78
79 #include "medmisc.h"
80 #include "meddraw.h"
81 #include "medsel.h"
82 #include "medrobot.h"
83 #include "medwall.h"
84 #include "eswitch.h"
85 #include "ehostage.h"
86 #include "centers.h"
87
88 #include "fuelcen.h"
89 #include "gameseq.h"
90 #include "newmenu.h"
91
92 //#define _MARK_ON 1
93 //#include <wsample.h>          //should come after inferno.h to get mark setting //Not included here.
94
95 #define COMPRESS_INTERVAL       5                       // seconds
96
97 //char *undo_status[128];
98
99 int initializing;
100
101 //these are instances of canvases, pointed to by variables below
102 grs_canvas _canv_editor_game;           //the game on the editor screen
103
104 //these are pointers to our canvases
105 grs_canvas *Canv_editor;                        //the editor screen
106 grs_canvas *Canv_editor_game=&_canv_editor_game; //the game on the editor screen
107
108 grs_canvas *canv_offscreen;             //for off-screen rendering
109 grs_canvas *Pad_text_canvas;            // Keypad text
110
111 grs_font *editor_font=NULL;
112
113 //where the editor is looking
114 vms_vector Ed_view_target={0,0,0};
115
116 int gamestate_not_restored = 0;
117
118 UI_WINDOW * EditorWindow;
119
120 int     Large_view_index = -1;
121
122 UI_GADGET_USERBOX * GameViewBox;
123 UI_GADGET_USERBOX * LargeViewBox;
124 UI_GADGET_USERBOX * GroupViewBox;
125
126 #if ORTHO_VIEWS
127 UI_GADGET_USERBOX * TopViewBox;
128 UI_GADGET_USERBOX * FrontViewBox;
129 UI_GADGET_USERBOX * RightViewBox;
130 #endif
131
132 UI_GADGET_ICON * ViewIcon;
133 UI_GADGET_ICON * AllIcon;
134 UI_GADGET_ICON * AxesIcon;
135 UI_GADGET_ICON * ChaseIcon;
136 UI_GADGET_ICON * OutlineIcon;
137 UI_GADGET_ICON * LockIcon;
138 //-NOLIGHTICON- UI_GADGET_ICON * LightIcon;
139
140 UI_EVENT * DemoBuffer = NULL;
141
142 //grs_canvas * BigCanvas[2];
143 //int CurrentBigCanvas = 0;
144 //int BigCanvasFirstTime = 1;
145
146 int     Found_seg_index=0;                              // Index in Found_segs corresponding to Cursegp
147
148
149 void print_status_bar( char message[DIAGNOSTIC_MESSAGE_MAX] ) {
150         int w,h,aw;
151
152         gr_set_current_canvas( NULL );
153         gr_set_curfont(editor_font);
154         gr_set_fontcolor( CBLACK, CGREY );
155         gr_get_string_size( message, &w, &h, &aw );
156         gr_string( 4, 583, message );
157         gr_set_fontcolor( CBLACK, CWHITE );
158         gr_setcolor( CGREY );
159         gr_rect( 4+w, 583, 799, 599 );
160 }
161
162 void print_diagnostic( char message[DIAGNOSTIC_MESSAGE_MAX] ) {
163         int w,h,aw;
164
165         gr_set_current_canvas( NULL );
166         gr_set_curfont(editor_font);
167         gr_set_fontcolor( CBLACK, CGREY );
168         gr_get_string_size( message, &w, &h, &aw );
169         gr_string( 4, 583, message );
170         gr_set_fontcolor( CBLACK, CWHITE );
171         gr_setcolor( CGREY );
172         gr_rect( 4+w, 583, 799, 599 );
173 }
174
175 static char status_line[DIAGNOSTIC_MESSAGE_MAX];
176
177 struct tm       Editor_status_last_time;
178
179 void editor_status( const char *format, ... )
180 {
181         va_list ap;
182
183         va_start(ap, format);
184         vsprintf(status_line, format, ap);
185         va_end(ap);
186
187         print_status_bar(status_line);
188
189         Editor_status_last_time = Editor_time_of_day;
190
191 }
192
193 //      int  tm_sec;    /* seconds after the minute -- [0,61] */
194 //      int  tm_min;    /* minutes after the hour       -- [0,59] */
195 //      int  tm_hour;   /* hours after midnight -- [0,23] */
196 //      int  tm_mday;   /* day of the month             -- [1,31] */
197 //      int  tm_mon;    /* months since January -- [0,11] */
198 //      int  tm_year;   /* years since 1900                             */
199 //      int  tm_wday;   /* days since Sunday            -- [0,6]  */
200 //      int  tm_yday;   /* days since January 1 -- [0,365]*/
201 //      int  tm_isdst;  /* Daylight Savings Time flag */
202
203 void clear_editor_status(void)
204 {
205         int cur_time = Editor_time_of_day.tm_hour * 3600 + Editor_time_of_day.tm_min*60 + Editor_time_of_day.tm_sec;
206         int erase_time = Editor_status_last_time.tm_hour * 3600 + Editor_status_last_time.tm_min*60 + Editor_status_last_time.tm_sec + EDITOR_STATUS_MESSAGE_DURATION;
207
208         if (cur_time > erase_time) {
209                 int     i;
210                 char    message[DIAGNOSTIC_MESSAGE_MAX];
211
212                 for (i=0; i<DIAGNOSTIC_MESSAGE_MAX-1; i++)
213                         message[i] = ' ';
214
215                 message[i] = 0;
216                 print_status_bar(message);
217                 Editor_status_last_time.tm_hour = 99;
218         }
219 }
220
221
222 void diagnostic_message( const char *format, ... )
223 {
224         char diag_line[DIAGNOSTIC_MESSAGE_MAX];
225
226         va_list ap;
227
228         va_start(ap, format);
229         vsprintf(diag_line, format, ap);
230         va_end(ap);
231
232         editor_status(diag_line);
233 }
234
235
236 static char sub_status_line[DIAGNOSTIC_MESSAGE_MAX];
237
238 void editor_sub_status( const char *format, ... )
239 {
240         int w,h,aw;
241         va_list ap;
242
243         va_start(ap, format);
244         vsprintf(sub_status_line, format, ap);
245         va_end(ap);
246
247         gr_set_current_canvas( NULL );
248         gr_set_curfont(editor_font);
249         gr_set_fontcolor( BM_XRGB(0,0,31), CGREY );
250         gr_get_string_size( sub_status_line, &w, &h, &aw );
251         gr_string( 500, 583, sub_status_line );
252         gr_set_fontcolor( CBLACK, CWHITE );
253         gr_setcolor( CGREY );
254         gr_rect( 500+w, 583, 799, 599 );
255 }
256
257 int DropIntoDebugger()
258 {
259         Int3();
260         return 1;
261 }
262
263
264 #ifdef INCLUDE_XLISP
265 int CallLisp()
266 {
267         medlisp_go();
268         return 1;
269 }
270 #endif
271
272
273 int ExitEditor()
274 {
275         if (SafetyCheck())  {
276                 ModeFlag = 1;
277         }
278         return 1;
279 }
280
281 int     GotoGameScreen()
282 {
283         stop_time();
284
285 //@@    init_player_stats();
286 //@@
287 //@@    Player_init.pos = Player->pos;
288 //@@    Player_init.orient = Player->orient;
289 //@@    Player_init.segnum = Player->segnum;    
290         
291 // -- must always save gamesave.sav because the restore-objects code relies on it
292 // -- that code could be made smarter and use the original file, if appropriate.
293 //      if (mine_changed) 
294         if (gamestate_not_restored == 0) {
295                 gamestate_not_restored = 1;
296                 save_level("GAMESAVE.LVL");
297                 editor_status("Gamestate saved.\n");
298                 mprintf((0, "Gamestate saved.\n"));
299         }
300
301         ai_reset_all_paths();
302
303         start_time();
304
305         ModeFlag = 3;
306         return 1;
307 }
308
309 int GotoMainMenu()
310 {
311         ModeFlag = 2;
312         return 1;
313 }
314
315
316 void ReadLispMacro( FILE * file, char * buffer )
317 {
318 //      char c;
319 //      int size=0;
320 //      int pcount = 0;
321 //      char text[100];
322 //      int i=0;
323         
324         fscanf( file, " { %s } ", buffer );
325
326 /*
327         while (1)
328         {
329                 c = text[i++];
330                 if (pcount > 0 )
331                         buffer[size++] = c;
332                 if ( c == '(' ) pcount++;
333                 if ( c == ')' ) break;
334         }
335         buffer[size++] = 0;
336 */
337
338         return;
339 }
340
341 static int (*KeyFunction[2048])();
342
343 void medkey_init()
344 {
345         FILE * keyfile;
346         char keypress[100];
347         int key;
348         int i;  //, size;
349         int np;
350         char * LispCommand;
351
352         MALLOC( LispCommand, char, DIAGNOSTIC_MESSAGE_MAX );
353
354         for (i=0; i<2048; i++ )
355                 KeyFunction[i] = NULL;
356
357         keyfile = fopen( "GLOBAL.KEY", "rt" );
358         if (keyfile)
359         {
360                 while (fscanf( keyfile, " %s %s ", keypress, LispCommand ) != EOF )
361                 {
362                         //ReadLispMacro( keyfile, LispCommand );
363
364                         if ( (key=DecodeKeyText( keypress ))!= -1 )
365                         {
366                                 Assert( key < 2048);
367                                 KeyFunction[key] = func_get( LispCommand, &np );
368                         } else {
369                                 Error( "Bad key %s in GLOBAL.KEY!", keypress );
370                         }
371                 }
372                 fclose(keyfile);
373         }
374         d_free( LispCommand );
375 }
376
377 void init_editor()
378 {
379         minit();
380
381         ui_init();
382
383         init_med_functions();   // Must be called before medlisp_init
384
385         ui_pad_read( 0, "segmove.pad" );
386         ui_pad_read( 1, "segsize.pad" );
387         ui_pad_read( 2, "curve.pad" );
388         ui_pad_read( 3, "texture.pad" );
389         ui_pad_read( 4, "object.pad" );
390         ui_pad_read( 5, "objmov.pad" );
391         ui_pad_read( 6, "group.pad" );
392         ui_pad_read( 7, "lighting.pad" );
393         ui_pad_read( 8, "test.pad" );
394
395         medkey_init();
396
397         editor_font = gr_init_font( "pc8x16.fnt" );
398         
399         menubar_init( "MED.MNU" );
400
401         canv_offscreen = gr_create_canvas(LVIEW_W,LVIEW_H);
402         
403         Draw_all_segments = 1;                                          // Say draw all segments, not just connected ones
404
405         init_autosave();
406   
407 //      atexit(close_editor);
408
409         Clear_window = 1;       //      do full window clear.
410 }
411
412 int ShowAbout()
413 {
414         MessageBox( -2, -2, 1,  "INFERNO Mine Editor\n\n"               \
415                                                                         "Copyright (c) 1993  Parallax Software Corp.",
416                                                                         "OK");
417         return 0;
418 }
419
420 void move_player_2_segment(segment *seg,int side);
421
422 int SetPlayerFromCurseg()
423 {
424         move_player_2_segment(Cursegp,Curside);
425         Update_flags |= UF_ED_STATE_CHANGED | UF_GAME_VIEW_CHANGED;
426         return 1;
427 }
428
429 int fuelcen_create_from_curseg()
430 {
431         Curseg2p->special = SEGMENT_IS_FUELCEN;
432         fuelcen_activate(Cursegp, Curseg2p->special);
433         return 1;
434 }
435
436 int repaircen_create_from_curseg()
437 {
438         Int3(); //      -- no longer supported!
439 //      Curseg2p->special = SEGMENT_IS_REPAIRCEN;
440 //      fuelcen_activate(Cursegp, Curseg2p->special);
441         return 1;
442 }
443
444 int controlcen_create_from_curseg()
445 {
446         Curseg2p->special = SEGMENT_IS_CONTROLCEN;
447         fuelcen_activate(Cursegp, Curseg2p->special);
448         return 1;
449 }
450
451 int robotmaker_create_from_curseg()
452 {
453         Curseg2p->special = SEGMENT_IS_ROBOTMAKER;
454         fuelcen_activate(Cursegp, Curseg2p->special);
455         return 1;
456 }
457
458 int fuelcen_reset_all() {
459         fuelcen_reset();
460         return 1;
461 }
462
463 int fuelcen_delete_from_curseg() {
464         fuelcen_delete( Cursegp );
465         return 1;
466 }
467
468
469 //@@//this routine places the viewer in the center of the side opposite to curside,
470 //@@//with the view toward the center of curside
471 //@@int SetPlayerFromCursegMinusOne()
472 //@@{
473 //@@    vms_vector vp;
474 //@@
475 //@@//  int newseg,newside;
476 //@@//  get_previous_segment(SEG_PTR_2_NUM(Cursegp),Curside,&newseg,&newside);
477 //@@//  move_player_2_segment(&Segments[newseg],newside);
478 //@@
479 //@@    med_compute_center_point_on_side(&Player->obj_position,Cursegp,Side_opposite[Curside]);
480 //@@    med_compute_center_point_on_side(&vp,Cursegp,Curside);
481 //@@    vm_vec_sub2(&vp,&Player->position);
482 //@@    vm_vector_2_matrix(&Player->orient,&vp,NULL,NULL);
483 //@@
484 //@@    Player->seg = SEG_PTR_2_NUM(Cursegp);
485 //@@
486 //@@    Update_flags |= UF_GAME_VIEW_CHANGED;
487 //@@    return 1;
488 //@@}
489
490 //this constant determines how much of the window will be occupied by the
491 //viewed side when SetPlayerFromCursegMinusOne() is called.  It actually
492 //determine how from from the center of the window the farthest point will be
493 #define SIDE_VIEW_FRAC (f1_0*8/10)      //80%
494
495
496 void move_player_2_segment_and_rotate(segment *seg,int side)
497 {
498         vms_vector vp;
499         vms_vector      upvec;
500         static int edgenum=0;
501
502         compute_segment_center(&ConsoleObject->pos,seg);
503         compute_center_point_on_side(&vp,seg,side);
504         vm_vec_sub2(&vp,&ConsoleObject->pos);
505
506         vm_vec_sub(&upvec, &Vertices[Cursegp->verts[Side_to_verts[Curside][edgenum%4]]], &Vertices[Cursegp->verts[Side_to_verts[Curside][(edgenum+3)%4]]]);
507         edgenum++;
508
509         vm_vector_2_matrix(&ConsoleObject->orient,&vp,&upvec,NULL);
510 //      vm_vector_2_matrix(&ConsoleObject->orient,&vp,NULL,NULL);
511
512         obj_relink( ConsoleObject-Objects, SEG_PTR_2_NUM(seg) );
513         
514 }
515
516 int SetPlayerFromCursegAndRotate()
517 {
518         move_player_2_segment_and_rotate(Cursegp,Curside);
519         Update_flags |= UF_ED_STATE_CHANGED | UF_GAME_VIEW_CHANGED;
520         return 1;
521 }
522
523
524 //sets the player facing curseg/curside, normal to face0 of curside, and
525 //far enough away to see all of curside
526 int SetPlayerFromCursegMinusOne()
527 {
528         vms_vector view_vec,view_vec2,side_center;
529         vms_vector corner_v[4];
530         vms_vector      upvec;
531         g3s_point corner_p[4];
532         int i;
533         fix max,view_dist=f1_0*10;
534         static int edgenum=0;
535         int newseg;
536
537         view_vec = Cursegp->sides[Curside].normals[0];
538         vm_vec_negate(&view_vec);
539
540         compute_center_point_on_side(&side_center,Cursegp,Curside);
541         vm_vec_copy_scale(&view_vec2,&view_vec,view_dist);
542         vm_vec_sub(&ConsoleObject->pos,&side_center,&view_vec2);
543
544         vm_vec_sub(&upvec, &Vertices[Cursegp->verts[Side_to_verts[Curside][edgenum%4]]], &Vertices[Cursegp->verts[Side_to_verts[Curside][(edgenum+3)%4]]]);
545         edgenum++;
546
547         vm_vector_2_matrix(&ConsoleObject->orient,&view_vec,&upvec,NULL);
548
549         gr_set_current_canvas(Canv_editor_game);
550         g3_start_frame();
551         g3_set_view_matrix(&ConsoleObject->pos,&ConsoleObject->orient,Render_zoom);
552
553         for (i=max=0;i<4;i++) {
554                 corner_v[i] = Vertices[Cursegp->verts[Side_to_verts[Curside][i]]];
555                 g3_rotate_point(&corner_p[i],&corner_v[i]);
556                 if (labs(corner_p[i].p3_x) > max) max = labs(corner_p[i].p3_x);
557                 if (labs(corner_p[i].p3_y) > max) max = labs(corner_p[i].p3_y);
558         }
559
560         view_dist = fixmul(view_dist,fixdiv(fixdiv(max,SIDE_VIEW_FRAC),corner_p[0].p3_z));
561         vm_vec_copy_scale(&view_vec2,&view_vec,view_dist);
562         vm_vec_sub(&ConsoleObject->pos,&side_center,&view_vec2);
563
564         //obj_relink(ConsoleObject-Objects, SEG_PTR_2_NUM(Cursegp) );
565         //update_object_seg(ConsoleObject);             //might have backed right out of curseg
566
567         newseg = find_point_seg(&ConsoleObject->pos,SEG_PTR_2_NUM(Cursegp) );
568         if (newseg != -1)
569                 obj_relink(ConsoleObject-Objects,newseg);
570
571         Update_flags |= UF_ED_STATE_CHANGED | UF_GAME_VIEW_CHANGED;
572         return 1;
573 }
574
575 int ToggleLighting(void)
576 {
577         char    outstr[80] = "[shift-L] ";
578         int     chindex;
579
580         Lighting_on++;
581         if (Lighting_on >= 2)
582                 Lighting_on = 0;
583
584         Update_flags |= UF_GAME_VIEW_CHANGED;
585
586         if (last_keypress == KEY_L + KEY_SHIFTED)
587                 chindex = 0;
588         else
589                 chindex = 10;
590
591         switch (Lighting_on) {
592                 case 0:
593                         strcpy(&outstr[chindex],"Lighting off.");
594                         break;
595                 case 1:
596                         strcpy(&outstr[chindex],"Static lighting.");
597                         break;
598                 case 2:
599                         strcpy(&outstr[chindex],"Ship lighting.");
600                         break;
601                 case 3:
602                         strcpy(&outstr[chindex],"Ship and static lighting.");
603                         break;
604         }
605
606         diagnostic_message(outstr);
607
608         return Lighting_on;
609 }
610
611 void find_concave_segs();
612
613 int FindConcaveSegs()
614 {
615         find_concave_segs();
616
617         Update_flags |= UF_ED_STATE_CHANGED;            //list may have changed
618
619         return 1;
620 }
621
622 int DosShell()
623 {
624         int ok, w, h;
625         grs_bitmap * save_bitmap;
626
627         ok = 1;
628
629         // Save the current graphics state.
630
631         w = grd_curscreen->sc_canvas.cv_bitmap.bm_w;
632         h = grd_curscreen->sc_canvas.cv_bitmap.bm_h;
633
634         save_bitmap = gr_create_bitmap( w, h );
635         gr_bm_ubitblt(w, h, 0, 0, 0, 0, &(grd_curscreen->sc_canvas.cv_bitmap), save_bitmap );
636
637         // gr_set_mode( SM_ORIGINAL );
638
639         printf( "\n\nType EXIT to return to Inferno" );
640         fflush(stdout);
641
642         key_close();
643 #ifdef __MSDOS__
644         ok = spawnl(P_WAIT,getenv("COMSPEC"), NULL );
645 #elif defined(__linux__)
646         system("");
647 #endif
648         key_init();
649
650         gr_set_mode(grd_curscreen->sc_mode);
651         gr_bm_ubitblt(w, h, 0, 0, 0, 0, save_bitmap, &(grd_curscreen->sc_canvas.cv_bitmap));
652         gr_free_bitmap( save_bitmap );
653         //gr_pal_setblock( 0, 256, grd_curscreen->pal );
654         //gr_use_palette_table();
655
656         return ok;
657
658 }
659
660 int ToggleOutlineMode()
661 {
662 #ifndef NDEBUG
663         int mode;
664
665         mode=toggle_outline_mode();
666
667         if (mode)
668          {
669                 if (last_keypress != KEY_O)
670                         diagnostic_message("[O] Outline Mode ON.");
671                 else
672                         diagnostic_message("Outline Mode ON.");
673          }
674         else
675          {
676                 if (last_keypress != KEY_O)
677                         diagnostic_message("[O] Outline Mode OFF.");
678                 else
679                         diagnostic_message("Outline Mode OFF.");
680          }
681
682         Update_flags |= UF_GAME_VIEW_CHANGED;
683         return mode;
684 #else
685         return 1;
686 #endif
687 }
688
689 //@@int do_reset_orient()
690 //@@{
691 //@@    slew_reset_orient(SlewObj);
692 //@@
693 //@@    Update_flags |= UF_GAME_VIEW_CHANGED;
694 //@@
695 //@@    * (ubyte *) 0x417 &= ~0x20;
696 //@@
697 //@@    return 1;
698 //@@}
699
700 int GameZoomOut()
701 {
702         Render_zoom = fixmul(Render_zoom,68985);
703         Update_flags |= UF_GAME_VIEW_CHANGED;
704         return 1;
705 }
706
707 int GameZoomIn()
708 {
709         Render_zoom = fixmul(Render_zoom,62259);
710         Update_flags |= UF_GAME_VIEW_CHANGED;
711         return 1;
712 }
713
714
715 int med_keypad_goto_0() {       ui_pad_goto(0); return 0;       }
716 int med_keypad_goto_1() {       ui_pad_goto(1); return 0;       }
717 int med_keypad_goto_2() {       ui_pad_goto(2); return 0;       }
718 int med_keypad_goto_3() {       ui_pad_goto(3); return 0;       }
719 int med_keypad_goto_4() {       ui_pad_goto(4); return 0;       }
720 int med_keypad_goto_5() {       ui_pad_goto(5); return 0;       }
721 int med_keypad_goto_6() {       ui_pad_goto(6); return 0;       }
722 int med_keypad_goto_7() {       ui_pad_goto(7); return 0;       }
723 int med_keypad_goto_8() {       ui_pad_goto(8); return 0;       }
724
725 #define PAD_WIDTH       30
726 #define PAD_WIDTH1      (PAD_WIDTH + 7)
727
728 int editor_screen_open = 0;
729
730 //setup the editors windows, canvases, gadgets, etc.
731 //called whenever the editor screen is selected
732 void init_editor_screen()
733 {       
734 //      grs_bitmap * bmp;
735
736         if (editor_screen_open) return;
737
738         grd_curscreen->sc_canvas.cv_font = editor_font;
739         
740         //create canvas for game on the editor screen
741         initializing = 1;
742         gr_set_current_canvas(Canv_editor);
743         Canv_editor->cv_font = editor_font;
744         gr_init_sub_canvas(Canv_editor_game,Canv_editor,GAMEVIEW_X,GAMEVIEW_Y,GAMEVIEW_W,GAMEVIEW_H);
745         
746         //Editor renders into full (320x200) game screen 
747
748         init_info = 1;
749
750         //do other editor screen setup
751
752         // Since the palette might have changed, find some good colors...
753         CBLACK = gr_find_closest_color( 1, 1, 1 );
754         CGREY = gr_find_closest_color( 28, 28, 28 );
755         CWHITE = gr_find_closest_color( 38, 38, 38 );
756         CBRIGHT = gr_find_closest_color( 60, 60, 60 );
757         CRED = gr_find_closest_color( 63, 0, 0 );
758
759         gr_set_curfont(editor_font);
760         gr_set_fontcolor( CBLACK, CWHITE );
761
762         EditorWindow = ui_open_window( 0 , 0, ED_SCREEN_W, ED_SCREEN_H, WIN_FILLED );
763
764         LargeViewBox    = ui_add_gadget_userbox( EditorWindow,LVIEW_X,LVIEW_Y,LVIEW_W,LVIEW_H);
765 #if ORTHO_VIEWS
766         TopViewBox              = ui_add_gadget_userbox( EditorWindow,TVIEW_X,TVIEW_Y,TVIEW_W,TVIEW_H);
767         FrontViewBox    = ui_add_gadget_userbox( EditorWindow,FVIEW_X,FVIEW_Y,FVIEW_W,FVIEW_H);
768         RightViewBox    = ui_add_gadget_userbox( EditorWindow,RVIEW_X,RVIEW_Y,RVIEW_W,RVIEW_H);
769 #endif
770         ui_gadget_calc_keys(EditorWindow);      //make tab work for all windows
771
772         GameViewBox     = ui_add_gadget_userbox( EditorWindow, GAMEVIEW_X, GAMEVIEW_Y, GAMEVIEW_W, GAMEVIEW_H );
773 //      GroupViewBox    = ui_add_gadget_userbox( EditorWindow,GVIEW_X,GVIEW_Y,GVIEW_W,GVIEW_H);
774
775 //      GameViewBox->when_tab = GameViewBox->when_btab = (UI_GADGET *) LargeViewBox;
776 //      LargeViewBox->when_tab = LargeViewBox->when_btab = (UI_GADGET *) GameViewBox;
777
778 //      ui_gadget_calc_keys(EditorWindow);      //make tab work for all windows
779
780         ViewIcon        = ui_add_gadget_icon( EditorWindow, "Lock\nview",       455,25+530,     40, 22, KEY_V+KEY_CTRLED, ToggleLockViewToCursegp );
781         AllIcon = ui_add_gadget_icon( EditorWindow, "Draw\nall",        500,25+530,     40, 22, KEY_A+KEY_CTRLED, ToggleDrawAllSegments );
782         AxesIcon        = ui_add_gadget_icon( EditorWindow, "Coord\naxes",545,25+530,           40, 22, KEY_D+KEY_CTRLED, ToggleCoordAxes );
783 //-NOLIGHTICON- LightIcon       = ui_add_gadget_icon( EditorWindow, "Light\ning",       590,25+530,     40, 22, KEY_L+KEY_SHIFTED,ToggleLighting );
784         ChaseIcon       = ui_add_gadget_icon( EditorWindow, "Chase\nmode",635,25+530,           40, 22, -1,                             ToggleChaseMode );
785         OutlineIcon = ui_add_gadget_icon( EditorWindow, "Out\nline",    680,25+530,     40, 22, KEY_O,                  ToggleOutlineMode );
786         LockIcon        = ui_add_gadget_icon( EditorWindow, "Lock\nstep", 725,25+530,   40, 22, KEY_L,                  ToggleLockstep );
787
788         meddraw_init_views(LargeViewBox->canvas);
789
790         //ui_add_gadget_button( EditorWindow, 460, 510, 50, 25, "Quit", ExitEditor );
791         //ui_add_gadget_button( EditorWindow, 520, 510, 50, 25, "Lisp", CallLisp );
792         //ui_add_gadget_button( EditorWindow, 580, 510, 50, 25, "Mine", MineMenu );
793         //ui_add_gadget_button( EditorWindow, 640, 510, 50, 25, "Help", DoHelp );
794         //ui_add_gadget_button( EditorWindow, 460, 540, 50, 25, "Macro", MacroMenu );
795         //ui_add_gadget_button( EditorWindow, 520, 540, 50, 25, "About", ShowAbout );
796         //ui_add_gadget_button( EditorWindow, 640, 540, 50, 25, "Shell", DosShell );
797
798         ui_pad_activate( EditorWindow, PAD_X, PAD_Y );
799         Pad_text_canvas = gr_create_sub_canvas(Canv_editor, PAD_X + 250, PAD_Y + 8, 180, 160);
800         ui_add_gadget_button( EditorWindow, PAD_X+6, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "<<",  med_keypad_goto_prev );
801         ui_add_gadget_button( EditorWindow, PAD_X+PAD_WIDTH1+6, PAD_Y+(30*5)+22, PAD_WIDTH, 20, ">>",  med_keypad_goto_next );
802
803         {       int     i;
804                 i = 0;  ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "SR",  med_keypad_goto_0 );
805                 i++;            ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "SS",  med_keypad_goto_1 );
806                 i++;            ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "CF",  med_keypad_goto_2 );
807                 i++;            ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "TM",  med_keypad_goto_3 );
808                 i++;            ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "OP",  med_keypad_goto_4 );
809                 i++;            ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "OR",  med_keypad_goto_5 );
810                 i++;            ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "GE",  med_keypad_goto_6 );
811                 i++;            ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "LI",  med_keypad_goto_7 );
812                 i++;            ui_add_gadget_button( EditorWindow, PAD_X+16+(i+2)*PAD_WIDTH1, PAD_Y+(30*5)+22, PAD_WIDTH, 20, "TT",  med_keypad_goto_8 );
813         }
814
815         gr_set_curfont(editor_font);
816         menubar_show();
817
818         // INIT TEXTURE STUFF
819         texpage_init( EditorWindow );
820         objpage_init( EditorWindow );
821
822         EditorWindow->keyboard_focus_gadget = (UI_GADGET *)LargeViewBox;
823
824         canv_offscreen->cv_font = grd_curscreen->sc_canvas.cv_font;
825 //      BigCanvas[0]->cv_font = grd_curscreen->sc_canvas.cv_font; 
826 //      BigCanvas[1]->cv_font = grd_curscreen->sc_canvas.cv_font; 
827 //      BigCanvasFirstTime = 1;
828
829         // Draw status box
830         gr_set_current_canvas( NULL );
831         gr_setcolor( CGREY );
832         gr_rect(STATUS_X,STATUS_Y,STATUS_X+STATUS_W-1,STATUS_Y+STATUS_H-1);                     //0, 582, 799, 599 );
833
834         // Draw icon box
835         // gr_set_current_canvas( NULL );
836         //  gr_setcolor( CBRIGHT );
837         //  gr_rect( 528, 2, 798, 22);
838         //  gr_setcolor( CGREY);
839         //  gr_rect( 530, 2, 799, 20);
840
841         Update_flags = UF_ALL;
842         initializing = 0;
843         editor_screen_open = 1;
844 }
845
846 //shutdown ui on the editor screen
847 void close_editor_screen()
848 {
849         if (!editor_screen_open) return;
850
851         editor_screen_open = 0;
852         ui_pad_deactivate();
853         gr_free_sub_canvas(Pad_text_canvas);
854
855         ui_close_window(EditorWindow);
856
857         close_all_windows();
858
859         // CLOSE TEXTURE STUFF
860         texpage_close();
861         objpage_close();
862
863         menubar_hide();
864
865 }
866
867 void med_show_warning(char *s)
868 {
869         grs_canvas *save_canv=grd_curcanv;
870
871         //gr_pal_fade_in(grd_curscreen->pal);   //in case palette is blacked
872
873         MessageBox(-2,-2,1,s,"OK");
874
875         gr_set_current_canvas(save_canv);
876
877 }
878
879 // Returns 1 if OK to trash current mine.
880 int SafetyCheck()
881 {
882         int x;
883                         
884         if (mine_changed) {
885                 stop_time();                            
886                 x = nm_messagebox( "Warning!", 2, "Cancel", "OK", "You are about to lose work." );
887                 if (x<1) {
888                         start_time();
889                         return 0;
890                 }
891                 start_time();
892         }
893         return 1;
894 }
895
896 //called at the end of the program
897 void close_editor() {
898
899         close_autosave();
900
901         menubar_close();
902         
903         gr_close_font(editor_font);
904
905         gr_free_canvas(canv_offscreen); canv_offscreen = NULL;
906
907         return;
908
909 }
910
911 //variables for find segments process
912
913 // ---------------------------------------------------------------------------------------------------
914 //      Subtract all elements in Found_segs from selected list.
915 void subtract_found_segments_from_selected_list(void)
916 {
917         int     s,f;
918
919         for (f=0; f<N_found_segs; f++) {
920                 int     foundnum = Found_segs[f];
921
922                 for (s=0; s<N_selected_segs; s++) {
923                         if (Selected_segs[s] == foundnum) {
924                                 Selected_segs[s] = Selected_segs[N_selected_segs-1];
925                                 N_selected_segs--;
926                                 break;
927                         }
928                 }
929         }
930 }
931
932 // ---------------------------------------------------------------------------------------------------
933 //      Add all elements in Found_segs to selected list.
934 void add_found_segments_to_selected_list(void) {
935         int     s,f;
936
937         for (f=0; f<N_found_segs; f++) {
938                 int     foundnum = Found_segs[f];
939
940                 for (s=0; s<N_selected_segs; s++)
941                         if (Selected_segs[s] == foundnum)
942                                 break;
943
944                 if (s == N_selected_segs)
945                         Selected_segs[N_selected_segs++] = foundnum;
946         }
947 }
948
949 void gamestate_restore_check() {
950         char Message[DIAGNOSTIC_MESSAGE_MAX];
951         obj_position Save_position;
952
953         if (gamestate_not_restored) {
954                 sprintf( Message, "Do you wish to restore game state?\n");
955         
956                 if (MessageBox( -2, -2, 2, Message, "Yes", "No" )==1) {
957
958                         // Save current position
959                         Save_position.pos = ConsoleObject->pos;
960                         Save_position.orient = ConsoleObject->orient;
961                         Save_position.segnum = ConsoleObject->segnum;
962
963                         load_level("GAMESAVE.LVL");
964
965                         // Restore current position
966                         if (Save_position.segnum <= Highest_segment_index) {
967                                 ConsoleObject->pos = Save_position.pos;
968                                 ConsoleObject->orient = Save_position.orient;
969                                 obj_relink(ConsoleObject-Objects,Save_position.segnum);
970                         }
971
972                         gamestate_not_restored = 0;
973                         Update_flags |= UF_WORLD_CHANGED;       
974                         }
975                 else
976                         gamestate_not_restored = 1;
977                 }
978 }
979
980 int RestoreGameState() {
981         load_level("GAMESAVE.LVL");
982         gamestate_not_restored = 0;
983
984         mprintf((0, "Gamestate restored.\n"));
985         editor_status("Gamestate restored.\n");
986
987         Update_flags |= UF_WORLD_CHANGED;
988         return 0;
989 }
990
991 extern void check_wall_validity(void);
992
993 // ---------------------------------------------------------------------------------------------------
994 //this function is the editor. called when editor mode selected.  runs until
995 //game mode or exit selected
996 void editor(void)
997 {
998         int w,h;
999         grs_bitmap * savedbitmap;
1000         editor_view *new_cv;
1001         static int padnum=0;
1002         vms_matrix      MouseRotMat,tempm;
1003         //@@short camera_objnum;                        //a camera for viewing
1004
1005         init_editor();
1006
1007         InitCurve();
1008
1009         restore_effect_bitmap_icons();
1010
1011         if (!set_screen_mode(SCREEN_EDITOR))    {
1012                 set_screen_mode(SCREEN_GAME);
1013                 Function_mode=FMODE_GAME;                       //force back into game
1014                 return;
1015         }
1016
1017         gr_set_current_canvas( NULL );
1018         gr_set_curfont(editor_font);
1019
1020         //Editor renders into full (320x200) game screen 
1021
1022         set_warn_func(med_show_warning);
1023
1024         keyd_repeat = 1;                // Allow repeat in editor
1025
1026 //      _MARK_("start of editor");//Nuked to compile -KRB
1027
1028         ui_mouse_hide();
1029
1030         ui_reset_idle_seconds();
1031
1032 //@@    //create a camera for viewing in the editor. copy position from ConsoleObject
1033 //@@    camera_objnum = obj_create(OBJ_CAMERA,0,ConsoleObject->segnum,&ConsoleObject->pos,&ConsoleObject->orient,0);
1034 //@@    Viewer = &Objects[camera_objnum];
1035 //@@    slew_init(Viewer);              //camera is slewing
1036
1037         Viewer = ConsoleObject;
1038         slew_init(ConsoleObject);
1039
1040         Update_flags = UF_ALL;
1041
1042         medlisp_update_screen();
1043
1044         //set the wire-frame window to be the current view
1045         current_view = &LargeView;
1046
1047         if (faded_in==0)
1048         {
1049                 faded_in = 1;
1050                 //gr_pal_fade_in( grd_curscreen->pal );
1051         }
1052
1053         w = GameViewBox->canvas->cv_bitmap.bm_w;
1054         h = GameViewBox->canvas->cv_bitmap.bm_h;
1055         
1056         savedbitmap = gr_create_bitmap(w, h );
1057
1058         gr_bm_ubitblt( w, h, 0, 0, 0, 0, &GameViewBox->canvas->cv_bitmap, savedbitmap );
1059
1060         gr_set_current_canvas( GameViewBox->canvas );
1061         gr_set_curfont(editor_font);
1062         //gr_setcolor( CBLACK );
1063         //gr_deaccent_canvas();
1064         //gr_grey_canvas();
1065         
1066         ui_mouse_show();
1067
1068         gr_set_curfont(editor_font);
1069         ui_pad_goto(padnum);
1070
1071         gamestate_restore_check();
1072
1073         while (Function_mode == FMODE_EDITOR) {
1074
1075                 gr_set_curfont(editor_font);
1076                 info_display_all(EditorWindow);
1077
1078                 ModeFlag = 0;
1079
1080                 // Update the windows
1081
1082                 // Only update if there is no key waiting and we're not in
1083                 // fast play mode.
1084                 if (!key_peekkey()) //-- && (MacroStatus != UI_STATUS_FASTPLAY))
1085                         medlisp_update_screen();
1086
1087                 //do editor stuff
1088                 gr_set_curfont(editor_font);
1089                 ui_mega_process();
1090                 last_keypress &= ~KEY_DEBUGGED;         //      mask off delete key bit which has no function in editor.
1091                 ui_window_do_gadgets(EditorWindow);
1092                 do_robot_window();
1093                 do_object_window();
1094                 do_wall_window();
1095                 do_trigger_window();
1096                 do_hostage_window();
1097                 do_centers_window();
1098                 check_wall_validity();
1099                 Assert(Num_walls>=0);
1100
1101                 if (Gameview_lockstep) {
1102                         static segment *old_cursegp=NULL;
1103                         static int old_curside=-1;
1104
1105                         if (old_cursegp!=Cursegp || old_curside!=Curside) {
1106                                 SetPlayerFromCursegMinusOne();
1107                                 old_cursegp = Cursegp;
1108                                 old_curside = Curside;
1109                         }
1110                 }
1111
1112 //              mprintf((0, "%d ", ui_get_idle_seconds() ));
1113
1114                 if ( ui_get_idle_seconds() > COMPRESS_INTERVAL ) 
1115                         {
1116                         med_compress_mine();
1117                         ui_reset_idle_seconds();
1118                         }
1119   
1120 //      Commented out because it occupies about 25% of time in twirling the mine.
1121 // Removes some Asserts....
1122 //              med_check_all_vertices();
1123                 clear_editor_status();          // if enough time elapsed, clear editor status message
1124                 TimedAutosave(mine_filename);
1125                 set_editor_time_of_day();
1126                 gr_set_current_canvas( GameViewBox->canvas );
1127                 
1128                 // Remove keys used for slew
1129                 switch(last_keypress)
1130                 {
1131                 case KEY_PAD9:
1132                 case KEY_PAD7:
1133                 case KEY_PADPLUS:
1134                 case KEY_PADMINUS:
1135                 case KEY_PAD8:
1136                 case KEY_PAD2:
1137                 case KEY_LBRACKET:
1138                 case KEY_RBRACKET:
1139                 case KEY_PAD1:
1140                 case KEY_PAD3:
1141                 case KEY_PAD6:
1142                 case KEY_PAD4:
1143                         last_keypress = 0;
1144                 }
1145                 if ((last_keypress&0xff)==KEY_LSHIFT) last_keypress=0;
1146                 if ((last_keypress&0xff)==KEY_RSHIFT) last_keypress=0;
1147                 if ((last_keypress&0xff)==KEY_LCTRL) last_keypress=0;
1148                 if ((last_keypress&0xff)==KEY_RCTRL) last_keypress=0;
1149 //              if ((last_keypress&0xff)==KEY_LALT) last_keypress=0;
1150 //              if ((last_keypress&0xff)==KEY_RALT) last_keypress=0;
1151
1152                 gr_set_curfont(editor_font);
1153                 menubar_do( last_keypress );
1154
1155                 //=================== DO FUNCTIONS ====================
1156
1157                 if ( KeyFunction[ last_keypress ] != NULL )     {
1158                         KeyFunction[last_keypress]();
1159                         last_keypress = 0;
1160                 }
1161                 switch (last_keypress)
1162                 {
1163                 case 0:
1164                 case KEY_Z:
1165                 case KEY_G:
1166                 case KEY_LALT:
1167                 case KEY_RALT:
1168                 case KEY_LCTRL:
1169                 case KEY_RCTRL:
1170                 case KEY_LSHIFT:
1171                 case KEY_RSHIFT:
1172                 case KEY_LAPOSTRO:
1173                         break;
1174                 case KEY_SHIFTED + KEY_L:
1175                         ToggleLighting();
1176                         break;
1177                 case KEY_F1:
1178                         render_3d_in_big_window = !render_3d_in_big_window;
1179                         Update_flags |= UF_ALL;
1180                         break;                  
1181                 default:
1182                         {
1183                         char kdesc[100];
1184                         GetKeyDescription( kdesc, last_keypress );
1185                         editor_status("Error: %s isn't bound to anything.", kdesc  );
1186                         }
1187                 }
1188
1189                 //================================================================
1190
1191                 if (ModeFlag==1)
1192                 {
1193                         close_editor_screen();
1194                         Function_mode=FMODE_EXIT;
1195                                 gr_free_bitmap( savedbitmap );
1196                         break;
1197                 }
1198
1199                 if (ModeFlag==2) //-- && MacroStatus==UI_STATUS_NORMAL )
1200                 {
1201                         close_editor_screen();
1202                         Function_mode = FMODE_MENU;
1203                         set_screen_mode(SCREEN_MENU);           //put up menu screen
1204                         gr_free_bitmap(savedbitmap);
1205                         break;
1206                 }
1207
1208                 if (ModeFlag==3) //-- && MacroStatus==UI_STATUS_NORMAL )
1209                 {
1210 //                      med_compress_mine();                                            //will be called anyways before game.
1211                         close_editor_screen();
1212                         Function_mode=FMODE_GAME;                       //force back into game
1213                         set_screen_mode(SCREEN_GAME);           //put up game screen
1214                         gr_free_bitmap( savedbitmap );
1215                         break;
1216                 }
1217
1218 //              if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)GameViewBox) current_view=NULL;
1219 //              if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)GroupViewBox) current_view=NULL;
1220
1221                 new_cv = current_view ;
1222
1223 #if ORTHO_VIEWS
1224                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)LargeViewBox) new_cv=&LargeView;
1225                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)TopViewBox)        new_cv=&TopView;
1226                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)FrontViewBox) new_cv=&FrontView;
1227                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)RightViewBox) new_cv=&RightView;
1228 #endif
1229                 if (new_cv != current_view ) {
1230                         current_view->ev_changed = 1;
1231                         new_cv->ev_changed = 1;
1232                         current_view = new_cv;
1233                 }
1234
1235                 calc_frame_time();
1236                 if (slew_frame(0)) {            //do movement and check keys
1237                         Update_flags |= UF_GAME_VIEW_CHANGED;
1238                         if (Gameview_lockstep) {
1239                                 Cursegp = &Segments[ConsoleObject->segnum];
1240                                 med_create_new_segment_from_cursegp();
1241                                 Update_flags |= UF_ED_STATE_CHANGED;
1242                         }
1243                 }
1244
1245                 // DO TEXTURE STUFF
1246                 texpage_do();
1247                 objpage_do();
1248
1249
1250                 // Process selection of Cursegp using mouse.
1251                 if (LargeViewBox->mouse_onme && LargeViewBox->b1_clicked && !render_3d_in_big_window) 
1252                 {
1253                         int     xcrd,ycrd;
1254                         xcrd = LargeViewBox->b1_drag_x1;
1255                         ycrd = LargeViewBox->b1_drag_y1;
1256
1257                         find_segments(xcrd,ycrd,LargeViewBox->canvas,&LargeView,Cursegp,Big_depth);     // Sets globals N_found_segs, Found_segs
1258
1259                         // If shift is down, then add segment to found list
1260                         if (keyd_pressed[ KEY_LSHIFT ] || keyd_pressed[ KEY_RSHIFT ])
1261                                 subtract_found_segments_from_selected_list();
1262                         else
1263                                 add_found_segments_to_selected_list();
1264
1265                         Found_seg_index = 0;    
1266                 
1267                         if (N_found_segs > 0) {
1268                                 sort_seg_list(N_found_segs,Found_segs,&ConsoleObject->pos);
1269                                 Cursegp = &Segments[Found_segs[0]];
1270                                 med_create_new_segment_from_cursegp();
1271                                 if (Lock_view_to_cursegp)
1272                                         set_view_target_from_segment(Cursegp);
1273                         }
1274
1275                         Update_flags |= UF_ED_STATE_CHANGED | UF_VIEWPOINT_MOVED;
1276                 }
1277
1278                 if (GameViewBox->mouse_onme && GameViewBox->b1_dragging) {
1279                         int     x, y;
1280                         x = GameViewBox->b1_drag_x2;
1281                         y = GameViewBox->b1_drag_y2;
1282
1283                         ui_mouse_hide();
1284                         gr_set_current_canvas( GameViewBox->canvas );
1285                         gr_setcolor( 15 );
1286                         gr_rect( x-1, y-1, x+1, y+1 );
1287                         ui_mouse_show();
1288
1289                 }
1290                 
1291                 // Set current segment and side by clicking on a polygon in game window.
1292                 //      If ctrl pressed, also assign current texture map to that side.
1293                 //if (GameViewBox->mouse_onme && (GameViewBox->b1_done_dragging || GameViewBox->b1_clicked)) {
1294                 if ((GameViewBox->mouse_onme && GameViewBox->b1_clicked && !render_3d_in_big_window) ||
1295                         (LargeViewBox->mouse_onme && LargeViewBox->b1_clicked && render_3d_in_big_window)) {
1296
1297                         int     xcrd,ycrd;
1298                         int seg,side,face,poly,tmap;
1299
1300                         if (render_3d_in_big_window) {
1301                                 xcrd = LargeViewBox->b1_drag_x1;
1302                                 ycrd = LargeViewBox->b1_drag_y1;
1303                         }
1304                         else {
1305                                 xcrd = GameViewBox->b1_drag_x1;
1306                                 ycrd = GameViewBox->b1_drag_y1;
1307                         }
1308         
1309                         //Int3();
1310
1311                         if (find_seg_side_face(xcrd,ycrd,&seg,&side,&face,&poly)) {
1312
1313
1314                                 if (seg<0) {                                                    //found an object
1315
1316                                         Cur_object_index = -seg-1;
1317                                         editor_status("Object %d selected.",Cur_object_index);
1318
1319                                         Update_flags |= UF_ED_STATE_CHANGED;
1320                                 }
1321                                 else {
1322
1323                                         //      See if either shift key is down and, if so, assign texture map
1324                                         if (keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT]) {
1325                                                 Cursegp = &Segments[seg];
1326                                                 Curside = side;
1327                                                 AssignTexture();
1328                                                 med_create_new_segment_from_cursegp();
1329                                                 editor_status("Texture assigned");
1330                                         } else if (keyd_pressed[KEY_G]) {
1331                                                 tmap = Segments[seg].sides[side].tmap_num;
1332                                                 texpage_grab_current(tmap);
1333                                                 editor_status( "Texture grabbed." );
1334                                         } else if (keyd_pressed[ KEY_LAPOSTRO] ) {
1335                                                 ui_mouse_hide();
1336                                                 move_object_to_mouse_click();
1337                                         } else {
1338                                                 Cursegp = &Segments[seg];
1339                                                 Curside = side;
1340                                                 med_create_new_segment_from_cursegp();
1341                                                 editor_status("Curseg and curside selected");
1342                                         }
1343                                 }
1344
1345                                 Update_flags |= UF_ED_STATE_CHANGED;
1346                         }
1347                         else 
1348                                 editor_status("Click on non-texture ingored");
1349
1350                 }
1351
1352                 // Allow specification of LargeView using mouse
1353                 if (keyd_pressed[ KEY_LCTRL ] || keyd_pressed[ KEY_RCTRL ]) {
1354                         ui_mouse_hide();
1355                         if ( (Mouse.dx!=0) && (Mouse.dy!=0) ) {
1356                                 GetMouseRotation( Mouse.dx, Mouse.dy, &MouseRotMat );
1357                                 vm_matrix_x_matrix(&tempm,&LargeView.ev_matrix,&MouseRotMat);
1358                                 LargeView.ev_matrix = tempm;
1359                                 LargeView.ev_changed = 1;
1360                                 Large_view_index = -1;                  // say not one of the orthogonal views
1361                         }
1362                 } else  {
1363                         ui_mouse_show();
1364                 }
1365
1366                 if ( keyd_pressed[ KEY_Z ] ) {
1367                         ui_mouse_hide();
1368                         if ( Mouse.dy!=0 ) {
1369                                 current_view->ev_dist += Mouse.dy*10000;
1370                                 current_view->ev_changed = 1;
1371                         }
1372                 } else {
1373                         ui_mouse_show();
1374                 }
1375
1376                 gr_update();
1377                 
1378         }
1379
1380 //      _MARK_("end of editor");//Nuked to compile -KRB
1381
1382         clear_warn_func(med_show_warning);
1383
1384         //kill our camera object
1385
1386         Viewer = ConsoleObject;                                 //reset viewer
1387         //@@obj_delete(camera_objnum);
1388
1389         padnum = ui_pad_get_current();
1390
1391         close_editor();
1392         ui_close();
1393
1394
1395 }
1396
1397 void test_fade(void)
1398 {
1399         int     i,c;
1400
1401         for (c=0; c<256; c++) {
1402                 printf("%4i: {%3i %3i %3i} ",c,gr_palette[3*c],gr_palette[3*c+1],gr_palette[3*c+2]);
1403                 for (i=0; i<16; i++) {
1404                         int col = gr_fade_table[256*i+c];
1405
1406                         printf("[%3i %3i %3i] ",gr_palette[3*col],gr_palette[3*col+1],gr_palette[3*col+2]);
1407                 }
1408                 if ( (c%16) == 15)
1409                         printf("\n");
1410                 printf("\n");
1411         }
1412 }
1413
1414 void dump_stuff(void)
1415 {
1416         int     i,j,prev_color;
1417
1418         printf("Palette:\n");
1419
1420         for (i=0; i<256; i++)
1421                 printf("%3i: %2i %2i %2i\n",i,gr_palette[3*i],gr_palette[3*i+1],gr_palette[3*i+2]);
1422
1423         for (i=0; i<16; i++) {
1424                 printf("\nFade table #%i\n",i);
1425                 for (j=0; j<256; j++) {
1426                         int     c = gr_fade_table[i*256 + j];
1427                         printf("[%3i %2i %2i %2i] ",c, gr_palette[3*c], gr_palette[3*c+1], gr_palette[3*c+2]);
1428                         if ((j % 8) == 7)
1429                                 printf("\n");
1430                 }
1431         }
1432
1433         printf("Colors indexed by intensity:\n");
1434         printf(". = change from previous, * = no change\n");
1435         for (j=0; j<256; j++) {
1436                 printf("%3i: ",j);
1437                 prev_color = -1;
1438                 for (i=0; i<16; i++) {
1439                         int     c = gr_fade_table[i*256 + j];
1440                         if (c == prev_color)
1441                                 printf("*");
1442                         else
1443                                 printf(".");
1444                         prev_color = c;
1445                 }
1446                 printf("  ");
1447                 for (i=0; i<16; i++) {
1448                         int     c = gr_fade_table[i*256 + j];
1449                         printf("[%3i %2i %2i %2i] ", c, gr_palette[3*c], gr_palette[3*c+1], gr_palette[3*c+2]);
1450                 }
1451                 printf("\n");
1452         }
1453
1454 }
1455
1456 #ifndef NDEBUG
1457 int MarkStart(void)
1458 {
1459         char mystr[30];
1460         sprintf(mystr,"mark %i start",Mark_count);
1461 //      _MARK_(mystr);//Nuked to compile -KRB
1462
1463         return 1;
1464 }
1465
1466 int MarkEnd(void)
1467 {
1468         char mystr[30];
1469         sprintf(mystr,"mark %i end",Mark_count);
1470         Mark_count++;
1471 //      _MARK_(mystr);//Nuked to compile -KRB
1472
1473         return 1;
1474 }
1475 #endif