Annotation of researchv10no/games/trek/phaser.c, revision 1.1.1.1

1.1       root        1: # include      "trek.h"
                      2: 
                      3: /**
                      4:  **    phaser control
                      5:  **/
                      6: 
                      7: CVNTAB Matab[]
                      8: {
                      9:        "a",            "utomatic",
                     10:        "m",            "anual",
                     11:        0
                     12: };
                     13: 
                     14: phaser()
                     15: {
                     16:        register int            i, k;
                     17:        int                     manual, flag, units, extra;
                     18:        int                     hit, reqd, n;
                     19:        double                  tot;
                     20:        int                     hits[MAXKLQUAD], hitreqd[MAXKLQUAD];
                     21:        double                  dustfac, tothe;
                     22: 
                     23:        if (Status.cond == DOCKED) {
                     24:                printf("Phasers cannot fire through starbase shields.\n");
                     25:                return;
                     26:        }
                     27:        if (Damage[PHASER]) {
                     28:                printf("Phaser control damaged.\n");
                     29:                return;
                     30:        }
                     31:        if (Status.shldup) {
                     32:                printf("Scotty: we will have to divert power from the shields.\n");
                     33:                if(!getynpar("Is that wise"))
                     34:                        return;
                     35:                Move.shldchg = 1;
                     36:        }
                     37:        if (Status.cloaked) {
                     38:                printf("Sulu: Captain, you know that we cannot fire while cloaked.\n");
                     39:                return;
                     40:        }
                     41:        manual = 0;
                     42:        if (Damage[COMPUTER])
                     43:                if(lineended()) {
                     44:                        printf("Computer damaged, manual mode selected\n");
                     45:                        manual = 1;
                     46:                }
                     47: 
                     48:        if (!manual)
                     49:        {
                     50:                if((manual=(getcodpar("Manual or automatic", Matab))) < 0) return;
                     51:        }
                     52:        if (!manual && Damage[COMPUTER])
                     53:        {
                     54:                printf("Computer damaged, manual selected\n");
                     55:                newline();
                     56:                manual = 1;
                     57:        }
                     58: 
                     59:        /* set up hits[] with amount for each klingon */
                     60:        flag = 1;
                     61:        n = Nkling;
                     62:        if (manual)
                     63:        {
                     64:                while (flag)
                     65:                {
                     66:                        printf("%d units available\n", Status.energy);
                     67:                        extra = 0;
                     68:                        for (i = 0; i < n; i++)
                     69:                        {
                     70:                                printf("Klingon at %d,%d:",
                     71:                                        Kling[i].x, Kling[i].y);
                     72:                                if(getintpar(" units to fire", &units)==0 || units < 0) return;
                     73:                                hits[i] = units;
                     74:                                extra =+ units;
                     75:                                if (extra > Status.energy)
                     76:                                {
                     77:                                        printf("Available energy exceeded. ");
                     78:                                        newline();
                     79:                                        break;
                     80:                                }
                     81:                        }
                     82:                        flag = i < n;
                     83:                        if (extra <= 0)
                     84:                                return;
                     85:                }
                     86:                Status.energy =- extra;
                     87:                extra = 0;
                     88:        }
                     89:        else
                     90:        {
                     91:                /* automatic distribution of power */
                     92:                printf("Phasers locked on target.  ");
                     93:                while (flag)
                     94:                {
                     95:                        printf("%d units available\n", Status.energy);
                     96:                        if(getintpar("Units to fire", &units)==0 || units < 0) return;
                     97:                        if (units > Status.energy)
                     98:                        {
                     99:                                printf("Available energy exceeded.  ");
                    100:                                newline();
                    101:                                continue;
                    102:                        }
                    103:                        flag = 0;
                    104:                        Status.energy =- units;
                    105:                        extra = units;
                    106:                        if (Nkling != 0)
                    107:                        {
                    108:                                tot = Nkling * (Nkling + 1) / 2;
                    109:                                for (i = 0; i < Nkling; i++)
                    110:                                {
                    111:                                        hits[i] = ((Nkling - i) / tot) * extra;
                    112:                                        extra =- hits[i];
                    113:                                        dustfac = 0.90;
                    114:                                        tothe = Kling[i].dist;
                    115:                                        hitreqd[i] = Kling[i].power / pow(dustfac, tothe) + 0.5;
                    116:                                        if (hits[i] > hitreqd[i])
                    117:                                        {
                    118:                                                extra =+ hits[i] - hitreqd[i];
                    119:                                                hits[i] = hitreqd[i];
                    120:                                        }
                    121:                                }
                    122:                                if (extra != 0)
                    123:                                {
                    124:                                        for (i = 0; i < Nkling; i++)
                    125:                                        {
                    126:                                                reqd = hitreqd[i] - hits[i];
                    127:                                                if (reqd <= 0)
                    128:                                                        continue;
                    129:                                                if (reqd >= extra)
                    130:                                                {
                    131:                                                        hits[i] =+ extra;
                    132:                                                        extra = 0;
                    133:                                                        break;
                    134:                                                }
                    135:                                                hits[i] = hitreqd[i];
                    136:                                                extra =- reqd;
                    137:                                        }
                    138:                                }
                    139:                        }
                    140:                }
                    141:        }
                    142: 
                    143:        /* actually fire the shots */
                    144:        Move.free = 0;
                    145:        k = 0;
                    146:        for (i = 0; i < n; i++)
                    147:        {
                    148:                if (hits[i] == 0)
                    149:                {
                    150:                        k =+ 1;
                    151:                        continue;
                    152:                }
                    153:                dustfac = 0.90 + 0.01 * franf();
                    154:                tothe = Kling[k].dist;
                    155:                hit = hits[i] * pow(dustfac, tothe) + 0.5;
                    156:                Kling[k].power =- hit;
                    157:                printf("%d unit hit\t", hit);
                    158:                if (Kling[k].power <= 0) {
                    159:                        extra =- Kling[k].power;
                    160:                        killk(Kling[k].x, Kling[k].y);
                    161:                } else {
                    162:                        printf("on Klingon at %d,%d\n", Kling[k].x, Kling[k].y);
                    163:                        k =+ 1;
                    164:                }
                    165:        }
                    166:        if (extra > 0)
                    167:                printf("%d units expended on empty space\n", extra);
                    168: }

unix.superglobalmegacorp.com

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