HEAD: sfllaw/jim's patch for xplc detection broke xplc detection in
[wvapps.git] / twc / src / twctransporter.cc
blob1203cbdaf6ca9415ca845156ded929f2281c1fa9
1 #include "twcgamecontroller.h"
2 #include "twcplayerclient.h"
3 #include <strutils.h>
5 void TWCShipIf::transporter_menu_input()
7 char *line = plyrcli.getline(0);
8 if (!line)
9 return;
10 trim_string(line);
12 switch (line[0])
14 case 's':
15 case 'S':
17 TWCShip *target = plyrcli.acquire_target("Transport to which ship?",
18 "That ship is docked.");
19 if (!target)
20 return;
22 TWCTransportSelfResults res = target->transport_self(plyrcli);
24 if (res == TSAlreadyCaptained)
25 plyrcli.print("That ship already has a captain.\n");
26 else if (res != TSAccessDenied)
28 ship.set_captain(NULL);
29 plyrcli.print("You beam aboard the %s.\n", target->name());
30 if (res == TSClaimedOwnership)
31 plyrcli.print("You claim ownership of the %s.\n",
32 target->name());
33 plyrcli.set_location(target);
35 break;
38 case 'c':
39 case 'C':
41 bool beam_to_ship = true;
42 plyrcli.print("Transport cargo to or from this ship? (t/f) ");
43 line = plyrcli.continue_getline();
44 if (!line)
45 return;
46 trim_string(line);
47 if (line[0] == 'f' || line[0] == 'f')
48 beam_to_ship = false;
50 TWCShip *target = plyrcli.acquire_target(
51 WvString("Transport cargo %s which ship?",
52 beam_to_ship ? "from" : "to"),
53 "That ship is docked.");
54 if (!target)
55 return;
57 TWCShip *from_ship, *to_ship;
58 if (beam_to_ship)
60 from_ship = target;
61 to_ship = &ship;
63 else
65 from_ship = &ship;
66 to_ship = target;
69 plyrcli.display_inventory(from_ship, true);
70 plyrcli.print("Beam which commodity aboard? ");
71 bool abort = false;
72 int comm_type = plyrcli.get_int(abort);
73 if (abort || !comm_type)
74 return;
75 ship.game->log("commodity type to beam is %s\n", comm_type);
76 int max = from_ship->onboard("commodities", comm_type);
77 if (max > to_ship->free_holds())
78 max = to_ship->free_holds();
79 plyrcli.print("Beam how many aboard (max %s)? ", max);
80 int num = plyrcli.get_int(abort);
81 if (abort)
82 return;
83 if (num > max)
84 plyrcli.print("You can't transport that many.\n");
85 else if (target->sector() != ship.sector())
86 plyrcli.print("The ship has left the sector!\n");
87 else if (!to_ship->beam_cargo_aboard(from_ship, comm_type, num))
88 plyrcli.print("Cargo transportation failed.\n");
89 else
90 plyrcli.print("Cargo transferred.\n");
92 break;
95 case 'q':
96 case 'Q':
97 transporter = false;
98 break;
100 default:
101 break;
106 #if 0
107 transport_comm_result TWCPlayerClient::transport_commodities(int from,
108 int to,
109 int comm_type,
110 int number)
112 int target, actor = loccfg().key().printable().num();
113 TWCEventType event_type;
114 if (to == actor)
116 target = from;
117 event_type = CargoBeamedTo;
119 else
121 target = to;
122 event_type = CargoBeamedFrom;
125 log("transport_commodities() from [%s] to [%s] actor [%s] target [%s]\n",
126 from, to, actor, target);
127 log("number of [%s] on from [%s] is [%s] (want [%s])\n", comm_type, from,
128 cfg["ships"][from]["holds"][comm_type].getint(), number);
129 if (check_shields(target) != CSAccessGranted)
130 return TCAccessDenied;
132 log("shields are down on target\n");
133 if (cfg["ships"][from]["holds"][comm_type].getint() < number)
134 return TCNotEnough;
136 log("doing actual transport\n");
137 int holds = cfg["ships"][to]["total holds"].getint();
138 UniConf::Iter i(cfg["ships"][to]["holds"]);
139 for (i.rewind(); i.next(); )
140 holds -= i().getint();
141 if (holds < number)
142 return TCFull;
144 decr_uniconf(cfg["ships"][from]["holds"][comm_type], number);
145 incr_uniconf(cfg["ships"][to]["holds"][comm_type], number);
147 WvString beam_result("%s holds of %s.\n", number,
148 cfg["commodities"][comm_type]["name"].get());
149 msg("%s beamed to %s from %s.\n", beam_result,
150 cfg["ships"][to]["name"].get(),
151 cfg["ships"][from]["name"].get());
153 game->announce(event_type, actor, target,
154 WvString("%s beams cargo %s %s.\n",
155 loccfg()["name"].get(),
156 comm_type == CargoBeamedTo ? "from" : "to",
157 cfg["ships"][target]["name"].get()),
158 loccfg()["location"].getint());
160 return TCSuccessful;
162 #endif
165 TWCTransportSelfResults TWCShip::transport_self(TWCPlayerClient &plyrcli)
167 if (captain())
168 return TSAlreadyCaptained;
170 if (check_shields(plyrcli) == CSAccessGranted)
172 check_claim_ownership(&plyrcli.player);
173 // player client must set location itself
174 return TSSuccessful;
176 return TSAccessDenied;
180 TWCCheckShieldsResults TWCShip::check_shields(TWCPlayerClient &plyrcli)
182 if (!are_shields_up() || owner() == &plyrcli.player)
183 return CSAccessGranted;
185 if (!password())
186 return CSAccessForbidden;
188 if (password() == plyrcli.try_passwd())
189 return CSAccessGranted;
191 return CSAccessDenied;
195 TWCCheckShieldsResults TWCShipIf::check_shields(TWCShip *target)
197 TWCCheckShieldsResults res = target->check_shields(plyrcli);
198 if (res == CSAccessForbidden)
199 plyrcli.print("You don't own this ship, its shields are up, and the "
200 "owner has forbidden transports.\n");
201 else if (res != CSAccessGranted)
202 plyrcli.print("Access denied.\n");
204 return res;