]> jfr.im git - irc/gameservirc.git/blame - gameserv/misc.cpp
Moved all but the do_ functions out of gameserv.cpp and updated dependencies
[irc/gameservirc.git] / gameserv / misc.cpp
CommitLineData
d9681dad 1/* This file includes miscellaneous functions that shouldn't be in gameserv.cpp */
2#include "extern.h"
3#include "flags.h"
4#include "aClient.h"
5#include "pouch.h"
6#include "item.h"
7
8void see_master(char *u)
9{
10 aClient *user;
11
12 if (!(user = find(u)))
13 {
14 notice(s_GameServ, u, "Fatal error. Contact a(n) <S admin. buf: %s", strtok(NULL, ""));
15 return;
16 }
17
18 if (!is_fighting(user) && is_playing(user))
19 {
20 Player *p = user->stats;
21 p->setMyMaster(&levels[p->getLevel() - 1].master);
22 p->setMonster(&levels[p->getLevel() - 1].master);
23 display_monster(u); // Since master is the same structure, use this function
24 }
25}
26
27void rolloverall()
28{
29 list<Player*>::iterator iter;
30 for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
31 {
32 for (iter = players[x].begin(); iter != players[x].end(); iter++)
33 {
34 rollover((*iter));
35 }
36 }
37}
38void refreshall()
39{
40 list<Player*>::iterator iter;
41 for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
42 {
43 for (iter = players[x].begin(); iter != players[x].end(); iter++)
44 {
45 refresh((*iter));
46 }
47 }
48}
49
50void rollover(Player *p)
51{
52 if (!p)
53 return;
54
55 p->addForestFights(numrolloverfights);
56
57 if (p->getForestFights() > maxforestfights)
58 p->setForestFights(maxforestfights);
59}
60
61void refresh(Player *p)
62{
63 if (!p)
64 return;
65
66 if (p->getHP() < p->getMaxHP())
67 p->healall();
68 if (p->getForestFights() < forestfights)
69 {
70 p->setForestFights(forestfights);
71 }
72
73 p->setPlayerFights(3);
74 setAlive(p);
75 clearMaster(p);
76}
77
78void do_refresh(char *u)
79{
80 char *name = strtok(NULL, " ");
81 aClient *user;
82 Player *p;
83
84 if (!(user = find(u)))
85 {
86 notice(s_GameServ, u, "Error: aClient not found. Contact a <S admin");
87 log("Error: aClient not found: %s", u);
88 return;
89 }
90 else if (isIgnore(user))
91 {
92#ifdef DEBUGMODE
93 log("Ignoring %s.", user->getNick());
94#endif
95 return;
96 }
97 else if (!isAdmin(user))
98 {
99 notice(s_GameServ, u, "You must be a <S admin to use this command!");
100 return;
101 }
102 if (!name)
103 {
104 notice(s_GameServ, u, "SYNTAX: REFRESH {ALL | NICK}");
105 return;
106 }
107 else if (stricmp(name, "ALL") == 0)
108 {
109 notice(s_GameServ, u, "Refreshing everyone's stats!");
110 refreshall();
111 }
112 else if ((p = findplayer(name)))
113 {
114 notice(s_GameServ, u, "Refreshing %s.", p->getName().c_str());
115 refresh(p);
116 }
117 else
118 {
119 notice(s_GameServ, u, "Player %s not found.", name);
120 return;
121 }
122}
123
124
125void resetall()
126{
127 list<Player*>::iterator iter;
128 for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
129 {
130 for (iter = players[x].begin(); iter != players[x].end(); iter++)
131 {
132 reset((*iter));
133 }
134 }
135}
136
137void reset(Player *p)
138{
139 item *tempItem;
140
141 if (!p)
142 return;
143
144 p->reset();
145 // Add the stick and clothes
146 tempItem = findItemByID(3001);
147 p->inventory->addItem((*Items.begin()))->use(p); // Add the stick
148 p->inventory->addItem(tempItem)->use(p); // Add Clothes
149}
150
151void updateTS(Player *p)
152{
153 if (!p)
154 return;
155
156#ifdef DEBUGMODE
157 log("Old timestamp for %s: %ld", p->getName().c_str(), p->lastcommand);
158#endif
159 p->lastcommand = time(NULL);
160#ifdef DEBUGMODE
161 log("New timestamp for %s: %ld", p->getName().c_str(), p->lastcommand);
162#endif
163 PF_cleartimedout(p);
164
165}
166
167
168
169void timeOutEvent(Player *p)
170{
171 if (!p || !is_playing(p->getClient())) // then they're not playing
172 return;
173
174 char *nick = p->getClient()->getNick();
175
176 if (player_fight(p->getClient()) && isYourTurn(p))
177 {
178 // Check to see if they were the idler or if it was the other
179 // person
180 if (!PF_timedout(p->getBattle()->stats))
181 {
182 // This person's last command was given earlier,
183 // so this person is the idler
184 notice(s_GameServ, nick, "You timed out "\
185 "during a fight. You lose your turn!");
186 notice(s_GameServ, p->getBattle()->getNick(),
187 "%s hesitated for too long. Your move.", p->getName().c_str());
188 clearYourTurn(p);
189 setYourTurn(p->getBattle()->stats);
190
191 // Update the TS for both players to give them another
192 // Chance to wake up, but if the other player doesn't
193 // Attack now, they both get logged out.
194 updateTS(p);
195 PF_settimedout(p);
196 display_players(p->getBattle());
197 return;
198 }
199 else
200 {
201 notice(s_GameServ, p->getBattle()->getNick(),
202 "You and %s timed out at the same time."\
203 " Don't fight if you're just going to "\
204 "sit there!", p->getName().c_str());
205 notice(s_GameServ, p->getClient()->getNick(),
206 "You and %s timed out at the same time."\
207 " Don't fight if you're just going to "\
208 "sit there!", p->getBattle()->stats->getName().c_str());
209 logout(p->getBattle());
210 logout(p->getClient());
211 clearYourTurn(p);
212 clearYourTurn(p->getBattle()->stats);
213 return;
214 }
215 }
216 else if (!player_fight(p->getClient()))
217 {
218 if (isAlive(p) && p->getGold() > 0)
219 {
220 // Place fun stuff here :)
221 int randnum = 1 + rand() % 100; // 1-100
222#define GSN(s) notice(s_GameServ, nick, s)
223#define GSN2(s, f) notice(s_GameServ, nick, s, f)
224
225 if (randnum < 50)
226 {
227 // 35-100% of your gold goes pffft - kain
228 int stolen = (35 + (rand() % 66)) * (p->getGold() / 100);
229
230 GSN("You stop for a moment to rest on the "\
231 "street corner. All of a sudden, you "\
232 "are ambushed from all sides by a hoarde "\
233 "of knife wielding thugs.");
234 GSN2("The thugs beat you into utter submission "\
235 "and steal %ld gold from you!", stolen);
236 p->subtractGold(stolen);
237 }
238 else if (randnum >= 50 && randnum < 75)
239 {
240 // 25-65% of your gold goes pffft - kain
241 int stolen = (25 + (rand() % 41)) * (p->getGold() / 100);
242 GSN("While dilly dallying around, you lose "\
243 "your sense of time. Little did you know, "\
244 "but thieves lifted your gold while you "\
245 "weren't watching.");
246 GSN2("Better luck next time... you lose %d gold", stolen);
247 p->subtractGold(stolen);
248 }
249 else if (randnum >= 75)
250 {
251 // 25-75% of your gold goes pffft - kain
252 int stolen = (25 + (rand() % 51)) * (p->getGold() / 100);
253 GSN("Good grief! A gaggle of gooey green ghostlike "\
254 "goblins grabbed your gold!");
255 GSN2("They stole %d gold from you!", stolen);
256 p->subtractGold(stolen);
257 }
258 }
259
260 // Always log out the user
261 logout(p->getClient());
262 }
263}
264void logout(aClient *user)
265{
266 if (user != NULL)
267 {
268 if (user->stats != NULL)
269 {
270
271 list<Player*>::iterator iter;
272 unsigned long hv = iHASH((unsigned char *) user->stats->getName().c_str());
273 iter = find(players[hv].begin(), players[hv].end(), user->stats);
274
275 if (iter == players[hv].end())
276 {
277 notice(s_GameServ, user->getNick(), "Fatal error. Contact "\
278 "<S Admin. Cannot find you in the players list. This should NEVER happen");
279 log("Error on logout(). Can't find %s in the players list",
280#ifdef P10
281 user->getRealNick()
282#else
283 user->getNick()
284#endif
285 );
286 user->stats = NULL;
287
288 return;
289 }
290 user->stats->delMonster();
291 user->stats->delMaster();
292 user->stats->delBattle();
293 clearDragonFight(user->stats);
294 clearYourTurn(user->stats);
295 clearPlaying(user);
296
297 user->stats->setClient(NULL);
298
299
300 if (player_fight(user))
301 {
302 clearYourTurn(user->stats->getBattle()->stats);
303 user->stats->getBattle()->stats->delBattle();
304 }
305
306#ifdef DEBUGMODE
307 log("Logged out player %s",
308#ifdef P10
309 user->getRealNick()
310#else
311 user->getNick()
312#endif
313 );
314#endif
315 }
316 }
317 if (user)
318 user->stats = NULL;
319}
320
321void end_turn(aClient *user)
322{
323 char *nick, *u = user->getNick();
324 Monster *fight;
325 aClient *battle;
326 int mhit;
327
328 nick = new char[strlen(user->getNick()) + 1];
329
330 if (!user || !is_playing(user) || !is_fighting(user))
331 goto endturn;
332
333 if (!player_fight(user) && !master_fight(user))
334 fight = user->stats->getMonster();
335 else
336 fight = user->stats->getMaster();
337 battle = user->stats->getBattle();
338
339 if (!player_fight(user))
340 {
341 // Opponent's Hit
342 mhit = (fight->strength / 2) + (rand() % (fight->strength / 2)) - (user->stats->getDefense());
343 }
344 else
345 {
346 // Opponent's Hit
347 mhit = (battle->stats->getStrength() / 2) + (rand() % (battle->stats->getStrength() / 2)) - user->stats->getDefense();
348 }
349 if (!player_fight(user))
350 {
351
352 if (mhit > 0)
353 {
354 notice(s_GameServ, u, "\1f%s\1f attacks with their \1f%s\1f for \ 2%d\ 2 damage!",
355 fight->name.c_str(), fight->weapon.c_str(), mhit);
356 }
357 else if (mhit <= 0)
358 notice(s_GameServ, u, "%s completely misses you!", fight->name.c_str());
359
360 if (mhit >= user->stats->getHP())
361 {
362 if (!master_fight(user))
363 {
364 notice(s_GameServ, u, "You have been \ 2\1fkilled\1f\ 2 by %s!", fight->name.c_str());
365 notice(s_GameServ, u, "You lose all gold on hand and lose 10 percent "\
366 "of your experience!");
367 user->stats->setGold(0);
368 user->stats->subtractExp((long int)(user->stats->getExp() * .10));
369 user->stats->setHP(0);
370 user->stats->delMonster();
371 clearAlive(user->stats);
372 goto endturn;
373 }
374 else
375 {
376 notice(s_GameServ, u, "%s has bested you! You will have to wait "\
377 "until tomorrow to try again", user->stats->getMaster()->name.c_str());
378 user->stats->delMonster();
379 user->stats->delMaster();
380 goto endturn;
381 }
382 }
383 else
384 {
385 if (mhit > 0)
386 user->stats->subtractHP(mhit);
387 display_monster(u);
388 goto endturn;
389 }
390 }
391 else
392 {
393 clearYourTurn(user->stats);
394 setYourTurn(battle->stats);
395 display_players(battle);
396 }
397 endturn:
398 delete []nick;
399}