]> git.pond.sub.org Git - empserver/blob - src/lib/commands/swap.c
955b990dfe80c6fd794bc9b5d8e2045bee14fa62
[empserver] / src / lib / commands / swap.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2012, 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  *  swap.c: Swap two sectors on the map
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Markus Armbruster, 2006-2010
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37
38 static void print_res(struct sctstr *);
39
40 /*
41  * Syntax: swap <SECT> <SECT>
42  */
43 int
44 swaps(void)
45 {
46     struct sctstr secta, sectb, tmp;
47     char buf[1024];
48     char *p;
49
50     if (!(p = getstarg(player->argp[1], "First sector : ", buf)) ||
51         !sarg_xy(p, &secta.sct_x, &secta.sct_y) ||
52         !getsect(secta.sct_x, secta.sct_y, &secta))
53         return RET_SYN;
54     print_res(&secta);
55     if (!(p = getstarg(player->argp[2], "Second sector : ", buf)) ||
56         !sarg_xy(p, &sectb.sct_x, &sectb.sct_y) ||
57         !getsect(sectb.sct_x, sectb.sct_y, &sectb))
58         return RET_SYN;
59     print_res(&sectb);
60     if (!confirm("Are you sure these are the two sectors you wish to swap? "))
61         return RET_FAIL;
62     if (!check_sect_ok(&secta) || !check_sect_ok(&sectb))
63         return RET_FAIL;
64     tmp = secta;
65     /* change the location of secta to that of sectb */
66     secta.sct_x = sectb.sct_x;
67     secta.sct_y = sectb.sct_y;
68     ef_set_uid(EF_SECTOR, &secta, sectb.sct_uid);
69     secta.sct_dist_x = sectb.sct_x;
70     secta.sct_dist_y = sectb.sct_y;
71     secta.sct_coastal = sectb.sct_coastal;
72     /* change the location of sectb to where secta was */
73     sectb.sct_x = tmp.sct_x;
74     sectb.sct_y = tmp.sct_y;
75     ef_set_uid(EF_SECTOR, &sectb, tmp.sct_uid);
76     sectb.sct_dist_x = tmp.sct_x;
77     sectb.sct_dist_y = tmp.sct_y;
78     sectb.sct_coastal = tmp.sct_coastal;
79     /* update coastal flag & put sectors */
80     putsect(&sectb);
81     set_coastal(&secta, sectb.sct_type, secta.sct_type);
82     putsect(&secta);
83     getsect(sectb.sct_x, sectb.sct_y, &sectb);
84     set_coastal(&sectb, secta.sct_type, sectb.sct_type);
85     putsect(&sectb);
86     pr("done\n");
87     return RET_OK;
88 }
89
90 static void
91 print_res(struct sctstr *sp)
92 {
93     pr("own   sect        eff  min gold fert oil uran\n");
94
95     pr("%3d ", sp->sct_own);
96
97     prxy("%4d,%-4d", sp->sct_x, sp->sct_y);
98     pr(" %c", dchr[sp->sct_type].d_mnem);
99     if (sp->sct_newtype != sp->sct_type)
100         pr("%c", dchr[sp->sct_newtype].d_mnem);
101     else
102         pr(" ");
103     pr("%4d%%", sp->sct_effic);
104     pr(" %4d", sp->sct_min);
105     pr("%5d", sp->sct_gmin);
106     pr("%5d", sp->sct_fertil);
107     pr("%4d", sp->sct_oil);
108     pr("%5d", sp->sct_uran);
109     pr("\n");
110 }