| 1 |
/************************************************************************ |
| 2 |
* AIPlayer.c Code for dopewars computer players * |
| 3 |
* Copyright (C) 1998-2022 Ben Webb * |
| 4 |
* Email: [email protected] * |
| 5 |
* WWW: https://dopewars.sourceforge.io/ * |
| 6 |
* * |
| 7 |
* This program is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU General Public License * |
| 9 |
* as published by the Free Software Foundation; either version 2 * |
| 10 |
* of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This program is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU General Public License * |
| 18 |
* along with this program; if not, write to the Free Software * |
| 19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, * |
| 20 |
* MA 02111-1307, USA. * |
| 21 |
************************************************************************/ |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
#ifdef HAVE_UNISTD_H |
| 26 |
#include <unistd.h> |
| 27 |
#endif |
| 28 |
#include <stdlib.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <glib.h> |
| 32 |
#include "configfile.h" |
| 33 |
#include "dopewars.h" |
| 34 |
#include "message.h" |
| 35 |
#include "nls.h" |
| 36 |
#include "tstring.h" |
| 37 |
#include "util.h" |
| 38 |
#include "AIPlayer.h" |
| 39 |
|
| 40 |
#ifdef NETWORKING |
| 41 |
static int HandleAIMessage(char *Message, Player *AIPlay); |
| 42 |
static void PrintAIMessage(char *Text); |
| 43 |
static void AIDealDrugs(Player *AIPlay); |
| 44 |
static void AIJet(Player *AIPlay); |
| 45 |
static void AIGunShop(Player *AIPlay); |
| 46 |
static void AIPayLoan(Player *AIPlay); |
| 47 |
static void AISendRandomMessage(Player *AIPlay); |
| 48 |
static void AISetName(Player *AIPlay); |
| 49 |
static void AIHandleQuestion(char *Data, AICode AI, Player *AIPlay, |
| 50 |
Player *From); |
| 51 |
|
| 52 |
#define MINSAFECASH 300 |
| 53 |
#define MINSAFEHEALTH 140 |
| 54 |
|
| 55 |
/* Reserve some space for picking up new guns */ |
| 56 |
#define SPACERESERVE 10 |
| 57 |
|
| 58 |
/* |
| 59 |
* Locations of the loan shark, bank, gun shop and pub |
| 60 |
* Note: these are not the same as the global variables |
| 61 |
* LoanSharkLoc, BankLoc, GunShopLoc and RoughPubLoc, |
| 62 |
* which are set locally. The remote server could |
| 63 |
* have different locations set, and the AI must work |
| 64 |
* out where these locations are for itself. |
| 65 |
*/ |
| 66 |
int RealLoanShark, RealBank, RealGunShop, RealPub; |
| 67 |
|
| 68 |
static void AIConnectFailed(NetworkBuffer *netbuf) |
| 69 |
{ |
| 70 |
GString *errstr; |
| 71 |
|
| 72 |
errstr = g_string_new(_("Connection closed by remote host")); |
| 73 |
if (netbuf->error) |
| 74 |
g_string_assign_error(errstr, netbuf->error); |
| 75 |
g_log(NULL, G_LOG_LEVEL_CRITICAL, |
| 76 |
_("Could not connect to dopewars server\n(%s)\n" |
| 77 |
"AI Player terminating abnormally."), errstr->str); |
| 78 |
g_string_free(errstr, TRUE); |
| 79 |
} |
| 80 |
|
| 81 |
static void AIStartGame(Player *AIPlay) |
| 82 |
{ |
| 83 |
Client = Network = TRUE; |
| 84 |
InitAbilities(AIPlay); |
| 85 |
SetAbility(AIPlay, A_DONEFIGHT, FALSE); |
| 86 |
SendAbilities(AIPlay); |
| 87 |
|
| 88 |
AISetName(AIPlay); |
| 89 |
g_message(_("Connection established\n")); |
| 90 |
} |
| 91 |
|
| 92 |
static void DisplayConnectStatus(NetworkBuffer *netbuf, NBStatus oldstatus, |
| 93 |
NBSocksStatus oldsocks) |
| 94 |
{ |
| 95 |
NBStatus status; |
| 96 |
NBSocksStatus sockstat; |
| 97 |
|
| 98 |
status = netbuf->status; |
| 99 |
sockstat = netbuf->sockstat; |
| 100 |
if (oldstatus == status && oldsocks == sockstat) |
| 101 |
return; |
| 102 |
|
| 103 |
switch (status) { |
| 104 |
case NBS_PRECONNECT: |
| 105 |
break; |
| 106 |
case NBS_SOCKSCONNECT: |
| 107 |
switch (sockstat) { |
| 108 |
case NBSS_METHODS: |
| 109 |
g_print(_("Connected to SOCKS server %s...\n"), Socks.name); |
| 110 |
break; |
| 111 |
case NBSS_USERPASSWD: |
| 112 |
g_print(_("Authenticating with SOCKS server\n")); |
| 113 |
break; |
| 114 |
case NBSS_CONNECT: |
| 115 |
g_print(_("Asking SOCKS for connect to %s...\n"), ServerName); |
| 116 |
break; |
| 117 |
} |
| 118 |
break; |
| 119 |
case NBS_CONNECTED: |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
static void NetBufAuth(NetworkBuffer *netbuf, gpointer data) |
| 125 |
{ |
| 126 |
g_print(_("Using Socks.Auth.User and Socks.Auth.Password " |
| 127 |
"for SOCKS5 authentication\n")); |
| 128 |
SendSocks5UserPasswd(netbuf, Socks.authuser, Socks.authpassword); |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* Main loop for AI players. Connects to server, plays game, |
| 133 |
* and then disconnects. |
| 134 |
*/ |
| 135 |
void AIPlayerLoop(struct CMDLINE *cmdline) |
| 136 |
{ |
| 137 |
GString *errstr; |
| 138 |
gchar *msg; |
| 139 |
Player *AIPlay; |
| 140 |
fd_set readfs, writefs; |
| 141 |
gboolean DoneOK, QuitRequest, datawaiting; |
| 142 |
int MaxSock; |
| 143 |
NBStatus oldstatus; |
| 144 |
NBSocksStatus oldsocks; |
| 145 |
NetworkBuffer *netbuf; |
| 146 |
|
| 147 |
InitConfiguration(cmdline); |
| 148 |
|
| 149 |
errstr = g_string_new(""); |
| 150 |
AIPlay = g_new(Player, 1); |
| 151 |
|
| 152 |
FirstClient = AddPlayer(0, AIPlay, FirstClient); |
| 153 |
g_message(_("AI Player started; attempting to contact " |
| 154 |
"server at %s:%d..."), ServerName, Port); |
| 155 |
|
| 156 |
/* Forget where the "special" locations are */ |
| 157 |
RealLoanShark = RealBank = RealGunShop = RealPub = -1; |
| 158 |
|
| 159 |
netbuf = &AIPlay->NetBuf; |
| 160 |
oldstatus = netbuf->status; |
| 161 |
oldsocks = netbuf->sockstat; |
| 162 |
|
| 163 |
if (!StartNetworkBufferConnect(netbuf, NULL, ServerName, Port)) { |
| 164 |
AIConnectFailed(netbuf); |
| 165 |
return; |
| 166 |
} else { |
| 167 |
SetNetworkBufferUserPasswdFunc(netbuf, NetBufAuth, NULL); |
| 168 |
if (netbuf->status == NBS_CONNECTED) { |
| 169 |
AIStartGame(AIPlay); |
| 170 |
} else { |
| 171 |
DisplayConnectStatus(netbuf, oldstatus, oldsocks); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
while (1) { |
| 176 |
FD_ZERO(&readfs); |
| 177 |
FD_ZERO(&writefs); |
| 178 |
MaxSock = 0; |
| 179 |
|
| 180 |
SetSelectForNetworkBuffer(netbuf, &readfs, &writefs, NULL, &MaxSock); |
| 181 |
|
| 182 |
oldstatus = netbuf->status; |
| 183 |
oldsocks = netbuf->sockstat; |
| 184 |
if (bselect(MaxSock, &readfs, &writefs, NULL, NULL) == -1) { |
| 185 |
if (errno == EINTR) |
| 186 |
continue; |
| 187 |
printf("Error in select\n"); |
| 188 |
exit(EXIT_FAILURE); |
| 189 |
} |
| 190 |
|
| 191 |
datawaiting = |
| 192 |
RespondToSelect(netbuf, &readfs, &writefs, NULL, &DoneOK); |
| 193 |
|
| 194 |
if (oldstatus != NBS_CONNECTED && |
| 195 |
(netbuf->status == NBS_CONNECTED || !DoneOK)) { |
| 196 |
if (DoneOK) |
| 197 |
AIStartGame(AIPlay); |
| 198 |
else { |
| 199 |
AIConnectFailed(netbuf); |
| 200 |
break; |
| 201 |
} |
| 202 |
} else if (netbuf->status != NBS_CONNECTED) { |
| 203 |
DisplayConnectStatus(netbuf, oldstatus, oldsocks); |
| 204 |
} |
| 205 |
if (datawaiting && netbuf->status == NBS_CONNECTED) { |
| 206 |
QuitRequest = FALSE; |
| 207 |
while ((msg = GetWaitingPlayerMessage(AIPlay)) != NULL) { |
| 208 |
if (HandleAIMessage(msg, AIPlay)) { |
| 209 |
QuitRequest = TRUE; |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
if (QuitRequest) { |
| 214 |
g_print(_("AI Player terminated OK.\n")); |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
if (!DoneOK) { |
| 219 |
g_print(_("Connection to server lost!\n")); |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
ShutdownNetwork(AIPlay); |
| 224 |
g_string_free(errstr, TRUE); |
| 225 |
FirstClient = RemovePlayer(AIPlay, FirstClient); |
| 226 |
} |
| 227 |
|
| 228 |
/* |
| 229 |
* Chooses a random name for the AI player, and informs the server |
| 230 |
*/ |
| 231 |
void AISetName(Player *AIPlay) |
| 232 |
{ |
| 233 |
char *AINames[] = { |
| 234 |
"Chip", "Dopey", "Al", "Dan", "Bob", "Fred", "Bert", "Jim" |
| 235 |
}; |
| 236 |
const gint NumNames = sizeof(AINames) / sizeof(AINames[0]); |
| 237 |
gchar *text; |
| 238 |
|
| 239 |
text = g_strdup_printf("AI) %s", AINames[brandom(0, NumNames)]); |
| 240 |
SetPlayerName(AIPlay, text); |
| 241 |
g_free(text); |
| 242 |
SendNullClientMessage(AIPlay, C_NONE, C_NAME, NULL, |
| 243 |
GetPlayerName(AIPlay)); |
| 244 |
g_print(_("Using name %s\n"), GetPlayerName(AIPlay)); |
| 245 |
} |
| 246 |
|
| 247 |
/* |
| 248 |
* Returns TRUE if it would be prudent to run away... |
| 249 |
*/ |
| 250 |
gboolean ShouldRun(Player *AIPlay) |
| 251 |
{ |
| 252 |
gint TotalHealth; |
| 253 |
|
| 254 |
if (TotalGunsCarried(AIPlay) == 0) |
| 255 |
return TRUE; |
| 256 |
|
| 257 |
TotalHealth = AIPlay->Health + AIPlay->Bitches.Carried * 100; |
| 258 |
return (TotalHealth < MINSAFEHEALTH); |
| 259 |
} |
| 260 |
|
| 261 |
/* |
| 262 |
* Decodes the fighting-related message "Msg", and then decides whether |
| 263 |
* to stand or run... |
| 264 |
*/ |
| 265 |
static void HandleCombat(Player *AIPlay, gchar *Msg) |
| 266 |
{ |
| 267 |
gchar *text; |
| 268 |
gchar *AttackName, *DefendName, *BitchName; |
| 269 |
FightPoint fp; |
| 270 |
int DefendHealth, DefendBitches, BitchesKilled, ArmPercent; |
| 271 |
gboolean CanRunHere, Loot, CanFire; |
| 272 |
|
| 273 |
if (HaveAbility(AIPlay, A_NEWFIGHT)) { |
| 274 |
ReceiveFightMessage(Msg, &AttackName, &DefendName, &DefendHealth, |
| 275 |
&DefendBitches, &BitchName, &BitchesKilled, |
| 276 |
&ArmPercent, &fp, &CanRunHere, &Loot, |
| 277 |
&CanFire, &text); |
| 278 |
} else { |
| 279 |
text = Msg; |
| 280 |
if (AIPlay->Flags & FIGHTING) |
| 281 |
fp = F_MSG; |
| 282 |
else |
| 283 |
fp = F_LASTLEAVE; |
| 284 |
CanFire = (AIPlay->Flags & CANSHOOT); |
| 285 |
CanRunHere = FALSE; |
| 286 |
} |
| 287 |
PrintAIMessage(text); |
| 288 |
|
| 289 |
if (ShouldRun(AIPlay)) { |
| 290 |
if (CanRunHere) { |
| 291 |
SendClientMessage(AIPlay, C_NONE, C_FIGHTACT, NULL, "R"); |
| 292 |
} else { |
| 293 |
AIDealDrugs(AIPlay); |
| 294 |
AIJet(AIPlay); |
| 295 |
} |
| 296 |
} else if (fp == F_LASTLEAVE) { |
| 297 |
AIJet(AIPlay); |
| 298 |
} else { |
| 299 |
SendClientMessage(AIPlay, C_NONE, C_FIGHTACT, NULL, "F"); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
/* |
| 304 |
* Performs appropriate processing on an incoming network message |
| 305 |
* "Message" for AI player "AIPlay". Returns 1 if the game should |
| 306 |
* be ended as a result, 0 otherwise. |
| 307 |
*/ |
| 308 |
int HandleAIMessage(char *Message, Player *AIPlay) |
| 309 |
{ |
| 310 |
char *Data, WasFighting; |
| 311 |
AICode AI; |
| 312 |
MsgCode Code; |
| 313 |
Player *From, *tmp; |
| 314 |
GSList *list; |
| 315 |
struct timeval tv; |
| 316 |
gboolean Handled; |
| 317 |
|
| 318 |
if (ProcessMessage(Message, AIPlay, &From, &AI, &Code, |
| 319 |
&Data, FirstClient) == -1) { |
| 320 |
g_warning("Bad network message. Oops."); |
| 321 |
return 0; |
| 322 |
} |
| 323 |
Handled = HandleGenericClientMessage(From, AI, Code, AIPlay, Data, NULL); |
| 324 |
switch (Code) { |
| 325 |
case C_ENDLIST: |
| 326 |
g_print(_("Players in this game:-\n")); |
| 327 |
for (list = FirstClient; list; list = g_slist_next(list)) { |
| 328 |
tmp = (Player *)list->data; |
| 329 |
g_print(" %s\n", GetPlayerName(tmp)); |
| 330 |
} |
| 331 |
break; |
| 332 |
case C_NEWNAME: |
| 333 |
AISetName(AIPlay); |
| 334 |
break; |
| 335 |
case C_FIGHTPRINT: |
| 336 |
HandleCombat(AIPlay, Data); |
| 337 |
break; |
| 338 |
case C_PRINTMESSAGE: |
| 339 |
PrintAIMessage(Data); |
| 340 |
if (AI == C_MISSFIGHT || strncmp(Data, "Too late", 8) == 0) { |
| 341 |
AIJet(AIPlay); |
| 342 |
} |
| 343 |
break; |
| 344 |
case C_MSG: |
| 345 |
g_print("%s: %s\n", GetPlayerName(From), Data); |
| 346 |
break; |
| 347 |
case C_MSGTO: |
| 348 |
g_print("%s->%s: %s\n", GetPlayerName(From), GetPlayerName(AIPlay), |
| 349 |
Data); |
| 350 |
break; |
| 351 |
case C_JOIN: |
| 352 |
g_print(_("%s joins the game.\n"), Data); |
| 353 |
break; |
| 354 |
case C_LEAVE: |
| 355 |
if (From != &Noone) { |
| 356 |
g_print(_("%s has left the game.\n"), Data); |
| 357 |
} |
| 358 |
break; |
| 359 |
case C_SUBWAYFLASH: |
| 360 |
dpg_print(_("Jetting to %tde with %P cash and %P debt\n"), |
| 361 |
Location[AIPlay->IsAt].Name, AIPlay->Cash, |
| 362 |
AIPlay->Debt); |
| 363 |
/* Use bselect rather than sleep, as this is portable to Win32 */ |
| 364 |
tv.tv_sec = AITurnPause; |
| 365 |
tv.tv_usec = 0; |
| 366 |
bselect(0, NULL, NULL, NULL, &tv); |
| 367 |
if (brandom(0, 100) < 10) |
| 368 |
AISendRandomMessage(AIPlay); |
| 369 |
break; |
| 370 |
case C_UPDATE: |
| 371 |
WasFighting = FALSE; |
| 372 |
if (From == &Noone) { |
| 373 |
if (AIPlay->Flags & FIGHTING) |
| 374 |
WasFighting = TRUE; |
| 375 |
ReceivePlayerData(AIPlay, Data, AIPlay); |
| 376 |
} else { |
| 377 |
ReceivePlayerData(AIPlay, Data, From); /* spy reports */ |
| 378 |
} |
| 379 |
if (!(AIPlay->Flags & FIGHTING) && WasFighting) { |
| 380 |
AIDealDrugs(AIPlay); |
| 381 |
AIJet(AIPlay); |
| 382 |
} |
| 383 |
if (AIPlay->Health == 0) { |
| 384 |
g_print(_("AI Player killed. Terminating normally.\n")); |
| 385 |
return 1; |
| 386 |
} |
| 387 |
break; |
| 388 |
case C_DRUGHERE: |
| 389 |
AIDealDrugs(AIPlay); |
| 390 |
AIJet(AIPlay); |
| 391 |
break; |
| 392 |
case C_GUNSHOP: |
| 393 |
AIGunShop(AIPlay); |
| 394 |
break; |
| 395 |
case C_LOANSHARK: |
| 396 |
AIPayLoan(AIPlay); |
| 397 |
break; |
| 398 |
case C_QUESTION: |
| 399 |
AIHandleQuestion(Data, AI, AIPlay, From); |
| 400 |
break; |
| 401 |
case C_HISCORE: |
| 402 |
case C_STARTHISCORE: |
| 403 |
break; |
| 404 |
case C_ENDHISCORE: |
| 405 |
g_print(_("Game time is up. Leaving game.\n")); |
| 406 |
return 1; |
| 407 |
case C_PUSH: |
| 408 |
g_print(_("AI Player pushed from the server.\n")); |
| 409 |
return 1; |
| 410 |
case C_QUIT: |
| 411 |
g_print(_("The server has terminated.\n")); |
| 412 |
return 1; |
| 413 |
default: |
| 414 |
if (!Handled) |
| 415 |
g_message("%s^%c^%s%s\n", GetPlayerName(From), Code, |
| 416 |
GetPlayerName(AIPlay), Data); |
| 417 |
break; |
| 418 |
} |
| 419 |
return 0; |
| 420 |
} |
| 421 |
|
| 422 |
/* |
| 423 |
* Prints a message received via a printmessage or question |
| 424 |
* network message, stored in "Text". |
| 425 |
*/ |
| 426 |
void PrintAIMessage(char *Text) |
| 427 |
{ |
| 428 |
unsigned i; |
| 429 |
gboolean SomeText = FALSE; |
| 430 |
|
| 431 |
for (i = 0; i < strlen(Text); i++) { |
| 432 |
if (Text[i] == '^') { |
| 433 |
if (SomeText) |
| 434 |
putchar('\n'); |
| 435 |
} else { |
| 436 |
putchar(Text[i]); |
| 437 |
SomeText = TRUE; |
| 438 |
} |
| 439 |
} |
| 440 |
putchar('\n'); |
| 441 |
} |
| 442 |
|
| 443 |
/* |
| 444 |
* Buys and sell drugs for AI player "AIPlay". |
| 445 |
*/ |
| 446 |
void AIDealDrugs(Player *AIPlay) |
| 447 |
{ |
| 448 |
price_t *Profit, MaxProfit; |
| 449 |
gchar *text; |
| 450 |
int i, LastHighest, Highest, Num, MinProfit; |
| 451 |
Profit = g_new(price_t, NumDrug); |
| 452 |
|
| 453 |
for (i = 0; i < NumDrug; i++) { |
| 454 |
Profit[i] = |
| 455 |
AIPlay->Drugs[i].Price - (Drug[i].MaxPrice + Drug[i].MinPrice) / 2; |
| 456 |
} |
| 457 |
MinProfit = 0; |
| 458 |
for (i = 0; i < NumDrug; i++) |
| 459 |
if (Profit[i] < MinProfit) |
| 460 |
MinProfit = Profit[i]; |
| 461 |
MinProfit--; |
| 462 |
for (i = 0; i < NumDrug; i++) |
| 463 |
if (Profit[i] < 0) |
| 464 |
Profit[i] = MinProfit - Profit[i]; |
| 465 |
LastHighest = -1; |
| 466 |
do { |
| 467 |
MaxProfit = MinProfit; |
| 468 |
Highest = -1; |
| 469 |
for (i = 0; i < NumDrug; i++) { |
| 470 |
if (Profit[i] > MaxProfit && i != LastHighest && |
| 471 |
(LastHighest == -1 || Profit[LastHighest] > Profit[i])) { |
| 472 |
Highest = i; |
| 473 |
MaxProfit = Profit[i]; |
| 474 |
} |
| 475 |
} |
| 476 |
LastHighest = Highest; |
| 477 |
if (Highest >= 0) { |
| 478 |
Num = AIPlay->Drugs[Highest].Carried; |
| 479 |
if (MaxProfit > 0 && Num > 0) { |
| 480 |
dpg_print(_("Selling %d %tde at %P\n"), Num, Drug[Highest].Name, |
| 481 |
AIPlay->Drugs[Highest].Price); |
| 482 |
AIPlay->CoatSize += Num; |
| 483 |
AIPlay->Cash += Num * AIPlay->Drugs[Highest].Price; |
| 484 |
text = g_strdup_printf("drug^%d^%d", Highest, -Num); |
| 485 |
SendClientMessage(AIPlay, C_NONE, C_BUYOBJECT, NULL, text); |
| 486 |
g_free(text); |
| 487 |
} |
| 488 |
if (AIPlay->Drugs[Highest].Price != 0 && |
| 489 |
AIPlay->CoatSize > SPACERESERVE) { |
| 490 |
Num = AIPlay->Cash / AIPlay->Drugs[Highest].Price; |
| 491 |
if (Num > AIPlay->CoatSize - SPACERESERVE) { |
| 492 |
Num = AIPlay->CoatSize - SPACERESERVE; |
| 493 |
} |
| 494 |
if (MaxProfit < 0 && Num > 0) { |
| 495 |
dpg_print(_("Buying %d %tde at %P\n"), Num, Drug[Highest].Name, |
| 496 |
AIPlay->Drugs[Highest].Price); |
| 497 |
text = g_strdup_printf("drug^%d^%d", Highest, Num); |
| 498 |
AIPlay->CoatSize -= Num; |
| 499 |
AIPlay->Cash -= Num * AIPlay->Drugs[Highest].Price; |
| 500 |
SendClientMessage(AIPlay, C_NONE, C_BUYOBJECT, NULL, text); |
| 501 |
g_free(text); |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
} while (Highest >= 0); |
| 506 |
g_free(Profit); |
| 507 |
} |
| 508 |
|
| 509 |
/* |
| 510 |
* Handles a visit to the gun shop by AI player "AIPlay". |
| 511 |
*/ |
| 512 |
void AIGunShop(Player *AIPlay) |
| 513 |
{ |
| 514 |
int i; |
| 515 |
int Bought; |
| 516 |
gchar *text; |
| 517 |
|
| 518 |
do { |
| 519 |
Bought = 0; |
| 520 |
for (i = 0; i < NumGun; i++) { |
| 521 |
if (TotalGunsCarried(AIPlay) < AIPlay->Bitches.Carried + 2 && |
| 522 |
Gun[i].Space <= AIPlay->CoatSize && |
| 523 |
Gun[i].Price <= AIPlay->Cash - MINSAFECASH) { |
| 524 |
AIPlay->Cash -= Gun[i].Price; |
| 525 |
AIPlay->CoatSize -= Gun[i].Space; |
| 526 |
AIPlay->Guns[i].Carried++; |
| 527 |
Bought++; |
| 528 |
dpg_print(_("Buying a %tde for %P at the gun shop\n"), |
| 529 |
Gun[i].Name, Gun[i].Price); |
| 530 |
text = g_strdup_printf("gun^%d^1", i); |
| 531 |
SendClientMessage(AIPlay, C_NONE, C_BUYOBJECT, NULL, text); |
| 532 |
g_free(text); |
| 533 |
} |
| 534 |
} |
| 535 |
} while (Bought); |
| 536 |
SendClientMessage(AIPlay, C_NONE, C_DONE, NULL, NULL); |
| 537 |
} |
| 538 |
|
| 539 |
/* |
| 540 |
* Decides on a new game location for AI player "AIPlay" and jets there. |
| 541 |
*/ |
| 542 |
void AIJet(Player *AIPlay) |
| 543 |
{ |
| 544 |
int NewLocation; |
| 545 |
char text[40]; |
| 546 |
|
| 547 |
if (!AIPlay) |
| 548 |
return; |
| 549 |
NewLocation = AIPlay->IsAt; |
| 550 |
if (RealLoanShark >= 0 |
| 551 |
&& AIPlay->Cash > (price_t)((float)AIPlay->Debt * 1.2)) { |
| 552 |
NewLocation = RealLoanShark; |
| 553 |
} else if (RealPub >= 0 && brandom(0, 100) < 30 |
| 554 |
&& AIPlay->Cash > MINSAFECASH * 10) { |
| 555 |
NewLocation = RealPub; |
| 556 |
} else if (RealGunShop >= 0 && brandom(0, 100) < 70 && |
| 557 |
TotalGunsCarried(AIPlay) < AIPlay->Bitches.Carried + 2 && |
| 558 |
AIPlay->Cash > MINSAFECASH * 5) { |
| 559 |
NewLocation = RealGunShop; |
| 560 |
} |
| 561 |
while (NewLocation == AIPlay->IsAt) |
| 562 |
NewLocation = brandom(0, NumLocation); |
| 563 |
sprintf(text, "%d", NewLocation); |
| 564 |
SendClientMessage(AIPlay, C_NONE, C_REQUESTJET, NULL, text); |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
* Pays off the loan of AI player "AIPlay" if this doesn't leave |
| 569 |
* the player with insufficient funds for further dealing. |
| 570 |
*/ |
| 571 |
void AIPayLoan(Player *AIPlay) |
| 572 |
{ |
| 573 |
gchar *prstr; |
| 574 |
|
| 575 |
if (AIPlay->Cash - AIPlay->Debt >= MINSAFECASH) { |
| 576 |
prstr = pricetostr(AIPlay->Debt); |
| 577 |
SendClientMessage(AIPlay, C_NONE, C_PAYLOAN, NULL, prstr); |
| 578 |
g_free(prstr); |
| 579 |
dpg_print(_("Debt of %P paid off to loan shark\n"), AIPlay->Debt); |
| 580 |
} |
| 581 |
SendClientMessage(AIPlay, C_NONE, C_DONE, NULL, NULL); |
| 582 |
} |
| 583 |
|
| 584 |
/* |
| 585 |
* Sends the answer "answer" from AI player "From" to the server, |
| 586 |
* claiming to be for player "To". Also prints the answer on the screen. |
| 587 |
*/ |
| 588 |
void AISendAnswer(Player *From, Player *To, char *answer) |
| 589 |
{ |
| 590 |
SendClientMessage(From, C_NONE, C_ANSWER, To, answer); |
| 591 |
puts(answer); |
| 592 |
} |
| 593 |
|
| 594 |
/* |
| 595 |
* Works out a sensible response to the question coded in "Data" and with |
| 596 |
* computer-readable code "AI", claiming to be from "From" and for AI |
| 597 |
* player "AIPlay", and sends it. |
| 598 |
*/ |
| 599 |
void AIHandleQuestion(char *Data, AICode AI, Player *AIPlay, Player *From) |
| 600 |
{ |
| 601 |
char *Prompt; |
| 602 |
|
| 603 |
if (From == &Noone) |
| 604 |
From = NULL; |
| 605 |
Prompt = Data; |
| 606 |
GetNextWord(&Prompt, ""); |
| 607 |
PrintAIMessage(Prompt); |
| 608 |
switch (AI) { |
| 609 |
case C_ASKLOAN: |
| 610 |
if (RealLoanShark == -1) { |
| 611 |
g_print(_("Loan shark located at %s\n"), |
| 612 |
Location[AIPlay->IsAt].Name); |
| 613 |
} |
| 614 |
RealLoanShark = AIPlay->IsAt; |
| 615 |
AISendAnswer(AIPlay, From, "Y"); |
| 616 |
break; |
| 617 |
case C_ASKGUNSHOP: |
| 618 |
if (RealGunShop == -1) { |
| 619 |
g_print(_("Gun shop located at %s\n"), |
| 620 |
Location[AIPlay->IsAt].Name); |
| 621 |
} |
| 622 |
RealGunShop = AIPlay->IsAt; |
| 623 |
AISendAnswer(AIPlay, From, "Y"); |
| 624 |
break; |
| 625 |
case C_ASKPUB: |
| 626 |
if (RealPub == -1) { |
| 627 |
g_print(_("Pub located at %s\n"), Location[AIPlay->IsAt].Name); |
| 628 |
} |
| 629 |
RealPub = AIPlay->IsAt; |
| 630 |
AISendAnswer(AIPlay, From, "Y"); |
| 631 |
break; |
| 632 |
case C_ASKBITCH: |
| 633 |
case C_ASKRUN: |
| 634 |
case C_ASKGUN: |
| 635 |
AISendAnswer(AIPlay, From, "Y"); |
| 636 |
break; |
| 637 |
case C_ASKRUNFIGHT: |
| 638 |
AISendAnswer(AIPlay, From, ShouldRun(AIPlay) ? "R" : "F"); |
| 639 |
break; |
| 640 |
case C_ASKBANK: |
| 641 |
if (RealBank == -1) { |
| 642 |
g_print(_("Bank located at %s\n"), Location[AIPlay->IsAt].Name); |
| 643 |
} |
| 644 |
RealBank = AIPlay->IsAt; |
| 645 |
AISendAnswer(AIPlay, From, "N"); |
| 646 |
break; |
| 647 |
case C_MEETPLAYER: |
| 648 |
if (TotalGunsCarried(AIPlay) > 0) |
| 649 |
AISendAnswer(AIPlay, From, "A"); |
| 650 |
else { |
| 651 |
AISendAnswer(AIPlay, From, "E"); |
| 652 |
AIJet(AIPlay); |
| 653 |
} |
| 654 |
break; |
| 655 |
case C_ASKSEW: |
| 656 |
AISendAnswer(AIPlay, From, AIPlay->Health < MINSAFEHEALTH ? "Y" : "N"); |
| 657 |
break; |
| 658 |
default: |
| 659 |
AISendAnswer(AIPlay, From, "N"); |
| 660 |
break; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
/* |
| 665 |
* Sends a random message to all other dopewars players. |
| 666 |
*/ |
| 667 |
void AISendRandomMessage(Player *AIPlay) |
| 668 |
{ |
| 669 |
char *RandomInsult[5] = { |
| 670 |
/* Random messages to send from the AI player to other players */ |
| 671 |
N_("Call yourselves drug dealers?"), |
| 672 |
N_("A trained monkey could do better..."), |
| 673 |
N_("Think you\'re hard enough to deal with the likes of me?"), |
| 674 |
N_("Zzzzz... are you dealing in candy or what?"), |
| 675 |
N_("Reckon I'll just have to shoot you for your own good.") |
| 676 |
}; |
| 677 |
|
| 678 |
SendClientMessage(AIPlay, C_NONE, C_MSG, NULL, |
| 679 |
_(RandomInsult[brandom(0, 5)])); |
| 680 |
} |
| 681 |
|
| 682 |
#else /* NETWORKING */ |
| 683 |
|
| 684 |
/* |
| 685 |
* Whoops - the user asked that we run an AI player, but the binary was |
| 686 |
* built without that compiled in. |
| 687 |
*/ |
| 688 |
void AIPlayerLoop(struct CMDLINE *cmdline) |
| 689 |
{ |
| 690 |
g_print(_("This binary has been compiled without networking support, " |
| 691 |
"and thus cannot act as an AI player.\nRecompile passing " |
| 692 |
"--enable-networking to the configure script.")); |
| 693 |
} |
| 694 |
|
| 695 |
#endif /* NETWORKING */ |