Skip to content

Commit

Permalink
Inventory QOL
Browse files Browse the repository at this point in the history
Refactored faster looting.
Added x10, x100 and stack looting.
Now gold and equipped items can't be sold.
  • Loading branch information
da3m0nsec authored and Try committed Feb 2, 2020
1 parent 2aec755 commit 5e0ca8b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 44 deletions.
94 changes: 67 additions & 27 deletions Game/ui/inventorymenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ void InventoryMenu::keyDownEvent(KeyEvent &e) {
e.ignore();
return;
}
e.accept();

auto& pg = activePage();
auto& sel = activePageSel();
Expand Down Expand Up @@ -204,11 +205,30 @@ void InventoryMenu::keyDownEvent(KeyEvent &e) {
else if(sel.sel+1<pg.size())
sel.sel++;
}
else if(e.key==KeyEvent::K_Z) {
lootMode = LootMode::Ten;
takeTimer.start(200);
onTakeStuff();
}
else if(e.key==KeyEvent::K_X) {
lootMode = LootMode::Hundred;
takeTimer.start(200);
onTakeStuff();
}
else if(e.key==KeyEvent::K_Space) {
lootMode = LootMode::Stack;
takeTimer.start(200);
onTakeStuff();
}
adjustScroll();
update();
}

void InventoryMenu::keyUpEvent(KeyEvent &e) {
if(e.key==KeyEvent::K_Space || e.key==KeyEvent::K_Z || e.key==KeyEvent::K_X){
takeTimer.stop();
lootMode = LootMode::Normal;
}
if(e.key==KeyEvent::K_ESCAPE || (e.key==KeyEvent::K_Tab && state!=State::Trade)){
close();
}
Expand All @@ -232,6 +252,7 @@ void InventoryMenu::mouseDownEvent(MouseEvent &e) {
player->useItem (r.clsId());
}
else if(state==State::Chest || state==State::Trade || state==State::Ransack) {
lootMode = LootMode::Normal;
takeTimer.start(200);
onTakeStuff();
}
Expand Down Expand Up @@ -309,39 +330,58 @@ InventoryMenu::PageLocal &InventoryMenu::activePageSel() {
return pageLocal[1];
}

void InventoryMenu::onTakeStuff() {
++takeCount;
for(int i = 0; i < pow(10,takeCount / 10); ++i) {
auto& page = activePage();
auto& sel = activePageSel();
if(sel.sel >= page.size())
return;
auto& r = page[sel.sel];
if(r.count() == 1) {
takeCount = 1;
void InventoryMenu::onTakeStuff() {
uint8_t itemCount = 0;
auto& page = activePage();
auto& sel = activePageSel();
if(sel.sel >= page.size())
return;
auto& r = page[sel.sel];
if(r.isEquiped()) {
return;
}

if(lootMode==LootMode::Normal) {
++takeCount;
itemCount = pow(10,takeCount / 10);
if(r.count() <= itemCount) {
itemCount = r.count();
takeCount = 0;
}
}
else if(lootMode==LootMode::Stack) {
itemCount = r.count();
}
else if(lootMode==LootMode::Ten) {
itemCount = 10;
}
else if(lootMode==LootMode::Hundred) {
itemCount = 100;
}
if(r.count() < itemCount) {
itemCount = r.count();
}

if(state==State::Chest) {
if(page.is(&player->inventory())) {
player->moveItem(r.clsId(),*chest);
} else {
player->addItem(r.clsId(),*chest);
}
if(state==State::Chest) {
if(page.is(&player->inventory())) {
player->moveItem(r.clsId(),*chest,itemCount);
} else {
player->addItem(r.clsId(),*chest,itemCount);
}
else if(state==State::Trade) {
if(page.is(&player->inventory())) {
player->sellItem(r.clsId(),*trader);
} else {
player->buyItem(r.clsId(),*trader);
}
}
else if(state==State::Trade) {
if(page.is(&player->inventory())) {
player->sellItem(r.clsId(),*trader,itemCount);
} else {
player->buyItem(r.clsId(),*trader,itemCount);
}
else if(state==State::Ransack) {
if(page.is(&trader->inventory())) {
player->addItem(r.clsId(),*trader);
}
}
else if(state==State::Ransack) {
if(page.is(&trader->inventory())) {
player->addItem(r.clsId(),*trader,itemCount);
}
adjustScroll();
}
adjustScroll();
}

void InventoryMenu::adjustScroll() {
Expand Down
8 changes: 8 additions & 0 deletions Game/ui/inventorymenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class InventoryMenu : public Tempest::Widget {
Ransack
};

enum class LootMode:uint8_t {
Normal=0,
Stack,
Ten,
Hundred
};

void close();
void open(Npc& pl);
void trade(Npc& pl,Npc& tr);
Expand Down Expand Up @@ -80,6 +87,7 @@ class InventoryMenu : public Tempest::Widget {
uint8_t page =0;
Tempest::Timer takeTimer;
size_t takeCount =0;
LootMode lootMode =LootMode::Normal;
InventoryRenderer renderer;

size_t columsCount=5;
Expand Down
29 changes: 17 additions & 12 deletions Game/world/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1987,34 +1987,39 @@ Item* Npc::addItem(std::unique_ptr<Item>&& i) {
return invent.addItem(std::move(i));
}

void Npc::addItem(uint32_t id, Interactive &chest) {
Inventory::trasfer(invent,chest.inventory(),nullptr,id,1,owner);
void Npc::addItem(uint32_t id, Interactive &chest, uint32_t count) {
Inventory::trasfer(invent,chest.inventory(),nullptr,id,count,owner);
}

void Npc::addItem(uint32_t id, Npc &from) {
Inventory::trasfer(invent,from.invent,&from,id,1,owner);
void Npc::addItem(uint32_t id, Npc &from, uint32_t count) {
Inventory::trasfer(invent,from.invent,&from,id,count,owner);
}

void Npc::moveItem(uint32_t id, Interactive &to) {
Inventory::trasfer(to.inventory(),invent,this,id,1,owner);
void Npc::moveItem(uint32_t id, Interactive &to, uint32_t count) {
Inventory::trasfer(to.inventory(),invent,this,id,count,owner);
}

void Npc::sellItem(uint32_t id, Npc &to) {
void Npc::sellItem(uint32_t id, Npc &to, uint32_t count) {
int32_t price = invent.sellPriceOf(id);
Inventory::trasfer(to.invent,invent,this,id,1,owner);
if(id==owner.script().goldId())
return;
Inventory::trasfer(to.invent,invent,this,id,count,owner);
invent.addItem(owner.script().goldId(),uint32_t(price),owner);
}

void Npc::buyItem(uint32_t id, Npc &from) {
void Npc::buyItem(uint32_t id, Npc &from, uint32_t count) {
if(id==owner.script().goldId())
return;
int32_t price = from.invent.priceOf(id);
if(price>int32_t(invent.goldCount())) {
if(price*count>int32_t(invent.goldCount())) {
owner.script().printCannotBuyError(*this);
if(int32_t(invent.goldCount())/price >= 1){
buyItem(id,from,int32_t(invent.goldCount())/price);
}
return;
}
Inventory::trasfer(invent,from.invent,nullptr,id,1,owner);
invent.delItem(owner.script().goldId(),uint32_t(price),*this);
Inventory::trasfer(invent,from.invent,nullptr,id,count,owner);
invent.delItem(owner.script().goldId(),uint32_t(price)*count,*this);
}

void Npc::clearInventory() {
Expand Down
10 changes: 5 additions & 5 deletions Game/world/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ class Npc final {
void useItem (uint32_t item, bool force=false);
void setCurrentItem(uint32_t item);
void unequipItem(uint32_t item);
void addItem (uint32_t id,Interactive& chest);
void addItem (uint32_t id,Npc& from);
void moveItem (uint32_t id,Interactive& to);
void sellItem (uint32_t id,Npc& to);
void buyItem (uint32_t id,Npc& from);
void addItem (uint32_t id,Interactive& chest,uint32_t count=1);
void addItem (uint32_t id,Npc& from,uint32_t count=1);
void moveItem (uint32_t id,Interactive& to,uint32_t count=1);
void sellItem (uint32_t id,Npc& to,uint32_t count=1);
void buyItem (uint32_t id,Npc& from,uint32_t count=1);
void clearInventory();
Item* currentArmour();
Item* currentMeleWeapon();
Expand Down

0 comments on commit 5e0ca8b

Please sign in to comment.