]> git.pond.sub.org Git - empserver/blob - src/lib/commands/atta.c
Update copyright notice.
[empserver] / src / lib / commands / atta.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
37 #include "player.h"
38 #include "file.h"
39 #include "sect.h"
40 #include "path.h"
41 #include "nat.h"
42 #include "xy.h"
43 #include "land.h"
44 #include "nsc.h"
45 #include "mission.h"
46 #include "ship.h"
47 #include "combat.h"
48 #include "commands.h"
49
50 int
51 atta(void)
52 {
53     struct combat off[6];       /* attacking sectors */
54     struct combat def[1];       /* defending sector */
55     coord newx, newy;
56     struct sctstr sect;
57     int fort_sup, ship_sup, land_sup, plane_sup;
58     struct emp_qelem olist;     /* attacking units */
59     struct emp_qelem dlist;     /* defending units */
60     int ototal;                 /* total attacking strength */
61     int a_engineer = 0;         /* attacker engineers are present */
62     int a_spy = 0;              /* the best attacker scout */
63     double osupport = 1.0;      /* attack support */
64     double dsupport = 1.0;      /* defense support */
65     int last, n;
66     s_char *p;
67     s_char buf[1024];
68     int rel;
69
70     att_combat_init(def, EF_SECTOR);
71     /*
72      * Collect input from the attacker
73      */
74
75     /* What are we attacking? */
76
77     if (!(p = getstarg(player->argp[1], "Sector :  ", buf)))
78         return RET_SYN;
79     if (!sarg_xy(p, &def->x, &def->y))
80         return RET_SYN;
81     if (att_abort(A_ATTACK, 0, def))
82         return RET_FAIL;
83
84     /* Show what we're attacking, and check treaties */
85
86     if (att_show(def))
87         return RET_FAIL;
88
89     /* Ask about offensive support */
90
91     att_ask_support(2, &fort_sup, &ship_sup, &land_sup, &plane_sup);
92     if (att_abort(A_ATTACK, 0, def)) {
93         att_empty_attack(A_ATTACK, 0, def);
94         return RET_OK;
95     }
96
97     /* initialize the off[] array */
98
99     for (n = 0, last = -1; n < 6; ++n) {        /* Directions */
100         newx = def->x + diroff[n + 1][0];
101         newy = def->y + diroff[n + 1][1];
102         getsect(newx, newy, &sect);     /* incase cross world boundary */
103         rel = getrel(getnatp(sect.sct_own), player->cnum);
104         if (!player->owner && rel != ALLIED)
105             continue;
106         att_combat_init(&off[++last], EF_SECTOR);
107         off[last].x = sect.sct_x;
108         off[last].y = sect.sct_y;
109     }
110     off->last = last;
111
112     /* Ask the player what he wants to attack with */
113
114     att_ask_offense(A_ATTACK, off, def, &olist, &a_spy, &a_engineer);
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, 0);
119     }
120
121     /*
122      * Estimate the defense strength and give the player a chance to abort
123      */
124
125     ototal = att_estimate_defense(A_ATTACK, off, &olist, def, a_spy);
126     if (att_abort(A_ATTACK, off, def)) {
127         pr("Attack aborted\n");
128         att_empty_attack(A_ATTACK, 0, def);
129         return att_free_lists(&olist, 0);
130     }
131
132     /*
133      * We have now got all the answers from the attacker.  From this point
134      * forward, we can assume that this battle is the _only_ thing
135      * happening in the game.
136      */
137
138     /* Get the real defense */
139
140     att_get_defense(&olist, def, &dlist, a_spy, ototal);
141
142     /* Get attacker and defender support */
143
144     att_get_support(A_ATTACK, fort_sup, ship_sup, land_sup, plane_sup,
145                     &olist, off, &dlist, def, &osupport, &dsupport,
146                     a_engineer);
147     if (att_abort(A_ATTACK, off, def)) {
148         pr("Attack aborted\n");
149         att_empty_attack(A_ATTACK, 0, def);
150         return att_free_lists(&olist, &dlist);
151     }
152
153     /*
154      * Death, carnage, and destruction.
155      */
156
157     att_fight(A_ATTACK, off, &olist, osupport, def, &dlist, dsupport);
158
159     return RET_OK;
160 }