| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "global.h" |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "sproto.h" |
| 25 |
|
| 26 |
static partylist *firstparty = NULL; |
| 27 |
static partylist *lastparty = NULL; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
partylist *party_form(object *op, const char *partyname) { |
| 41 |
partylist *party; |
| 42 |
char buf[MAX_BUF]; |
| 43 |
|
| 44 |
strlcpy(buf, partyname, sizeof(buf)); |
| 45 |
replace_unprintable_chars(buf); |
| 46 |
|
| 47 |
if (party_find(buf) != NULL) |
| 48 |
return NULL; |
| 49 |
|
| 50 |
party_leave(op); |
| 51 |
party = (partylist *)malloc(sizeof(partylist)); |
| 52 |
party->partyname = strdup_local(buf); |
| 53 |
#ifdef PARTY_KILL_LOG |
| 54 |
party->total_exp = 0; |
| 55 |
party->kills = 0; |
| 56 |
#endif |
| 57 |
party->passwd[0] = '\0'; |
| 58 |
party->next = NULL; |
| 59 |
party->partyleader = strdup_local(op->name); |
| 60 |
draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS, |
| 61 |
"You have formed party: %s", |
| 62 |
party->partyname); |
| 63 |
op->contr->party = party; |
| 64 |
|
| 65 |
if (lastparty) { |
| 66 |
lastparty->next = party; |
| 67 |
lastparty = lastparty->next; |
| 68 |
} else { |
| 69 |
firstparty = party; |
| 70 |
lastparty = firstparty; |
| 71 |
} |
| 72 |
|
| 73 |
return party; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
void party_join(object *op, partylist *party) { |
| 86 |
char buf[MAX_BUF]; |
| 87 |
|
| 88 |
if (op->contr->party == party) { |
| 89 |
draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_ERROR, |
| 90 |
"You are already a member of party: %s", |
| 91 |
party->partyname); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
party_leave(op); |
| 96 |
|
| 97 |
op->contr->party = party; |
| 98 |
draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS, |
| 99 |
"You have joined party: %s\n", |
| 100 |
party->partyname); |
| 101 |
snprintf(buf, MAX_BUF, "%s joins party %s", op->name, party->partyname); |
| 102 |
party_send_message(op, buf); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static void remove_if_unused(partylist *party) { |
| 109 |
for (player *pl = first_player; pl != NULL; pl = pl->next) { |
| 110 |
if (pl->party == party) |
| 111 |
return; |
| 112 |
} |
| 113 |
party_remove(party); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
void party_leave(object *op) { |
| 124 |
char buf[MAX_BUF]; |
| 125 |
|
| 126 |
partylist *party = op->contr->party; |
| 127 |
if (party == NULL) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_SUCCESS, |
| 132 |
"You leave party %s.", |
| 133 |
party->partyname); |
| 134 |
snprintf(buf, sizeof(buf), "%s leaves party %s.", op->name, party->partyname); |
| 135 |
party_send_message(op, buf); |
| 136 |
op->contr->party = NULL; |
| 137 |
remove_if_unused(party); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
partylist *party_find(const char *partyname) { |
| 149 |
partylist *party; |
| 150 |
|
| 151 |
for (party = firstparty; party; party = party->next) { |
| 152 |
if (strcmp(party->partyname, partyname) == 0) |
| 153 |
return party; |
| 154 |
} |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
void party_remove(partylist *party) { |
| 165 |
partylist *previousparty; |
| 166 |
partylist **walk; |
| 167 |
player *pl; |
| 168 |
|
| 169 |
previousparty = NULL; |
| 170 |
for (walk = &firstparty; *walk != NULL; walk = &(*walk)->next) { |
| 171 |
if (*walk == party) { |
| 172 |
if (party == lastparty) |
| 173 |
lastparty = previousparty; |
| 174 |
*walk = party->next; |
| 175 |
for (pl = first_player; pl != NULL; pl = pl->next) { |
| 176 |
if (pl->party == party) |
| 177 |
pl->party = NULL; |
| 178 |
} |
| 179 |
free(party->partyleader); |
| 180 |
free(party->partyname); |
| 181 |
free(party); |
| 182 |
return; |
| 183 |
} |
| 184 |
previousparty = *walk; |
| 185 |
} |
| 186 |
LOG(llevError, "party_remove: I was asked to remove party %s, but it could not be found.\n", |
| 187 |
party->partyname); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
partylist *party_get_first(void) { |
| 197 |
return firstparty; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
partylist *party_get_next(const partylist *party) { |
| 209 |
return party->next; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
void party_obsolete_parties(void) { |
| 216 |
partylist *party; |
| 217 |
partylist *next; |
| 218 |
for (party = firstparty; party != NULL; party = next) { |
| 219 |
next = party->next; |
| 220 |
remove_if_unused(party); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
const char *party_get_password(const partylist *party) { |
| 233 |
return party->passwd; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
void party_set_password(partylist *party, const char *password) { |
| 245 |
strlcpy(party->passwd, password, sizeof(party->passwd)); |
| 246 |
replace_unprintable_chars(party->passwd); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
int party_confirm_password(const partylist *party, const char *password) { |
| 260 |
return strcmp(party->passwd, password) == 0; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
void party_send_message(object *op, const char *message) { |
| 275 |
player * const self = op->contr; |
| 276 |
partylist * const party = self->party; |
| 277 |
|
| 278 |
for (player *pl = first_player; pl != NULL; pl = pl->next) |
| 279 |
if (pl->party == party && pl != self) |
| 280 |
draw_ext_info(NDI_WHITE, 0, pl->ob, MSG_TYPE_COMMUNICATION, MSG_TYPE_COMMUNICATION_PARTY, |
| 281 |
message); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
const char *party_get_leader(const partylist *party) { |
| 293 |
return party->partyleader; |
| 294 |
} |
| 295 |
|
| 296 |
#ifdef PARTY_KILL_LOG |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
void party_add_kill(partylist *party, const char *killer, const char *dead, long exp) { |
| 310 |
int i, pos; |
| 311 |
|
| 312 |
if (party->kills >= PARTY_KILL_LOG) { |
| 313 |
pos = PARTY_KILL_LOG-1; |
| 314 |
for (i = 0; i < PARTY_KILL_LOG-1; i++) |
| 315 |
memcpy(&(party->party_kills[i]), &(party->party_kills[i+1]), sizeof(party->party_kills[0])); |
| 316 |
} else |
| 317 |
pos = party->kills; |
| 318 |
party->kills++; |
| 319 |
party->total_exp += exp; |
| 320 |
party->party_kills[pos].exp = exp; |
| 321 |
strncpy(party->party_kills[pos].killer, killer, MAX_NAME); |
| 322 |
strncpy(party->party_kills[pos].dead, dead, MAX_NAME); |
| 323 |
party->party_kills[pos].killer[MAX_NAME] = 0; |
| 324 |
party->party_kills[pos].dead[MAX_NAME] = 0; |
| 325 |
} |
| 326 |
#endif |