File: interface.c

package info (click to toggle)
ale-clone 1.15pre16.3-1
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 2,432 kB
  • ctags: 3,217
  • sloc: ansic: 37,483; sh: 1,105; makefile: 343
file content (352 lines) | stat: -rw-r--r-- 8,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
**	A clone of a famous game.
*/
/**@name interface.c	-	The interface. */
/*
**	(c) Copyright 1998,1999 by Lutz Sammer
**
**	$Id: interface.c,v 1.17 1999/11/25 00:59:30 root Exp $
*/

//@{

/*----------------------------------------------------------------------------
--	Includes
----------------------------------------------------------------------------*/

#include <stdio.h>

#include "clone.h"
#include "video.h"
#include "sound_id.h"
#include "unitsound.h"
#include "unittype.h"
#include "player.h"
#include "unit.h"
#include "interface.h"
#include "ccl.h"
#include "tileset.h"
#include "map.h"
#include "minimap.h"
#include "network.h"

/*----------------------------------------------------------------------------
--	Declaration
----------------------------------------------------------------------------*/

local enum _key_state_ {
    KeyStateCommand,		/// keys -> commands
    KeyStateInput,		/// keys -> line editor
} KeyState;			/// current keyboard state

/*----------------------------------------------------------------------------
--	Variables
----------------------------------------------------------------------------*/

local char Input[50];		/// line input for messages/long commands
local int InputIndex;		/// current index into input
local char InputStatusLine[80];	/// Last input status line
global char GamePaused;		/// Current pause state

/*----------------------------------------------------------------------------
--	Functions
----------------------------------------------------------------------------*/

/**
**	Show input.
*/
local void ShowInput(void)
{
    sprintf(InputStatusLine,"MESSAGE:%s~_",Input);
    ClearStatusLine();
    SetStatusLine(InputStatusLine);
}

/**
**	Handle keys in command mode.
**
**	@param key	Key scancode.
**	@return		True, if key is handles; otherwise false.
*/
local int CommandKey(int key)
{
    switch( key ) {
	case '\r':
	    KeyState=KeyStateInput;
	    Input[0]='\0';
	    InputIndex=0;
	    ShowInput();
	    return 1;
	case '^':
	    UnSelectAll();
            break;
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            if( KeyModifiers&ModifierControl ) {
                //  dirty atoi version :)
                SetGroup(Selected,NumSelected,key-48);
            } else {
                SelectGroup(key-48);
            }
            UpdateBottomPanel();
            MustRedraw|=RedrawCursor|RedrawMap|RedrawPanels;
            break;
#if 0
    IfDebug(
	case '0':
	    ++ThisPlayer;
	    if( ThisPlayer==&Players[PlayerMax] ) {
		ThisPlayer=&Players[0];
	    }
	    MustRedraw=RedrawEverything;
	    break;

	case '1':
	    --ThisPlayer;
	    if( ThisPlayer<&Players[0] ) {
		ThisPlayer=&Players[PlayerMax-1];
	    }
	    MustRedraw=RedrawEverything;
	    break;
    );
#endif
        case KeyCodePause:
	case 'P':			// If pause-key didn't work
            if(GamePaused) {
                GamePaused=0;
                SetStatusLine("Game Resumed");
	    } else {
                GamePaused=1;
                SetStatusLine("Game Paused");
	    }
            break;

	case '+':
	    VideoSyncSpeed+=10;
	    InitVideoSync();
	    SetStatusLine("Faster");
	    break;

	case '-':
	    VideoSyncSpeed-=10;
	    InitVideoSync();
	    SetStatusLine("Slower");
	    break;

	case 'S':			// SMALL s is needed for panel
	    SaveAll();
	    break;

	case 'c':
	    if(	NumSelected==1 ) {
		MapCenter(Selected[0]->X,Selected[0]->Y);
	    }
	    break;

//	TAB toggles minimap.
//	FIXME: more...
	case '\t':
	    DebugLevel1("TAB\n");
	    MinimapWithTerrain^=1;
	    MustRedraw|=RedrawMinimap;
	    break;
	    // FIXME: shift+TAB

	case 'q':
	    Exit(0);
	case KeyCodeUp:
	    if( MapY ) {
		if( KeyModifiers&ModifierControl ) {
		    if( MapY<MapHeight/2 ) {
			MapY=0;
		    } else {
			MapY-=MapHeight/2;
		    }
		} else {
		    --MapY;
		}
		MustRedraw|=RedrawMaps|RedrawMinimapCursor;
	    }
	    break;
	case KeyCodeDown:
	    if( MapY<TheMap.Height-MapHeight ) {
		if( KeyModifiers&ModifierControl ) {
		    if( MapY<TheMap.Height-MapHeight-MapHeight/2 ) {
			MapY+=MapHeight/2;
		    } else {
			MapY=TheMap.Height-MapHeight;
		    }
		} else {
		    ++MapY;
		}
		MustRedraw|=RedrawMaps|RedrawMinimapCursor;
	    }
	    break;
	case KeyCodeLeft:
	    if( MapX ) {
		if( KeyModifiers&ModifierControl ) {
		    if( MapX<MapWidth/2 ) {
			MapX=0;
		    } else {
			MapX-=MapWidth/2;
		    }
		} else {
		    --MapX;
		}
		MustRedraw|=RedrawMaps|RedrawMinimapCursor;
	    }
	    break;
	case KeyCodeRight:
	    if( MapX<TheMap.Width-MapWidth ) {
		if( KeyModifiers&ModifierControl ) {
		    if( MapX<TheMap.Width-MapWidth-MapWidth/2 ) {
			MapX+=MapWidth/2;
		    } else {
			MapX=TheMap.Width-MapWidth;
		    }
		} else {
		    ++MapX;
		}
		MustRedraw|=RedrawMaps|RedrawMinimapCursor;
	    }
	    break;

	default:
	    DebugLevel3("Key %d\n",key);
	    return 0;
    }
    return 1;
}

