mirror of
https://github.com/sfall-team/sfall.git
synced 2026-07-27 16:52:34 -07:00
Corrected code in Arrays.cpp
This commit is contained in:
@@ -375,7 +375,7 @@ static int GetExplosionDamage(int pid) {
|
||||
GetDamage(pid, min, max);
|
||||
}
|
||||
|
||||
DWORD arrayId = script::TempArray(2, 0);
|
||||
DWORD arrayId = script::CreateTempArray(2, 0);
|
||||
script::arrays[arrayId].val[0] = min;
|
||||
script::arrays[arrayId].val[1] = max;
|
||||
|
||||
|
||||
@@ -271,7 +271,7 @@ long LoadArrays(HANDLE h) {
|
||||
arrayVar.keyHash[arrayVar.val[j]] = j;
|
||||
}
|
||||
}
|
||||
while (arrays.find(nextArrayID) != arrays.end()) nextArrayID++;
|
||||
while (ArrayExist(nextArrayID)) nextArrayID++;
|
||||
if (nextArrayID == 0) nextArrayID++;
|
||||
|
||||
arrays.insert(array_pair(nextArrayID, arrayVar));
|
||||
@@ -302,7 +302,7 @@ void SaveArrays(HANDLE h) {
|
||||
for (it = savedArrays.begin(); it != savedArrays.end(); ++it) {
|
||||
arrayIt = arrays.find(it->second);
|
||||
if (arrayIt != arrays.end()) {
|
||||
sArrayVar &var = arrays[it->second];
|
||||
sArrayVar &var = arrayIt->second; //arrays[it->second];
|
||||
SaveArrayElement(&var.key, h); // type, key/length of key and string of key
|
||||
WriteFile(h, &var.flags, 4, &unused, 0);
|
||||
elCount = var.val.size();
|
||||
@@ -393,11 +393,11 @@ DWORD CreateArray(int len, DWORD flags) {
|
||||
}
|
||||
sArrayVar var(len, flags);
|
||||
|
||||
if (!var.isAssoc()) {
|
||||
if (!var.isAssoc() && len) {
|
||||
var.val.resize(len);
|
||||
}
|
||||
|
||||
while (arrays.find(nextArrayID) != arrays.end()) nextArrayID++;
|
||||
while (ArrayExist(nextArrayID)) nextArrayID++;
|
||||
if (nextArrayID == 0) nextArrayID++;
|
||||
|
||||
if (arraysBehavior == 0) {
|
||||
@@ -409,7 +409,7 @@ DWORD CreateArray(int len, DWORD flags) {
|
||||
return nextArrayID++;
|
||||
}
|
||||
|
||||
DWORD TempArray(DWORD len, DWORD flags) {
|
||||
DWORD CreateTempArray(DWORD len, DWORD flags) {
|
||||
DWORD id = CreateArray(len, flags);
|
||||
temporaryArrays.insert(id);
|
||||
return id;
|
||||
@@ -434,7 +434,7 @@ void DeleteAllTempArrays() {
|
||||
}
|
||||
|
||||
ScriptValue GetArrayKey(DWORD id, int index) {
|
||||
if (arrays.find(id) == arrays.end() || index < -1 || index > arrays[id].size()) {
|
||||
if (!ArrayExist(id) || index < -1 || index > arrays[id].size()) {
|
||||
return ScriptValue(0);
|
||||
}
|
||||
if (index == -1) { // special index to indicate if array is associative
|
||||
@@ -461,9 +461,8 @@ ScriptValue GetArrayKey(DWORD id, int index) {
|
||||
}
|
||||
|
||||
ScriptValue GetArray(DWORD id, const ScriptValue& key) {
|
||||
if (arrays.find(id) == arrays.end()) {
|
||||
return 0;
|
||||
}
|
||||
if (!ArrayExist(id)) return 0;
|
||||
|
||||
int el;
|
||||
sArrayVar &arr = arrays[id];
|
||||
if (arr.isAssoc()) {
|
||||
@@ -539,8 +538,7 @@ void setArray(DWORD id, const ScriptValue& key, const ScriptValue& val, bool all
|
||||
}
|
||||
|
||||
void SetArray(DWORD id, const ScriptValue& key, const ScriptValue& val, bool allowUnset) {
|
||||
if (!ArrayExist(id)) return;
|
||||
setArray(id, key, val, allowUnset);
|
||||
if (ArrayExist(id)) setArray(id, key, val, allowUnset);
|
||||
}
|
||||
|
||||
int LenArray(DWORD id) {
|
||||
@@ -605,7 +603,7 @@ static void MapSort(sArrayVar& arr, int type) {
|
||||
}
|
||||
|
||||
long ResizeArray(DWORD id, int newlen) {
|
||||
if (newlen == -1 || arrays.find(id) == arrays.end()) return 0;
|
||||
if (newlen == -1 || !ArrayExist(id)) return 0;
|
||||
|
||||
sArrayVar &arr = arrays[id];
|
||||
int arrSize = arr.size();
|
||||
@@ -652,7 +650,7 @@ void FixArray(DWORD id) {
|
||||
}
|
||||
|
||||
ScriptValue ScanArray(DWORD id, const ScriptValue& val) {
|
||||
if (arrays.find(id) == arrays.end()) {
|
||||
if (!ArrayExist(id)) {
|
||||
return ScriptValue(-1);
|
||||
}
|
||||
char step = arrays[id].isAssoc() ? 2 : 1;
|
||||
@@ -680,7 +678,7 @@ DWORD LoadArray(const ScriptValue& key) {
|
||||
sArrayElement keyEl = sArrayElement(key.rawValue(), key.type());
|
||||
|
||||
if (keyEl.type == DataType::STR && strcmp(keyEl.strVal, get_all_arrays_special_key) == 0) { // this is a special case to get temp array containing all saved arrays
|
||||
DWORD tmpArrId = TempArray(savedArrays.size(), 0);
|
||||
DWORD tmpArrId = CreateTempArray(savedArrays.size(), 0);
|
||||
if (tmpArrId > 0) {
|
||||
std::vector<sArrayElement>::iterator elIt;
|
||||
ArrayKeysMap::iterator it;
|
||||
@@ -744,7 +742,7 @@ long StackArray(const ScriptValue& key, const ScriptValue& val) {
|
||||
if (!arrays[stackArrayId].isAssoc()) { // automatically resize array to fit one new element
|
||||
size_t size = arrays[stackArrayId].val.size();
|
||||
if (size >= ARRAY_MAX_SIZE) return 0;
|
||||
if (key.rawValue() >= size) arrays[stackArrayId].val.resize(size + 10); // resize with a small margin
|
||||
if (key.rawValue() >= size) arrays[stackArrayId].val.resize(size + 1);
|
||||
}
|
||||
setArray(stackArrayId, key, val, false);
|
||||
return 0;
|
||||
|
||||
@@ -89,6 +89,7 @@ struct sArrayVarOld
|
||||
|
||||
#define ARRAYFLAG_ASSOC (1) // is map
|
||||
#define ARRAYFLAG_CONSTVAL (2) // don't update value of key if the key exists in map
|
||||
#define ARRAYFLAG_RESERVED (4)
|
||||
|
||||
typedef std::unordered_map<sArrayElement, DWORD, sArrayElement_HashFunc, sArrayElement_EqualFunc> ArrayKeysMap;
|
||||
|
||||
@@ -110,7 +111,8 @@ public:
|
||||
sArrayVar() : flags(0), key() {}
|
||||
|
||||
sArrayVar(int len, int flag) : flags(flag), key() {
|
||||
if (len > 0) val.reserve(len + 10);
|
||||
if (len == 0) val.reserve((flag & 0x7FFF0000) >> 16);
|
||||
if (len < 0) val.reserve(20); // for assoc
|
||||
}
|
||||
|
||||
bool isAssoc() const {
|
||||
@@ -168,7 +170,7 @@ void DESetArray(int id, const DWORD* types, const char* data);
|
||||
DWORD CreateArray(int len, DWORD flags);
|
||||
|
||||
// same as CreateArray, but creates temporary array instead (will die at the end of the frame)
|
||||
DWORD TempArray(DWORD len, DWORD flags);
|
||||
DWORD CreateTempArray(DWORD len, DWORD flags);
|
||||
|
||||
// destroys array
|
||||
void FreeArray(DWORD id);
|
||||
|
||||
@@ -87,7 +87,7 @@ void op_resize_array(OpcodeContext& ctx) {
|
||||
}
|
||||
|
||||
void op_temp_array(OpcodeContext& ctx) {
|
||||
auto arrayId = TempArray(ctx.arg(0).rawValue(), ctx.arg(1).rawValue());
|
||||
auto arrayId = CreateTempArray(ctx.arg(0).rawValue(), ctx.arg(1).rawValue());
|
||||
ctx.setReturn(arrayId);
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ static DWORD ListAsArray(DWORD type) {
|
||||
std::vector<fo::GameObject*> vec = std::vector<fo::GameObject*>();
|
||||
FillListVector(type, vec);
|
||||
size_t sz = vec.size();
|
||||
DWORD id = TempArray(sz, 0);
|
||||
DWORD id = CreateTempArray(sz, 0);
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
arrays[id].val[i].set((long)vec[i]);
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ void mf_get_sfall_arg_at(OpcodeContext& ctx) {
|
||||
|
||||
void op_get_sfall_args(OpcodeContext& ctx) {
|
||||
DWORD argCount = HookCommon::GetHSArgCount();
|
||||
DWORD id = TempArray(argCount, 0);
|
||||
DWORD id = CreateTempArray(argCount, 0);
|
||||
DWORD* args = HookCommon::GetHSArgs();
|
||||
for (DWORD i = 0; i < argCount; i++) {
|
||||
arrays[id].val[i].set(*(long*)&args[i]);
|
||||
|
||||
@@ -157,7 +157,7 @@ static const SfallMetarule metarules[] = {
|
||||
|
||||
// returns current contents of metarule table
|
||||
static void mf_get_metarule_table(OpcodeContext& ctx) {
|
||||
DWORD arrId = TempArray(metaruleTable.size(), 0);
|
||||
DWORD arrId = CreateTempArray(metaruleTable.size(), 0);
|
||||
int i = 0;
|
||||
for (auto it = metaruleTable.begin(); it != metaruleTable.end(); ++it) {
|
||||
arrays[arrId].val[i].set(it->first.c_str());
|
||||
|
||||
@@ -856,7 +856,7 @@ static std::string GetIniFilePath(const ScriptValue &arg) {
|
||||
|
||||
void mf_get_ini_sections(OpcodeContext& ctx) {
|
||||
if (!GetPrivateProfileSectionNamesA(ScriptExtender::gTextBuffer, ScriptExtender::TextBufferSize(), GetIniFilePath(ctx.arg(0)).c_str())) {
|
||||
ctx.setReturn(TempArray(0, 0));
|
||||
ctx.setReturn(CreateTempArray(0, 0));
|
||||
return;
|
||||
}
|
||||
std::vector<char*> sections;
|
||||
@@ -866,7 +866,7 @@ void mf_get_ini_sections(OpcodeContext& ctx) {
|
||||
section += std::strlen(section) + 1;
|
||||
}
|
||||
size_t sz = sections.size();
|
||||
int arrayId = TempArray(sz, 0);
|
||||
int arrayId = CreateTempArray(sz, 0);
|
||||
auto& arr = arrays[arrayId];
|
||||
|
||||
for (size_t i = 0; i < sz; ++i) {
|
||||
@@ -879,7 +879,7 @@ void mf_get_ini_sections(OpcodeContext& ctx) {
|
||||
|
||||
void mf_get_ini_section(OpcodeContext& ctx) {
|
||||
auto section = ctx.arg(1).strValue();
|
||||
int arrayId = TempArray(-1, 0); // associative
|
||||
int arrayId = CreateTempArray(-1, 0); // associative
|
||||
|
||||
if (GetPrivateProfileSectionA(section, ScriptExtender::gTextBuffer, ScriptExtender::TextBufferSize(), GetIniFilePath(ctx.arg(0)).c_str())) {
|
||||
auto& arr = arrays[arrayId];
|
||||
|
||||
@@ -186,7 +186,7 @@ void op_make_path(OpcodeContext& ctx) {
|
||||
|
||||
char pathData[800];
|
||||
long pathLength = fo::func::make_path_func(objFrom, objFrom->tile, tileTo, pathData, checkFlag, (void*)func);
|
||||
auto arrayId = TempArray(pathLength, 0);
|
||||
auto arrayId = CreateTempArray(pathLength, 0);
|
||||
for (int i = 0; i < pathLength; i++) {
|
||||
arrays[arrayId].val[i].set((long)pathData[i]);
|
||||
}
|
||||
@@ -209,7 +209,7 @@ void op_obj_blocking_at(OpcodeContext& ctx) {
|
||||
void op_tile_get_objects(OpcodeContext& ctx) {
|
||||
DWORD tile = ctx.arg(0).rawValue(),
|
||||
elevation = ctx.arg(1).rawValue();
|
||||
DWORD arrayId = TempArray(0, 4);
|
||||
DWORD arrayId = CreateTempArray(0, 4);
|
||||
auto obj = fo::func::obj_find_first_at_tile(elevation, tile);
|
||||
while (obj) {
|
||||
arrays[arrayId].push_back(reinterpret_cast<long>(obj));
|
||||
@@ -221,7 +221,7 @@ void op_tile_get_objects(OpcodeContext& ctx) {
|
||||
void op_get_party_members(OpcodeContext& ctx) {
|
||||
auto includeHidden = ctx.arg(0).rawValue();
|
||||
int actualCount = fo::var::partyMemberCount;
|
||||
DWORD arrayId = TempArray(0, 4);
|
||||
DWORD arrayId = CreateTempArray(0, 4);
|
||||
auto partyMemberList = fo::var::partyMemberList;
|
||||
for (int i = 0; i < actualCount; i++) {
|
||||
auto obj = reinterpret_cast<fo::GameObject*>(partyMemberList[i * 4]);
|
||||
@@ -464,7 +464,7 @@ void mf_get_object_ai_data(OpcodeContext& ctx) {
|
||||
value = cap->called_freq;
|
||||
break;
|
||||
case 14:
|
||||
arrayId = TempArray(3, 0);
|
||||
arrayId = CreateTempArray(3, 0);
|
||||
arrays[arrayId].val[0].set(cap->chem_primary_desire);
|
||||
arrays[arrayId].val[1].set(cap->chem_primary_desire1);
|
||||
arrays[arrayId].val[2].set(cap->chem_primary_desire2);
|
||||
@@ -521,7 +521,7 @@ void mf_objects_in_radius(OpcodeContext& ctx) {
|
||||
objects.reserve(25);
|
||||
fo::GetObjectsTileRadius(objects, ctx.arg(0).rawValue(), radius, elev, type);
|
||||
size_t sz = objects.size();
|
||||
DWORD id = TempArray(sz, 0);
|
||||
DWORD id = CreateTempArray(sz, 0);
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
arrays[id].val[i].set((long)objects[i]);
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ static int __stdcall StringSplit(const char* str, const char* split) {
|
||||
size_t count, splitLen = strlen(split);
|
||||
if (!splitLen) {
|
||||
count = strlen(str);
|
||||
id = TempArray(count, 0);
|
||||
id = CreateTempArray(count, 0);
|
||||
for (DWORD i = 0; i < count; i++) {
|
||||
arrays[id].val[i].set(&str[i], 1);
|
||||
}
|
||||
@@ -138,7 +138,7 @@ static int __stdcall StringSplit(const char* str, const char* split) {
|
||||
count++;
|
||||
ptr = newptr + splitLen;
|
||||
}
|
||||
id = TempArray(count, 0);
|
||||
id = CreateTempArray(count, 0);
|
||||
ptr = str;
|
||||
count = 0;
|
||||
while (true) {
|
||||
|
||||
@@ -191,7 +191,7 @@ void mf_set_map_enter_position(OpcodeContext& ctx) {
|
||||
}
|
||||
|
||||
void mf_get_map_enter_position(OpcodeContext& ctx) {
|
||||
DWORD id = TempArray(3, 0);
|
||||
DWORD id = CreateTempArray(3, 0);
|
||||
arrays[id].val[0].set((long)fo::var::tile);
|
||||
arrays[id].val[1].set((long)fo::var::elevation);
|
||||
arrays[id].val[2].set((long)fo::var::rotation);
|
||||
|
||||
Reference in New Issue
Block a user