/******************** ResolveTree *************************/ TArray<int32> APOTLStructure::AllocateResources(APOTLStructure* To, TMap<FString, int32>& Resources, EAllocationType Type, int32 Sequence, bool Consume, int32 Key) { TArray<int32> KeyIndexes; for (auto& Resource : Resources) { int32 KeyIndex = AllocateResource(To, Resource.Key, Resource.Value, Type, Sequence, Consume, -1); KeyIndexes.Add(KeyIndex); } return KeyIndexes; }
void SoundResource::AddInternalRef() { //if ref > 0 load sound buffer internal_ref_count++; if(internal_ref_count > 0) { bActiveResource = true; AllocateResource(); //load sound buffer } }
/* * Do the tasks on all segments. */ void PerformSharedStorageOpTasks(SharedStorageOpTasks *tasks, enum SharedStorageOp op) { int i, j = 0; if (tasks->numTasks == 0) return; SharedStorageOpStmt *stat = makeNode(SharedStorageOpStmt); stat->op = op; stat->relFileNode = palloc(sizeof(RelFileNode) * tasks->numTasks); stat->segmentFileNum = palloc(sizeof(int) * tasks->numTasks); stat->relationName = palloc(sizeof(char *) * tasks->numTasks); QueryContextInfo *contextdisp = CreateQueryContextInfo(); for (i = 0, j = 0; i < tasks->numTasks; ++i) { SharedStorageOpTask *task = tasks->tasks + i; prepareDispatchedCatalogTablespace(contextdisp, task->node.spcNode); stat->relFileNode[j] = task->node; stat->segmentFileNum[j] = task->segno; stat->relationName[j] = task->relname; ++j; } stat->numTasks = j; FinalizeQueryContextInfo(contextdisp); int gp_segments_for_planner_before = gp_segments_for_planner; QueryResource *resource = AllocateResource(QRL_INHERIT, 0, 0, tasks->numTasks, 1, NULL, 0); DispatchDataResult result; dispatch_statement_node((Node *) stat, contextdisp, resource, &result); dispatch_free_result(&result); FreeResource(resource); DropQueryContextInfo(contextdisp); pfree(stat->relFileNode); pfree(stat->relationName); pfree(stat->segmentFileNum); pfree(stat); gp_segments_for_planner = gp_segments_for_planner_before; }
/******************** AllocateResource *************************/ int32 APOTLStructure::AllocateResource(APOTLStructure* To, FString ResourceKey, int32 Quantity, EAllocationType Type, int32 Sequence, bool Consume, int32 Key) { if (Key >= 0) { FST_ResourceAllocation Allocation; Allocation.To = To; Allocation.From = this; Allocation.ResourceKey = ResourceKey; Allocation.Type = Type; Allocation.Quantity = Quantity; Allocation.Sequence = Sequence; //~~ Remove resources from FreeResources on this, not the caller ~~// if (Consume) { if (FreeResources.Contains(ResourceKey) && FreeResources[ResourceKey] >= Quantity) { FreeResources[ResourceKey] = FreeResources[ResourceKey] - Quantity; if (FreeResources[ResourceKey] == 0) { FreeResources.Remove(ResourceKey); } } else { bool Bug = true; return -1; //~~ If FreeResources doesn't have the resource. Just for safe handling ~~// } } //~~ Add the allocation to root for keeping ~~// if (this->Root->AllocatedResources.Contains(Key)) { //~~ Overwrite existing allocation ~~// this->Root->AllocatedResources[Key] = Allocation; } else { this->Root->AllocatedResources.Add(Key, Allocation); } } else { //~~ Call itself to allocate the resource with the right keyindex ~~// Key = MakeAllocationKey(); AllocateResource(To, ResourceKey, Quantity, Type, Sequence, Consume, Key); } return Key; }
static int InitializeElementRecord( Ttk_ElementClass *eclass, /* Element instance to initialize */ Ttk_Style style, /* Style table */ char *widgetRecord, /* Source of widget option values */ Tk_OptionTable optionTable, /* Option table describing widget record */ Tk_Window tkwin, /* Corresponding window */ Ttk_State state) /* Widget or element state */ { char *elementRecord = eclass->elementRecord; OptionMap optionMap = GetOptionMap(eclass,optionTable); int nResources = eclass->nResources; Ttk_ResourceCache cache = style->cache; Ttk_ElementOptionSpec *elementOption = eclass->specPtr->options; int i; for (i=0; i<nResources; ++i, ++elementOption) { Tcl_Obj **dest = (Tcl_Obj **) (elementRecord + elementOption->offset); const char *optionName = elementOption->optionName; Tcl_Obj *dynamicSetting = Ttk_StyleMap(style, optionName, state); Tcl_Obj *widgetValue = 0; Tcl_Obj *elementDefault = eclass->defaultValues[i]; if (optionMap[i]) { widgetValue = *(Tcl_Obj **) (widgetRecord + optionMap[i]->objOffset); } if (widgetValue) { *dest = widgetValue; } else if (dynamicSetting) { *dest = dynamicSetting; } else { Tcl_Obj *styleDefault = Ttk_StyleDefault(style, optionName); *dest = styleDefault ? styleDefault : elementDefault; } if (!AllocateResource(cache, tkwin, dest, elementOption->type)) { return 0; } } return 1; }
/******************** ProcessDecay *************************/ void APOTLStructure::ProcessDecay() { // Wheat 4(+5) // [0],[1],[2] // Wheat +5, 4, 0 // Wheat 4(+1) (+5-4) // Flour 0(+2) // [0],[1],[2] // Wheat +1, 4-4=0, 0 // Flour +2, 0, 0 //? Production always adds to index zero, and billing always takes from last indexes. Consume from back through the array; //? Decay check will be mad after resource request are calculated. It is the only way that I know the amount of consumtion. //? Then make resource allocations based on MaxAge check //?? Reverse decay ??// //!! allocation have allready been reverse, but what about the queue? !!// //!! Backup/copy at start of each turn? !!// //DecayQueue // ID#1 [4,0,2,0,2] // ID#2 [2] // ID#3 [0,2] //~~ Overwrite decay queue with a copy of the backup ~~// DecayQueue = DecayQueueBackup; FString ADASdasd; //~~ Move freeResources up the decay queue ~~// for (auto& Decay : DecayQueue) { //FString ResourceKey = Decay.Key; TArray<int32>& Queue = Decay.Value; Queue.Insert(0, 0); /* for (int32 i = Queue.Num() - 1; i >= 0; i--) { } */ } //~~ Add production to decay queue ~~// for (auto& AllocatedResource : AllocatedResources) { FST_ResourceAllocation& Allocation = AllocatedResource.Value; if (Allocation.Type == EAllocationType::FactoryProduction) { static const FString ContextString(TEXT("GENERAL")); FST_Resource* ResourceData = GameInstance->DATA_Resources->FindRow<FST_Resource>(*Allocation.ResourceKey, ContextString); if (ResourceData->MaxAge == 0) { //int32 AllocationIndex = AllocateResource(nullptr, Allocation.ResourceKey, Allocation.Quantity, EAllocationType::Decay, -1, false, -1); //Allocation.Type = EAllocationType::Decay; //!! Almost right. But missing the decay information for displaying to the user (+1,-1). Allocation.Type = EAllocationType::ProductionDecay; } if (DecayQueue.Contains(Allocation.ResourceKey)) { TArray<int32>& Queue = DecayQueue[Allocation.ResourceKey]; if (Queue.IsValidIndex(0)) { Queue[0] += Allocation.Quantity; } else { Queue.Add(Allocation.Quantity); } } else { TArray<int32> Queue; Queue.Add(Allocation.Quantity); DecayQueue.Add(AllocatedResource.Value.ResourceKey, Queue); } } //~~ If bill and from root, then remove from decay que end ~~// else if (Allocation.Type == EAllocationType::FactoryBilling && Allocation.From == this) { if (DecayQueue.Contains(Allocation.ResourceKey)) { TArray<int32>& Queue = DecayQueue[Allocation.ResourceKey]; if (Queue.Num() > 0) { Queue[Queue.Num() - 1] -= Allocation.Quantity; } } } } /* //~~ Debug print ~~// int32 Count = 100; for (auto& Decay : DecayQueue) { Count++; FString ResourceKey = Decay.Key; TArray<int32>& Queue = Decay.Value; FString PrintString = ResourceKey + " : "; for (int32 i = 0; i < Queue.Num(); i++) { int32 QueuItem = Queue[i]; PrintString += FString::FromInt(QueuItem) + ", "; } GEngine->AddOnScreenDebugMessage(Count, 60.0f, FColor::Yellow, PrintString); } */ //~~ Make decay allocations based on maxage and decay queue ~~// for (auto& Decay : DecayQueue) { FString ResourceKey = Decay.Key; TArray<int32>& Queue = Decay.Value; static const FString ContextString(TEXT("GENERAL")); FST_Resource* ResourceData = GameInstance->DATA_Resources->FindRow<FST_Resource>(*ResourceKey, ContextString); if (ResourceData && ResourceData->MaxAge != -1) { for (int32 i = Queue.Num() - 1; i >= 0; i--) { if (ResourceData->MaxAge == 0) { Queue.RemoveAt(i); } else if (i >= ResourceData->MaxAge) { int32 AllocationIndex = AllocateResource(nullptr, ResourceKey, Queue[i], EAllocationType::Decay, -1, true, -1); Queue.RemoveAt(i); } } } } /* //~~ Debug print ~~// Count = 200; for (auto& Decay : DecayQueue) { Count++; FString ResourceKey = Decay.Key; TArray<int32>& Queue = Decay.Value; FString PrintString = ResourceKey + " : "; for (int32 i = 0; i < Queue.Num(); i++) { int32 QueuItem = Queue[i]; PrintString += FString::FromInt(QueuItem) + ", "; } GEngine->AddOnScreenDebugMessage(Count, 60.0f, FColor::Blue, PrintString); } */ // WP 3 - Production x 2 // Wheat 1 - Production x 1 }
/******************** ProcessResourceRequests *************************/ void APOTLStructure::ProcessResourceRequests() { int32 i; int32 HighestSequence = -1; TMap<int32, TArray<FST_ResourceRequest>> SortedResourceRequests; //~~ Get highest sequence from all of the requests ~~// for (i = 0; i < ResourceRequests.Num(); i++) { FST_ResourceRequest& ResourceRequest = ResourceRequests[i]; if (ResourceRequest.Sequence > HighestSequence) { HighestSequence = ResourceRequest.Sequence; } } //~~ Init SortedResourceRequests. Make empty arrays ~~// for (i = 0; i <= HighestSequence; i++) { TArray<FST_ResourceRequest> List; SortedResourceRequests.Add(i, List); } //~~ Add resource request to sorted tmap ~~// for (i = 0; i < ResourceRequests.Num(); i++) { FST_ResourceRequest& ResourceRequest = ResourceRequests[i]; int32 Sequence = ResourceRequest.Sequence; if (SortedResourceRequests.Contains(Sequence)) { SortedResourceRequests[Sequence].Add(ResourceRequest); } else { GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Red, "ERROR: SortedResourceRequests could not recivce request with a sequence not found "); } } //~~ Allocate and process the requests ~~// for (i = 0; i <= HighestSequence; i++) //~~ Handle the request from sequence zero and up ~~// { TArray<FST_ResourceRequest>& RequestList = SortedResourceRequests[i]; for (int32 ii = 0; ii < RequestList.Num(); ii++) // Loop through sequence row list. Ex 0[{R}, {R}, {R}] { FST_ResourceRequest& ResourceRequest = RequestList[ii]; if (ResourceRequest.Request.Num() > 1) { FString Sasdsa; } if (HasResourcesAvailable(ResourceRequest.Request, true, i)) //~~ If self has the resources required. (i) is the sequence, not (ii), it is just a list index for the nested array. ~~// { TArray<int32> AllocationIndexes; //~~ Requirements ~~// for (auto& ReqResource : ResourceRequest.Request) //~~ Loop each resource in request ~~// { //? AllocateResource with group id for grouping multiple allocations? if (FreeResources.Contains(ReqResource.Key)) { int32 AvailableQuantity = FreeResources[ReqResource.Key]; if (AvailableQuantity > ReqResource.Value) { AvailableQuantity = ReqResource.Value; } //int32 AllocationIndex = AllocateResource(ResourceRequest.From, ReqResource.Key, AvailableQuantity, EAllocationType::FactoryBilling, ii, true, -1); int32 AllocationIndex = AllocateResource(ResourceRequest.From, ReqResource.Key, AvailableQuantity, EAllocationType::FactoryBilling, i, true, -1); AllocationIndexes.Add(AllocationIndex); ReqResource.Value = ReqResource.Value - AvailableQuantity; } if (ReqResource.Value > 0) //~~ If freeresources couldn't meet the requiement of the resource quantity, then make reallocations ~~// { for (auto& AllocatedResource : AllocatedResources) { FST_ResourceAllocation& Allocation = AllocatedResource.Value; if (Allocation.ResourceKey == ReqResource.Key && Allocation.Sequence < ResourceRequest.Sequence && Allocation.To == this && Allocation.Type == EAllocationType::FactoryProduction && Allocation.Quantity > 0) { //!! BUG BEGIN if (ReqResource.Value < Allocation.Quantity) //~~ If request if less than allocation quantity, then split allocation ~~// { //Allocation.Quantity = Allocation.Quantity - ReqResource.Value; //~~ Subtract resource request quantity from the allocation, and then let it be ~~// //int32 NewAllocationAmount = Allocation.Quantity - ReqResource.Value; int32 NewAllocationAmount = ReqResource.Value; Allocation.Quantity = Allocation.Quantity - ReqResource.Value; //~~ Subtract resource request quantity from the allocation, and then let it be ~~// //Split logic //~~ Make new allocation with ~~// //int32 AllocationIndex = Allocation.From->AllocateResource(ResourceRequest.From, ReqResource.Key, ReqResource.Value, EAllocationType::FactoryBilling, ii, false, -1); int32 AllocationIndex = Allocation.From->AllocateResource(ResourceRequest.From, ReqResource.Key, NewAllocationAmount, EAllocationType::FactoryBilling, Allocation.Sequence, false, -1); AllocationIndexes.Add(AllocationIndex); ReqResource.Value = 0; } else if (ReqResource.Value >= Allocation.Quantity) //~~ If request is more or equal to allocation quantity, then just change allocation to target ~~// { Allocation.To = ResourceRequest.From; Allocation.Type = EAllocationType::FactoryBilling; //! Maybe it needs to be a unique type, like FactoryProductionReallocation or something. //ReqResource.Value = ReqResource.Value - AvailableQuantity; //ReqResource.Value = ReqResource.Value - Allocation.Quantity; FString TestValue = FString::FromInt(Allocation.Quantity); ReqResource.Value = ReqResource.Value - Allocation.Quantity; // 3 ? = 4 - 4 // ? = 4 - 2; } //!! BUG END } if (ReqResource.Value == 0) { break; } } if (ReqResource.Value > 0) { FString Bug; } } } //~~ Payoff ~~// for (auto& Resource : ResourceRequest.Payoff) //~~ Loop each resource in payoff ~~// { //~~ Add resource payoff to the keeper of the factory, and then allocate it to root afterwards ~~// //ResourceRequest.From->AddResource(Resource.Key, Resource.Value, EResourceList::Free); //~~ Add Resources and then consume them with AllocateResource(true) ~~// //!~~ No need for adding them, and then consuming them anymore. AllocateResource can not be called with consume: false ~~// //int32 AllocationIndex = ResourceRequest.From->AllocateResource(this, Resource.Key, Resource.Value, EAllocationType::FactoryProduction, ii, false, -1); // Maybe ii + 1 ? I think not. Are checked if sequence is lower, not lower or equal int32 AllocationIndex = ResourceRequest.From->AllocateResource(this, Resource.Key, Resource.Value, EAllocationType::FactoryProduction, i, false, -1); // Maybe ii + 1 ? I think not. Are checked if sequence is lower, not lower or equal if (ResourceRequest.Factory) { ResourceRequest.Factory->AllocationIndex = AllocationIndex; } AllocationIndexes.Add(AllocationIndex); } ResourceRequest.RequestMet = true; } else { } } } }
char *ReadFileChunk (const char * const filename_s) { /* The value that we will return */ char *result_s = NULL; /* * Allocate the Resource. * We know that it's a file so we use the protocol for a file, * PROTOCOL_FILE_S, and we do not need a title for the Resource * so we send it NULL. */ Resource res_p = AllocateResource (PROTOCOL_FILE_S, filename_s, NULL); if (res_p) { /* * Now that we have the Resource, let the Grassroots system find * the appropriate Handler. */ Handler *handler_p = GetResourceHandler (res_p, NULL); if (handler_p) { /* Scroll 20 bytes into the file */ if (SeekHandler (handler_p, 20, SEEK_SET)) { /* * We are going to try and read in 16 bytes so allocate * the memory needed including the extra byte for the * terminating \0 */ const size_t buffer_size = 16; char *buffer_s = (char *) AllocMemory ((buffer_size + 1) * sizeof (char)); if (buffer_s) { /* Read in the data */ if (ReadFromHandler (handler_p, buffer_s, buffer_size) == buffer_size) { /* Add the terminating \0 */ * (buffer_s + buffer_size) = '\0'; /* Store the value for returning */ result_s = buffer_s; } /* if (fread (buffer_s, 1, buffer_size, in_f) == buffer_size) */ else { PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to read value from %s", filename_s); /* We failed to read in the value so we can release the buffer memory */ FreeMemory (buffer_s); } } /* if (buffer_s) */ else { PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to allocate memory for buffer when reading %s", filename_s); } } /* if (SeekHandler (handler_p, 20, SEEK_SET)) */ else { PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to seek in %s", filename_s); } CloseHandler (handler_p); } /* if (handler_p) */ else { PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to find a Handler for %s", filename_s); } FreeResource (res_p); } /* if (res_p) */ else { PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to allocate Resource for %s", filename_s); } return result_s; }