game2001/server/gameserver/metadata.cc
2019-03-28 16:43:17 +08:00

108 lines
3.7 KiB
C++

#include "precompile.h"
#include "metadata.h"
namespace MetaData
{
void Drop::Init()
{
std::vector<std::string> item_list;
{
a8::Split(i->item_id(), item_list, '|');
}
std::vector<std::string> num_list;
{
a8::Split(i->num(), num_list, '|');
}
std::vector<std::string> weight_list;
{
a8::Split(i->weight(), weight_list, '|');
}
assert(item_list.size() == num_list.size() &&
item_list.size() == weight_list.size());
total_weight = 0;
for (size_t i = 0; i < item_list.size(); ++i) {
total_weight += a8::XValue(weight_list[i]).GetInt();
if (this->i->type() == 1) {
std::tuple<int, int, int> item_tuple = std::make_tuple(
a8::XValue(item_list[i]),
a8::XValue(num_list[i]),
a8::XValue(weight_list[i])
);
items.push_back(item_tuple);
} else {
std::tuple<int, int, int> item_tuple = std::make_tuple(
a8::XValue(item_list[i]),
a8::XValue(num_list[i]),
total_weight
);
items.push_back(item_tuple);
}
}
assert(i->type() == 1 || i->type() == 2);
}
void Drop::RandItems(std::vector<std::tuple<int, int>>& drop_items)
{
if (i->type() == 1) {
for (auto& item : items) {
if ((rand() % 10000) <= std::get<2>(item)) {
drop_items.push_back(std::make_tuple(
std::get<0>(item),
std::get<1>(item)
));
}
}
} else if (total_weight > 0) {
int rnd = rand() % total_weight;
for (auto& item : items) {
if (std::get<2>(item) >= rnd) {
drop_items.push_back(std::make_tuple(
std::get<0>(item),
std::get<1>(item)
));
break;
}
}
}
}
void Building::Init()
{
for (auto& door_meta : i->doorobj()) {
Door* p_door = nullptr;
for (auto& door : doors) {
if (door.door_id == door_meta.id()) {
p_door = &door;
break;
}
}
if (!p_door) {
p_door = &a8::FastAppend(doors);
p_door->door_id = door_meta.id();
}
switch (door_meta.type()) {
case 1:
{
p_door->state0 = &door_meta;
}
break;
case 2:
{
p_door->state1 = &door_meta;
}
break;
}
}//end for
for (auto& door : doors) {
if (door.door_id == 0 ||
!door.state0 || !door.state1) {
abort();
}
}
}
}