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