cc: actions in scripts can now have conditions

This commit is contained in:
Crypto City 2021-05-15 11:21:43 +00:00
parent 64fd08c610
commit e350d4ef49
5 changed files with 26 additions and 2 deletions

View File

@ -183,8 +183,16 @@ bool load_script(const std::string &source, ScriptHandle &script, std::string *e
static void print_action_string(const Action &a, std::string &source, const std::string &base_indent, const std::string &indent)
{
const std::string I(base_indent);
std::string I(base_indent);
std::string s;
if (!a.conditions.empty())
{
source += I + "if (";
for (const Expression &e: a.conditions)
source += get_expression_string(e);
source += ")\n";
I += base_indent;
}
switch (a.type)
{
case action_none:
@ -1119,6 +1127,12 @@ static bool execute(cryptonote::cc_command_t cmd, BlockchainStateProxy &proxy, g
if (action.type == action_none)
return true;
for (const Expression &e: action.conditions)
{
if (!is_condition_true(proxy, e, account, owner, city, seed, salt))
return true;
}
cryptonote::cc_account_data_t ad;
const auto value_signed = action.ops.empty() || action.ops[0].type == op_none ? std::make_pair<uint64_t, bool>(0, false) : get_attribute(proxy, action.ops[0], account, owner, city, seed, salt);
const uint64_t value = value_signed.first;

View File

@ -233,6 +233,7 @@ struct Action
{
uint32_t type{action_none};
std::string str;
std::vector<Expression> conditions;
std::vector<Operand> ops;
std::vector<WeightedAction> actions;
@ -240,6 +241,7 @@ struct Action
VERSION_FIELD(0)
VARINT_FIELD(type)
FIELD(str)
FIELD(conditions)
FIELD(ops)
FIELD(actions)
END_SERIALIZE()

View File

@ -359,6 +359,8 @@ void set_partial_state_action(script_partial_state_t *state, ActionType type, co
a.ops.push_back(std::move(state->operands[state->operands.size() - num_ops + i]));
for (unsigned int i = 0; i < num_ops; ++i)
state->operands.pop_back();
a.conditions = std::move(state->expressions);
state->expressions.clear();
}
void set_partial_state_action_pick_count_up(script_partial_state_t *state)
@ -406,6 +408,8 @@ void set_partial_state_action_pick(script_partial_state_t *state)
}
pick.type = action_pick;
state->pick = 0;
pick.conditions = std::move(state->expressions);
state->expressions.clear();
}
void set_partial_state_action_percent(script_partial_state_t *state)
@ -428,6 +432,8 @@ void set_partial_state_action_percent(script_partial_state_t *state)
percent.actions[0].weight = std::move(state->operands.back());
state->actions.pop_back();
state->operands.pop_back();
percent.conditions = std::move(state->expressions);
state->expressions.clear();
}
void set_partial_state_state_init_actions(script_partial_state_t *state)

View File

@ -106,6 +106,7 @@ labour { return LABOUR; }
coin { return COIN; }
food { return FOOD; }
custom { return CUSTOM; }
if { return IF; }
\+ { return ADD; }
- { return SUB; }

View File

@ -36,7 +36,7 @@ static script_partial_state_t *state = NULL;
%token <number> ADD SUB MUL MIN MAX NOT
%token <number> PERCENT PAY CONSUME TEMPERATURE_NUMBER TEMPERATURE RANDOM
%token <number> IMAGE UI CHOICES CHOICE NEXT SELECTED ENABLED ACTIONS PUBLIC IS BLOCKCHAIN BY OVERRIDES ID
%token <number> GEMSTONE BLOCK LABOUR COIN FOOD CUSTOM
%token <number> GEMSTONE BLOCK LABOUR COIN FOOD CUSTOM IF
%type <number> script script_contents script_content choice_contents choice_content
%type <number> name owner description icon precondition reserves state public
@ -116,6 +116,7 @@ action: action_percent
| action_set_local_variable
| action_set_global_variable
| action_none
| IF '(' expression ')' action
;
action_percent: PERCENT operand action { set_partial_state_action_percent(state); }