111589Sleres #ifndef lint 2*15330Sedward static char *sccsid = "@(#)dr_1.c 1.9 83/10/31"; 311589Sleres #endif 412619Sroot 514007Sedward #include "driver.h" 611589Sleres 715202Sedward main(argc, argv) 815202Sedward int argc; 915202Sedward char **argv; 1015202Sedward { 1115202Sedward register int n; 1215202Sedward register struct ship *sp; 1315202Sedward int nat[NNATION]; 1415202Sedward 1515202Sedward if (argc != 2) 1615202Sedward exit(1); 1715202Sedward (void) signal(SIGINT, SIG_IGN); 1815202Sedward (void) signal(SIGQUIT, SIG_IGN); 1915202Sedward (void) srand(getpid()); 2015202Sedward /* ;;; add code here to check the game number. */ 2115202Sedward game = atoi(argv[1]); 2215202Sedward cc = &scene[game]; 2315202Sedward ls = &cc->ship[cc->vessels]; 2415202Sedward if (sync_open() < 0) { 2515202Sedward perror("driver: syncfile"); 2615202Sedward exit(1); 2715202Sedward } 2815202Sedward for (n = 0; n < NNATION; n++) 2915202Sedward nat[n] = 0; 3015202Sedward foreachship(sp) { 3115202Sedward sp->file = (struct File *) calloc(1, sizeof (struct File)); 3215202Sedward if (sp == NULL) { 3315202Sedward (void) printf("driver: OUT OF MEMORY\n"); 3415202Sedward exit(0); 3515202Sedward } 3615202Sedward sp->file->loadL = L_ROUND; 3715202Sedward sp->file->loadR = L_ROUND; 3815202Sedward sp->file->readyR = R_LOADED|R_INITIAL; 3915202Sedward sp->file->readyL = R_LOADED|R_INITIAL; 4015202Sedward sp->file->stern = nat[sp->nationality]++; 4115202Sedward sp->file->dir = sp->shipdir; 4215202Sedward sp->file->row = sp->shiprow; 4315202Sedward sp->file->col = sp->shipcol; 4415202Sedward } 4515202Sedward windspeed = cc->windspeed; 4615202Sedward winddir = cc->winddir; 4715202Sedward for (;;) { 4815202Sedward Sync(); 4915202Sedward next(); 5015202Sedward unfoul(); 5115202Sedward checkup(); 5215202Sedward prizecheck(); 5315202Sedward moveall(); 5415202Sedward /* 5515202Sedward readpos(); 5615202Sedward */ 5715202Sedward thinkofgrapples(); 5815202Sedward boardcomp(); 5915202Sedward compcombat(); 6015202Sedward /* 6115202Sedward readpos(); 6215202Sedward */ 6315202Sedward resolve(); 6415202Sedward reload(); 6515202Sedward checksails(); 6615202Sedward Sync(); 6715202Sedward sleep(7); 6815202Sedward } 6915202Sedward } 7015202Sedward 7111589Sleres unfoul() 7211589Sleres { 7314007Sedward register int k; 7414007Sedward register struct ship *sp; 7514007Sedward struct ship *to; 7614007Sedward register int nat; 7711589Sleres 7814007Sedward foreachship(sp) { 7914007Sedward if (sp->file->captain[0]) 8014007Sedward continue; 8114007Sedward nat = capship(sp)->nationality; 8214007Sedward for (k = 0; k < NSHIP; k++) { 8314007Sedward if (sp->file->fouls[k].turnfoul == 0) 8414007Sedward continue; 8514007Sedward to = sp->file->fouls[k].toship; 8614007Sedward if (nat != capship(to)->nationality) 8714007Sedward continue; 8814007Sedward if (!toughmelee(sp, to, 0, 0)) 8914007Sedward continue; 9014007Sedward if (die() <= 2) 9114007Sedward cleanfoul(sp, to, k); 9211589Sleres } 9311589Sleres } 9411589Sleres } 9511589Sleres 9611589Sleres boardcomp() 9711589Sleres { 9812619Sroot int crew[3]; 9914007Sedward register struct ship *sp, *sq; 10011589Sleres 10114007Sedward foreachship(sp) { 10214007Sedward if (*sp->file->captain) 10314007Sedward continue; 10414007Sedward if (!fouled(sp) && !grappled(sp)) 10514007Sedward continue; 10614007Sedward if (sp->file->dir == 0) 10714007Sedward continue; 10814007Sedward if (sp->file->struck || sp->file->captured != 0) 10914007Sedward continue; 11014007Sedward crew[0] = sp->specs->crew1 != 0; 11114007Sedward crew[1] = sp->specs->crew2 != 0; 11214007Sedward crew[2] = sp->specs->crew3 != 0; 11314007Sedward foreachship(sq) { 11414007Sedward if (!Xsnagged2(sp, sq)) 11514007Sedward continue; 11614007Sedward if (meleeing(sp, sq)) 11714007Sedward continue; 11814007Sedward if (!sq->file->dir 11914007Sedward || sp->nationality == capship(sq)->nationality) 12014007Sedward continue; 12114007Sedward switch (sp->specs->class - sq->specs->class) { 12214007Sedward case -3: case -4: case -5: 12314007Sedward if (crew[0]) { 12414007Sedward /* OBP */ 12514007Sedward sendbp(sp, sq, crew[0]*100, 0); 12614007Sedward crew[0] = 0; 12714007Sedward } else if (crew[1]){ 12814007Sedward /* OBP */ 12914007Sedward sendbp(sp, sq, crew[1]*10, 0); 13014007Sedward crew[1] = 0; 13111589Sleres } 13214007Sedward break; 13314007Sedward case -2: 13414007Sedward if (crew[0] || crew[1]) { 13514007Sedward /* OBP */ 13614007Sedward sendbp(sp, sq, crew[0]*100+crew[1]*10, 13714007Sedward 0); 13814007Sedward crew[0] = crew[1] = 0; 13914007Sedward } 14014007Sedward break; 14114007Sedward case -1: case 0: case 1: 14214007Sedward if (crew[0]) { 14314007Sedward /* OBP */ 14414007Sedward sendbp(sp, sq, crew[0]*100+crew[1]*10, 14514007Sedward 0); 14614007Sedward crew[0] = crew[1] = 0; 14714007Sedward } 14814007Sedward break; 14914007Sedward case 2: case 3: case 4: case 5: 15014007Sedward /* OBP */ 15114007Sedward sendbp(sp, sq, crew[0]*100+crew[1]*10+crew[2], 15214007Sedward 0); 15314007Sedward crew[0] = crew[1] = crew[2] = 0; 15414007Sedward break; 15511589Sleres } 15614007Sedward } 15711589Sleres } 15811589Sleres } 15911589Sleres 16011589Sleres fightitout(from, to, key) 16114007Sedward struct ship *from, *to; 16214007Sedward int key; 16311589Sleres { 16414007Sedward struct ship *fromcap, *tocap; 16514007Sedward int crewfrom[3], crewto[3], menfrom, mento; 16611589Sleres int pcto, pcfrom, fromstrength, strengthto, frominjured, toinjured; 16714007Sedward int topoints; 16814007Sedward int index, totalfrom = 0, totalto = 0; 16914007Sedward int count; 17011589Sleres char message[60]; 17111589Sleres 17214007Sedward menfrom = mensent(from, to, crewfrom, &fromcap, &pcfrom, key); 17314007Sedward mento = mensent(to, from, crewto, &tocap, &pcto, 0); 17414007Sedward if (fromcap == 0) 17511589Sleres fromcap = from; 17614007Sedward if (tocap == 0) 17711589Sleres tocap = to; 178*15330Sedward if (key) { 179*15330Sedward if (!menfrom) { /* if crew surprised */ 180*15330Sedward if (fromcap == from) 181*15330Sedward menfrom = from->specs->crew1 182*15330Sedward + from->specs->crew2 183*15330Sedward + from->specs->crew3; 184*15330Sedward else 185*15330Sedward menfrom = from->file->pcrew; 186*15330Sedward } else { 187*15330Sedward menfrom *= 2; /* DBP's fight at an advantage */ 188*15330Sedward } 189*15330Sedward } 19014007Sedward fromstrength = menfrom * fromcap->specs->qual; 19114007Sedward strengthto = mento * tocap->specs->qual; 19214007Sedward for (count = 0; 19314007Sedward (fromstrength < strengthto * 3 && strengthto < fromstrength * 3 19414007Sedward || fromstrength == -1) && count < 4; 19514007Sedward count++) { 19611589Sleres index = fromstrength/10; 19711589Sleres if (index > 8) 19811589Sleres index = 8; 19911589Sleres toinjured = MT[index][2 - die() / 3]; 20011589Sleres totalto += toinjured; 20111589Sleres index = strengthto/10; 20211589Sleres if (index > 8) 20311589Sleres index = 8; 20411589Sleres frominjured = MT[index][2 - die() / 3]; 20511589Sleres totalfrom += frominjured; 20611589Sleres menfrom -= frominjured; 20711589Sleres mento -= toinjured; 20814007Sedward fromstrength = menfrom * fromcap->specs->qual; 20914007Sedward strengthto = mento * tocap->specs->qual; 21011589Sleres } 21114007Sedward if (fromstrength >= strengthto * 3 || count == 4) { 21211589Sleres unboard(to, from, 0); 21311589Sleres subtract(from, totalfrom, crewfrom, fromcap, pcfrom); 21411589Sleres subtract(to, totalto, crewto, tocap, pcto); 21514007Sedward makesignal(from, "boarders from %s repelled", to); 21614007Sedward (void) sprintf(message, "killed in melee: %d. %s: %d", 21714007Sedward totalto, from->shipname, totalfrom); 21814007Sedward Write(W_SIGNAL, to, 1, (int) message, 0, 0, 0); 21911589Sleres if (key) 22014007Sedward return 1; 22114007Sedward } else if (strengthto >= fromstrength * 3) { 22211589Sleres unboard(from, to, 0); 22311589Sleres subtract(from, totalfrom, crewfrom, fromcap, pcfrom); 22411589Sleres subtract(to, totalto, crewto, tocap, pcto); 22514007Sedward if (key) { 22611589Sleres if (fromcap != from) 22714007Sedward Write(W_POINTS, fromcap, 0, 22814007Sedward fromcap->file->points - 22914007Sedward from->file->struck 23014007Sedward ? from->specs->pts 23114007Sedward : 2 * from->specs->pts, 23214007Sedward 0, 0, 0); 23311589Sleres 23411589Sleres /* ptr1 points to the shipspec for the ship that was just unboarded. 23511589Sleres I guess that what is going on here is that the pointer is multiplied 23611589Sleres or something. */ 23711589Sleres 23814007Sedward Write(W_CAPTURED, from, 0, to-SHIP(0), 0, 0, 0); 23914007Sedward topoints = 2 * from->specs->pts + to->file->points; 24014007Sedward if (from->file->struck) 24114007Sedward topoints -= from->specs->pts; 24214007Sedward Write(W_POINTS, to, 0, topoints, 0, 0, 0); 24311589Sleres mento = crewto[0] ? crewto[0] : crewto[1]; 24414007Sedward if (mento) { 24511589Sleres subtract(to, mento, crewto, tocap, pcto); 24614007Sedward subtract(from, - mento, crewfrom, to, 0); 24711589Sleres } 24814007Sedward (void) sprintf(message, "captured by the %s!", 24914007Sedward to->shipname); 25014007Sedward Write(W_SIGNAL, from, 1, (int) message, 0, 0, 0); 25114007Sedward (void) sprintf(message, "killed in melee: %d. %s: %d", 25214007Sedward totalto, from->shipname, totalfrom); 25314007Sedward Write(W_SIGNAL, to, 1, (int) message, 0, 0, 0); 25411589Sleres mento = 0; 25514007Sedward return 0; 25611589Sleres } 25711589Sleres } 25814007Sedward return 0; 25914007Sedward } 26011589Sleres 26111589Sleres resolve() 26211589Sleres { 26314007Sedward int thwart; 26414007Sedward register struct ship *sp, *sq; 26511589Sleres 26614007Sedward foreachship(sp) { 26714007Sedward if (sp->file->dir == 0) 26814007Sedward continue; 26915316Sedward thwart = 2; 270*15330Sedward for (sq = sp + 1; sq < ls; sq++) 27114007Sedward if (sq->file->dir && meleeing(sp, sq) && meleeing(sq, sp)) 27214007Sedward (void) fightitout(sp, sq, 0); 27314007Sedward foreachship(sq) { 27414007Sedward if (sq->file->dir && meleeing(sq, sp)) 27514007Sedward thwart = fightitout(sp, sq, 1); 27614007Sedward if (!thwart) 27714007Sedward break; 27811589Sleres } 27914007Sedward foreachship(sq) { 28014007Sedward if (sq->file->dir && meleeing(sq, sp)) 28114007Sedward unboard(sq, sp, 0); 28214007Sedward unboard(sp, sq, 0); 28314007Sedward } 28414007Sedward unboard(sp, sp, 1); 28514007Sedward if (thwart == 2) 28614007Sedward unboard(sp, sp, 1); 28711589Sleres } 28811589Sleres } 28911589Sleres 29011589Sleres compcombat() 29111589Sleres { 29214007Sedward register n; 29314007Sedward register struct ship *sp; 29414007Sedward struct ship *closest; 29511589Sleres int crew[3], men = 0, target, temp; 29615311Sedward int r, guns, ready, load, car; 29715311Sedward int index, rakehim, sternrake; 29814007Sedward int shootat, hit; 29911589Sleres 30014007Sedward foreachship(sp) { 30114007Sedward if (sp->file->captain[0] || sp->file->dir == 0) 30214007Sedward continue; 30314007Sedward crew[0] = sp->specs->crew1; 30414007Sedward crew[1] = sp->specs->crew2; 30514007Sedward crew[2] = sp->specs->crew3; 30614007Sedward for (n = 0; n < 3; n++) { 30714007Sedward if (sp->file->OBP[n].turnsent) 30814007Sedward men += sp->file->OBP[n].mensent; 30914007Sedward } 31014007Sedward for (n = 0; n < 3; n++) { 31114007Sedward if (sp->file->DBP[n].turnsent) 31214007Sedward men += sp->file->DBP[n].mensent; 31314007Sedward } 31414007Sedward if (men){ 31514007Sedward crew[0] = men/100 ? 0 : crew[0] != 0; 31614007Sedward crew[1] = (men%100)/10 ? 0 : crew[1] != 0; 31714007Sedward crew[2] = men%10 ? 0 : crew[2] != 0; 31814007Sedward } 31914007Sedward for (r = 0; r < 2; r++) { 32014007Sedward if (!crew[2]) 32114007Sedward continue; 32214007Sedward if (sp->file->struck) 32314007Sedward continue; 32414007Sedward if (r) { 32514007Sedward ready = sp->file->readyR; 32614007Sedward guns = sp->specs->gunR; 32714007Sedward car = sp->specs->carR; 32814007Sedward } else { 32914007Sedward ready = sp->file->readyL; 33014007Sedward guns = sp->specs->gunL; 33114007Sedward car = sp->specs->carL; 33211589Sleres } 33314007Sedward if (!guns && !car) 33414007Sedward continue; 33514007Sedward if ((ready & R_LOADED) == 0) 33614007Sedward continue; 33714007Sedward closest = closestenemy(sp, r ? 'r' : 'l', 0); 33814007Sedward if (closest == 0) 33914007Sedward continue; 34014007Sedward if (range(closest, sp) > range(sp, closestenemy(sp, r ? 'r' : 'l', 1))) 34114007Sedward continue; 34214007Sedward if (closest->file->struck) 34314007Sedward continue; 34414007Sedward target = range(sp, closest); 34514007Sedward if (target > 10) 34614007Sedward continue; 34714007Sedward if (!guns && target >= 3) 34814007Sedward continue; 34914007Sedward load = L_ROUND; 35014007Sedward if (target == 1 && sp->file->loadwith == L_GRAPE) 35114007Sedward load = L_GRAPE; 35214007Sedward if (target <= 3 && closest->file->FS) 35314007Sedward load = L_CHAIN; 35414007Sedward if (target == 1 && load != L_GRAPE) 35514007Sedward load = L_DOUBLE; 35614007Sedward if (load > L_CHAIN && target < 6) 35714007Sedward shootat = HULL; 35814007Sedward else 35914007Sedward shootat = RIGGING; 36014007Sedward rakehim = gunsbear(sp, closest) 36114007Sedward && !gunsbear(closest, sp); 36214007Sedward temp = portside(closest, sp, 1) 36314007Sedward - closest->file->dir + 1; 36414007Sedward if (temp < 1) 36514007Sedward temp += 8; 36614007Sedward if (temp > 8) 36714007Sedward temp -= 8; 36814007Sedward sternrake = temp > 4 && temp < 6; 36914007Sedward index = guns; 37014007Sedward if (target < 3) 37114007Sedward index += car; 37214007Sedward index = (index - 1) / 3; 37314007Sedward index = index > 8 ? 8 : index; 37414007Sedward if (!rakehim) 37514007Sedward hit = HDT[index][target-1]; 37614007Sedward else 37714007Sedward hit = HDTrake[index][target-1]; 37814007Sedward if (rakehim && sternrake) 37914007Sedward hit++; 38014007Sedward hit += QUAL[index][capship(sp)->specs->qual - 1]; 38114007Sedward for (n = 0; n < 3 && sp->file->captured == 0; n++) 38214007Sedward if (!crew[n]) 38314007Sedward if (index <= 5) 38414007Sedward hit--; 38514007Sedward else 38614007Sedward hit -= 2; 38715311Sedward if (ready & R_INITIAL) { 38815311Sedward if (!r) 38915311Sedward sp->file->readyL &= ~R_INITIAL; 39015311Sedward else 39115311Sedward sp->file->readyR &= ~R_INITIAL; 39214007Sedward if (index <= 3) 39314007Sedward hit++; 39411589Sleres else 39514007Sedward hit += 2; 39615311Sedward } 39714007Sedward if (sp->file->captured != 0) 39814007Sedward if (index <= 1) 39914007Sedward hit--; 40014007Sedward else 40114007Sedward hit -= 2; 40214007Sedward hit += AMMO[index][load - 1]; 40314007Sedward temp = sp->specs->class; 40414007Sedward if ((temp >= 5 || temp == 1) && windspeed == 5) 40514007Sedward hit--; 40614007Sedward if (windspeed == 6 && temp == 4) 40714007Sedward hit -= 2; 40814007Sedward if (windspeed == 6 && temp <= 3) 40914007Sedward hit--; 41014007Sedward if (hit >= 0) { 41114007Sedward if (load != L_GRAPE) 41214007Sedward hit = hit > 10 ? 10 : hit; 41314007Sedward table(shootat, load, hit, closest, sp, die()); 41411589Sleres } 41511589Sleres } 41611589Sleres } 41711589Sleres } 41811589Sleres 41911589Sleres next() 42011589Sleres { 42114007Sedward if (++turn % 55 == 0) 42215202Sedward if (alive) 42315202Sedward alive = 0; 42414007Sedward else 42515202Sedward people = 0; 42615202Sedward if (people <= 0 || windspeed == 7) { 42715202Sedward sync_close(1); 42811589Sleres exit(0); 42911589Sleres } 43014007Sedward Write(W_TURN, SHIP(0), 0, turn, 0, 0, 0); 43114007Sedward if (turn % 7 == 0) { 43214007Sedward if (die() >= cc->windchange || !windspeed) { 43314007Sedward switch (die()) { 43414007Sedward case 1: 43514007Sedward winddir = 1; 43614007Sedward break; 43714007Sedward case 2: 43814007Sedward break; 43914007Sedward case 3: 44014007Sedward winddir++; 44114007Sedward break; 44214007Sedward case 4: 44314007Sedward winddir--; 44414007Sedward break; 44514007Sedward case 5: 44614007Sedward winddir += 2; 44714007Sedward break; 44814007Sedward case 6: 44914007Sedward winddir -= 2; 45014007Sedward break; 45114007Sedward } 45214007Sedward if (winddir > 8) 45314007Sedward winddir -= 8; 45414007Sedward if (winddir < 1) 45514007Sedward winddir += 8; 45614007Sedward if (windspeed) 45714007Sedward switch (die()) { 45811589Sleres case 1: 45911589Sleres case 2: 46014007Sedward windspeed--; 46111589Sleres break; 46211589Sleres case 5: 46311589Sleres case 6: 46414007Sedward windspeed++; 46511589Sleres break; 46611589Sleres } 46711589Sleres else 46811589Sleres windspeed++; 46914007Sedward Write(W_WIND, SHIP(0), 0, winddir, windspeed, 0, 0); 47014007Sedward } 47111589Sleres } 47211589Sleres } 47315237Sedward 47415237Sedward /*VARARGS2*/ 47515237Sedward Signal(fmt, ship, a, b, c) 47615237Sedward char *fmt; 47715237Sedward struct ship *ship; 47815237Sedward { 47915237Sedward } 480