| File: | src/games/sail/misc.c |
| Warning: | line 184, column 3 Value stored to 'flag' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* $OpenBSD: misc.c,v 1.11 2019/06/28 13:32:52 deraadt Exp $ */ |
| 2 | /* $NetBSD: misc.c,v 1.3 1995/04/22 10:37:03 cgd Exp $ */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1983, 1993 |
| 6 | * The Regents of the University of California. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the University nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #include <ctype.h> |
| 34 | #include <err.h> |
| 35 | #ifdef LOCK_EX |
| 36 | #include <fcntl.h> |
| 37 | #endif |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | #include "extern.h" |
| 44 | #include "pathnames.h" |
| 45 | |
| 46 | #define distance(x,y)(abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2) |
| 47 | |
| 48 | /* XXX */ |
| 49 | int |
| 50 | range(struct ship *from, struct ship *to) |
| 51 | { |
| 52 | int bow1r, bow1c, bow2r, bow2c; |
| 53 | int stern1r, stern1c, stern2c, stern2r; |
| 54 | int bb, bs, sb, ss, result; |
| 55 | |
| 56 | if (!to->file->dir) |
| 57 | return -1; |
| 58 | stern1r = bow1r = from->file->row; |
| 59 | stern1c = bow1c = from->file->col; |
| 60 | stern2r = bow2r = to->file->row; |
| 61 | stern2c = bow2c = to->file->col; |
| 62 | result = bb = distance(bow2r - bow1r, bow2c - bow1c)(abs(bow2r - bow1r) >= abs(bow2c - bow1c) ? abs(bow2r - bow1r ) + abs(bow2c - bow1c)/2 : abs(bow2c - bow1c) + abs(bow2r - bow1r )/2); |
| 63 | if (bb < 5) { |
| 64 | stern2r += dr[to->file->dir]; |
| 65 | stern2c += dc[to->file->dir]; |
| 66 | stern1r += dr[from->file->dir]; |
| 67 | stern1c += dc[from->file->dir]; |
| 68 | bs = distance((bow2r - stern1r), (bow2c - stern1c))(abs((bow2r - stern1r)) >= abs((bow2c - stern1c)) ? abs((bow2r - stern1r)) + abs((bow2c - stern1c))/2 : abs((bow2c - stern1c )) + abs((bow2r - stern1r))/2); |
| 69 | sb = distance((bow1r - stern2r), (bow1c - stern2c))(abs((bow1r - stern2r)) >= abs((bow1c - stern2c)) ? abs((bow1r - stern2r)) + abs((bow1c - stern2c))/2 : abs((bow1c - stern2c )) + abs((bow1r - stern2r))/2); |
| 70 | ss = distance((stern2r - stern1r) ,(stern2c - stern1c))(abs((stern2r - stern1r)) >= abs((stern2c - stern1c)) ? abs ((stern2r - stern1r)) + abs((stern2c - stern1c))/2 : abs((stern2c - stern1c)) + abs((stern2r - stern1r))/2); |
| 71 | result = min(bb, min(bs, min(sb, ss)))((bb) < (((bs) < (((sb) < (ss) ? (sb) : (ss))) ? (bs ) : (((sb) < (ss) ? (sb) : (ss))))) ? (bb) : (((bs) < ( ((sb) < (ss) ? (sb) : (ss))) ? (bs) : (((sb) < (ss) ? ( sb) : (ss)))))); |
| 72 | } |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | struct ship * |
| 77 | closestenemy(struct ship *from, int side, int anyship) |
| 78 | { |
| 79 | struct ship *sp; |
| 80 | char a; |
| 81 | int olddist = 30000, dist; |
| 82 | struct ship *closest = 0; |
| 83 | |
| 84 | a = capship(from)((from)->file->captured?(from)->file->captured:(from ))->nationality; |
| 85 | foreachship(sp)for ((sp) = cc->ship; (sp) < ls; (sp)++) { |
| 86 | if (sp == from) |
| 87 | continue; |
| 88 | if (sp->file->dir == 0) |
| 89 | continue; |
| 90 | if (a == capship(sp)((sp)->file->captured?(sp)->file->captured:(sp))->nationality && !anyship) |
| 91 | continue; |
| 92 | if (side && gunsbear(from, sp) != side) |
| 93 | continue; |
| 94 | dist = range(from, sp); |
| 95 | if (dist < olddist) { |
| 96 | closest = sp; |
| 97 | olddist = dist; |
| 98 | } |
| 99 | } |
| 100 | return closest; |
| 101 | } |
| 102 | |
| 103 | int |
| 104 | angle(int dr, int dc) |
| 105 | { |
| 106 | int i; |
| 107 | |
| 108 | if (dc >= 0 && dr > 0) |
| 109 | i = 0; |
| 110 | else if (dr <= 0 && dc > 0) |
| 111 | i = 2; |
| 112 | else if (dc <= 0 && dr < 0) |
| 113 | i = 4; |
| 114 | else |
| 115 | i = 6; |
| 116 | dr = abs(dr); |
| 117 | dc = abs(dc); |
| 118 | if ((i == 0 || i == 4) && dc * 2.4 > dr) { |
| 119 | i++; |
| 120 | if (dc > dr * 2.4) |
| 121 | i++; |
| 122 | } else if ((i == 2 || i == 6) && dr * 2.4 > dc) { |
| 123 | i++; |
| 124 | if (dr > dc * 2.4) |
| 125 | i++; |
| 126 | } |
| 127 | return i % 8 + 1; |
| 128 | } |
| 129 | |
| 130 | /* checks for target bow or stern */ |
| 131 | int |
| 132 | gunsbear(struct ship *from, struct ship *to) |
| 133 | { |
| 134 | int Dr, Dc, i; |
| 135 | int ang; |
| 136 | |
| 137 | Dr = from->file->row - to->file->row; |
| 138 | Dc = to->file->col - from->file->col; |
| 139 | for (i = 2; i; i--) { |
| 140 | if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1) |
| 141 | ang += 8; |
| 142 | if (ang >= 2 && ang <= 4) |
| 143 | return 'r'; |
| 144 | if (ang >= 6 && ang <= 7) |
| 145 | return 'l'; |
| 146 | Dr += dr[to->file->dir]; |
| 147 | Dc += dc[to->file->dir]; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /* returns true if fromship is shooting at onship's starboard side */ |
| 153 | int |
| 154 | portside(struct ship *from, struct ship *on, int quick) |
| 155 | { |
| 156 | int ang; |
| 157 | int Dr, Dc; |
| 158 | |
| 159 | Dr = from->file->row - on->file->row; |
| 160 | Dc = on->file->col - from->file->col; |
| 161 | if (quick == -1) { |
| 162 | Dr += dr[on->file->dir]; |
| 163 | Dc += dc[on->file->dir]; |
| 164 | } |
| 165 | ang = angle(Dr, Dc); |
| 166 | if (quick != 0) |
| 167 | return ang; |
| 168 | ang = (ang + 4 - on->file->dir - 1) % 8 + 1; |
| 169 | return ang < 5; |
| 170 | } |
| 171 | |
| 172 | int |
| 173 | colours(struct ship *sp) |
| 174 | { |
| 175 | char flag; |
| 176 | |
| 177 | if (sp->file->struck) { |
| 178 | flag = '!'; |
| 179 | return flag; |
| 180 | } |
| 181 | if (sp->file->explode) |
| 182 | flag = '#'; |
| 183 | if (sp->file->sink) |
| 184 | flag = '~'; |
Value stored to 'flag' is never read | |
| 185 | flag = *countryname[capship(sp)((sp)->file->captured?(sp)->file->captured:(sp))->nationality]; |
| 186 | return sp->file->FS ? flag : tolower((unsigned char)flag); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | logger(struct ship *s) |
| 191 | { |
| 192 | FILE *fp; |
| 193 | int persons; |
| 194 | int n; |
| 195 | struct logs log[NLOG10]; |
| 196 | float net; |
| 197 | struct logs *lp; |
| 198 | |
| 199 | setegid(egid); |
| 200 | if ((fp = fopen(_PATH_LOGFILE"/var/games/saillog", "r+")) == NULL((void *)0)) { |
| 201 | setegid(gid); |
| 202 | return; |
| 203 | } |
| 204 | setegid(gid); |
| 205 | #ifdef LOCK_EX |
| 206 | if (flock(fileno(fp)(!__isthreaded ? ((fp)->_file) : (fileno)(fp)), LOCK_EX) == -1) |
| 207 | return; |
| 208 | #endif |
| 209 | net = (float)s->file->points / s->specs->pts; |
| 210 | persons = getw(fp); |
| 211 | n = fread((char *)log, sizeof(struct logs), NLOG10, fp); |
| 212 | for (lp = &log[n]; lp < &log[NLOG10]; lp++) |
| 213 | lp->l_name[0] = lp->l_uid = lp->l_shipnum |
| 214 | = lp->l_gamenum = lp->l_netpoints = 0; |
| 215 | if (fseek(fp, 0L, SEEK_SET0) == -1) |
| 216 | err(1, "fseek"); |
| 217 | if (persons < 0) |
| 218 | (void) putw(1, fp); |
| 219 | else |
| 220 | (void) putw(persons + 1, fp); |
| 221 | for (lp = log; lp < &log[NLOG10]; lp++) |
| 222 | if (net > (float)lp->l_netpoints |
| 223 | / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) { |
| 224 | (void) fwrite((char *)log, |
| 225 | sizeof (struct logs), lp - log, fp); |
| 226 | (void) strlcpy(log[NLOG10-1].l_name, s->file->captain, |
| 227 | sizeof log[NLOG10-1].l_name); |
| 228 | log[NLOG10-1].l_uid = getuid(); |
| 229 | log[NLOG10-1].l_shipnum = s->file->index; |
| 230 | log[NLOG10-1].l_gamenum = game; |
| 231 | log[NLOG10-1].l_netpoints = s->file->points; |
| 232 | (void) fwrite((char *)&log[NLOG10-1], |
| 233 | sizeof (struct logs), 1, fp); |
| 234 | (void) fwrite((char *)lp, |
| 235 | sizeof (struct logs), &log[NLOG10-1] - lp, fp); |
| 236 | break; |
| 237 | } |
| 238 | #ifdef LOCK_EX |
| 239 | (void) flock(fileno(fp)(!__isthreaded ? ((fp)->_file) : (fileno)(fp)), LOCK_UN); |
| 240 | #endif |
| 241 | (void) fclose(fp); |
| 242 | } |