File: cmd_space.c

package info (click to toggle)
cthugha 1.4-2
  • links: PTS
  • area: non-free
  • in suites: sarge, woody
  • size: 2,840 kB
  • ctags: 1,889
  • sloc: cpp: 14,904; sh: 2,717; ansic: 1,559; makefile: 224
file content (133 lines) | stat: -rw-r--r-- 3,232 bytes parent folder | download | duplicates (2)
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
/* 
 * cmd_space.c
 *
 * Generate a translation table -> write table to stdout.
 * based on mkspace. 
 */

/* 
   mkspace.c
	
   Changes by Harald Deischinger to compile with cthugha-L under Linux.
   Changes:
       int -> short
       random

       short -> tint
       unsigned tint -> utint

       const buff

       removed enum bool
*/

/*
// Create a "space flight" translation table for Cthugha.
// The effect is that of a fast forward motion. A small region at the center
// of the screen is Randomized to make the screen less empty and more
// tinteresting. A backward flight option is also available.
//
// Compiled using Borland C 3.1.
//
// By Ofer Faigon, Sep 1994.
//     ofer@brm.co.il
//     (or oferf@itexjct.jct.ac.il)
//
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "../src/cthugha.h"
#include "../src/cth_buffer.h"
#include "../src/imath.h"

int BUFF_WIDTH;
int BUFF_HEIGHT;

#define Y_CENTER  (BUFF_HEIGHT / 2)
#define X_CENTER  (BUFF_WIDTH / 2)

#define DEFAULT_SPEED 100
#define DEFAULT_RAND   70

int main (int argc, char **argv) {
    int    x, y, dx, dy, map_x, map_y;
    int    speed, Randomness;
    int   reverse;
    unsigned int map;

    if (argc < 3  ||  argc > 6) {
	char  *p = strrchr(argv[0], '\\');
	p = p ? p+1 : argv[0];
	fprintf(stderr,"\"Space flight\" table generator for CTHUGHA.\n");
	fprintf(stderr,"The generated table creates an effect of flying forward through a\n");
	fprintf(stderr,"3-D space.\n");
	fprintf(stderr,"\n");
	fprintf(stderr,"Usage:  %s WIDTH HEIGHT [speed [Randomness ['r']]]\n", p);
	fprintf(stderr,"  Speed should be 30..300 (default %d)\n", DEFAULT_SPEED);
	fprintf(stderr,"  Randomness should be 0..100 (default %d)\n", DEFAULT_RAND);
	fprintf(stderr,"  Adding a fourth argument will create a backward movement.\n");
	return 1;
    }

    BUFF_WIDTH = atoi(argv[1]);
    BUFF_HEIGHT = atoi(argv[2]);
    speed = (argc > 3) ? atoi(argv[3]) : DEFAULT_SPEED;
    Randomness = (argc > 4) ? atoi(argv[4]) : DEFAULT_RAND;
    reverse = (argc > 5);

    speed = min(max(speed,30),300);
    Randomness = min(max(Randomness,0),100);

/*    printf ("Generating table %s with %sspeed=%d and Randomness=%d\n",
	     fileName, reverse ? "reverse " : "", speed, Randomness); */

    for (y = 0; y < BUFF_HEIGHT; y++) {

	for (x = 0; x < BUFF_WIDTH; x++) {
	    dx = x - X_CENTER;
	    dy = y - Y_CENTER;

	    if (! reverse  &&  abs(dx) < 30  &&  abs(dy) < 20  &&
	    Random(abs(dx)+abs(dy)) < 4) {

		map_x = Random(BUFF_WIDTH);
		map_y = Random(BUFF_HEIGHT);
	    } else {
		int  speedFactor;
		long sp;

		if (Randomness == 0)
		    sp = speed;
		else {
		    speedFactor = Random(Randomness + 1) - Randomness / 3;
		    sp = speed * (100L + speedFactor) / 100L;
		}

		if (reverse)
		    sp = (-sp);

		map_x = (int)(x - (dx * sp) / 700);
		map_y = (int)(y - (dy * sp) / 700);
	    }

	    if (map_y >= BUFF_HEIGHT || map_y < 0  ||
		map_x >= BUFF_WIDTH  || map_x < 0 ) {
		map_x = 0;
		map_y = 0;
	    }

	    map = map_y * BUFF_WIDTH + map_x;

	    if (fwrite (&map, sizeof(int), 1, stdout) != 1) {
		fprintf(stderr,"\n*** Error while writing to output file (disk full?)\n");
		return 3;
	    }
	}
    }

    return 0;
}