File: clone.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 (602 lines) | stat: -rw-r--r-- 14,189 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
**	A clone of a famous game.
*/
/**@name clone.c	-	The main file. */
/*
**	(c) Copyright 1998,1999 by Lutz Sammer
**
**	$Id: clone.c,v 1.76 1999/12/17 15:30:45 root Exp root $
*/

//@{

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#ifdef __CYGWIN__
#include <getopt.h>
#endif
#ifdef __MINGW32__
#include <SDL/SDL.h>
extern int opterr;
extern int optind;
extern int optopt;
extern char *optarg;

int getopt(int argc, char *const*argv, const char *opt);
#endif

#include "clone.h"

#include "video.h"
#include "image.h"
#include "tileset.h"
#include "map.h"
#include "minimap.h"
#include "sound_id.h"
#include "unitsound.h"
#include "icons.h"
#include "button.h"
#include "unittype.h"
#include "player.h"
#include "unit.h"
#include "missile.h"
#include "actions.h"
#include "construct.h"
#include "ai.h"
#include "ccl.h"
#include "cursor.h"
#include "upgrade.h"
#include "font.h"
#include "interface.h"
#include "sound_server.h"
#include "sound.h"
#include "network.h"

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

#ifdef DEBUG
extern unsigned AllocatedGraphicMemory;
extern unsigned CompressedGraphicMemory;
#endif

/*----------------------------------------------------------------------------
--	Speedups
----------------------------------------------------------------------------*/

global int SpeedMine=SPEED_MINE;	/// speed factor for mine gold
global int SpeedGold=SPEED_GOLD;	/// speed factor for getting gold
global int SpeedChop=SPEED_CHOP;	/// speed factor for chop
global int SpeedWood=SPEED_WOOD;	/// speed factor for getting wood
global int SpeedHaul=SPEED_HAUL;	/// speed factor for haul oil
global int SpeedOil=SPEED_OIL;		/// speed factor for getting oil
global int SpeedBuild=SPEED_BUILD;	/// speed factor for building
global int SpeedTrain=SPEED_TRAIN;	/// speed factor for training
global int SpeedUpgrade=SPEED_UPGRADE;	/// speed factor for upgrading
global int SpeedResearch=SPEED_RESEARCH;/// speed factor for researching

/*============================================================================
==	DISPLAY
============================================================================*/

// FIXME: move to video header file
global int VideoWidth;			/// window width in pixels
global int VideoHeight;			/// window height in pixels

global int FrameCounter;		/// current frame number
global int SlowFrameCounter;		/// profile, frames out of sync

// FIXME: not the correct place
global enum MustRedraw_e MustRedraw=RedrawEverything;	/// redraw flags

/*----------------------------------------------------------------------------
--	SAVE/LOAD
----------------------------------------------------------------------------*/

/**
**	Test function for the later save functions.
*/
global void SaveAll(void)
{
    FILE* file;
    time_t now;
    const char* name;

    name="save_file_of_clone.ccl";
    file=fopen(name,"wb");
    if( !file ) {
	fprintf(stderr,"Can't save to `%s'\n",name);
	return;
    }
    fprintf(file,";;; -----------------------------------------\n");
    fprintf(file,";;; Save file generated by clone version " VERSION "\n");
    time(&now);
    fprintf(file,";;;\tDate: %s",ctime(&now));
    fprintf(file,";;; -----------------------------------------\n\n\n");

    SaveUnitTypes(file);
    SaveUnits(file);
    SaveMap(file);

    fclose(file);
}

/*----------------------------------------------------------------------------
--	BUTTONS
----------------------------------------------------------------------------*/

/**
**	Defines position and size of the different buttons.
*/
global Button Buttons[MaxButtons] = {
// Menu button
    {  24,  2,168,16 },
// 9 Character portraits
    {   6,166, 46,38 },
    {  62,166, 46,38 },
    { 118,166, 46,38 },
    {   6,218, 46,38 },
    {  62,218, 46,38 },
    { 118,218, 46,38 },
    {   6,270, 46,38 },
    {  62,270, 46,38 },
    { 118,270, 46,38 },
// 9 Buttons interface
    {   6,337, 46,38 },
    {  62,337, 46,38 },
    { 118,337, 46,38 },
    {   6,384, 46,38 },
    {  62,384, 46,38 },
    { 118,384, 46,38 },
    {   6,431, 46,38 },
    {  62,431, 46,38 },
    { 118,431, 46,38 },
};

