Public Repository for the Magicbane Shadowbane Emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
15 KiB

// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.gameManager;
import engine.loot.ModTableEntry;
import engine.loot.ModTypeTableEntry;
import engine.loot.WorkOrder;
import engine.mbEnums;
12 months ago
import engine.net.DispatchMessage;
import engine.net.client.msg.ItemProductionMsg;
import engine.objects.City;
import engine.objects.Item;
import engine.objects.ItemTemplate;
import engine.objects.NPC;
import engine.powers.EffectsBase;
import org.pmw.tinylog.Logger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public enum ForgeManager implements Runnable {
12 months ago
// MB Dev notes:
12 months ago
// Class handles all forge rolling mechanics for the game.
12 months ago
//
// WorkOrders may be submitted from any thread: usually (ItemProductionMsgHandler)
// as concurrency is managed by the same lock used by the warehouse (city.cityTransactionLock)
// WorkOrders are persisted then reconstituted at bootstrap from table dyn.workorders
//
12 months ago
// p.s. replaces garbage code that looked like it was written by a crack head with face boils.
12 months ago
FORGE_MANAGER;
public static final BlockingQueue<WorkOrder> forge = new DelayQueue<>();
12 months ago
public static final AtomicInteger workOrderCounter = new AtomicInteger(0);
12 months ago
public static final ConcurrentHashMap<NPC, ConcurrentHashMap.KeySetView<WorkOrder, Boolean>> vendorWorkOrderLookup = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<Item, WorkOrder> itemWorkOrderLookup = new ConcurrentHashMap<>();
@Override
public void run() {
12 months ago
WorkOrder workOrder;
while (true) {
12 months ago
// ForgeManager.forge is a delayQueue (priority queue using an epoc sort)
// workOrders are popped and processed when the completion time has passed.
12 months ago
//
// workOrders can be enqueued from any thread such as from a db handler
// however the interface is .submit(workOrder).
12 months ago
try {
workOrder = forge.take();
if (workOrder.total_produced >= workOrder.total_to_produce) {
// Complete this workOrder.
12 months ago
for (Item workOrderItem : workOrder.cooking) {
workOrderItem.flags.add(mbEnums.ItemFlags.Identified);
ItemProductionMsg outMsg = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, workOrderItem, mbEnums.ProductionActionType.CONFIRM_PRODUCE, true);
DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
}
workOrder.runCompleted = true;
12 months ago
// Update workOrder on disk
DbManager.WarehouseQueries.UPDATE_WORKORDER(workOrder);
}
if (workOrder.runCompleted)
continue;
12 months ago
// Move current cooking batch to vendor inventory
12 months ago
completeWorkOrderBatch(workOrder);
12 months ago
// Create new set of in-memory only virtual items
forgeWorkOrderBatch(workOrder);
// enQueue this workOrder again; back into the oven
// until all items for this workOrder are completed.
forge.add(workOrder);
12 months ago
// Debugging: Logger.info(workOrder.toString());
} catch (Exception e) {
Logger.error(e);
}
}
}
public static void start() {
Thread forgeManager;
forgeManager = new Thread(FORGE_MANAGER);
forgeManager.setName("Forge Manager");
forgeManager.start();
}
public static int submit(WorkOrder workOrder) {
// Must have a city to roll anything
City city = workOrder.vendor.building.getCity();
if (city == null)
return 58; //58: The formula is beyond the means of this facility
// Concurrency is rightly managed by same lock as warehouse
city.transactionLock.writeLock().lock();
// Make sure vendor can roll the formulae, warehouse can
// afford this wordOrder and other related checks.
12 months ago
int validation_result = WorkOrder.validate(workOrder);
// The return code is used by the ItemProductionMsgHandler as a
// popup error message for the player.
12 months ago
if (validation_result != 0)
return validation_result;
try {
12 months ago
// Configure this production run.
12 months ago
12 months ago
workOrder.workOrderID = workOrderCounter.incrementAndGet();
12 months ago
workOrder.rollingDuration = ForgeManager.calcRollingDuration(workOrder);
workOrder.completionTime = System.currentTimeMillis() + workOrder.rollingDuration;
workOrder.slots_used = calcAvailableSlots(workOrder);
12 months ago
workOrder.total_produced = 0;
// Single item configuration
if (!workOrder.multiple_slot_request && workOrder.total_to_produce == 0)
workOrder.total_to_produce = 1;
12 months ago
// Set total cost for this production run
12 months ago
12 months ago
workOrder.total_to_produce *= workOrder.slots_used;
workOrder.production_cost = calcProductionCost(workOrder);
12 months ago
workOrder.production_cost_total.putAll(workOrder.production_cost);
workOrder.production_cost_total.forEach((key, value) -> workOrder.production_cost_total.put(key, value * workOrder.total_to_produce));
12 months ago
// Withdraw gold and resource costs
12 months ago
if (!WorkOrder.withdrawWorkOrderCost(workOrder))
12 months ago
return 58; //58: The formula is beyond the means of this facility
12 months ago
// Create new batch of virtual items
12 months ago
forgeWorkOrderBatch(workOrder);
12 months ago
// Enqueue workOrder for next completion cycle
// and assign it to the vendor
vendorWorkOrderLookup.get(workOrder.vendor).add(workOrder);
12 months ago
forge.add(workOrder);
// Save workOrder to disk
DbManager.WarehouseQueries.UPDATE_WORKORDER(workOrder);
} catch (Exception e) {
Logger.error(e);
} finally {
city.transactionLock.writeLock().unlock();
}
Logger.info(workOrder.toString());
return validation_result;
}
public static long calcRollingDuration(WorkOrder workOrder) {
float rollingDuration;
rollingDuration = workOrder.vendor.getBuilding().getRank() * -5L + 40;
12 months ago
rollingDuration = TimeUnit.MINUTES.toMillis((long) rollingDuration);
rollingDuration *= Float.parseFloat(ConfigManager.MB_PRODUCTION_RATE.getValue());
ItemTemplate template = ItemTemplate.templates.get(workOrder.templateID);
// Bane circles
if (template.item_bane_rank > 0)
rollingDuration = (long) template.item_bane_rank * 60 * 60 * 3 * 1000 * Float.parseFloat(ConfigManager.MB_PRODUCTION_RATE.getValue());
return (long) rollingDuration;
}
public static int calcAvailableSlots(WorkOrder workOrder) {
// Slots available in a forge are based on the npc rank
int availableSlots = workOrder.vendor.getRank();
12 months ago
// Subtract slots currently assigned to npc workOrders
for (WorkOrder npcWorkOrder : ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor))
availableSlots = availableSlots - npcWorkOrder.cooking.size();
// Single item rolls are always a single slot
12 months ago
if (availableSlots > 0 && !workOrder.multiple_slot_request)
availableSlots = 1;
return availableSlots;
}
public static HashMap<mbEnums.ResourceType, Integer> calcProductionCost(WorkOrder workOrder) {
12 months ago
// Calculate production cost for a single run of this workOrder
HashMap<mbEnums.ResourceType, Integer> production_cost = new HashMap<>();
ItemTemplate template = ItemTemplate.templates.get(workOrder.templateID);
// Add gold and resource costs from template
production_cost.put(mbEnums.ResourceType.GOLD, template.item_value);
production_cost.putAll(template.item_resource_cost);
// Calculate cost of prefix and suffix
if (workOrder.prefixToken != 0) {
EffectsBase prefix = PowersManager.getEffectByToken(workOrder.prefixToken);
production_cost.putAll(PowersManager._effect_costMaps.get(prefix.getIDString()));
}
if (workOrder.suffixToken != 0) {
EffectsBase suffix = PowersManager.getEffectByToken(workOrder.suffixToken);
production_cost.putAll(PowersManager._effect_costMaps.get(suffix.getIDString()));
}
return production_cost;
}
public static Item forgeItem(WorkOrder workOrder) {
12 months ago
// Create new virtual item from specified template
12 months ago
ItemTemplate template = ItemTemplate.templates.get(workOrder.templateID);
Item forgedItem = new Item(workOrder.templateID);
12 months ago
// forgedItem gets a negative id; a virtual item which is not persisted
12 months ago
forgedItem.objectUUID = ItemManager.lastNegativeID.getAndDecrement();
forgedItem.containerType = mbEnums.ItemContainerType.FORGE;
forgedItem.ownerID = workOrder.vendor.getObjectUUID();
12 months ago
12 months ago
// item.upgradeDate is serialized (ItemProductionMsg)
// for vendor forge window completion time.
12 months ago
forgedItem.setDateToUpgrade(workOrder.completionTime);
12 months ago
// Assign a prefix and suffix to this item if random rolled
if (workOrder.prefixToken == 0)
forgedItem.prefixToken = calcRandomMod(workOrder.vendor, mbEnums.ItemModType.PREFIX, template.modTable);
else
forgedItem.prefixToken = workOrder.prefixToken;
if (workOrder.suffixToken == 0)
forgedItem.suffixToken = calcRandomMod(workOrder.vendor, mbEnums.ItemModType.SUFFIX, template.modTable);
else
forgedItem.suffixToken = workOrder.suffixToken;
12 months ago
// Random rolled items are unidentified until completed
if (workOrder.prefixToken == 0 && workOrder.suffixToken == 0)
forgedItem.flags.remove(mbEnums.ItemFlags.Identified);
else
forgedItem.flags.add(mbEnums.ItemFlags.Identified);
// Add virtual item to in-memory caches
workOrder.cooking.add(forgedItem);
DbManager.addToCache(forgedItem);
itemWorkOrderLookup.put(forgedItem, workOrder);
return forgedItem;
}
public static void completeWorkOrderBatch(WorkOrder workOrder) {
ArrayList<Item> toRemove = new ArrayList<>();
for (Item virutalItem : workOrder.cooking) {
// Identify completed items
virutalItem.flags.add(mbEnums.ItemFlags.Identified);
virutalItem.containerType = mbEnums.ItemContainerType.INVENTORY;
// Persist item
Item completedItem = DbManager.ItemQueries.PERSIST(virutalItem);
12 months ago
// Copy Prefix and Suffix tokens from virtual item.
completedItem.prefixToken = virutalItem.prefixToken;
completedItem.suffixToken = virutalItem.suffixToken;
// Add effects to these tokens. Writes to disk.
ItemManager.applyItemEffects(completedItem);
// Add to the vendor inventory
workOrder.vendor.charItemManager.addItemToInventory(completedItem);
ItemProductionMsg outMsg1 = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, completedItem, mbEnums.ProductionActionType.DEPOSIT, true);
DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg1, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
ItemProductionMsg outMsg2 = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, completedItem, mbEnums.ProductionActionType.CONFIRM_DEPOSIT, true);
DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg2, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
toRemove.add(virutalItem);
}
for (Item virtualItem : toRemove) {
12 months ago
// Remove virtual items from the forge window
ItemProductionMsg outMsg = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, virtualItem, mbEnums.ProductionActionType.CONFIRM_SETPRICE, true);
DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
12 months ago
// Remove virtual item from all collections
workOrder.cooking.remove(virtualItem);
itemWorkOrderLookup.remove(virtualItem);
DbManager.removeFromCache(virtualItem);
}
}
12 months ago
public static void forgeWorkOrderBatch(WorkOrder workOrder) {
12 months ago
// Completion time for this batch is in the future
workOrder.completionTime = System.currentTimeMillis() + workOrder.rollingDuration;
for (int i = 0; i < workOrder.slots_used; ++i) {
Item forged_item = forgeItem(workOrder);
12 months ago
// Update NPC window
ItemProductionMsg outMsg = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, forged_item, mbEnums.ProductionActionType.CONFIRM_PRODUCE, true);
12 months ago
DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg, mbEnums.DispatchChannel.SECONDARY, 700, false, false);
workOrder.total_produced = workOrder.total_produced + 1;
}
12 months ago
// Save updated state to disk
DbManager.WarehouseQueries.UPDATE_WORKORDER(workOrder);
}
public static int calcRandomMod(NPC vendor, mbEnums.ItemModType itemModType, int modTable) {
12 months ago
// Random prefix or suffix token based on item.template.modtable
int modifier = 0;
ModTypeTableEntry modTypeTableEntry = null;
12 months ago
ModTableEntry modTableEntry;
int rollForModifier;
switch (itemModType) {
case PREFIX:
int randomPrefix = vendor.getModTypeTable().get(vendor.getItemModTable().indexOf(modTable));
modTypeTableEntry = ModTypeTableEntry.rollTable(randomPrefix, ThreadLocalRandom.current().nextInt(1, 100 + 1));
break;
case SUFFIX:
int randomSuffix = vendor.getModSuffixTable().get(vendor.getItemModTable().indexOf(modTable));
modTypeTableEntry = ModTypeTableEntry.rollTable(randomSuffix, ThreadLocalRandom.current().nextInt(1, 100 + 1));
break;
}
if (modTypeTableEntry == null)
return 0;
rollForModifier = ThreadLocalRandom.current().nextInt(1, 100 + 1);
if (rollForModifier < 80) {
int randomModifier = LootManager.TableRoll(vendor.getLevel(), false);
modTableEntry = ModTableEntry.rollTable(modTypeTableEntry.modTableID, randomModifier);
EffectsBase effectsBase = PowersManager.getEffectByIDString(modTableEntry.action);
modifier = effectsBase.getToken();
}
return modifier;
}
}