BSD 4_4 release
[unix-history] / usr / src / games / sail / pl_6.c
index 054291b..4daaa59 100644 (file)
@@ -1,20 +1,56 @@
+/*
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)pl_6.c      2.2 83/12/09";
-#endif
+static char sccsid[] = "@(#)pl_6.c     8.1 (Berkeley) 5/31/93";
+#endif /* not lint */
 
 #include "player.h"
 
 repair()
 {
        char c;
 
 #include "player.h"
 
 repair()
 {
        char c;
-       char *repairs;
-       struct shipspecs *ptr;
+       register char *repairs;
+       register struct shipspecs *ptr = mc;
+       register int count;
+
+#define FIX(x, m) (m - ptr->x > count \
+       ? (ptr->x += count, count = 0) : (count -= m - ptr->x, ptr->x = m))
 
        if (repaired || loaded || fired || changed || turned()) {
                Signal("No hands free to repair", (struct ship *)0);
                return;
        }
 
        if (repaired || loaded || fired || changed || turned()) {
                Signal("No hands free to repair", (struct ship *)0);
                return;
        }
-       ptr = mc;
        c = sgetch("Repair (hull, guns, rigging)? ", (struct ship *)0, 1);
        switch (c) {
                case 'h':
        c = sgetch("Repair (hull, guns, rigging)? ", (struct ship *)0, 1);
        switch (c) {
                case 'h':
@@ -30,45 +66,55 @@ repair()
                        Signal("Avast heaving!", (struct ship *)0);
                        return;
        }
                        Signal("Avast heaving!", (struct ship *)0);
                        return;
        }
-       repaired = 1;
        if (++*repairs >= 3) {
        if (++*repairs >= 3) {
+               count = 2;
                switch (c) {
                switch (c) {
-               case 'h':
-                       if (ptr->hull < ptr->guns/4)
-                               Write(W_HULL, ms, 0,
-                                       ptr->hull + 2, 0, 0, 0);
-                       else
-                               c = 0;
+               case 'h': {
+                       int max = ptr->guns/4;
+                       if (ptr->hull < max) {
+                               FIX(hull, max);
+                               Write(W_HULL, ms, 0, ptr->hull, 0, 0, 0);
+                       }
                        break;
                        break;
+                       }
                case 'g':
                        if (ptr->gunL < ptr->gunR) {
                case 'g':
                        if (ptr->gunL < ptr->gunR) {
-                               if (ptr->gunL + ptr->carL < ptr->guns/5)
-                                       Write(W_GUNL, ms, 0,
-                                               ptr->gunL + 2, ptr->carL, 0, 0);
-                               else
-                                       c = 0;
-                       } else
-                               if (ptr->gunR + ptr->carR < ptr->guns/5)
-                                       Write(W_GUNR, ms, 0,
-                                               ptr->gunR + 2, ptr->carR, 0, 0);
-                               else
-                                       c = 0;
+                               int max = ptr->guns/5 - ptr->carL;
+                               if (ptr->gunL < max) {
+                                       FIX(gunL, max);
+                                       Write(W_GUNL, ms, 0, ptr->gunL,
+                                               ptr->carL, 0, 0);
+                               }
+                       } else {
+                               int max = ptr->guns/5 - ptr->carR;
+                               if (ptr->gunR < max) {
+                                       FIX(gunR, max);
+                                       Write(W_GUNR, ms, 0, ptr->gunR,
+                                               ptr->carR, 0, 0);
+                               }
+                       }
                        break;
                case 'r':
                        break;
                case 'r':
-                       if (!ptr->rig4)
-                               Write(W_RIG4, ms, 0,
-                                       ptr->rig4 + 2, 0, 0, 0);
-                       else if (!ptr->rig3)
-                               Write(W_RIG3, ms, 0, 2, 0, 0, 0);
-                       else if (!ptr->rig2)
-                               Write(W_RIG2, ms, 0, 2, 0, 0, 0);
-                       else if (ptr->rig1 < 4)
-                               Write(W_RIG1, ms, 0, 2, 0, 0, 0);
-                       else
-                               c = 0;
+#define X 2
+                       if (ptr->rig4 >= 0 && ptr->rig4 < X) {
+                               FIX(rig4, X);
+                               Write(W_RIG4, ms, 0, ptr->rig4, 0, 0, 0);
+                       }
+                       if (count && ptr->rig3 < X) {
+                               FIX(rig3, X);
+                               Write(W_RIG3, ms, 0, ptr->rig3, 0, 0, 0);
+                       }
+                       if (count && ptr->rig2 < X) {
+                               FIX(rig2, X);
+                               Write(W_RIG2, ms, 0, ptr->rig2, 0, 0, 0);
+                       }
+                       if (count && ptr->rig1 < X) {
+                               FIX(rig1, X);
+                               Write(W_RIG1, ms, 0, ptr->rig1, 0, 0, 0);
+                       }
                        break;
                }
                        break;
                }
-               if (!c) {
+               if (count == 2) {
                        Signal("Repairs completed.", (struct ship *)0);
                        *repairs = 2;
                } else {
                        Signal("Repairs completed.", (struct ship *)0);
                        *repairs = 2;
                } else {
@@ -81,6 +127,7 @@ repair()
        blockalarm();
        draw_slot();
        unblockalarm();
        blockalarm();
        draw_slot();
        unblockalarm();
+       repaired = 1;
 }
 
 turned()
 }
 
 turned()