]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/improve.c
commands: Rename the command functions
[empserver] / src / lib / commands / improve.c
index e69c3feb172af3270ec18cf677f79e86da07c14d..fa135c7b9f99735817a9af4bb9419a623c2d64a3 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *
  *  Known contributors to this file:
  *     Steve McClure, 1996-2000
+ *     Markus Armbruster, 2004-2016
  */
 
 #include <config.h>
 
+#include "chance.h"
 #include "commands.h"
 
 int
-improve(void)
+c_improve(void)
 {
+    struct sctintrins *incp;
     struct sctstr sect;
     int nsect;
     struct nstr_sect nstr;
@@ -46,12 +49,8 @@ improve(void)
     int type;
     int value;
     int ovalue;
-    int maxup;
+    int maxup, i, lim;
     struct natstr *natp;
-    int lneeded;
-    int hneeded;
-    int mneeded;
-    int dneeded;
     int wanted;
 
     p = getstarg(player->argp[1],
@@ -68,8 +67,9 @@ improve(void)
     } else
        return RET_SYN;
 
-    if (!intrchr[type].in_enable) {
-       pr("%s improvement is disabled.\n", intrchr[type].in_name);
+    incp = &intrchr[type];
+    if (!incp->in_enable) {
+       pr("%s improvement is disabled.\n", incp->in_name);
        return RET_FAIL;
     }
 
@@ -88,7 +88,7 @@ improve(void)
            value = sect.sct_defense;
        sprintf(prompt, "Sector %s has a %s of %d%%.  Improve how much? ",
                xyas(sect.sct_x, sect.sct_y, player->cnum),
-               intrchr[type].in_name, value);
+               incp->in_name, value);
        p = getstarg(player->argp[3], prompt, buf);
        if (!p || !*p)
            continue;
@@ -102,60 +102,52 @@ improve(void)
            maxup = wanted;
        if (!maxup)
            continue;
-       lneeded = intrchr[type].in_lcms * maxup;
-       if (sect.sct_item[I_LCM] < lneeded) {
-           lneeded = sect.sct_item[I_LCM];
-           maxup = lneeded / intrchr[type].in_lcms;
-           if (maxup <= 0) {
-               pr("Not enough lcms in %s\n",
-                  xyas(sect.sct_x, sect.sct_y, player->cnum));
-               continue;
-           }
+
+       lim = (sect. sct_mobil - 1) * 100 / incp->in_bmobil;
+       if (lim <= 0) {
+           pr("Not enough mobility in %s\n",
+              xyas(sect.sct_x, sect.sct_y, player->cnum));
+           continue;
        }
-       hneeded = intrchr[type].in_hcms * maxup;
-       if (sect.sct_item[I_HCM] < hneeded) {
-           hneeded = sect.sct_item[I_HCM];
-           maxup = hneeded / intrchr[type].in_hcms;
-           if (maxup <= 0) {
-               pr("Not enough hcms in %s\n",
-                  xyas(sect.sct_x, sect.sct_y, player->cnum));
+       if (maxup > lim)
+           maxup = lim;
+
+       for (i = I_NONE + 1; i <= I_MAX; i++) {
+           if (!incp->in_mat[i])
                continue;
-           }
-       }
-       mneeded = intrchr[type].in_mcost * maxup;
-       if ((sect.sct_mobil - 1) < mneeded) {
-           mneeded = sect.sct_mobil - 1;
-           if (mneeded < 0)
-               mneeded = 0;
-           maxup = mneeded / intrchr[type].in_mcost;
-           if (maxup <= 0) {
-               pr("Not enough mobility in %s\n",
+           lim = sect.sct_item[i] * 100 / incp->in_mat[i];
+           if (lim <= 0) {
+               pr("Not enough %s in %s\n",
+                  ichr[i].i_name,
                   xyas(sect.sct_x, sect.sct_y, player->cnum));
-               continue;
            }
+           if (maxup > lim)
+               maxup = lim;
        }
-       dneeded = intrchr[type].in_dcost * maxup;
+       if (maxup <= 0)
+           continue;
+
        natp = getnatp(player->cnum);
-       if (player->dolcost + dneeded > natp->nat_money) {
+       lim = (natp->nat_money - player->dolcost) * 100 / incp->in_cost;
+       if (lim <= 0) {
            pr("Not enough money left to improve %s by %d%%\n",
               xyas(sect.sct_x, sect.sct_y, player->cnum), maxup);
            break;
        }
-       lneeded = intrchr[type].in_lcms * maxup;
-       hneeded = intrchr[type].in_hcms * maxup;
-       mneeded = intrchr[type].in_mcost * maxup;
-       dneeded = intrchr[type].in_dcost * maxup;
-       player->dolcost += dneeded;
-       sect.sct_item[I_LCM] -= lneeded;
-       sect.sct_item[I_HCM] -= hneeded;
-       sect.sct_mobil -= mneeded;
+       if (maxup > lim)
+           maxup = lim;
+
+       for (i = I_NONE + 1; i <= I_MAX; i++)
+           sect.sct_item[i] -= roundavg(maxup * incp->in_mat[i] / 100.0);
+       sect.sct_mobil -= roundavg(maxup * incp->in_bmobil / 100.0);
+       player->dolcost += maxup * incp->in_cost / 100.0;
        ovalue = value;
        value += maxup;
        if (CANT_HAPPEN(value > 100))
            value = 100;
        pr("Sector %s %s increased from %d%% to %d%%\n",
           xyas(sect.sct_x, sect.sct_y, player->cnum),
-          intrchr[type].in_name, ovalue, value);
+          incp->in_name, ovalue, value);
        if (type == INT_ROAD)
            sect.sct_road = value;
        else if (type == INT_RAIL)