/**
**	Defines position and size of the training queue buttons.
*/
global Button Buttons2[6] = {
    {   6,216, 46,38 },
    {  62,216, 46,38 },
    { 118,216, 46,38 },
    {   6,263, 46,38 },
    {  62,263, 46,38 },
    { 118,263, 46,38 }
};

/*----------------------------------------------------------------------------
--	INIT
----------------------------------------------------------------------------*/

/**
**	Initialise.
**
**		Load all graphics, sounds.
*/
global void CloneInit(void)
{
    char* PalettePath;
    DebugLevel3("Terrain %d\n",TheMap.Terrain);

    // FIXME: must use palette from tileset!!
    // FIXME: this must be extendable!!

    switch( TheMap.Terrain ) {
	case TilesetSummer:
            PalettePath = strdcat(CloneLibPath, "/summer.rgb");
	    break;
	case TilesetWinter:
            PalettePath = strdcat(CloneLibPath, "/winter.rgb");
	    break;
	case TilesetWasteland:
            PalettePath = strdcat(CloneLibPath, "/wasteland.rgb");
	    break;
	case TilesetSwamp:
            PalettePath = strdcat(CloneLibPath, "/swamp.rgb");
	    break;
	default:
	    DebugLevel2("Unknown Terrain %d\n",TheMap.Terrain);
            PalettePath = strdcat(CloneLibPath, "/summer.rgb");
	    break;
    }
    LoadRGB(GlobalPalette, PalettePath);
    // GlobalPalette=(typeof(GlobalPalette))PalettePNG);

    VideoCreatePalette(GlobalPalette);

    //
    //	Graphic part
    //
    LoadIcons();
    LoadImages();
    LoadCursors();
    LoadFonts();
    LoadTileset();
    InitUnitButtons();
    LoadUnitSprites();
    LoadMissileSprites();
    LoadConstructions();
    LoadDecorations();

    IfDebug(
	DebugLevel0("Graphics uses %d bytes (%d KB, %d MB)\n"
		,AllocatedGraphicMemory
		,AllocatedGraphicMemory/1024
		,AllocatedGraphicMemory/1024/1024);
	DebugLevel0("Compressed graphics uses %d bytes (%d KB, %d MB)\n"
		,CompressedGraphicMemory
		,CompressedGraphicMemory/1024
		,CompressedGraphicMemory/1024/1024);
    );

    // FIXME: johns: did this belong into LoadMap!
    CreateMinimap();			// create minimap for pud
    InitMapFogOfWar();			// build tables for fog of war
    PreprocessMap();			// Adjust map for use
    MapColorCycle();			// Setup color cycle

    //
    //	Sound part
    //
    //FIXME: check if everything is really loaded
    LoadUnitSounds();
    MapUnitSounds();

#ifdef WITH_SOUND
    IfDebug(
	DebugLevel0("Sounds uses %d bytes (%d KB, %d MB)\n"
		,AllocatedSoundMemory
		,AllocatedSoundMemory/1024
		,AllocatedSoundMemory/1024/1024);
	DebugLevel0("Compressed sounds uses %d bytes (%d KB, %d MB)\n"
		,CompressedSoundMemory
		,CompressedSoundMemory/1024
		,CompressedSoundMemory/1024/1024);
    );
#endif

    //
    //	Network part
    //
    InitNetwork();

    //
    //  Init units' groups
    //
    InitGroups();

    //
    //	Init players?
    //
    DebugPlayers();
    PlayersInitAi();

    //
    //  Upgrades
    //
    UpgradesInit();

    //
    //  Dependencies
    //
    InitDeps();

    //
    //  Buttons (botpanel)
    //
    InitButtons();

    DebugLevel3("Center map for %d at %d,%d\n"
	    ,ThisPlayer-Players
	    ,ThisPlayer->X,ThisPlayer->Y);
    MapCenter(ThisPlayer->X,ThisPlayer->Y);
}

