Annotation of 43BSDTahoe/games/sail/misc.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1983 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that the above copyright notice and this paragraph are
        !             7:  * duplicated in all such forms and that any documentation,
        !             8:  * advertising materials, and other materials related to such
        !             9:  * distribution and use acknowledge that the software was developed
        !            10:  * by the University of California, Berkeley.  The name of the
        !            11:  * University may not be used to endorse or promote products derived
        !            12:  * from this software without specific prior written permission.
        !            13:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            14:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            15:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            16:  */
        !            17: 
        !            18: #ifndef lint
        !            19: static char sccsid[] = "@(#)misc.c     5.3 (Berkeley) 6/18/88";
        !            20: #endif /* not lint */
        !            21: 
        !            22: #include "externs.h"
        !            23: 
        !            24: #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
        !            25: 
        !            26: /* XXX */
        !            27: range(from, to)
        !            28: struct ship *from, *to;
        !            29: {
        !            30:        register bow1r, bow1c, bow2r, bow2c;
        !            31:        int stern1r, stern1c, stern2c, stern2r;
        !            32:        register int bb, bs, sb, ss, result;
        !            33: 
        !            34:        if (!to->file->dir)
        !            35:                return -1;
        !            36:        stern1r = bow1r = from->file->row;
        !            37:        stern1c = bow1c = from->file->col;
        !            38:        stern2r = bow2r = to->file->row;
        !            39:        stern2c = bow2c = to->file->col;
        !            40:        result = bb = distance(bow2r - bow1r, bow2c - bow1c);
        !            41:        if (bb < 5) {
        !            42:                stern2r += dr[to->file->dir];
        !            43:                stern2c += dc[to->file->dir];
        !            44:                stern1r += dr[from->file->dir];
        !            45:                stern1c += dc[from->file->dir];
        !            46:                bs = distance((bow2r - stern1r), (bow2c - stern1c));
        !            47:                sb = distance((bow1r - stern2r), (bow1c - stern2c));
        !            48:                ss = distance((stern2r - stern1r) ,(stern2c - stern1c));
        !            49:                result = min(bb, min(bs, min(sb, ss)));
        !            50:        }
        !            51:        return result;
        !            52: }
        !            53: 
        !            54: struct ship *
        !            55: closestenemy(from, side, anyship)
        !            56: register struct ship *from;
        !            57: char side, anyship;
        !            58: {
        !            59:        register struct ship *sp;
        !            60:        register char a;
        !            61:        int olddist = 30000, dist;
        !            62:        struct ship *closest = 0;
        !            63: 
        !            64:        a = capship(from)->nationality;
        !            65:        foreachship(sp) {
        !            66:                if (sp == from)
        !            67:                        continue;
        !            68:                if (sp->file->dir == 0)
        !            69:                        continue;
        !            70:                if (a == capship(sp)->nationality && !anyship)
        !            71:                        continue;
        !            72:                if (side && gunsbear(from, sp) != side)
        !            73:                        continue;
        !            74:                dist = range(from, sp);
        !            75:                if (dist < olddist) {
        !            76:                        closest = sp;
        !            77:                        olddist = dist;
        !            78:                }
        !            79:        }
        !            80:        return closest;
        !            81: }
        !            82: 
        !            83: angle(dr, dc)
        !            84: register dr, dc;
        !            85: {
        !            86:        register i;
        !            87: 
        !            88:        if (dc >= 0 && dr > 0)
        !            89:                i = 0;
        !            90:        else if (dr <= 0 && dc > 0)
        !            91:                i = 2;
        !            92:        else if (dc <= 0 && dr < 0)
        !            93:                i = 4;
        !            94:        else
        !            95:                i = 6;
        !            96:        dr = abs(dr);
        !            97:        dc = abs(dc);
        !            98:        if ((i == 0 || i == 4) && dc * 2.4 > dr) {
        !            99:                i++;
        !           100:                if (dc > dr * 2.4)
        !           101:                        i++;
        !           102:        } else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
        !           103:                i++;
        !           104:                if (dr > dc * 2.4)
        !           105:                        i++;
        !           106:        }
        !           107:        return i % 8 + 1;
        !           108: }
        !           109: 
        !           110: gunsbear(from, to)             /* checks for target bow or stern */
        !           111: register struct ship *from, *to;
        !           112: {
        !           113:        int Dr, Dc, i;
        !           114:        register ang;
        !           115: 
        !           116:        Dr = from->file->row - to->file->row;
        !           117:        Dc = to->file->col - from->file->col;
        !           118:        for (i = 2; i; i--) {
        !           119:                if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1)
        !           120:                        ang += 8;
        !           121:                if (ang >= 2 && ang <= 4)
        !           122:                        return 'r';
        !           123:                if (ang >= 6 && ang <= 7)
        !           124:                        return 'l';
        !           125:                Dr += dr[to->file->dir];
        !           126:                Dc += dc[to->file->dir];
        !           127:        }
        !           128:        return 0;
        !           129: }
        !           130: 
        !           131: portside(from, on, quick)
        !           132: register struct ship *from, *on;
        !           133: int quick;                     /* returns true if fromship is */
        !           134: {                              /* shooting at onship's starboard side */
        !           135:        register ang;
        !           136:        register Dr, Dc;
        !           137: 
        !           138:        Dr = from->file->row - on->file->row;
        !           139:        Dc = on->file->col - from->file->col;
        !           140:        if (quick == -1) {
        !           141:                Dr += dr[on->file->dir];
        !           142:                Dc += dc[on->file->dir];
        !           143:        }
        !           144:        ang = angle(Dr, Dc);
        !           145:        if (quick != 0)
        !           146:                return ang;
        !           147:        ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
        !           148:        return ang < 5;
        !           149: }
        !           150: 
        !           151: colours(sp)
        !           152: register struct ship *sp;
        !           153: {
        !           154:        register char flag;
        !           155: 
        !           156:        if (sp->file->struck)
        !           157:                flag = '!';
        !           158:        if (sp->file->explode)
        !           159:                flag = '#';
        !           160:        if (sp->file->sink)
        !           161:                flag = '~';
        !           162:        if (sp->file->struck)
        !           163:                return flag;
        !           164:        flag = *countryname[capship(sp)->nationality];
        !           165:        return sp->file->FS ? flag : tolower(flag);
        !           166: }
        !           167: 
        !           168: #include <sys/file.h>
        !           169: log(s)
        !           170: register struct ship *s;
        !           171: {
        !           172:        FILE *fp;
        !           173:        int persons;
        !           174:        int n;
        !           175:        struct logs log[NLOG];
        !           176:        float net;
        !           177:        register struct logs *lp;
        !           178: 
        !           179:        if ((fp = fopen(LOGFILE, "r+")) == NULL)
        !           180:                return;
        !           181: #ifdef LOCK_EX
        !           182:        if (flock(fileno(fp), LOCK_EX) < 0)
        !           183:                return;
        !           184: #endif
        !           185:        net = (float)s->file->points / s->specs->pts;
        !           186:        persons = getw(fp);
        !           187:        n = fread((char *)log, sizeof(struct logs), NLOG, fp);
        !           188:        for (lp = &log[n]; lp < &log[NLOG]; lp++)
        !           189:                lp->l_name[0] = lp->l_uid = lp->l_shipnum
        !           190:                        = lp->l_gamenum = lp->l_netpoints = 0;
        !           191:        rewind(fp);
        !           192:        if (persons < 0)
        !           193:                (void) putw(1, fp);
        !           194:        else
        !           195:                (void) putw(persons + 1, fp);
        !           196:        for (lp = log; lp < &log[NLOG]; lp++)
        !           197:                if (net > (float)lp->l_netpoints
        !           198:                    / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) {
        !           199:                        (void) fwrite((char *)log,
        !           200:                                sizeof (struct logs), lp - log, fp);
        !           201:                        (void) strcpy(log[NLOG-1].l_name, s->file->captain);
        !           202:                        log[NLOG-1].l_uid = getuid();
        !           203:                        log[NLOG-1].l_shipnum = s->file->index;
        !           204:                        log[NLOG-1].l_gamenum = game;
        !           205:                        log[NLOG-1].l_netpoints = s->file->points;
        !           206:                        (void) fwrite((char *)&log[NLOG-1],
        !           207:                                sizeof (struct logs), 1, fp);
        !           208:                        (void) fwrite((char *)lp,
        !           209:                                sizeof (struct logs), &log[NLOG-1] - lp, fp);
        !           210:                        break;
        !           211:                }
        !           212: #ifdef LOCK_EX
        !           213:        (void) flock(fileno(fp), LOCK_UN);
        !           214: #endif
        !           215:        (void) fclose(fp);
        !           216: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.