forked from SevenChords/CipesAtHome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.h
185 lines (163 loc) · 9.34 KB
/
calculator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "base.h"
#include <stdbool.h>
#include "inventory.h"
#include "recipes.h"
#include "start.h"
#include "absl/base/attributes.h"
// Represent the action at a particular node in the roadmap
enum Action {
Begin,
Cook,
Sort_Alpha_Asc,
Sort_Alpha_Des,
Sort_Type_Asc,
Sort_Type_Des,
Ch5
};
// What do we do after producing a recipe?
enum HandleOutput {
Toss, // Toss the recipe itself
Autoplace, // The recipe is placed in an empty slot
TossOther // Toss a different item to make room
};
// Information pertaining to cooking a recipe
struct Cook {
int numItems;
enum Type_Sort item1;
int itemIndex1;
enum Type_Sort item2;
int itemIndex2;
enum Type_Sort output;
enum HandleOutput handleOutput;
enum Type_Sort toss;
int indexToss;
};
// You can't typedef the above struct as Cook as that conflicts with the Cook enum value.
// Try Cook_s or something instead.
// Information pertaining to Chapter 5 evaluation
struct CH5 {
int indexDriedBouquet; // index of item we toss to make room for Dried Bouquet
int indexCoconut; // index of item we toss to make room for Coconut
enum Action ch5Sort; // The sort type we perform after Coconut
int indexKeelMango; // index of item we toss to make room for Keel Mango
int indexCourageShell; // index of item we toss to make room for Courage Shell
int indexThunderRage; // index of Thunder Rage when we use it during Smorg (if 0-9, TR is removed)
int lateSort; // 0 - sort after Coconut, 1 - sort after Keel Mango
};
struct CH5_Eval {
int frames_DB;
int frames_CO;
int frames_KM;
int frames_CS;
int DB_place_index;
int CO_place_index;
int KM_place_index;
int CS_place_index;
int TR_use_index;
int frames_HD;
int frames_MC;
int frames_TR;
int sort_frames;
enum Action sort;
};
// Overall data pertaining to what we did at a particular point in the roadmap
struct MoveDescription {
enum Action action; // Cook, sort, handle CH5,...
void *data; // This data may be either of type Cook, CH5, or NULL if we are just sorting
int framesTaken; // How many frames were wasted to perform this move
int totalFramesTaken; // Cummulative frame loss
};
struct BranchPath {
int moves; // Represents how many nodes we've traversed down a particular branch (0 for root, 57 for leaf node)
struct Inventory inventory;
struct MoveDescription description;
struct BranchPath *prev;
struct BranchPath *next;
outputCreatedArray_t outputCreated; // Array of 58 items, true if item was produced, false if not; indexed by recipe ordering
int numOutputsCreated; // Number of valid outputCreated entries to eliminate a lengthy for-loop
struct BranchPath **legalMoves; // Represents possible next paths to take
int numLegalMoves;
ssize_t capacityLegalMoves;
int totalSorts;
};
// Structure to return the head and tail of an optimized roadmap
struct OptimizeResult {
struct BranchPath *root;
struct BranchPath *last;
};
// optimizeRoadmap functions
struct BranchPath* copyAllNodes(struct BranchPath* newNode, const struct BranchPath* oldNode);
struct OptimizeResult optimizeRoadmap(const struct BranchPath* root);
void reallocateRecipes(struct BranchPath *newRoot, const enum Type_Sort *rearranged_recipes, int num_rearranged_recipes);
int removeRecipesForReallocation(struct BranchPath *node, enum Type_Sort *rearranged_recipes);
// Legal move functions
ABSL_MUST_USE_RESULT // Output is newly allocated and needs to be freed at some point
struct BranchPath* createLegalMove(struct BranchPath* node, struct Inventory inventory, struct MoveDescription description, const outputCreatedArray_t outputsFulfilled, int numOutputsFulfilled);
void filterOut2Ingredients(struct BranchPath* node);
void finalizeChapter5Eval(struct BranchPath* node, struct Inventory inventory, struct CH5* ch5Data, int temp_frame_sum, const outputCreatedArray_t outputsFulfilled, int numOutputsFulfilled);
void finalizeLegalMove(struct BranchPath* node, int tempFrames, struct MoveDescription useDescription, struct Inventory tempInventory, const outputCreatedArray_t tempOutputsFulfilled, int numOutputsFulfilled, enum HandleOutput tossType, enum Type_Sort toss, int tossIndex);
void freeLegalMove(struct BranchPath* node, int index);
int getInsertionIndex(const struct BranchPath* node, int frames);
void insertIntoLegalMoves(int insertIndex, struct BranchPath* newLegalMove, struct BranchPath* curNode);
void popAllButFirstLegalMove(struct BranchPath* node);
void shiftDownLegalMoves(struct BranchPath *node, int lowerBound, int uppderBound);
void shiftUpLegalMoves(struct BranchPath* node, int startIndex);
// Cooking functions
void createCookDescription2Items(const struct BranchPath* node, struct Recipe recipe, struct ItemCombination combo, struct Inventory* tempInventory, int* ingredientLoc, int* tempFrames, int viableItems, struct MoveDescription* useDescription);
void createCookDescription1Item(const struct BranchPath* node, struct Recipe recipe, struct ItemCombination combo, struct Inventory* tempInventory, int* ingredientLoc, int* tempFrames, int viableItems, struct MoveDescription* useDescription);
struct MoveDescription createCookDescription(const struct BranchPath* node, struct Recipe recipe, struct ItemCombination combo, struct Inventory *tempInventory, int* tempFrames, int viableItems);
void fulfillRecipes(struct BranchPath* curNode);
void generateCook(struct MoveDescription* description, const struct ItemCombination combo, const struct Recipe recipe, const int* ingredientLoc, int swap);
void handleRecipeOutput(struct BranchPath* curNode, struct Inventory tempInventory, int tempFrames, struct MoveDescription useDescription, const outputCreatedArray_t tempOutputsFulfilled, int numOutputsFulfilled, enum Type_Sort output, int viableItems);
void tryTossInventoryItem(struct BranchPath* curNode, struct Inventory tempInventory, struct MoveDescription useDescription, const outputCreatedArray_t tempOutputsFulfilled, int numOutputsFulfilled, enum Type_Sort output, int tempFrames, int viableItems);
// Chapter 5 functions
void fulfillChapter5(struct BranchPath* curNode);
void handleChapter5Eval(struct BranchPath* node, struct Inventory inventory, const outputCreatedArray_t outputsFulfilled, int numOutputsFulfilled, struct CH5_Eval eval);
void handleChapter5EarlySortEndItems(struct BranchPath* node, struct Inventory inventory, const outputCreatedArray_t outputsFulfilled, int numOutputsFulfilled, struct CH5_Eval eval);
void handleChapter5Sorts(struct BranchPath* node, struct Inventory inventory, const outputCreatedArray_t outputsFulfilled, int numOutputsFulfilled, struct CH5_Eval eval);
void handleChapter5LateSortEndItems(struct BranchPath* node, struct Inventory inventory, const outputCreatedArray_t outputsFulfilled, int numOutputsFulfilled, struct CH5_Eval eval);
void handleDBCOAllocation0Nulls(struct BranchPath* curNode, struct Inventory tempInventory, const outputCreatedArray_t tempOutputsFulfilled, int numOutputsFulfilled, struct CH5_Eval eval);
void handleDBCOAllocation1Null(struct BranchPath* curNode, struct Inventory tempInventory, const outputCreatedArray_t tempOutputsFulfilled, int numOutputsFulfilled, struct CH5_Eval eval);
void handleDBCOAllocation2Nulls(struct BranchPath* curNode, struct Inventory tempInventory, const outputCreatedArray_t tempOutputsFulfilled, int numOutputsFulfilled, struct CH5_Eval eval);
struct CH5* createChapter5Struct(struct CH5_Eval eval, int lateSort);
// Initialization functions
void initializeInvFrames();
void initializeRecipeList();
// File output functions
void printCh5Data(const struct BranchPath* curNode, const struct MoveDescription desc, FILE* fp);
void printCh5Sort(const struct CH5* ch5Data, FILE* fp);
void printCookData(const struct BranchPath* curNode, const struct MoveDescription desc, FILE* fp);
void printFileHeader(FILE* fp);
void printInventoryData(const struct BranchPath* curNode, FILE* fp);
void printOutputsCreated(const struct BranchPath* curNode, FILE* fp);
void printNodeDescription(const struct BranchPath * curNode, FILE * fp);
void printResults(const char* filename, const struct BranchPath* path);
void printSortData(FILE* fp, enum Action curNodeAction);
// Select and random methodology functions
void handleSelectAndRandom(struct BranchPath* curNode, int select, int randomise);
void shuffleLegalMoves(struct BranchPath* node);
void softMin(struct BranchPath *node);
// Sorting functions
int alpha_sort(const void* elem1, const void* elem2);
int alpha_sort_reverse(const void* elem1, const void* elem2);
struct Inventory getSortedInventory(struct Inventory inventory, enum Action sort);
int getSortFrames(enum Action action);
void handleSorts(struct BranchPath* curNode);
int type_sort(const void* elem1, const void* elem2);
int type_sort_reverse(const void* elem1, const void* elem2);
// Frame calculation and optimization functions
void applyJumpStorageFramePenalty(struct BranchPath *node);
void generateFramesTaken(struct MoveDescription* description, const struct BranchPath* node, int framesTaken);
int selectSecondItemFirst(const int* ingredientLoc, int nulls, int viableItems);
void swapItems(int* ingredientLoc);
// General node functions
void freeAllNodes(struct BranchPath* node);
void freeNode(struct BranchPath *node);
struct BranchPath* initializeRoot();
// Other
void periodicGithubCheck();
// void logIterations(int ID, int stepIndex, const struct BranchPath * curNode, long iterationCount, long iterationLimit, int level);
struct Result calculateOrder(int rawID, long max_branches);
#endif