|
|
1.1 root 1: #ifndef lint
2: static char *sccsid = "@(#)screen.c 1.2 83/05/20";
3: #endif
4:
5: #include "externs.h"
6: #include <sys/types.h>
7: #include <sys/stat.h>
8:
9: extern int choke();
10:
11: readpos()
12: {
13: register int n;
14: register postype *p = pos;
15: register ships *s = scene[game].ship;
16:
17: for (n = scene[game].vessels; n > 0; n--, p++, s++){
18: p -> row = s -> shiprow;
19: p -> col = s -> shipcol;
20: p -> dir = s -> shipdir;
21: if (s -> file -> explode == 2 || s -> file -> sink == 2)
22: p -> dir = 0;
23: }
24: }
25:
26: makesignal(fmt, shipnum, owner)
27: int shipnum, owner;
28: char *fmt;
29: {
30: int junk;
31: char message[60];
32:
33: sprintf(message, fmt, scene[game].ship[shipnum].shipname, colours(shipnum), sterncolor(shipnum, &junk, &junk));
34: Write(FILES + owner, 1, 164, message);
35: }
36:
37:
38: #define PI 3.1415926535
39:
40: float contable[8] = {1.5708, 0.7854, 0.0, -0.7854, -1.5708, -2.3562, -PI, 2.3562};
41:
42: angle(dr, dc)
43: register dr, dc;
44: {
45: register i;
46:
47: if (dc >= 0 && dr > 0)
48: i = 0;
49: else if (dr <= 0 && dc > 0)
50: i = 2;
51: else if (dc <= 0 && dr < 0)
52: i = 4;
53: else
54: i = 6;
55: dr = abs(dr);
56: dc = abs(dc);
57: if ((i == 0 || i == 4) && dc * 2.4 > dr) {
58: i++;
59: if (dc > dr * 2.4)
60: i++;
61: } else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
62: i++;
63: if (dr > dc * 2.4)
64: i++;
65: }
66: return i % 8 + 1;
67: }
68:
69: char gunsbear(fromship, toship) /* checks for target bow or stern */
70: {
71: int dr, dc, i;
72: register ang;
73: register postype *from = &pos[fromship];
74: register postype *to = &pos[toship];
75:
76: dr = from->row - to->row;
77: dc = to->col - from->col;
78: for (i = 2; i; i--) {
79: if ((ang = angle(dr, dc) - from->dir + 1) < 1)
80: ang += 8;
81: if (ang >= 2 && ang <= 4)
82: return 'r';
83: if (ang >= 6 && ang <= 7)
84: return 'l';
85: drdc(&dr, &dc, to->dir);
86: }
87: return 0;
88: }
89:
90: portside(fromship, onship, quick)
91: register fromship, onship;
92: int quick; /* returns true if fromship is */
93: { /* shooting at onship's starboard side */
94: register ang;
95: int dr, dc;
96:
97: dr = pos[fromship].row - pos[onship].row;
98: dc = pos[onship].col - pos[fromship].col;
99: if (quick == -1)
100: drdc(&dr, &dc, pos[onship].dir);
101: ang = angle(dr, dc);
102: if (quick != 0)
103: return ang;
104: ang = (ang + 4 - pos[onship].dir - 1) % 8 + 1;
105: return ang < 5;
106: }
107:
108: int tantable[40] = {0,100,197,291,381,464,540,610,675,733,785,833,876,915,951,983,1012,1039,1064,1086,1107,1126,1144,1161,1176,1190,1204,1216,1227,1239,1249,1259,1268,1277,1285,1293,1300,1307,1313,1470};
109:
110: double arctan(y,x)
111: int y,x;
112: {
113: int sx, sy;
114: register int index;
115:
116: sy = y < 0 ? -1 : 1;
117: sx = x < 0 ? -1 : 1;
118: y *= sy;
119: x *= sx;
120: if (!x) return((double) PI/2 * sy);
121: index = (10*y)/x + 0.5;
122: if (index > 39) index = 39;
123: return((double) (sy * (sx < 0 ? PI : 0 + sx*(tantable[index]/1000))));
124: }
125:
126: boarders(from, key)
127: int from, key;
128: {
129: register int n;
130: struct BP *ptr;
131:
132: ptr = key ? scene[game].ship[from].file -> DBP : scene[game].ship[from].file -> OBP ;
133: for (n=0; n < 3; n++){
134: if (ptr[n].turnsent)
135: return(1);
136: }
137: return(0);
138: }
139:
140: lengthof(string)
141: char *string;
142: {
143: register int n;
144:
145: for (n=0; string[n]; n++);
146: return(n);
147: }
148:
149:
150: Write(address, string, offset, value)
151: int string, address, offset, value;
152: {
153: int fd;
154: char file[25];
155: int *ptr;
156: char *cptr;
157: struct BP *bptr;
158: struct snag *sptr;
159:
160: if (string){
161: sprintf(Outbuf + buffercount, "%d %d %d %s\n", address, string, offset, value);
162: }
163: else {
164: sprintf(Outbuf + buffercount, "%d %d %d %d\n", address, string, offset, value);
165: }
166: buffercount += lengthof(Outbuf + buffercount);
167: if (address < 32){
168: ptr = &scene[game].ship[address].shipnum;
169: *(ptr + (offset - 2)/2) = value;
170: } else if (address < 64){
171: address -= SPECS;
172: ptr = &specs[scene[game].ship[address].shipnum].bs;
173: *(ptr + offset/2) = value;
174: } else if (address < 128){
175: address -= FILES;
176: ptr = &scene[game].ship[address].file -> points;
177: if (offset >= 30 && offset < 48){
178: bptr = scene[game].ship[address].file -> OBP;
179: switch(((offset - 30) % 6) / 2){
180: case 0:
181: bptr[(offset - 30)/6].turnsent = value;
182: break;
183: case 1:
184: bptr[(offset - 30)/6].toship = value;
185: break;
186: case 2:
187: bptr[(offset - 30)/6].mensent = value;
188: break;
189: }
190: }
191: if (offset >= 48 && offset < 66){
192: bptr = scene[game].ship[address].file -> DBP;
193: switch(((offset - 48) % 6) / 2){
194: case 0:
195: bptr[(offset - 48)/6].turnsent = value;
196: break;
197: case 1:
198: bptr[(offset - 48)/6].toship = value;
199: break;
200: case 2:
201: bptr[(offset - 48)/6].mensent = value;
202: break;
203: }
204: }
205: if (offset >= 84 && offset < 124){
206: sptr = scene[game].ship[address].file -> fouls;
207: switch(((offset - 84) % 4) / 2){
208: case 0:
209: sptr[(offset - 84) / 4].turnfoul = value;
210: break;
211: case 1:
212: sptr[(offset - 84) / 4].toship = value;
213: break;
214: }
215: }
216: if (offset >= 124 && offset < 164){
217: sptr = scene[game].ship[address].file -> grapples;
218: switch(((offset - 124) % 4) / 2){
219: case 0:
220: sptr[(offset - 124) / 4].turnfoul = value;
221: break;
222: case 1:
223: sptr[(offset - 124) / 4].toship = value;
224: break;
225: }
226: }
227: if (offset > 72) ptr = &scene[game].ship[address].file -> drift;
228: if (offset > 164) ptr = &scene[game].ship[address].file -> RH;
229: cptr = scene[game].ship[address].file -> captain;
230: if (offset == 72) cptr = scene[game].ship[address].file -> last;
231: if (offset == 164) cptr = scene[game].ship[address].file -> signal;
232: if (!string)
233: *(ptr + (offset - (offset > 72 ? (offset < 164 ? 82 : 224) : 20))/2 ) = value;
234: else
235: strcpy(cptr, (char *) value);
236: } else {
237: ptr = &scene[game].winddir;
238: *(ptr + offset/2) = value;
239: }
240: }
241:
242: sync()
243: {
244: char file[25];
245: char source[25];
246: int address, string;
247: int offset, value[40];
248: int fd, *ptr;
249: char *cptr;
250: struct BP *bptr;
251: struct snag *sptr;
252: register int n;
253: int (*sig1)(), (*sig2)();
254:
255: sig1 = signal(SIGHUP, SIG_IGN);
256: sig2 = signal(SIGINT, SIG_IGN);
257: sprintf(file, "/tmp/sync.%d", game);
258: sprintf(source, "/tmp/.%d", game);
259: for(n = 0; link(source, file) == -1 && n < 30; n++)
260: sleep(2);
261: fseek(syncfile, lastsync, 0);
262: while(fscanf(syncfile, "%d%d%d", &address, &string, &offset) != EOF){
263: if (string){
264: fgets((char *) value, 60, syncfile);
265: rmend((char *) value);
266: while(*(char *)value == ' ')
267: strcpy((char *) value, (char *) value + 1);
268: }
269: else
270: fscanf(syncfile, "%d", value);
271: if (address < 32){
272: ptr = &scene[game].ship[address].shipnum;
273: *(ptr + (offset - 2)/2) = value[0];
274: continue;
275: }
276: if (address < 64){
277: address -= SPECS;
278: ptr = &specs[scene[game].ship[address].shipnum].bs;
279: *(ptr + offset/2) = value[0];
280: continue;
281: }
282: if (address < 128){
283: address -= FILES;
284: ptr = &scene[game].ship[address].file -> points;
285: if (offset >= 30 && offset < 48){
286: bptr = scene[game].ship[address].file -> OBP;
287: switch(((offset - 30) % 6) / 2){
288: case 0:
289: bptr[(offset - 30)/6].turnsent = value[0];
290: break;
291: case 1:
292: bptr[(offset - 30)/6].toship = value[0];
293: break;
294: case 2:
295: bptr[(offset - 30)/6].mensent = value[0];
296: break;
297: }
298: }
299: if (offset >= 48 && offset < 66){
300: bptr = scene[game].ship[address].file -> DBP;
301: switch(((offset - 48) % 6) / 2){
302: case 0:
303: bptr[(offset - 48)/6].turnsent = value[0];
304: break;
305: case 1:
306: bptr[(offset - 48)/6].toship = value[0];
307: break;
308: case 2:
309: bptr[(offset - 48)/6].mensent = value[0];
310: break;
311: }
312: }
313: if (offset >= 84 && offset < 124){
314: sptr = scene[game].ship[address].file -> fouls;
315: switch(((offset - 84) % 4) / 2){
316: case 0:
317: sptr[(offset - 84) / 4].turnfoul = value[0];
318: break;
319: case 1:
320: sptr[(offset - 84) / 4].toship = value[0];
321: break;
322: }
323: }
324: if (offset >= 124 && offset < 164){
325: sptr = scene[game].ship[address].file -> grapples;
326: switch(((offset - 124) % 4) / 2){
327: case 0:
328: sptr[(offset - 124) / 4].turnfoul = value[0];
329: break;
330: case 1:
331: sptr[(offset - 124) / 4].toship = value[0];
332: break;
333: }
334: }
335: if (offset > 72) ptr = &scene[game].ship[address].file -> drift;
336: if (offset > 164) ptr = &scene[game].ship[address].file -> RH;
337: cptr = scene[game].ship[address].file -> captain;
338: if (offset == 72) cptr = scene[game].ship[address].file -> last;
339: if (offset == 164) cptr = scene[game].ship[address].file -> signal;
340: if (!string)
341: *(ptr + (offset - (offset > 72 ? (offset < 164 ? 82 : 224) : 20))/2 ) = value[0];
342: else
343: strcpy(cptr, (char *) value);
344: continue;
345: }
346: ptr = &scene[game].winddir;
347: *(ptr + offset/2) = value[0];
348: }
349: if (buffercount){
350: fseek(syncfile, 0L, 2);
351: fprintf(syncfile, "%s", Outbuf);
352: fflush(syncfile);
353: buffercount = 0;
354: }
355: lastsync = ftell(syncfile);
356: unlink(file);
357: signal(SIGHUP, sig1);
358: signal(SIGINT, sig2);
359: }
360:
361: rmend(str)
362: char *str;
363: {
364: register int n;
365:
366: for (n=0; *(str + n); n++);
367: if (n)
368: *(str + n - 1) = '\0';
369: }
370:
371: char
372: colours(ship)
373: int ship;
374: {
375: char flag;
376: int index;
377: struct File *ptr;
378:
379: ptr = scene[game].ship[ship].file;
380: if (ptr -> struck)
381: flag = '!';
382: if (ptr -> explode)
383: flag = '#';
384: if (ptr -> sink)
385: flag = '~';
386: if (ptr -> struck)
387: return(flag);
388: if (ptr -> captured > -1)
389: index = scene[game].ship[ptr -> captured].nationality;
390: else
391: index = scene[game].ship[ship].nationality;
392: switch(index){
393:
394: case 0:
395: flag = 'a';
396: break;
397: case 1:
398: flag = 'b';
399: break;
400: case 2:
401: flag = 's';
402: break;
403: case 3:
404: flag = 'f';
405: break;
406: }
407: return(ptr -> FS ? flag - 32 : flag);
408: }
409:
410: char
411: sterncolor(ship, sternr, sternc)
412: int ship, *sternr, *sternc;
413: {
414: int sr, sc;
415: int cap;
416: cap = scene[game].ship[ship].file -> captured;
417: sr = pos[ship].row;
418: sc = pos[ship].col;
419: drdc(&sr,&sc,pos[ship].dir);
420: *sternr = sr;
421: *sternc = sc;
422: ship -= nation[scene[game].ship[ship].nationality];
423: return(ship + '0' - 10*(cap > -1));
424: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.