Skip to content

Commit

Permalink
refactor: start implementing subscreen scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyV99 committed Sep 9, 2023
1 parent 28d9aaf commit e19d0fd
Show file tree
Hide file tree
Showing 30 changed files with 1,011 additions and 160 deletions.
1 change: 1 addition & 0 deletions src/base/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void set_bitl(int32_t bitstr,int32_t bit,byte val);
#define NUMSCRIPTSITEMSPRITE 256
#define NUMSCRIPTSCOMBODATA 512
#define NUMSCRIPTSGENERIC 512
#define NUMSCRIPTSSUBSCREEN 256

//Script-related
#define INITIAL_A 2
Expand Down
12 changes: 7 additions & 5 deletions src/base/zdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ enum {ENC_METHOD_192B104=0, ENC_METHOD_192B105, ENC_METHOD_192B185, ENC_METHOD_2
#define V_HEROSPRITES 16
#define V_SUBSCREEN 8
#define V_ITEMDROPSETS 2
#define V_FFSCRIPT 21
#define V_FFSCRIPT 22
#define V_SFX 8
#define V_FAVORITES 3

Expand Down Expand Up @@ -1570,10 +1570,11 @@ class refInfo
//to implement
dword dropsetref, pondref, warpringref, doorsref, zcoloursref, rgbref, paletteref, palcycleref, tunesref;
dword gamedataref, cheatsref;
dword fileref, subscreenref, comboidref, directoryref, rngref, stackref, paldataref;
dword fileref, comboidref, directoryref, rngref, stackref, paldataref;
dword bottletyperef, bottleshopref, genericdataref;
int32_t combosref, comboposref;
int32_t portalref, saveportalref;
dword subdataref, subpageref, subwidgref;
//byte ewpnclass, lwpnclass, guyclass; //Not implemented

//byte ewpnclass, lwpnclass, guyclass; //Not implemented
Expand Down Expand Up @@ -1616,14 +1617,15 @@ enum class ScriptType {
Ewpn,
DMap,
ItemSprite,
ActiveSubscreen,
PassiveSubscreen,
ScriptedActiveSubscreen,
ScriptedPassiveSubscreen,
Combo,
OnMap,
Generic,
GenericFrozen,
EngineSubscreen,
First = Global,
Last = GenericFrozen,
Last = EngineSubscreen,
};
const char* ScriptTypeToString(ScriptType type);

Expand Down
8 changes: 7 additions & 1 deletion src/dialog/compilezscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ extern FFScript FFCore;
extern std::vector<string> asffcscripts, asglobalscripts, asitemscripts,
asnpcscripts, aseweaponscripts, aslweaponscripts,
asplayerscripts, asdmapscripts, asscreenscripts,
asitemspritescripts, ascomboscripts, asgenericscripts;
asitemspritescripts, ascomboscripts, asgenericscripts,
assubscreenscripts;
extern std::map<int32_t, script_slot_data > globalmap;

byte compile_success_sample = 0;
Expand Down Expand Up @@ -340,6 +341,8 @@ bool do_compile_and_slots(int assign_mode, bool delay)
ascomboscripts.push_back("<none>");
asgenericscripts.clear();
asgenericscripts.push_back("<none>");
assubscreenscripts.clear();
assubscreenscripts.push_back("<none>");
clear_map_states();
globalmap[0].updateName("~Init"); //force name to ~Init

Expand Down Expand Up @@ -387,6 +390,9 @@ bool do_compile_and_slots(int assign_mode, bool delay)
case scrTypeIdGlobal:
asglobalscripts.push_back(name);
break;
case scrTypeIdSusbcrData:
assubscreenscripts.push_back(name);
break;
}
}

Expand Down
52 changes: 46 additions & 6 deletions src/parser/BuildVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,22 @@ void BuildOpcodes::caseExprArrow(ASTExprArrow& host, void* param)
Function* readfunc = isarray ? host.arrayFunction : host.readFunction;
assert(readfunc->isInternal());