/*----------------------------------------------------------------------------
--	Random
----------------------------------------------------------------------------*/

/**
**	Syncron rand.
*/
global int SyncRand(void)
{
    static unsigned rnd = 0x87654321;
    int val;

    val=rnd>>16;

    rnd=rnd*(0x12345678 * 4 + 1) + 1;

    return val;
}

/*----------------------------------------------------------------------------
--	Utility
----------------------------------------------------------------------------*/

/**
**	String duplicate/concatenate (two arguments)
*/
global char *strdcat(const char *l, const char *r) {
    char *res = malloc(strlen(l) + strlen(r) + 1);
    if (res) {
        strcpy(res, l);
        strcat(res, r);
    }
    return res;
}

/**
**	String duplicate/concatenate (three arguments)
*/
global char* strdcat3(const char* l, const char* m, const char* r) {
    char* res = malloc(strlen(l) + strlen(m) + strlen(r) + 1);
    if (res) {
        strcpy(res, l);
	strcat(res, m);
        strcat(res, r);
    }
    return res;
}

/*============================================================================
==	MAIN
============================================================================*/

global char* TitleScreen;		/// Titlescreen show by startup
global char* CloneLibPath;		/// Path for data
global int FlagRevealMap;		/// Flag must reveal the map

	///	Filename for the map to load
local const char* MapName;

/**
**	Main, called from guile main.
**
**	@param	argc	Number of arguments.
**	@param	argv	Vector of arguments.
*/
global int main1(int argc __attribute__ ((unused)),char** argv __attribute__ ((unused)))
{
    InitVideo();			// setup video display
#ifdef WITH_SOUND
    if( InitSound() ) {			// setup sound card
	SoundOff=1;
	SoundFildes=-1;
    }
#endif
    //
    //	Show title screen.
    //
    if( TitleScreen ) {
	DisplayPicture(TitleScreen);
    }
    Invalidate();

    //
    //  Units Memory Management
    //
    InitUnitsMemory();
    UpdateStats();
    InitUnitTypes();

    //
    //	Load the map.
    //
    LoadMap(MapName,&TheMap);
    if( FlagRevealMap ) {
	RevealMap();
    }

    CloneInit();
    if (SoundFildes!=-1) {
	// must be done after sounds are loaded
	InitSoundClient();
    }

    SetStatusLine("Clone V" VERSION ", (c) 1998,1999 by ALE.");
    SetMessage("Do it! Do it now!");
    // FIXME: Add this again:
    if( ThisPlayer->Race==PlayerRaceHuman ) {
	DefaultReverseText=0;
    //	PlaySound(SoundBasicHumanVoicesSelected1);
    }
    if( ThisPlayer->Race==PlayerRaceOrc ) {
	DefaultReverseText=1;
    //	PlaySound(SoundBasicOrcVoicesSelected1);
    }

    printf("Clone V" VERSION ", (c) 1998,1999 by ALE\nwritten by Lutz Sammer, Fabrice Rossi, Vladi Shabanski, Patrice Fortier,\nJon Gabrielson and others.\nCompile options ");
#ifdef USE_CCL
    printf("CCL ");
#endif
#ifdef GUILE_GTK
    printf("GTK ");
#endif
#ifdef USE_THREAD
    printf("THREAD ");
#endif
#ifdef DEBUG
    printf("DEBUG ");
#endif
#ifdef USE_ZLIB
    printf("ZLIB ");
#endif
#ifdef USE_BZ2LIB
    printf("BZ2LIB ");
#endif
#ifdef USE_GLIB
    printf("GLIB ");
#endif
#ifdef USE_SDL
    printf("SDL ");
#endif
#ifdef USE_X11
    printf("X11 ");
#endif
    printf("\n\nDISCLAIMER:\n\
This software is provided as-is.  The author can not be held liable for any\n\
damage that might arise from the use of this software.\n\
Use it at your own risk.\n");

    CloneMainLoop();

    return 0;
}

