forked from SecretsOTheP/EQMacEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforage.cpp
514 lines (425 loc) · 14.6 KB
/
forage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* EQEMu: Everquest Server Emulator
Copyright (C) 2001-2002 EQEMu Development Team (http://eqemu.org)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "../common/global_define.h"
#include "../common/eqemu_logsys.h"
#include "../common/fastmath.h"
#include "../common/misc_functions.h"
#include "../common/rulesys.h"
#include "../common/strings.h"
#include "entity.h"
#include "forage.h"
#include "npc.h"
#include "quest_parser_collection.h"
#include "string_ids.h"
#include "titles.h"
#include "water_map.h"
#include "zonedb.h"
#include <iostream>
#ifdef _WINDOWS
#define snprintf _snprintf
#endif
struct NPCType;
//max number of items which can be in the foraging table
//for a given zone.
#define FORAGE_ITEM_LIMIT 50
uint32 ZoneDatabase::GetZoneForage(uint32 ZoneID, uint8 skill) {
uint32 item[FORAGE_ITEM_LIMIT];
uint32 chance[FORAGE_ITEM_LIMIT];
uint32 ret;
for (int c=0; c < FORAGE_ITEM_LIMIT; c++) {
item[c] = 0;
}
uint32 chancepool = 0;
std::string query = StringFormat("SELECT itemid, chance FROM "
"forage WHERE zoneid = '%i' and level <= '%i' "
"LIMIT %i", ZoneID, skill, FORAGE_ITEM_LIMIT);
auto results = QueryDatabase(query);
if (!results.Success()) {
return 0;
}
uint8 index = 0;
for (auto row = results.begin(); row != results.end(); ++row, ++index) {
if (index >= FORAGE_ITEM_LIMIT)
break;
item[index] = atoi(row[0]);
chance[index] = atoi(row[1]) + chancepool;
Log(Logs::General, Logs::General, "Possible Forage: %d with a %d chance total to %d chancepool", item[index], chance[index] - chancepool, chance[index]);
chancepool = chance[index];
}
if(chancepool == 0 || index < 1)
return 0;
if(index == 1) {
return item[0];
}
ret = 0;
uint32 rindex = zone->random.Int(1, chancepool);
for(int i = 0; i < index; i++) {
if(rindex <= chance[i]) {
ret = item[i];
break;
}
}
return ret;
}
uint32 ZoneDatabase::GetZoneFishing(uint32 ZoneID, uint8 skill)
{
uint32 item[50];
uint32 chance[50];
uint32 chancepool = 0;
uint32 ret = 0;
for (int c=0; c<50; c++) {
item[c]=0;
chance[c]=0;
}
std::string query = StringFormat("SELECT itemid, chance "
"FROM fishing WHERE (zoneid = '%i' || zoneid = 0) AND skill_level <= '%i'",
ZoneID, skill);
auto results = QueryDatabase(query);
if (!results.Success()) {
return 0;
}
uint8 index = 0;
for (auto row = results.begin(); row != results.end(); ++row, ++index) {
if (index >= 50)
break;
item[index] = atoi(row[0]);
chance[index] = atoi(row[1])+chancepool;
chancepool = chance[index];
}
if (index <= 0)
return 0;
uint32 random = zone->random.Int(1, chancepool);
for (int i = 0; i < index; i++)
{
if (random > chance[i])
continue;
ret = item[i];
break;
}
return ret;
}
//we need this function to immediately determine, after we receive OP_Fishing, if we can even try to fish, otherwise we have to wait a while to get the failure
bool Client::CanFish() {
if(fishing_timer.Enabled())
{
Message_StringID(CC_Default, ALREADY_FISHING); //You are already fishing!
return false;
}
if(m_inv.GetItem(EQ::invslot::slotCursor))
{
Message_StringID(CC_User_Skills, FISHING_HANDS_FULL);
return false;
}
//make sure we still have a fishing pole on:
const EQ::ItemInstance* Pole = m_inv[EQ::invslot::slotPrimary];
int32 bslot = m_inv.HasItemByUse(EQ::item::ItemTypeFishingBait, 1, invWhereWorn|invWherePersonal);
const EQ::ItemInstance* Bait = nullptr;
if (bslot != INVALID_INDEX)
Bait = m_inv.GetItem(bslot);
if(!Pole || !Pole->IsType(EQ::item::ItemClassCommon) || Pole->GetItem()->ItemType != EQ::item::ItemTypeFishingPole) {
if (m_inv.HasItemByUse(EQ::item::ItemTypeFishingPole, 1, invWhereWorn|invWherePersonal|invWhereBank|invWhereTrading|invWhereCursor)) //We have a fishing pole somewhere, just not equipped
Message_StringID(MT_Skills, FISHING_EQUIP_POLE); //You need to put your fishing pole in your primary hand.
else //We don't have a fishing pole anywhere
Message_StringID(MT_Skills, FISHING_NO_POLE); //You can't fish without a fishing pole, go buy one.
return false;
}
if (!Bait || !Bait->IsType(EQ::item::ItemClassCommon) || Bait->GetItem()->ItemType != EQ::item::ItemTypeFishingBait) {
Message_StringID(MT_Skills, FISHING_NO_BAIT); //You can't fish without fishing bait, go buy some.
return false;
}
if(zone->zonemap != nullptr && zone->watermap != nullptr && RuleB(Watermap, CheckForWaterWhenFishing)) {
glm::vec3 rodPosition;
// Tweak Rod and LineLength if required
const float RodLength = RuleR(Watermap, FishingRodLength);
const float LineLength = RuleR(Watermap, FishingLineLength);
const float LineExtension = RuleR(Watermap, FishingLineExtension);
int HeadingDegrees;
HeadingDegrees = (int) ((GetHeading()*360)/256);
HeadingDegrees = HeadingDegrees % 360;
rodPosition.x = m_Position.x + RodLength * sin(HeadingDegrees * M_PI/180.0f);
rodPosition.y = m_Position.y + RodLength * cos(HeadingDegrees * M_PI/180.0f);
glm::vec3 dest;
dest.x = rodPosition.x;
dest.y = rodPosition.y;
dest.z = m_Position.z;
if (!CheckLosFN(dest.x, dest.y, dest.z, 0.0f)) {
// fishing into a wall to reach water on other side?
Message_StringID(MT_Skills, FISHING_LAND); //Trying to catch land sharks perhaps?
return false;
}
rodPosition.z = dest.z - LineLength;
bool in_lava = zone->watermap->InLava(rodPosition);
bool in_water = zone->watermap->InWater(rodPosition) || zone->watermap->InVWater(rodPosition);
if (GetZoneID() == powater) {
if (zone->IsWaterZone(rodPosition.z))
in_water = true;
else
in_water = false;
}
Log(Logs::General, Logs::Maps, "Fishing Rod is at %4.3f, %4.3f, %4.3f (dest.z: %4.3f), InWater says %d, InLava says %d Region is: %d RodLength: %f LineLength: %f", rodPosition.x, rodPosition.y, rodPosition.z, dest.z, in_water, in_lava, zone->watermap->ReturnRegionType(rodPosition), RodLength, LineLength);
if (in_lava) {
Message_StringID(MT_Skills, FISHING_LAVA); //Trying to catch a fire elemental or something?
return false;
}
if(!in_water) {
// Our line may be too long, and we are going underworld. Reel our line in, and try again.
rodPosition.z = dest.z - (LineLength/2);
in_water = zone->watermap->InWater(rodPosition) || zone->watermap->InVWater(rodPosition);
if (GetZoneID() == powater) {
if (zone->IsWaterZone(rodPosition.z))
in_water = true;
else
in_water = false;
}
Log(Logs::General, Logs::Maps, "Trying again with new Z %4.3f InWater now says %d", rodPosition.z, in_water);
if(!in_water)
{
// Our line may be too short. Reel our line out using extension, and try again
rodPosition.z = dest.z - (LineLength+LineExtension);
in_water = zone->watermap->InWater(rodPosition) || zone->watermap->InVWater(rodPosition);
if (GetZoneID() == powater) {
if (zone->IsWaterZone(rodPosition.z))
in_water = true;
else
in_water = false;
}
Log(Logs::General, Logs::Maps, "Trying again with new Z %4.3f InWater now says %d", rodPosition.z, in_water);
if(!in_water)
{
Message_StringID(MT_Skills, FISHING_LAND); //Trying to catch land sharks perhaps?
return false;
}
}
}
}
return true;
}
void Client::GoFish()
{
fishing_timer.Disable();
//we're doing this a second time (1st in Client::Handle_OP_Fishing) to make sure that, between when we started fishing & now, we're still able to fish (in case we move, change equip, etc)
if (!CanFish()) //if we can't fish here, we don't need to bother with the rest
return;
//multiple entries yeilds higher probability of dropping...
uint32 common_fish_ids[MAX_COMMON_FISH_IDS] = {
1038, // Tattered Cloth Sandals
1038, // Tattered Cloth Sandals
1038, // Tattered Cloth Sandals
13019, // Fresh Fish
13076, // Fish Scales
13076, // Fish Scales
7007, // Rusty Dagger
7007, // Rusty Dagger
7007 // Rusty Dagger
};
//success formula is not researched at all
int fishing_skill = GetSkill(EQ::skills::SkillFishing); //will take into account skill bonuses on pole & bait
uint16 fishing_mod = 0;
//make sure we still have a fishing pole on:
int32 bslot = m_inv.HasItemByUse(EQ::item::ItemTypeFishingBait, 1, invWhereWorn|invWherePersonal);
const EQ::ItemInstance* Bait = nullptr;
if (bslot != INVALID_INDEX)
Bait = m_inv.GetItem(bslot);
//if the bait isnt equipped, need to add its skill bonus
if(bslot >= EQ::invslot::GENERAL_BEGIN && Bait != nullptr && Bait->GetItem()->SkillModType == EQ::skills::SkillFishing) {
fishing_skill += fishing_skill * (Bait->GetItem()->SkillModValue/100);
if (fishing_skill > HARD_SKILL_CAP)
{
fishing_skill = HARD_SKILL_CAP;
}
}
if (fishing_skill > 100)
fishing_mod = 100 + ((fishing_skill - 100) / 2);
else
fishing_mod = fishing_skill;
uint8 success = SKILLUP_FAILURE;
if (zone->random.Int(0,175) < fishing_mod) {
uint32 food_id = 0;
if (zone->random.Int(0, 299) <= fishing_mod)
{
food_id = database.GetZoneFishing(m_pp.zone_id, fishing_skill);
}
//consume bait, should we always consume bait on success?
DeleteItemInInventory(bslot, 1, true); //do we need client update?
if(food_id == 0) {
int index = zone->random.Int(0, MAX_COMMON_FISH_IDS-1);
food_id = common_fish_ids[index];
}
const EQ::ItemData* food_item = database.GetItem(food_id);
EQ::ItemInstance* inst = database.CreateItem(food_item, 1);
if(inst != nullptr) {
if(CheckLoreConflict(inst->GetItem()))
{
Message_StringID(CC_Default, DUP_LORE);
safe_delete(inst);
}
else
{
if (food_item->ItemType == EQ::item::ItemTypeFood)
{
Message_StringID(CC_User_Skills, FISHING_SUCCESS, food_item->Name);
}
else
{
Message_StringID(CC_User_Skills, FISHING_SUCCESS_SOMETHING);
}
PushItemOnCursorWithoutQueue(inst);
safe_delete(inst);
inst = m_inv.GetItem(EQ::invslot::slotCursor);
success = SKILLUP_SUCCESS;
}
if(inst) {
std::vector<std::any> args;
args.push_back(inst);
parse->EventPlayer(EVENT_FISH_SUCCESS, this, "", inst->GetID(), &args);
}
}
}
else
{
//chance to use bait when you dont catch anything...
if (zone->random.Int(0, 4) == 1)
{
DeleteItemInInventory(bslot, 1, true);
Message_StringID(MT_Skills, FISHING_LOST_BAIT); //You lost your bait!
}
else
{
bool spilled_beer = false;
if (zone->random.Int(0, 15) == 1) //give about a 1 in 15 chance to spill your beer.
{
if (SpillBeer())
{
Message_StringID(MT_Skills, FISHING_SPILL_BEER); //You spill your beer while bringing in your line.
spilled_beer = true;
}
}
if (!spilled_beer)
{
Message_StringID(MT_Skills, FISHING_FAILED); //You didn't catch anything.
}
}
parse->EventPlayer(EVENT_FISH_FAILURE, this, "", 0);
}
//chance to break fishing pole...
uint16 break_chance = 49;
if(fishing_skill > 49)
break_chance = fishing_skill;
if (zone->random.Int(0, break_chance) == 1) {
Message_StringID(MT_Skills, FISHING_POLE_BROKE); //Your fishing pole broke!
DeleteItemInInventory(EQ::invslot::slotPrimary, 0, true);
}
if(CheckIncreaseSkill(EQ::skills::SkillFishing, nullptr, zone->skill_difficulty[EQ::skills::SkillFishing].difficulty), success)
{
if(title_manager.IsNewTradeSkillTitleAvailable(EQ::skills::SkillFishing, GetRawSkill(EQ::skills::SkillFishing)))
NotifyNewTitlesAvailable();
}
}
void Client::ForageItem(bool guarantee) {
int skill_level = GetSkill(EQ::skills::SkillForage);
uint8 success = SKILLUP_FAILURE;
// these may need to be fine tuned, I am just guessing here
if (guarantee || zone->random.Int(0,199) < skill_level)
{
uint32 foragedfood = 0;
uint32 stringid = FORAGE_NOEAT;
if (RuleB(Character, ForageNeedFoodorDrink))
{
// If we're hungry or thirsty, we want to prefer food or water.
if(Hungry() && Thirsty())
{
if(m_pp.hunger_level <= m_pp.thirst_level)
foragedfood = 13047;
else
foragedfood = 13044;
}
// We only need food.
else if(Hungry())
{
foragedfood = 13047;
}
// We only need water.
else if(Thirsty())
{
foragedfood = 13044;
}
}
if (foragedfood == 0)
foragedfood = database.GetZoneForage(m_pp.zone_id, skill_level);
if (foragedfood > 0)
{
int16 inv_slot_id = GetInv().HasItem(foragedfood, 1, invWhereCursor);
if (inv_slot_id != INVALID_INDEX && !GetGM()) {
// we already have this item on the cursor - so stop sending it here.
Message_StringID(MT_Skills, FORAGE_FAILED);
return;
}
}
const EQ::ItemData* food_item = database.GetItem(foragedfood);
if(!food_item)
{
Log(Logs::General, Logs::Error, "nullptr returned from database.GetItem (%d) in ClientForageItem", foragedfood);
return;
}
if(foragedfood == 13106)
stringid = FORAGE_GRUBS;
else
{
switch(food_item->ItemType)
{
case EQ::item::ItemTypeFood:
stringid = FORAGE_FOOD;
break;
case EQ::item::ItemTypeDrink:
if(strstr(food_item->Name, "Water"))
stringid = FORAGE_WATER;
else
stringid = FORAGE_DRINK;
break;
default:
break;
}
}
EQ::ItemInstance* inst = database.CreateItem(food_item, 1);
if(inst != nullptr)
{
// check to make sure it isn't a foraged lore item
if(CheckLoreConflict(inst->GetItem()))
{
Message_StringID(CC_Default, DUP_LORE);
safe_delete(inst);
}
else
{
Message_StringID(MT_Skills, stringid);
SummonItem(inst->GetID(), inst->GetCharges());
safe_delete(inst);
inst = m_inv.GetItem(EQ::invslot::slotCursor);
}
if(inst) {
std::vector<std::any> args;
args.push_back(inst);
parse->EventPlayer(EVENT_FORAGE_SUCCESS, this, "", inst->GetID(), &args);
}
success = SKILLUP_SUCCESS;
}
}
else
{
Message_StringID(MT_Skills, FORAGE_FAILED);
parse->EventPlayer(EVENT_FORAGE_FAILURE, this, "", 0);
}
CheckIncreaseSkill(EQ::skills::SkillForage, nullptr, zone->skill_difficulty[EQ::skills::SkillForage].difficulty, success);
}