]> git.pond.sub.org Git - empserver/blob - src/lib/commands/atta.c
99e87e1866ce2ab98224aae3ceb439703e3c4ce6
[empserver] / src / lib / commands / atta.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  atta.c: Attack another sector
29  *
30  *  Known contributors to this file:
31  *     Dave Pare
32  *     Ken Stevens, 1995
33  *     Steve McClure, 1996
34  */
35
36 #include <config.h>
37
38 #include "combat.h"
39 #include "commands.h"
40 #include "land.h"
41 #include "mission.h"
42 #include "path.h"
43 #include "ship.h"
44
45 int
46 atta(void)
47 {
48     struct combat off[6];       /* attacking sectors */
49     struct combat def[1];       /* defending sector */
50     coord newx, newy;
51     struct sctstr sect;
52     int fort_sup, ship_sup, land_sup, plane_sup;
53     struct emp_qelem olist;     /* attacking units */
54     struct emp_qelem dlist;     /* defending units */
55     int ototal;                 /* total attacking strength */
56     int a_engineer = 0;         /* attacker engineers are present */
57     int a_spy = 0;              /* the best attacker scout */
58     double osupport = 1.0;      /* attack support */
59     double dsupport = 1.0;      /* defense support */
60     int last, n;
61     char *p;
62     char buf[1024];
63     int rel;
64
65     att_combat_init(def, EF_SECTOR);
66     /*
67      * Collect input from the attacker
68      */
69
70     /* What are we attacking? */
71
72     if (!(p = getstarg(player->argp[1], "Sector :  ", buf)))
73         return RET_SYN;
74     if (!sarg_xy(p, &def->x, &def->y))
75         return RET_SYN;
76     if (att_abort(A_ATTACK, NULL, def))
77         return RET_FAIL;
78
79     /* Show what we're attacking, and check treaties */
80
81     if (att_show(def))
82         return RET_FAIL;
83
84     /* Ask about offensive support */
85
86     att_ask_support(2, &fort_sup, &ship_sup, &land_sup, &plane_sup);
87     if (att_abort(A_ATTACK, NULL, def)) {
88         att_empty_attack(A_ATTACK, 0, def);
89         return RET_OK;
90     }
91
92     /* initialize the off[] array */
93
94     for (n = 0, last = -1; n < 6; ++n) {        /* Directions */
95         newx = def->x + diroff[n + 1][0];
96         newy = def->y + diroff[n + 1][1];
97         getsect(newx, newy, &sect);     /* incase cross world boundary */
98         rel = getrel(getnatp(sect.sct_own), player->cnum);
99         if (!player->owner && rel != ALLIED)
100             continue;
101         att_combat_init(&off[++last], EF_SECTOR);
102         off[last].x = sect.sct_x;
103         off[last].y = sect.sct_y;
104     }
105     off->last = last;
106
107     /* Ask the player what he wants to attack with */
108
109     att_ask_offense(A_ATTACK, off, def, &olist, &a_spy, &a_engineer);
110     if (att_abort(A_ATTACK, off, def)) {
111         pr("Attack aborted\n");
112         att_empty_attack(A_ATTACK, 0, def);
113         return att_free_lists(&olist, NULL);
114     }
115
116     ototal = att_get_offense(A_ATTACK, off, &olist, def);
117     if (att_abort(A_ATTACK, off, def)) {
118         pr("Attack aborted\n");
119         att_empty_attack(A_ATTACK, 0, def);
120         return att_free_lists(&olist, NULL);
121     }
122
123     /*
124      * We have now got all the answers from the attacker.  From this point
125      * forward, we can assume that this battle is the _only_ thing
126      * happening in the game.
127      */
128
129     /* Get the real defense */
130
131     att_get_defense(&olist, def, &dlist, a_spy, ototal);
132
133     /* Get attacker and defender support */
134
135     att_get_support(A_ATTACK, fort_sup, ship_sup, land_sup, plane_sup,
136                     &olist, off, &dlist, def, &osupport, &dsupport,
137                     a_engineer);
138     if (att_abort(A_ATTACK, off, def)) {
139         pr("Attack aborted\n");
140         att_empty_attack(A_ATTACK, 0, def);
141         return att_free_lists(&olist, &dlist);
142     }
143
144     /*
145      * Death, carnage, and destruction.
146      */
147
148     att_fight(A_ATTACK, off, &olist, osupport, def, &dlist, dsupport);
149
150     return RET_OK;
151 }