if(readfunc->getFlag(FUNCFLAG_INLINE))
if(readfunc->getFlag(FUNCFLAG_NIL))
{
bool skipptr = readfunc->internal_flags & IFUNCFLAG_SKIPPOINTER;
if (!skipptr)
{
//visit the lhs of the arrow
visit(host.left.get(), param);
}

if(isIndexed)
{
visit(host.index.get(), param);
}
addOpcode(new OSetImmediate(new VarArgument(EXP1), new LiteralArgument(0)));
}
else if(readfunc->getFlag(FUNCFLAG_INLINE))
{
if (!(readfunc->internal_flags & IFUNCFLAG_SKIPPOINTER))
{
Expand Down Expand Up @@ -1292,7 +1307,7 @@ void BuildOpcodes::caseExprCall(ASTExprCall& host, void* param)

auto* optarg = opcodeTargets.back();
int32_t startRefCount = arrayRefs.size(); //Store ref count
if(func.prototype) //Prototype function
if(func.getFlag(FUNCFLAG_NIL) || func.prototype) //Prototype/Nil function
{
//Visit each parameter, in case there are side-effects; but don't push the results, as they are unneeded.
for (auto it = host.parameters.begin();
Expand Down Expand Up @@ -1324,10 +1339,10 @@ void BuildOpcodes::caseExprCall(ASTExprCall& host, void* param)
DataType const& retType = *func.returnType;
if(retType != DataType::ZVOID)
{
int32_t retval = 0;
if (std::optional<int32_t> val = func.defaultReturn->getCompileTimeValue(NULL, scope))
{
addOpcode(new OSetImmediate(new VarArgument(EXP1), new LiteralArgument(*val)));
}
retval = *val;
addOpcode(new OSetImmediate(new VarArgument(EXP1), new LiteralArgument(retval)));
}
}
}
Expand Down Expand Up @@ -3361,7 +3376,32 @@ void LValBOHelper::caseExprArrow(ASTExprArrow &host, void *param)
int32_t isIndexed = (host.index != NULL);
assert(host.writeFunction->isInternal());

if(host.writeFunction->getFlag(FUNCFLAG_INLINE))
if(host.writeFunction->getFlag(FUNCFLAG_NIL))
{
bool skipptr = host.writeFunction->internal_flags & IFUNCFLAG_SKIPPOINTER;
bool needs_pushpop = isIndexed || !skipptr;
if(needs_pushpop)
addOpcode(new OPushRegister(new VarArgument(EXP1)));
if (!skipptr)
{
//Get lval
BuildOpcodes oc(scope);
oc.parsing_user_class = parsing_user_class;
oc.visit(host.left.get(), param);
addOpcodes(oc.getResult());
}

if(isIndexed)
{
BuildOpcodes oc2(scope);
oc2.parsing_user_class = parsing_user_class;
oc2.visit(host.index.get(), param);
addOpcodes(oc2.getResult());
}
if(needs_pushpop)
addOpcode(new OPopRegister(new VarArgument(EXP1)));
}
else if(host.writeFunction->getFlag(FUNCFLAG_INLINE))
{
if (!(host.writeFunction->internal_flags & IFUNCFLAG_SKIPPOINTER))
{
Expand Down
12 changes: 6 additions & 6 deletions src/parser/ByteCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2011,8 +2011,8 @@ string ZScript::VarToString(int32_t ID)
case GAMETRIGGROUPS: return "GAMETRIGGROUPS";
case GAMEOVERRIDEITEMS: return "GAMEOVERRIDEITEMS";
case DMAPDATASUBSCRO: return "DMAPDATASUBSCRO";
case RESRVD_VAR_EMILY45: return "RESRVD_VAR_EMILY45";
case RESRVD_VAR_EMILY46: return "RESRVD_VAR_EMILY46";
case REFSUBSCREENPAGE: return "REFSUBSCREENPAGE";
case REFSUBSCREENWIDG: return "REFSUBSCREENWIDG";
case RESRVD_VAR_EMILY47: return "RESRVD_VAR_EMILY47";
case RESRVD_VAR_EMILY48: return "RESRVD_VAR_EMILY48";
case RESRVD_VAR_EMILY49: return "RESRVD_VAR_EMILY49";
Expand Down Expand Up @@ -2648,13 +2648,13 @@ string OArrayPop::toString() const
{
return "ARRAYPOP";
}
string OResrvdOpEmily17::toString() const
string OLoadSubscreenDataRV::toString() const
{
return "RESRVD_OP_EMILY_17";
return "LOADSUBDATARV " + getFirstArgument()->toString() + "," + getSecondArgument()->toString();
}
string OResrvdOpEmily18::toString() const
string ONumSubscreensV::toString() const
{
return "RESRVD_OP_EMILY_18";
return "NUMSUBSCREENSV " + getArgument()->toString();
}
string OResrvdOpEmily19::toString() const
{
Expand Down
14 changes: 8 additions & 6 deletions src/parser/ByteCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -1550,8 +1550,8 @@
#define GAMETRIGGROUPS 1463
#define GAMEOVERRIDEITEMS 1464
#define DMAPDATASUBSCRO 1465
#define RESRVD_VAR_EMILY45 1466
#define RESRVD_VAR_EMILY46 1467
#define REFSUBSCREENPAGE 1466
#define REFSUBSCREENWIDG 1467
#define RESRVD_VAR_EMILY47 1468
#define RESRVD_VAR_EMILY48 1469
#define RESRVD_VAR_EMILY49 1470
Expand Down Expand Up @@ -2483,22 +2483,24 @@ namespace ZScript
return new OArrayPop();
}
};
class OResrvdOpEmily17 : public Opcode
class OLoadSubscreenDataRV : public BinaryOpcode
{
public:
OLoadSubscreenDataRV(Argument *A, Argument *B) : BinaryOpcode(A,B) {}
std::string toString() const;
Opcode* clone() const
{
return new OResrvdOpEmily17();
return new OLoadSubscreenDataRV(a->clone(),b->clone());
}
};
class OResrvdOpEmily18 : public Opcode
class ONumSubscreensV : public UnaryOpcode
{
public:
ONumSubscreensV(Argument *A) : UnaryOpcode(A) {}
std::string toString() const;
Opcode* clone() const
{
return new OResrvdOpEmily18();
return new ONumSubscreensV(a->clone());
}
};
class OResrvdOpEmily19 : public Opcode
Expand Down
18 changes: 17 additions & 1 deletion src/parser/LibrarySymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ void setIndexedVariable(int32_t refVar, Function* function, int32_t var)
function->giveCode(code);
}

void handleNil(int32_t refVar, Function* function)
{
function->setFlag(FUNCFLAG_INLINE);
if(refVar == NUL)
function->setFlag(IFUNCFLAG_SKIPPOINTER);
int32_t label = function->getLabel();
vector<shared_ptr<Opcode>> code;
addOpcode2(code, new ONoOp());
LABELBACK(label);
function->giveCode(code);
}

void LibrarySymbols::addSymbolsToScope(Scope& scope)
{
if(!table) return;
Expand Down Expand Up @@ -236,7 +248,11 @@ void LibrarySymbols::addSymbolsToScope(Scope& scope)
}
// Generate function code for getters/setters
int32_t label = function->getLabel();
switch(setorget)
if(function->getFlag(FUNCFLAG_NIL))
{
handleNil(refVar, function);
}
else switch(setorget)
{
case GETTER:
if (isArray)
Expand Down
2 changes: 2 additions & 0 deletions src/parser/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ namespace ZScript
ZCLID_PALDATA,
ZCLID_PORTAL,
ZCLID_SAVPORTAL,
ZCLID_SUBSCREENPAGE,
ZCLID_SUBSCREENWIDGET,
ZCLID_END
};

Expand Down
10 changes: 9 additions & 1 deletion src/parser/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ DataTypeClassConst DataType::CBOTTLESHOP(ZCLID_BOTTLESHOP, "const bottleshopdata
DataTypeClassConst DataType::CGENERICDATA(ZCLID_GENERICDATA, "const genericdata");
DataTypeClassConst DataType::CPORTAL(ZCLID_PORTAL, "const portal");
DataTypeClassConst DataType::CSAVEDPORTAL(ZCLID_SAVPORTAL, "const savedportal");
DataTypeClassConst DataType::CSUBSCREENPAGE(ZCLID_SUBSCREENPAGE, "const SubscreenPage");
DataTypeClassConst DataType::CSUBSCREENWIDGET(ZCLID_SUBSCREENWIDGET, "const SubscreenWidget");
//Class: Var Types
DataTypeClass DataType::BITMAP(ZCLID_BITMAP, "Bitmap", &CBITMAP);
DataTypeClass DataType::CHEATS(ZCLID_CHEATS, "Cheats", &CCHEATS);
Expand Down Expand Up @@ -226,6 +228,8 @@ DataTypeClass DataType::BOTTLESHOP(ZCLID_BOTTLESHOP, "bottleshopdata", &CBOTTLES
DataTypeClass DataType::GENERICDATA(ZCLID_GENERICDATA, "genericdata", &CGENERICDATA);
DataTypeClass DataType::PORTAL(ZCLID_PORTAL, "portal", &CPORTAL);
DataTypeClass DataType::SAVEDPORTAL(ZCLID_SAVPORTAL, "savedportal", &CSAVEDPORTAL);
DataTypeClass DataType::SUBSCREENPAGE(ZCLID_SUBSCREENPAGE, "SubscreenPage", &CSUBSCREENPAGE);
DataTypeClass DataType::SUBSCREENWIDGET(ZCLID_SUBSCREENWIDGET, "SubscreenWidget", &CSUBSCREENWIDGET);

////////////////////////////////////////////////////////////////
// DataType
Expand Down Expand Up @@ -265,6 +269,8 @@ DataType const* DataType::get(DataTypeId id)
case ZTID_COMBOS: return &COMBOS;
case ZTID_SPRITEDATA: return &SPRITEDATA;
case ZTID_SUBSCREENDATA: return &SUBSCREENDATA;
case ZTID_SUBSCREENPAGE: return &SUBSCREENPAGE;
case ZTID_SUBSCREENWIDGET: return &SUBSCREENWIDGET;
case ZTID_FILE: return &FILE;
case ZTID_DIRECTORY: return &DIRECTORY;
case ZTID_STACK: return &STACK;
Expand Down Expand Up @@ -319,6 +325,8 @@ DataTypeClass const* DataType::getClass(int32_t classId)
case ZCLID_COMBOS: return &COMBOS;
case ZCLID_SPRITEDATA: return &SPRITEDATA;
case ZCLID_SUBSCREENDATA: return &SUBSCREENDATA;
case ZCLID_SUBSCREENPAGE: return &SUBSCREENPAGE;
case ZCLID_SUBSCREENWIDGET: return &SUBSCREENWIDGET;
case ZCLID_FILE: return &FILE;
case ZCLID_DIRECTORY: return &DIRECTORY;
case ZCLID_STACK: return &STACK;
Expand Down Expand Up @@ -725,7 +733,7 @@ namespace // file local
{"itemsprite", ZTID_ITEM},
{"untyped", ZTID_VOID},
{"combodata", ZTID_COMBOS},
{"subscreendata", ZTID_VOID},
{"subscreendata", ZTID_SUBSCREENDATA},
{"generic",ZTID_GENERICDATA},
};
}
Expand Down
16 changes: 15 additions & 1 deletion src/parser/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ namespace ZScript
ZTID_PALDATA,
ZTID_PORTAL,
ZTID_SAVPORTAL,
ZTID_SUBSCREENPAGE,
ZTID_SUBSCREENWIDGET,
ZTID_CLASS_END,

ZTID_END = ZTID_CLASS_END
Expand Down Expand Up @@ -245,6 +247,10 @@ namespace ZScript
return "SAVEDPORTAL";
case ZTID_ZINFO:
return "ZINFO";
case ZTID_SUBSCREENPAGE:
return "SUBSCREENPAGE";
case ZTID_SUBSCREENWIDGET:
return "SUBSCREENWIDGET";
default:
return "INT";
/*char buf[16];
Expand Down Expand Up @@ -366,6 +372,10 @@ namespace ZScript
return ZTID_PORTAL;
else if(name == "SAVEDPORTAL")
return ZTID_SAVPORTAL;
else if(name == "SUBSCREENPAGE")
return ZTID_SUBSCREENPAGE;
else if(name == "SUBSCREENWIDGET")
return ZTID_SUBSCREENWIDGET;

return ZTID_VOID;
}
Expand Down Expand Up @@ -496,6 +506,8 @@ namespace ZScript
static DataTypeClassConst CGENERICDATA;
static DataTypeClassConst CPORTAL;
static DataTypeClassConst CSAVEDPORTAL;
static DataTypeClassConst CSUBSCREENPAGE;
static DataTypeClassConst CSUBSCREENWIDGET;
//Class: Var Types
static DataTypeClass BITMAP;
static DataTypeClass CHEATS;
Expand Down Expand Up @@ -533,6 +545,8 @@ namespace ZScript
static DataTypeClass GENERICDATA;
static DataTypeClass PORTAL;
static DataTypeClass SAVEDPORTAL;
static DataTypeClass SUBSCREENPAGE;
static DataTypeClass SUBSCREENWIDGET;
};

bool operator==(DataType const&, DataType const&);
Expand Down Expand Up @@ -774,7 +788,7 @@ namespace ZScript
case idComboData:
return ScriptType::Combo;
case idSubscreenData:
return ScriptType::None;
return ScriptType::EngineSubscreen;
case idGenericScript:
return ScriptType::Generic;
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser/ffscript.lpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ paldata TOKEN( PALDATA );
bottledata TOKEN( BOTTLEDATA );
bottleshopdata TOKEN( BOTTLESHOPDATA );
subscreendata TOKEN( SUBSCREENDATA );
subscreenpage TOKEN( SUBSCREENPAGE );
subscreenwidget TOKEN( SUBSCREENWIDGET );
generic TOKEN( GENERIC );
genericdata TOKEN( GENERICDATA );
portal TOKEN( PORTAL );
Expand Down
4 changes: 4 additions & 0 deletions src/parser/ffscript.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ void trunc_str(std::string& str, size_t sz, std::string const& header, int32_t r
%token BOTTLEDATA
%token BOTTLESHOPDATA
%token SUBSCREENDATA
%token SUBSCREENPAGE
%token SUBSCREENWIDGET
%token GENERIC
%token GENERICDATA
%token PORTAL
Expand Down Expand Up @@ -592,6 +594,8 @@ DataType : ZCONST DataType {
| PORTAL {$$ = new ASTDataType(DataType::PORTAL, @$);}
| SAVEDPORTAL {$$ = new ASTDataType(DataType::SAVEDPORTAL, @$);}
| SUBSCREENDATA {$$ = new ASTDataType(DataType::SUBSCREENDATA, @$);}
| SUBSCREENPAGE {$$ = new ASTDataType(DataType::SUBSCREENPAGE, @$);}
| SUBSCREENWIDGET {$$ = new ASTDataType(DataType::SUBSCREENWIDGET, @$);}
| TUNE {$$ = new ASTDataType(DataType::TUNES, @$);}
| WARPRING {$$ = new ASTDataType(DataType::WARPRING, @$);}
| PALDATA {$$ = new ASTDataType(DataType::PALDATA, @$);}
Expand Down

0 comments on commit e19d0fd

Please sign in to comment.