From 502935c061ea913180f0a77a7406f5292e697c79 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sat, 6 Apr 2013 21:21:57 +0000 Subject: Mineshafts: Added random loot to generated chests and fixed chest direction git-svn-id: http://mc-server.googlecode.com/svn/trunk@1365 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/ChestEntity.cpp | 54 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 11 deletions(-) (limited to 'source/ChestEntity.cpp') diff --git a/source/ChestEntity.cpp b/source/ChestEntity.cpp index 2f038897c..d3c062924 100644 --- a/source/ChestEntity.cpp +++ b/source/ChestEntity.cpp @@ -9,6 +9,7 @@ #include "World.h" #include "Root.h" #include "Pickup.h" +#include "Noise.h" #include @@ -22,11 +23,20 @@ class cRoot; +cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ) : + super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ) +{ + cBlockEntityWindowOwner::SetBlockEntity(this); +} + + + + + cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) : - cBlockEntity(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World) + super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World) { - m_Content = new cItem[ c_ChestHeight * c_ChestWidth ]; - SetBlockEntity(this); // cBlockEntityWindowOwner + cBlockEntityWindowOwner::SetBlockEntity(this); } @@ -40,8 +50,6 @@ cChestEntity::~cChestEntity() { Window->OwnerDestroyed(); } - - delete [] m_Content; } @@ -92,13 +100,37 @@ void cChestEntity::SetSlot(int a_Slot, const cItem & a_Item) -#define READ(File, Var) \ - if (File.Read(&Var, sizeof(Var)) != sizeof(Var)) \ - { \ - LOGERROR("ERROR READING cChestEntity %s FROM FILE (line %d)", #Var, __LINE__); \ - return false; \ +void cChestEntity::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, int a_CountLootProbabs, int a_NumSlots, int a_Seed) +{ + // Calculate the total weight: + int TotalProbab = 1; + for (int i = 0; i < a_CountLootProbabs; i++) + { + TotalProbab += a_LootProbabs[i].m_Weight; } - + + // Pick the loot items: + cNoise Noise(a_Seed); + for (int i = 0; i < a_NumSlots; i++) + { + int Rnd = (Noise.IntNoise1DInt(i) / 7); + int LootRnd = Rnd % TotalProbab; + Rnd >>= 8; + cItem CurrentLoot = cItem(E_ITEM_BOOK, 1, 0); // TODO: enchantment + for (int j = 0; j < a_CountLootProbabs; j++) + { + LootRnd -= a_LootProbabs[i].m_Weight; + if (LootRnd < 0) + { + CurrentLoot = a_LootProbabs[i].m_Item; + CurrentLoot.m_ItemCount = a_LootProbabs[i].m_MinAmount + (Rnd % (a_LootProbabs[i].m_MaxAmount - a_LootProbabs[i].m_MinAmount)); + Rnd >>= 8; + break; + } + } // for j - a_LootProbabs[] + SetSlot(Rnd % ARRAYCOUNT(m_Content), CurrentLoot); + } // for i - NumSlots +} -- cgit v1.2.3