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