/**
**	Handle keys in input mode.
**
**	@param key	Key scancode.
**	@return		True input finished.
*/
local int InputKey(int key)
{
    switch( key ) {
	case '\r':
#ifdef USE_CCL
	    if( Input[0]=='(' ) {
		CclCommand(Input);
	    } else {
#endif
                // Handle cheats
		// FIXME: disable cheats
                if (strcmp(Input, "hatchet") == 0)
                  {
                  SpeedChop = 52/2;
                  SetMessage( "Wow -- I got jigsaw!" );
                  } else
                if (strcmp(Input, "glittering prizes") == 0)
                  {
                  ThisPlayer->Gold += 12000;
                  ThisPlayer->Wood += 5000;
                  ThisPlayer->Oil  += 5000;
		  MustRedraw|=RedrawResources;
		  SetMessage( "!!! :)" );
                  } else
                if (strcmp(Input, "on screen") == 0)
                  {
                  RevealMap();
                  } else
                if (strcmp(Input, "fow on") == 0)
                  {
                  TheMap.NoFogOfWar = 0;
                  MapUpdateVisible();
                  SetMessage( "Fog Of War is now ON" );
                  } else
                if (strcmp(Input, "fow off") == 0)
                  {
                  TheMap.NoFogOfWar = 1;
                  MapUpdateVisible();
                  SetMessage( "Fog Of War is now OFF" );
                  } else
                if (strcmp(Input, "fast debug") == 0)
                  {
                  SpeedMine=10;         /// speed factor for mine gold
                  SpeedGold=10;         /// speed factor for getting gold
                  SpeedChop=10;         /// speed factor for chop
                  SpeedWood=10;         /// speed factor for getting wood
                  SpeedHaul=10;         /// speed factor for haul oil
                  SpeedOil=10;          /// speed factor for getting oil
                  SpeedBuild=10;        /// speed factor for building
                  SpeedTrain=10;        /// speed factor for training
                  SpeedUpgrade=10;      /// speed factor for upgrading
                  SpeedResearch=10;     /// speed factor for researching
                  SetMessage( "FAST DEBUG SPEED" );
                  } else
                if (strcmp(Input, "normal debug") == 0)
                  {
                  SpeedMine=1;         /// speed factor for mine gold
                  SpeedGold=1;         /// speed factor for getting gold
                  SpeedChop=1;         /// speed factor for chop
                  SpeedWood=1;         /// speed factor for getting wood
                  SpeedHaul=1;         /// speed factor for haul oil
                  SpeedOil=1;          /// speed factor for getting oil
                  SpeedBuild=1;        /// speed factor for building
                  SpeedTrain=1;        /// speed factor for training
                  SpeedUpgrade=1;      /// speed factor for upgrading
                  SpeedResearch=1;     /// speed factor for researching
                  SetMessage( "NORMAL DEBUG SPEED" );
                  }

		// FIXME: only to selected players
		NetworkChatMessage(Input);
#ifdef USE_CCL
	    }
#endif
	case '\e':
	    ClearStatusLine();
	    KeyState=KeyStateCommand;
	    return 1;
	case '\b':
	    DebugLevel3("Key <-\n");
	    if( InputIndex ) {
		Input[--InputIndex]='\0';
		ShowInput();
	    }
	    return 1;
	default:
	    if( key>=' ' && key<=256 ) {
		if( InputIndex<sizeof(Input) ) {
		    DebugLevel3("Key %c\n",key);
		    Input[InputIndex++]=key;
		    Input[InputIndex]='\0';
		    ShowInput();
		}
		return 1;
	    }
	    break;
    }
    return 0;
}

/**
**	Handle key down.
**
**	@param key	Key scancode.
**	@return		True, if key is handles; otherwise false.
*/
global int HandleKeyDown(int key)
{
    switch( KeyState ) {
	case KeyStateCommand:
	    return CommandKey(key);
	case KeyStateInput:
	    return InputKey(key);
    }
    return 0;
}

//@}