]> icculus.org git repositories - btb/d2x.git/blob - main/editor/ehostage.c
imported missing editor files from d1x
[btb/d2x.git] / main / editor / ehostage.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13 /*
14  * $Source: /cvs/cvsroot/d2x/main/editor/ehostage.c,v $
15  * $Revision: 1.1 $
16  * $Author: btb $
17  * $Date: 2004-12-19 13:54:27 $
18  * 
19  * Routines for placing hostages, etc...
20  * 
21  * $Log: not supported by cvs2svn $
22  * Revision 1.1.1.1  1999/06/14 22:02:53  donut
23  * Import of d1x 1.37 source.
24  *
25  * Revision 2.0  1995/02/27  11:35:45  john
26  * Version 2.0! No anonymous unions, Watcom 10.0, with no need
27  * for bitmaps.tbl.
28  * 
29  * Revision 1.17  1995/01/14  19:18:05  john
30  * First version of object paging.
31  * 
32  * Revision 1.16  1994/11/20  14:11:41  matt
33  * Show object number in hostage window
34  * 
35  * Revision 1.15  1994/11/19  19:55:46  matt
36  * Added code to full support different hostage head clip & message for
37  * each hostage.
38  * 
39  * 
40  * Revision 1.14  1994/10/28  15:03:27  john
41  * Made digi_play_sample use volume.
42  * 
43  * 
44  * Revision 1.13  1994/10/23  02:11:39  matt
45  * Got rid of obsolete hostage_info stuff
46  * 
47  * Revision 1.12  1994/10/04  13:15:44  john
48  * Changed PLAY_SOUND to digi_play_sample.
49  * 
50  * Revision 1.11  1994/08/02  14:17:28  mike
51  * Clean up dialog boxes.
52  * 
53  * Revision 1.10  1994/07/22  17:19:17  yuan
54  * Working on dialog box for refuel/repair/material/control centers.
55  * 
56  * Revision 1.9  1994/07/06  15:22:34  john
57  * Added new sound.
58  * 
59  * 
60  * Revision 1.8  1994/07/06  14:26:07  john
61  * Added vclip.
62  * 
63  * Revision 1.7  1994/07/06  13:25:52  john
64  * Added compress hostages functions.
65  * 
66  * Revision 1.6  1994/07/06  12:52:27  john
67  * Fixed warnings.
68  * 
69  * Revision 1.5  1994/07/06  12:43:04  john
70  * Made generic messages for hostages.
71  * 
72  * Revision 1.4  1994/07/06  11:49:01  john
73  * Made adding hostage update current object.
74  * 
75  * Revision 1.3  1994/07/06  10:56:00  john
76  * New structures for hostages.
77  * 
78  * Revision 1.2  1994/07/01  17:57:13  john
79  * First version of not-working hostage system
80  * 
81  * 
82  * Revision 1.1  1994/07/01  14:21:44  john
83  * Initial revision
84  * 
85  * 
86  */
87
88
89 #ifdef RCS
90 static char rcsid[] = "$Id: ehostage.c,v 1.1 2004-12-19 13:54:27 btb Exp $";
91 #endif
92
93 #include <stdlib.h>
94 #include <stdio.h>
95 #ifndef __LINUX__
96 #include <conio.h>
97 #include <dos.h>
98 #include <direct.h>
99 #endif 
100 #include <string.h>
101 #include <math.h>
102
103 #include "screens.h"
104 #include "inferno.h"
105 #include "segment.h"
106 #include "editor.h"
107
108 #include "timer.h"
109 #include "objpage.h"
110 #include "fix.h"
111 #include "mono.h"
112 #include "error.h"
113 #include "kdefs.h"
114 #include        "object.h"
115 #include "polyobj.h"
116 #include "game.h"
117 #include "powerup.h"
118 #include "ai.h"
119 #include "hostage.h"
120 #include "eobject.h"
121 #include "medwall.h"
122 #include "eswitch.h"
123 #include "medrobot.h"
124 #include "key.h"
125 #include "bm.h"
126 #include "sounds.h"
127 #include "centers.h"
128 #include "piggy.h"
129
130 //-------------------------------------------------------------------------
131 // Variables for this module...
132 //-------------------------------------------------------------------------
133 static UI_WINDOW                                *MainWindow = NULL;
134 static UI_GADGET_USERBOX        *HostageViewBox;
135 static UI_GADGET_INPUTBOX       *HostageText;
136 static UI_GADGET_BUTTON         *QuitButton;
137 static int                                              CurrentHostageIndex=-1;
138 static int                                              LastHostageIndex=-1;
139
140 static fix                      Vclip_animation_time=0;                 // How long the rescue sequence has been playing
141 static fix                      Vclip_playback_speed=0;                         // Calculated internally.  Frames/second of vclip.
142 static vclip            *Vclip_ptr = NULL;                              // Used for the vclip on monitor
143
144 void vclip_play( vclip * vc, fix frame_time )   
145 {
146         int bitmapnum;
147
148         if ( vc == NULL )
149                 return;
150
151         if ( vc != Vclip_ptr )  {
152                 // Start new vclip
153                 Vclip_ptr = vc;
154                 Vclip_animation_time = 1;
155
156                 // Calculate the frame/second of the playback
157                 Vclip_playback_speed = fixdiv(i2f(Vclip_ptr->num_frames),Vclip_ptr->play_time);
158         }
159
160         if ( Vclip_animation_time <= 0 )
161                 return;
162
163         // Find next bitmap in the vclip
164         bitmapnum = f2i(Vclip_animation_time);
165
166         // Check if vclip is done playing.
167         if (bitmapnum >= Vclip_ptr->num_frames)         {
168                 Vclip_animation_time    = 1;                                                                                    // Restart this vclip
169                 bitmapnum = 0;
170         }
171
172         PIGGY_PAGE_IN( Vclip_ptr->frames[bitmapnum] );
173         gr_bitmap(0,0,&GameBitmaps[Vclip_ptr->frames[bitmapnum].index] );
174         
175         Vclip_animation_time += fixmul(frame_time, Vclip_playback_speed );
176 }
177
178
179
180 static char HostageMessage[]  = "  ";
181
182 static fix Time;
183
184 int SelectPrevHostage() {
185         int start=0;
186
187         do      {
188                 CurrentHostageIndex--;
189                 if ( CurrentHostageIndex < 0 ) CurrentHostageIndex = MAX_HOSTAGES-1;
190                 start++;
191                 if ( start > MAX_HOSTAGES ) break;
192         } while ( !hostage_is_valid( CurrentHostageIndex ) );
193
194         if (hostage_is_valid( CurrentHostageIndex ) )   {
195                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
196         } else {
197                 CurrentHostageIndex =-1;
198         }
199         
200         return CurrentHostageIndex;
201 }
202
203
204 int SelectNextHostage() {
205         int start=0;
206
207         do      {
208                 CurrentHostageIndex++;
209                 if ( CurrentHostageIndex >= MAX_HOSTAGES ) CurrentHostageIndex = 0;
210                 start++;
211                 if ( start > MAX_HOSTAGES ) break;
212         } while ( !hostage_is_valid( CurrentHostageIndex ) );
213
214         if (hostage_is_valid( CurrentHostageIndex ) )   {
215                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
216         } else {
217                 CurrentHostageIndex =-1;
218         }
219         
220         return CurrentHostageIndex;
221 }
222
223
224 int SelectClosestHostage()      {
225         int start=0;
226
227         while ( !hostage_is_valid( CurrentHostageIndex ) )      {
228                 CurrentHostageIndex++;
229                 if ( CurrentHostageIndex >= MAX_HOSTAGES ) CurrentHostageIndex = 0;
230                 start++;
231                 if ( start > MAX_HOSTAGES ) break;
232         }
233
234         if (hostage_is_valid( CurrentHostageIndex ) )   {
235                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
236         } else {
237                 CurrentHostageIndex =-1;
238         }
239         
240         return CurrentHostageIndex;
241 }
242
243
244 int PlaceHostage()      {
245         int ctype,i;
246         vms_vector      cur_object_loc;
247
248         //update_due_to_new_segment();
249         compute_segment_center(&cur_object_loc, Cursegp);
250
251         ctype = -1;
252         for (i=0; i<Num_total_object_types; i++ )       {
253                 if (ObjType[i] == OL_HOSTAGE )  {
254                         ctype = i;      
255                         break;
256                 }
257         }
258
259         Assert( ctype != -1 );
260
261         if (place_object(Cursegp, &cur_object_loc, ctype )==0)  {
262                 Int3();         // Debug below
263                 i=place_object(Cursegp, &cur_object_loc, ctype );
264                 return 1;
265         }
266
267         if (hostage_object_is_valid( Cur_object_index ) )       {
268                 CurrentHostageIndex     = Objects[Cur_object_index].id;
269         } else {
270                 Int3();         // Get John! (Object should be valid)
271                 i=hostage_object_is_valid( Cur_object_index );  // For debugging only
272         }
273
274         return 0;
275 }
276
277 int CompressHostages()
278 {
279         hostage_compress_all();
280
281         return 0;
282 }
283
284 //@@int SelectPrevVclip()       {
285 //@@    if (!hostage_is_valid( CurrentHostageIndex ) )  
286 //@@            return 0;
287 //@@
288 //@@    if ( Hostages[CurrentHostageIndex].type == 0 )
289 //@@            Hostages[CurrentHostageIndex].type = N_hostage_types-1;
290 //@@    else
291 //@@            Hostages[CurrentHostageIndex].type--;
292 //@@    
293 //@@    if ( Hostages[CurrentHostageIndex].type >= N_hostage_types )
294 //@@            Hostages[CurrentHostageIndex].type = 0;
295 //@@    
296 //@@    return 1;
297 //@@}
298 //@@
299 //@@int SelectNextVclip()       {
300 //@@    if (!hostage_is_valid( CurrentHostageIndex ) )  
301 //@@            return 0;
302 //@@
303 //@@    Hostages[CurrentHostageIndex].type++;
304 //@@    if ( Hostages[CurrentHostageIndex].type >= N_hostage_types )
305 //@@            Hostages[CurrentHostageIndex].type = 0;
306 //@@
307 //@@    return 1;
308 //@@}
309
310 int SelectNextFace()
311 {
312         int start = Hostages[CurrentHostageIndex].vclip_num;
313         
314         if (!hostage_is_valid( CurrentHostageIndex ) )  
315                 return 0;
316
317         do {
318                 Hostages[CurrentHostageIndex].vclip_num++;
319                 if ( Hostages[CurrentHostageIndex].vclip_num >= MAX_HOSTAGES)
320                         Hostages[CurrentHostageIndex].vclip_num = 0;
321
322                 if (Hostages[CurrentHostageIndex].vclip_num == start)
323                         return 0;
324
325         } while (Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].num_frames == 0);
326
327         return 1;
328 }
329
330 int SelectPrevFace()
331 {
332         int start = Hostages[CurrentHostageIndex].vclip_num;
333         
334         if (!hostage_is_valid( CurrentHostageIndex ) )  
335                 return 0;
336
337         do {
338                 Hostages[CurrentHostageIndex].vclip_num--;
339                 if ( Hostages[CurrentHostageIndex].vclip_num < 0)
340                         Hostages[CurrentHostageIndex].vclip_num = MAX_HOSTAGES-1;
341
342                 if (Hostages[CurrentHostageIndex].vclip_num == start)
343                         return 0;
344
345         } while (Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].num_frames == 0);
346
347         return 1;
348 }
349
350 int PlayHostageSound()  {
351         int sound_num;
352
353         if (!hostage_is_valid( CurrentHostageIndex ) )  
354                 return 0;
355
356         sound_num = Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].sound_num;
357
358         if ( sound_num > -1 )   {
359                 digi_play_sample( sound_num, F1_0 );
360         }
361
362         return 1;       
363 }
364
365 //@@int find_next_hostage_sound()       {
366 //@@    int start=0,n;
367 //@@
368 //@@    n = Hostages[CurrentHostageIndex].sound_num;
369 //@@    do      {
370 //@@            n++;
371 //@@            if ( n < SOUND_HOSTAGE_VOICES ) n = SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS-1;
372 //@@            if ( n >= SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS ) n = SOUND_HOSTAGE_VOICES;
373 //@@            start++;
374 //@@            if ( start > MAX_HOSTAGE_SOUNDS ) break;
375 //@@    } while ( Sounds[n] == NULL );
376 //@@
377 //@@    if ( Sounds[n] == NULL )
378 //@@            Hostages[CurrentHostageIndex].sound_num = -1;
379 //@@    else    {
380 //@@            Hostages[CurrentHostageIndex].sound_num = n;
381 //@@            PlayHostageSound();
382 //@@    }
383 //@@    return 1;
384 //@@}
385 //@@
386 //@@int find_prev_hostage_sound()       {
387 //@@    int start=0,n;
388 //@@
389 //@@    n = Hostages[CurrentHostageIndex].sound_num;
390 //@@    do      {
391 //@@            n--;
392 //@@            if ( n < SOUND_HOSTAGE_VOICES ) n = SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS-1;
393 //@@            if ( n >= SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS ) n = SOUND_HOSTAGE_VOICES;
394 //@@            start++;
395 //@@            if ( start > MAX_HOSTAGE_SOUNDS ) break;
396 //@@    } while ( Sounds[n] == NULL );
397 //@@
398 //@@    if ( Sounds[n] == NULL )
399 //@@            Hostages[CurrentHostageIndex].sound_num = -1;
400 //@@    else    {
401 //@@            Hostages[CurrentHostageIndex].sound_num = n;
402 //@@            PlayHostageSound();
403 //@@    }
404 //@@    return 1;
405 //@@}
406
407
408 //-------------------------------------------------------------------------
409 // Called from the editor... does one instance of the hostage dialog box
410 //-------------------------------------------------------------------------
411 int do_hostage_dialog()
412 {
413         int i;
414
415         // Only open 1 instance of this window...
416         if ( MainWindow != NULL ) return 0;
417         
418         // Close other windows
419         close_all_windows();
420
421         CurrentHostageIndex = 0;
422         SelectClosestHostage();
423
424         // Open a window with a quit button
425         MainWindow = ui_open_window( TMAPBOX_X+10, TMAPBOX_Y+20, 765-TMAPBOX_X, 545-TMAPBOX_Y, WIN_DIALOG );
426         QuitButton = ui_add_gadget_button( MainWindow, 20, 222, 48, 40, "Done", NULL );
427
428         ui_wprintf_at( MainWindow, 10, 32,"&Message:" );
429         HostageText = ui_add_gadget_inputbox( MainWindow, 10, 50, HOSTAGE_MESSAGE_LEN, HOSTAGE_MESSAGE_LEN, HostageMessage );
430
431         // The little box the hostage vclip will play in.
432         HostageViewBox = ui_add_gadget_userbox( MainWindow,10, 90+10, 64, 64 );
433
434         // A bunch of buttons...
435         i = 90;
436 //@@    ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Type", SelectPrevVclip );
437 //@@    ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Type >>", SelectNextVclip );i += 29;         
438 //@@    ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Sound",  find_prev_hostage_sound );
439 //@@    ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Sound >>", find_next_hostage_sound );i += 29;                
440
441         ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Face", SelectPrevFace );
442         ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Face >>", SelectNextFace );i += 29;          
443         ui_add_gadget_button( MainWindow,155,i,140, 26, "Play sound", PlayHostageSound );i += 29;               
444         ui_add_gadget_button( MainWindow,155,i,140, 26, "Next Hostage", SelectNextHostage );    i += 29;                
445         ui_add_gadget_button( MainWindow,155,i,140, 26, "Prev Hostage", SelectPrevHostage ); i += 29;           
446         ui_add_gadget_button( MainWindow,155,i,140, 26, "Compress All", CompressHostages ); i += 29;            
447         ui_add_gadget_button( MainWindow,155,i,140, 26, "Delete", ObjectDelete );       i += 29;                
448         ui_add_gadget_button( MainWindow,155,i,140, 26, "Create New", PlaceHostage );   i += 29;                
449         
450         Time = timer_get_fixed_seconds();
451
452         LastHostageIndex = -2;          // Set to some dummy value so everything works ok on the first frame.
453         
454 //      if ( CurrentHostageIndex == -1 )
455 //              SelectNextHostage();
456
457         return 1;
458
459 }
460
461 void hostage_close_window()
462 {
463         if ( MainWindow!=NULL ) {
464                 ui_close_window( MainWindow );
465                 MainWindow = NULL;
466         }
467 }
468
469 void do_hostage_window()
470 {
471         fix DeltaTime, Temp;
472
473         if ( MainWindow == NULL ) return;
474
475         SelectClosestHostage();
476
477         //------------------------------------------------------------
478         // Call the ui code..
479         //------------------------------------------------------------
480         ui_button_any_drawn = 0;
481         ui_window_do_gadgets(MainWindow);
482
483         //------------------------------------------------------------
484         // If we change objects, we need to reset the ui code for all
485         // of the radio buttons that control the ai mode.  Also makes
486         // the current AI mode button be flagged as pressed down.
487         //------------------------------------------------------------
488         if (LastHostageIndex != CurrentHostageIndex )   {
489
490                 if ( CurrentHostageIndex > -1 ) 
491                         strcpy( HostageText->text, Hostages[CurrentHostageIndex].text );
492                 else
493                         strcpy(HostageText->text, " " );
494
495                 HostageText->position = strlen(HostageText->text);
496                 HostageText->oldposition = HostageText->position;
497                 HostageText->status=1;
498                 HostageText->first_time = 1;
499
500         }
501
502         //------------------------------------------------------------
503         // If any of the radio buttons that control the mode are set, then
504         // update the cooresponding AI state.
505         //------------------------------------------------------------
506         if ( CurrentHostageIndex > -1 ) 
507                 strcpy( Hostages[CurrentHostageIndex].text, HostageText->text );
508
509         //------------------------------------------------------------
510         // A simple frame time counter for spinning the objects...
511         //------------------------------------------------------------
512         Temp = timer_get_fixed_seconds();
513         DeltaTime = Temp - Time;
514         Time = Temp;
515
516         //------------------------------------------------------------
517         // Redraw the object in the little 64x64 box
518         //------------------------------------------------------------
519         if (CurrentHostageIndex > -1 )  {
520                 int vclip_num;
521                 
522                 vclip_num = Hostages[CurrentHostageIndex].vclip_num;
523
524                 Assert(vclip_num != -1);
525
526                 gr_set_current_canvas( HostageViewBox->canvas );
527
528                 if ( vclip_num > -1 )   {
529                         vclip_play( &Hostage_face_clip[vclip_num], DeltaTime ); 
530                 } else {
531                         gr_clear_canvas( CGREY );
532                 }
533         } else {
534                 // no hostage, so just blank out
535                 gr_set_current_canvas( HostageViewBox->canvas );
536                 gr_clear_canvas( CGREY );
537         }
538
539         //------------------------------------------------------------
540         // If anything changes in the ui system, redraw all the text that
541         // identifies this robot.
542         //------------------------------------------------------------
543         if (ui_button_any_drawn || (LastHostageIndex != CurrentHostageIndex) )  {
544                 if ( CurrentHostageIndex > -1 ) {
545                         ui_wprintf_at( MainWindow, 10, 15, "Hostage: %d   Object: %d", CurrentHostageIndex, Hostages[CurrentHostageIndex].objnum );
546                         //@@ui_wprintf_at( MainWindow, 10, 73, "Type: %d   Sound: %d   ", Hostages[CurrentHostageIndex].type, Hostages[CurrentHostageIndex].sound_num );
547                         ui_wprintf_at( MainWindow, 10, 73, "Face: %d   ", Hostages[CurrentHostageIndex].vclip_num);
548                 }       else {
549                         ui_wprintf_at( MainWindow, 10, 15, "Hostage: none " );
550                         //@@ui_wprintf_at( MainWindow, 10, 73, "Type:    Sound:       " );
551                         ui_wprintf_at( MainWindow, 10, 73, "Face:         " );
552                 }
553                 Update_flags |= UF_WORLD_CHANGED;
554         }
555
556         if ( QuitButton->pressed || (last_keypress==KEY_ESC))   {
557                 hostage_close_window();
558                 return;
559         }               
560
561         LastHostageIndex = CurrentHostageIndex;
562 }
563
564
565