]> icculus.org git repositories - btb/d2x.git/blob - main/editor/macro.c
use physfs instead of non-existing cflib.h
[btb/d2x.git] / main / editor / macro.c
1 /* $Id: macro.c,v 1.6 2005-01-25 19:31:56 schaffner 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 recording/playing/saving macros
18  *
19  */
20
21 #ifdef RCS
22 static char rcsid[] = "$Id: macro.c,v 1.6 2005-01-25 19:31:56 schaffner Exp $";
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #include "conf.h"
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <math.h>
33 #include <string.h>
34
35 #include "inferno.h"
36 #include "segment.h"
37 #include "editor.h"
38
39 #include "gr.h"
40 #include "ui.h"
41 #include "key.h"
42 #include "fix.h"
43 #include "mono.h"
44 #include "3d.h"
45 #include "mouse.h"
46 #include "bm.h"
47 #include "error.h"
48 #include "medlisp.h"
49
50 #include "kdefs.h"
51
52 #include "u_mem.h"
53
54 #define MAX_NUM_EVENTS 10000
55
56 UI_EVENT * RecordBuffer;
57
58 int MacroNumEvents = 0;
59 int MacroStatus = 0;
60
61 static char filename[128] = "*.MIN";
62
63 int MacroRecordAll()
64 {
65         if ( MacroStatus== UI_STATUS_NORMAL )
66         {
67                 if (RecordBuffer) free( RecordBuffer );
68                 MALLOC( RecordBuffer, UI_EVENT, MAX_NUM_EVENTS );
69                 ui_record_events( MAX_NUM_EVENTS, RecordBuffer, UI_RECORD_MOUSE | UI_RECORD_KEYS );
70                 MacroStatus = UI_STATUS_RECORDING;
71         }
72         return 1;
73 }
74
75 int MacroRecordKeys()
76 {
77         if ( MacroStatus== UI_STATUS_NORMAL )
78         {
79                 if (RecordBuffer) free( RecordBuffer );
80                 MALLOC( RecordBuffer, UI_EVENT, MAX_NUM_EVENTS );
81                 ui_record_events( MAX_NUM_EVENTS, RecordBuffer, UI_RECORD_KEYS );
82                 MacroStatus = UI_STATUS_RECORDING;
83         }
84         return 1;
85 }
86
87 int MacroPlayNormal()
88 {
89         if (MacroStatus== UI_STATUS_NORMAL && MacroNumEvents > 0 && RecordBuffer )
90         {
91                 ui_set_playback_speed( 1 );
92                 ui_play_events_realtime(MacroNumEvents, RecordBuffer);
93                 MacroStatus = UI_STATUS_PLAYING;
94         }
95         return 1;
96 }
97
98 int MacroPlayFast()
99 {
100         if (MacroStatus== UI_STATUS_NORMAL && MacroNumEvents > 0 && RecordBuffer )
101         {
102                 ui_mouse_hide();
103                 ui_play_events_fast(MacroNumEvents, RecordBuffer);
104                 MacroStatus = UI_STATUS_FASTPLAY;
105         }
106         return 1;
107 }
108
109 int MacroSave()
110 {
111
112         if (MacroNumEvents < 1 )
113         {
114                 MessageBox( -2, -2, 1, "No macro has been defined to save!", "Oops" );
115                 return 1;
116         }
117
118         if (ui_get_filename( filename, "*.MAC", "SAVE MACRO" ))
119         {
120                 PHYSFS_file *fp = PHYSFS_openWrite(filename);
121
122                 if (!fp) return 0;
123                 RecordBuffer[0].type = 7;
124                 RecordBuffer[0].frame = 0;
125                 RecordBuffer[0].data = MacroNumEvents;
126                 PHYSFS_write(fp, RecordBuffer, sizeof(UI_EVENT), MacroNumEvents);
127                 PHYSFS_close(fp);
128         }
129         return 1;
130 }
131
132 int MacroLoad()
133 {
134         if (ui_get_filename( filename, "*.MAC", "LOAD MACRO" ))
135         {
136                 PHYSFS_file *fp = PHYSFS_openRead(filename);
137                 int length;
138
139                 if (!fp)
140                         return 0;
141                 if (RecordBuffer)
142                         free( RecordBuffer );
143                 length = PHYSFS_fileLength(fp);
144                 RecordBuffer = d_malloc(length);
145                 if (!RecordBuffer)
146                         return 0;
147
148                 PHYSFS_read(fp, RecordBuffer, length, 1);
149                 PHYSFS_close(fp);
150                 MacroNumEvents = RecordBuffer[0].data;
151         }
152         return 1;
153 }
154
155 void macro_free_buffer()
156 {
157         if ( RecordBuffer ) 
158                 free(RecordBuffer);
159 }
160
161 int MacroMenu()
162 {
163         int x;
164         char * MenuItems[] = { "Play fast",
165                                            "Play normal",
166                                            "Record all",
167                                            "Record keys",
168                                            "Save macro",
169                                            "Load macro" };
170
171         x = MenuX( -1, -1, 6, MenuItems );
172
173         switch( x )
174         {
175         case 1:
176                 MacroPlayFast();
177                 break;
178         case 2:
179                 MacroPlayNormal();
180                 break;
181         case 3:
182                 MacroRecordAll();
183                 break;
184         case 4:
185                 MacroRecordKeys();
186                 break;
187         case 5:     // Save
188                 MacroSave();
189                 break;
190         case 6:     // Load
191                 MacroLoad();
192                 break;
193         }
194         return 1;
195 }
196