]> icculus.org git repositories - btb/d2x.git/blob - main/editor/ehostage.c
Move old logs to ChangeLog-old
[btb/d2x.git] / main / editor / ehostage.c
1 /* $Id: ehostage.c,v 1.2 2004-12-19 14:52:48 btb 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  * Routines for placing hostages, etc...
18  *
19  */
20
21
22 #ifdef RCS
23 static char rcsid[] = "$Id: ehostage.c,v 1.2 2004-12-19 14:52:48 btb Exp $";
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifndef __LINUX__
29 #include <conio.h>
30 #include <dos.h>
31 #include <direct.h>
32 #endif 
33 #include <string.h>
34 #include <math.h>
35
36 #include "screens.h"
37 #include "inferno.h"
38 #include "segment.h"
39 #include "editor.h"
40
41 #include "timer.h"
42 #include "objpage.h"
43 #include "fix.h"
44 #include "mono.h"
45 #include "error.h"
46 #include "kdefs.h"
47 #include        "object.h"
48 #include "polyobj.h"
49 #include "game.h"
50 #include "powerup.h"
51 #include "ai.h"
52 #include "hostage.h"
53 #include "eobject.h"
54 #include "medwall.h"
55 #include "eswitch.h"
56 #include "medrobot.h"
57 #include "key.h"
58 #include "bm.h"
59 #include "sounds.h"
60 #include "centers.h"
61 #include "piggy.h"
62
63 //-------------------------------------------------------------------------
64 // Variables for this module...
65 //-------------------------------------------------------------------------
66 static UI_WINDOW                                *MainWindow = NULL;
67 static UI_GADGET_USERBOX        *HostageViewBox;
68 static UI_GADGET_INPUTBOX       *HostageText;
69 static UI_GADGET_BUTTON         *QuitButton;
70 static int                                              CurrentHostageIndex=-1;
71 static int                                              LastHostageIndex=-1;
72
73 static fix                      Vclip_animation_time=0;                 // How long the rescue sequence has been playing
74 static fix                      Vclip_playback_speed=0;                         // Calculated internally.  Frames/second of vclip.
75 static vclip            *Vclip_ptr = NULL;                              // Used for the vclip on monitor
76
77 void vclip_play( vclip * vc, fix frame_time )   
78 {
79         int bitmapnum;
80
81         if ( vc == NULL )
82                 return;
83
84         if ( vc != Vclip_ptr )  {
85                 // Start new vclip
86                 Vclip_ptr = vc;
87                 Vclip_animation_time = 1;
88
89                 // Calculate the frame/second of the playback
90                 Vclip_playback_speed = fixdiv(i2f(Vclip_ptr->num_frames),Vclip_ptr->play_time);
91         }
92
93         if ( Vclip_animation_time <= 0 )
94                 return;
95
96         // Find next bitmap in the vclip
97         bitmapnum = f2i(Vclip_animation_time);
98
99         // Check if vclip is done playing.
100         if (bitmapnum >= Vclip_ptr->num_frames)         {
101                 Vclip_animation_time    = 1;                                                                                    // Restart this vclip
102                 bitmapnum = 0;
103         }
104
105         PIGGY_PAGE_IN( Vclip_ptr->frames[bitmapnum] );
106         gr_bitmap(0,0,&GameBitmaps[Vclip_ptr->frames[bitmapnum].index] );
107         
108         Vclip_animation_time += fixmul(frame_time, Vclip_playback_speed );
109 }
110
111
112
113 static char HostageMessage[]  = "  ";
114
115 static fix Time;
116
117 int SelectPrevHostage() {
118         int start=0;
119
120         do      {
121                 CurrentHostageIndex--;
122                 if ( CurrentHostageIndex < 0 ) CurrentHostageIndex = MAX_HOSTAGES-1;
123                 start++;
124                 if ( start > MAX_HOSTAGES ) break;
125         } while ( !hostage_is_valid( CurrentHostageIndex ) );
126
127         if (hostage_is_valid( CurrentHostageIndex ) )   {
128                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
129         } else {
130                 CurrentHostageIndex =-1;
131         }
132         
133         return CurrentHostageIndex;
134 }
135
136
137 int SelectNextHostage() {
138         int start=0;
139
140         do      {
141                 CurrentHostageIndex++;
142                 if ( CurrentHostageIndex >= MAX_HOSTAGES ) CurrentHostageIndex = 0;
143                 start++;
144                 if ( start > MAX_HOSTAGES ) break;
145         } while ( !hostage_is_valid( CurrentHostageIndex ) );
146
147         if (hostage_is_valid( CurrentHostageIndex ) )   {
148                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
149         } else {
150                 CurrentHostageIndex =-1;
151         }
152         
153         return CurrentHostageIndex;
154 }
155
156
157 int SelectClosestHostage()      {
158         int start=0;
159
160         while ( !hostage_is_valid( CurrentHostageIndex ) )      {
161                 CurrentHostageIndex++;
162                 if ( CurrentHostageIndex >= MAX_HOSTAGES ) CurrentHostageIndex = 0;
163                 start++;
164                 if ( start > MAX_HOSTAGES ) break;
165         }
166
167         if (hostage_is_valid( CurrentHostageIndex ) )   {
168                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
169         } else {
170                 CurrentHostageIndex =-1;
171         }
172         
173         return CurrentHostageIndex;
174 }
175
176
177 int PlaceHostage()      {
178         int ctype,i;
179         vms_vector      cur_object_loc;
180
181         //update_due_to_new_segment();
182         compute_segment_center(&cur_object_loc, Cursegp);
183
184         ctype = -1;
185         for (i=0; i<Num_total_object_types; i++ )       {
186                 if (ObjType[i] == OL_HOSTAGE )  {
187                         ctype = i;      
188                         break;
189                 }
190         }
191
192         Assert( ctype != -1 );
193
194         if (place_object(Cursegp, &cur_object_loc, ctype )==0)  {
195                 Int3();         // Debug below
196                 i=place_object(Cursegp, &cur_object_loc, ctype );
197                 return 1;
198         }
199
200         if (hostage_object_is_valid( Cur_object_index ) )       {
201                 CurrentHostageIndex     = Objects[Cur_object_index].id;
202         } else {
203                 Int3();         // Get John! (Object should be valid)
204                 i=hostage_object_is_valid( Cur_object_index );  // For debugging only
205         }
206
207         return 0;
208 }
209
210 int CompressHostages()
211 {
212         hostage_compress_all();
213
214         return 0;
215 }
216
217 //@@int SelectPrevVclip()       {
218 //@@    if (!hostage_is_valid( CurrentHostageIndex ) )  
219 //@@            return 0;
220 //@@
221 //@@    if ( Hostages[CurrentHostageIndex].type == 0 )
222 //@@            Hostages[CurrentHostageIndex].type = N_hostage_types-1;
223 //@@    else
224 //@@            Hostages[CurrentHostageIndex].type--;
225 //@@    
226 //@@    if ( Hostages[CurrentHostageIndex].type >= N_hostage_types )
227 //@@            Hostages[CurrentHostageIndex].type = 0;
228 //@@    
229 //@@    return 1;
230 //@@}
231 //@@
232 //@@int SelectNextVclip()       {
233 //@@    if (!hostage_is_valid( CurrentHostageIndex ) )  
234 //@@            return 0;
235 //@@
236 //@@    Hostages[CurrentHostageIndex].type++;
237 //@@    if ( Hostages[CurrentHostageIndex].type >= N_hostage_types )
238 //@@            Hostages[CurrentHostageIndex].type = 0;
239 //@@
240 //@@    return 1;
241 //@@}
242
243 int SelectNextFace()
244 {
245         int start = Hostages[CurrentHostageIndex].vclip_num;
246         
247         if (!hostage_is_valid( CurrentHostageIndex ) )  
248                 return 0;
249
250         do {
251                 Hostages[CurrentHostageIndex].vclip_num++;
252                 if ( Hostages[CurrentHostageIndex].vclip_num >= MAX_HOSTAGES)
253                         Hostages[CurrentHostageIndex].vclip_num = 0;
254
255                 if (Hostages[CurrentHostageIndex].vclip_num == start)
256                         return 0;
257
258         } while (Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].num_frames == 0);
259
260         return 1;
261 }
262
263 int SelectPrevFace()
264 {
265         int start = Hostages[CurrentHostageIndex].vclip_num;
266         
267         if (!hostage_is_valid( CurrentHostageIndex ) )  
268                 return 0;
269
270         do {
271                 Hostages[CurrentHostageIndex].vclip_num--;
272                 if ( Hostages[CurrentHostageIndex].vclip_num < 0)
273                         Hostages[CurrentHostageIndex].vclip_num = MAX_HOSTAGES-1;
274
275                 if (Hostages[CurrentHostageIndex].vclip_num == start)
276                         return 0;
277
278         } while (Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].num_frames == 0);
279
280         return 1;
281 }
282
283 int PlayHostageSound()  {
284         int sound_num;
285
286         if (!hostage_is_valid( CurrentHostageIndex ) )  
287                 return 0;
288
289         sound_num = Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].sound_num;
290
291         if ( sound_num > -1 )   {
292                 digi_play_sample( sound_num, F1_0 );
293         }
294
295         return 1;       
296 }
297
298 //@@int find_next_hostage_sound()       {
299 //@@    int start=0,n;
300 //@@
301 //@@    n = Hostages[CurrentHostageIndex].sound_num;
302 //@@    do      {
303 //@@            n++;
304 //@@            if ( n < SOUND_HOSTAGE_VOICES ) n = SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS-1;
305 //@@            if ( n >= SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS ) n = SOUND_HOSTAGE_VOICES;
306 //@@            start++;
307 //@@            if ( start > MAX_HOSTAGE_SOUNDS ) break;
308 //@@    } while ( Sounds[n] == NULL );
309 //@@
310 //@@    if ( Sounds[n] == NULL )
311 //@@            Hostages[CurrentHostageIndex].sound_num = -1;
312 //@@    else    {
313 //@@            Hostages[CurrentHostageIndex].sound_num = n;
314 //@@            PlayHostageSound();
315 //@@    }
316 //@@    return 1;
317 //@@}
318 //@@
319 //@@int find_prev_hostage_sound()       {
320 //@@    int start=0,n;
321 //@@
322 //@@    n = Hostages[CurrentHostageIndex].sound_num;
323 //@@    do      {
324 //@@            n--;
325 //@@            if ( n < SOUND_HOSTAGE_VOICES ) n = SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS-1;
326 //@@            if ( n >= SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS ) n = SOUND_HOSTAGE_VOICES;
327 //@@            start++;
328 //@@            if ( start > MAX_HOSTAGE_SOUNDS ) break;
329 //@@    } while ( Sounds[n] == NULL );
330 //@@
331 //@@    if ( Sounds[n] == NULL )
332 //@@            Hostages[CurrentHostageIndex].sound_num = -1;
333 //@@    else    {
334 //@@            Hostages[CurrentHostageIndex].sound_num = n;
335 //@@            PlayHostageSound();
336 //@@    }
337 //@@    return 1;
338 //@@}
339
340
341 //-------------------------------------------------------------------------
342 // Called from the editor... does one instance of the hostage dialog box
343 //-------------------------------------------------------------------------
344 int do_hostage_dialog()
345 {
346         int i;
347
348         // Only open 1 instance of this window...
349         if ( MainWindow != NULL ) return 0;
350         
351         // Close other windows
352         close_all_windows();
353
354         CurrentHostageIndex = 0;
355         SelectClosestHostage();
356
357         // Open a window with a quit button
358         MainWindow = ui_open_window( TMAPBOX_X+10, TMAPBOX_Y+20, 765-TMAPBOX_X, 545-TMAPBOX_Y, WIN_DIALOG );
359         QuitButton = ui_add_gadget_button( MainWindow, 20, 222, 48, 40, "Done", NULL );
360
361         ui_wprintf_at( MainWindow, 10, 32,"&Message:" );
362         HostageText = ui_add_gadget_inputbox( MainWindow, 10, 50, HOSTAGE_MESSAGE_LEN, HOSTAGE_MESSAGE_LEN, HostageMessage );
363
364         // The little box the hostage vclip will play in.
365         HostageViewBox = ui_add_gadget_userbox( MainWindow,10, 90+10, 64, 64 );
366
367         // A bunch of buttons...
368         i = 90;
369 //@@    ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Type", SelectPrevVclip );
370 //@@    ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Type >>", SelectNextVclip );i += 29;         
371 //@@    ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Sound",  find_prev_hostage_sound );
372 //@@    ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Sound >>", find_next_hostage_sound );i += 29;                
373
374         ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Face", SelectPrevFace );
375         ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Face >>", SelectNextFace );i += 29;          
376         ui_add_gadget_button( MainWindow,155,i,140, 26, "Play sound", PlayHostageSound );i += 29;               
377         ui_add_gadget_button( MainWindow,155,i,140, 26, "Next Hostage", SelectNextHostage );    i += 29;                
378         ui_add_gadget_button( MainWindow,155,i,140, 26, "Prev Hostage", SelectPrevHostage ); i += 29;           
379         ui_add_gadget_button( MainWindow,155,i,140, 26, "Compress All", CompressHostages ); i += 29;            
380         ui_add_gadget_button( MainWindow,155,i,140, 26, "Delete", ObjectDelete );       i += 29;                
381         ui_add_gadget_button( MainWindow,155,i,140, 26, "Create New", PlaceHostage );   i += 29;                
382         
383         Time = timer_get_fixed_seconds();
384
385         LastHostageIndex = -2;          // Set to some dummy value so everything works ok on the first frame.
386         
387 //      if ( CurrentHostageIndex == -1 )
388 //              SelectNextHostage();
389
390         return 1;
391
392 }
393
394 void hostage_close_window()
395 {
396         if ( MainWindow!=NULL ) {
397                 ui_close_window( MainWindow );
398                 MainWindow = NULL;
399         }
400 }
401
402 void do_hostage_window()
403 {
404         fix DeltaTime, Temp;
405
406         if ( MainWindow == NULL ) return;
407
408         SelectClosestHostage();
409
410         //------------------------------------------------------------
411         // Call the ui code..
412         //------------------------------------------------------------
413         ui_button_any_drawn = 0;
414         ui_window_do_gadgets(MainWindow);
415
416         //------------------------------------------------------------
417         // If we change objects, we need to reset the ui code for all
418         // of the radio buttons that control the ai mode.  Also makes
419         // the current AI mode button be flagged as pressed down.
420         //------------------------------------------------------------
421         if (LastHostageIndex != CurrentHostageIndex )   {
422
423                 if ( CurrentHostageIndex > -1 ) 
424                         strcpy( HostageText->text, Hostages[CurrentHostageIndex].text );
425                 else
426                         strcpy(HostageText->text, " " );
427
428                 HostageText->position = strlen(HostageText->text);
429                 HostageText->oldposition = HostageText->position;
430                 HostageText->status=1;
431                 HostageText->first_time = 1;
432
433         }
434
435         //------------------------------------------------------------
436         // If any of the radio buttons that control the mode are set, then
437         // update the cooresponding AI state.
438         //------------------------------------------------------------
439         if ( CurrentHostageIndex > -1 ) 
440                 strcpy( Hostages[CurrentHostageIndex].text, HostageText->text );
441
442         //------------------------------------------------------------
443         // A simple frame time counter for spinning the objects...
444         //------------------------------------------------------------
445         Temp = timer_get_fixed_seconds();
446         DeltaTime = Temp - Time;
447         Time = Temp;
448
449         //------------------------------------------------------------
450         // Redraw the object in the little 64x64 box
451         //------------------------------------------------------------
452         if (CurrentHostageIndex > -1 )  {
453                 int vclip_num;
454                 
455                 vclip_num = Hostages[CurrentHostageIndex].vclip_num;
456
457                 Assert(vclip_num != -1);
458
459                 gr_set_current_canvas( HostageViewBox->canvas );
460
461                 if ( vclip_num > -1 )   {
462                         vclip_play( &Hostage_face_clip[vclip_num], DeltaTime ); 
463                 } else {
464                         gr_clear_canvas( CGREY );
465                 }
466         } else {
467                 // no hostage, so just blank out
468                 gr_set_current_canvas( HostageViewBox->canvas );
469                 gr_clear_canvas( CGREY );
470         }
471
472         //------------------------------------------------------------
473         // If anything changes in the ui system, redraw all the text that
474         // identifies this robot.
475         //------------------------------------------------------------
476         if (ui_button_any_drawn || (LastHostageIndex != CurrentHostageIndex) )  {
477                 if ( CurrentHostageIndex > -1 ) {
478                         ui_wprintf_at( MainWindow, 10, 15, "Hostage: %d   Object: %d", CurrentHostageIndex, Hostages[CurrentHostageIndex].objnum );
479                         //@@ui_wprintf_at( MainWindow, 10, 73, "Type: %d   Sound: %d   ", Hostages[CurrentHostageIndex].type, Hostages[CurrentHostageIndex].sound_num );
480                         ui_wprintf_at( MainWindow, 10, 73, "Face: %d   ", Hostages[CurrentHostageIndex].vclip_num);
481                 }       else {
482                         ui_wprintf_at( MainWindow, 10, 15, "Hostage: none " );
483                         //@@ui_wprintf_at( MainWindow, 10, 73, "Type:    Sound:       " );
484                         ui_wprintf_at( MainWindow, 10, 73, "Face:         " );
485                 }
486                 Update_flags |= UF_WORLD_CHANGED;
487         }
488
489         if ( QuitButton->pressed || (last_keypress==KEY_ESC))   {
490                 hostage_close_window();
491                 return;
492         }               
493
494         LastHostageIndex = CurrentHostageIndex;
495 }
496
497
498