-
Notifications
You must be signed in to change notification settings - Fork 1
/
etc.c
163 lines (140 loc) · 3.02 KB
/
etc.c
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
#include "midway.h"
int angle(int dr, int dc)
{
int add = 0, sub = 0;
if (dr <= 0 && dc < 0)
add = 180;
if (dr > 0 && dc < 0)
sub = 360;
if (dr < 0 && dc >= 0)
sub = 180;
if (abs(dc) > abs(dr) * 2.4)
return(sub ? sub - 90 : 90 + add);
else if (abs(dr) > abs(dc) * 2.4)
return(sub == 360 ? 0 : sub + add);
else
return(sub ? sub - 45 : 45 + add);
}
int planesize(struct squadron *planes)
{
register int n = 0;
for (; planes; planes = planes -> s_next)
n++;
return(n);
}
void wreadstr(WINDOW *win, char *str)
{
echo();
wgetstr(win, str);
noecho();
}
void inform(char *fmt, int jerry)
{
scrollrow++;
if (scrollrow > 11) scrollrow = 0;
wmove(notes, scrollrow, 0);
wclrtoeol(notes);
if (jerry) {
wprintw(notes, fmt, shiplist[jerry -1].row, shiplist[jerry -1].col);
} else
wprintw(notes, fmt);
wrefresh(notes);
if (automatic == -2)
automatic = 0;
}
char *daytime(int time, char buf[])
{
float minutes;
int hours, mins, over = ZERODAY;
minutes = time * 2.5 + ZEROMIN;
hours = minutes/60 + ZEROHOUR;
mins = (int) minutes % 60;
while (hours >= 24) {
hours -= 24;
over++;
}
sprintf(buf, "June %d %02d:%02d:00", over, hours, mins);
return(buf);
}
/* to see if a ship hit by a shell */
/* shiphit(type, dir, hit.r - ship.r, hit.c - ship.c) != ' ' */
char shiphit(int type, int dir, int row, int col)
{
if (col >= -2 && col <= 2 && row >= -2 && row <= 2)
return(shapes[type - CV][dir][row+2][col+2]);
else
return(' ');
}
/* sees if a plane is hit */
int hit(int ran, int ar, int ac, int br, int bc, int dir, int offset)
{
int dr, dc;
if (range(ar,ac,br,bc) < 5) {
vshape(dir, offset, &dr, &dc);
return(ar - br - dr <= ran && ar - br - dr >= -ran && ac - bc - dc <= ran && ac - bc - dc >= -ran);
} else
return(0);
}
void vshape(int dir, int offset, int *dr, int *dc)
{
switch(dir/45) {
case 0:
*dr = offset < 0 ? -offset : offset;
*dc = offset;
break;
case 1:
*dr = offset < 0 ? 0 : offset;
*dc = offset > 0 ? 0 : offset;
break;
case 2:
*dr = offset;
*dc = offset > 0 ? -offset : offset;
break;
case 3:
*dr = offset < 0 ? offset : 0;
*dc = offset < 0 ? 0 : -offset;
break;
case 4:
*dr = offset < 0 ? offset : -offset;
*dc = -offset;
break;
case 5:
*dr = offset < 0 ? 0 : -offset;
*dc = offset < 0 ? -offset : 0;
break;
case 6:
*dr = -offset;
*dc = offset < 0 ? -offset : offset;
break;
case 7:
*dr = offset < 0 ? -offset : 0;
*dc = offset < 0 ? 0 : offset;
break;
}
}
void ditch(struct squadron *planes, struct squadron **head)
{
if (planes -> type == DEL)
return;
planes -> type = DEL;
if (planes -> previous)
planes -> previous -> s_next = planes -> s_next;
else {
*head = planes -> s_next;
}
if (planes -> s_next)
planes -> s_next -> previous = planes -> previous;
/*
* XXX results in double free crashes
* XXX ought to mark the freed object to check later
* free(planes);
*/
}
int range(int ar, int ac, int br, int bc)
{
ar -= br;
ac -= bc;
ar = abs(ar);
ac = abs(ac);
return(max(ar,ac));
}