/**
**	Exit clone.
**
**	Called from 'q'.
*/
global volatile void Exit(int err)
{
    IfDebug(
	extern unsigned PfCounterFail;
	extern unsigned PfCounterOk;
	extern unsigned PfCounterDepth;
	extern unsigned PfCounterNotReachable;
    );

    NetworkQuit();
    DebugLevel0("Frames %d, Slow frames %d = %d%%\n"
	,FrameCounter,SlowFrameCounter
	,(SlowFrameCounter*100)/FrameCounter);
    IfDebug(
	UnitCacheStatistic();
	DebugLevel0("Path: Error: %u(%u) OK: %u Depth: %u\n"
		,PfCounterFail,PfCounterNotReachable
		,PfCounterOk,PfCounterDepth);
    );
    DebugLevel0("Thanks for playing clone.\n");
    exit(err);
}

/**
**	Display the usage.
*/
local void Usage(void)
{
    printf("Clone V" VERSION ", (c) 1998,1999 by ALE\nwritten by Lutz Sammer, Fabrice Rossi, Vladi Shabanski, Patrice Fortier,\nJon Gabrielson and others.\n\n\
Usage: clone [OPTIONS] [map.pud|map.pud.gz|map.cm|map.cm.gz]\n\
\t-d datapath\tpath to clone data\n\
\t-c file.ccl\tccl start file\n\
\t-f factor\tComputer units cost factor\n\
\t-h\t\tHelp shows this page\n\
\t-p players\tNumber of players\n\
\t-s sleep\tNumber of frames for the AI to sleep before they starts\n\
\t-f factor\tComputer units built time factor\n\
\t-F\tFullscreen videomode\n\
\t-W\tWindowed videomode\n\
");
}

/**
**	The main program: initialise, parse options and arguments.
**
**	@param	argc	Number of arguments.
**	@param	argv	Vector of arguments.
*/
#ifdef __MINGW32__
global int mymain(int argc,char** argv)
#else
global int main(int argc,char** argv)
#endif
{
    //
    //	Setup some defaults.
    //
    CloneLibPath=CLONE_LIB_PATH;
    TitleScreen=strdup("graphic/title.png");
    // CloneLibPath prefixed after it's been decided properly for the next two
    MapName="default.pud";	// .gz automatic appended, 
#ifdef USE_CCL
    CclStartFile="ccl/clone.ccl";
#endif

    // FIXME: Parse options before ccl oder after?

    //
    //	Parse commandline
    //
    for( ;; ) {
	switch( getopt(argc,argv,"c:d:f:hp:s:t:FW?") ) {
#ifdef USE_CCL
	    case 'c':
		CclStartFile=optarg;
		continue;
#endif
            case 'd':
                CloneLibPath=optarg;
                continue;
	    case 'f':
		AiCostFactor=atoi(optarg);
		continue;
	    case 'p':
		NetPlayers=atoi(optarg);
		continue;
	    case 's':
		AiSleep=atoi(optarg);
		continue;
	    case 't':
		AiTimeFactor=atoi(optarg);
		continue;

	    case 'F':
		VideoFullScreen=1;
		continue;
	    case 'W':
		VideoFullScreen=0;
		continue;

	    case -1:
		break;
	    case '?':
	    case 'h':
	    default:
		Usage();
		exit(-1);
	}
	break;
    }

    if( argc-optind>1 ) {
	fprintf(stderr,"too many files\n");
	Usage();
	exit(-1);
    }

    if( argc-optind ) {
	MapName=argv[optind];
	--argc;
    }

    if (MapName[0] != '/' && MapName[0] != '.') {
        MapName = strdcat3(CloneLibPath, "/", MapName);
    }
#ifdef USE_CCL
    if (CclStartFile[0] != '/' && CclStartFile[0] != '.') {
        CclStartFile = strdcat3(CloneLibPath, "/", CclStartFile);
    }
#endif

#ifdef USE_CCL
    CclInit();				// load configurations!
#endif

    main1(argc,argv);			// CclInit may not return!

    return 0;
}

//@}