Commit 9efe07b3 authored by twanvl's avatar twanvl

cards dependencies, clearing order cache

parent 72d6958f
...@@ -129,7 +129,21 @@ ScriptValueP make_iterator(const Set& set) { ...@@ -129,7 +129,21 @@ ScriptValueP make_iterator(const Set& set) {
} }
void mark_dependency_member(Set* value, const String& name, const Dependency& dep) { void mark_dependency_member(Set* value, const String& name, const Dependency& dep) {
// TODO // is it the card list?
if (name == _("cards")) {
value->game->dependent_scripts_cards.push_back(dep);
return;
}
// is it the keywords?
if (name == _("keywords")) {
value->game->dependent_scripts_keywords.push_back(dep);
return;
}
// is it in the set data?
IndexMap<FieldP,ValueP>::const_iterator it = value->data.find(name);
if (it != value->data.end()) {
(*it)->fieldP->dependent_scripts.push_back(dep);
}
} }
void mark_dependency_member(const SetP& value, const String& name, const Dependency& dep) { void mark_dependency_member(const SetP& value, const String& name, const Dependency& dep) {
mark_dependency_member(value.get(), name, dep); mark_dependency_member(value.get(), name, dep);
...@@ -157,6 +171,9 @@ int Set::positionOfCard(const CardP& card, const ScriptValueP& order_by) { ...@@ -157,6 +171,9 @@ int Set::positionOfCard(const CardP& card, const ScriptValueP& order_by) {
} }
return order->find(card); return order->find(card);
} }
void Set::clearOrderCache() {
order_cache.clear();
}
// ----------------------------------------------------------------------------- : Styling // ----------------------------------------------------------------------------- : Styling
......
...@@ -93,6 +93,8 @@ class Set : public Packaged { ...@@ -93,6 +93,8 @@ class Set : public Packaged {
/// Find the position of a card in this set, when the card list is sorted using the given cirterium /// Find the position of a card in this set, when the card list is sorted using the given cirterium
int positionOfCard(const CardP& card, const ScriptValueP& order_by); int positionOfCard(const CardP& card, const ScriptValueP& order_by);
/// Clear the order_cache used by positionOfCard
void clearOrderCache();
protected: protected:
virtual String typeName() const; virtual String typeName() const;
......
...@@ -33,6 +33,11 @@ class Dependency { ...@@ -33,6 +33,11 @@ class Dependency {
DependencyType type : 5; ///< Type of the dependent script DependencyType type : 5; ///< Type of the dependent script
size_t index : 27; ///< index into an IndexMap size_t index : 27; ///< index into an IndexMap
void* data; ///< Extra pointer data void* data; ///< Extra pointer data
/// This dependency, but dependent on all cards instead of just one
inline Dependency makeCardIndependend() const {
return Dependency(type == DEP_CARD_FIELD ? DEP_CARDS_FIELD : type, index, data);
}
}; };
// ----------------------------------------------------------------------------- : EOF // ----------------------------------------------------------------------------- : EOF
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <script/value.hpp> #include <script/value.hpp>
#include <script/context.hpp> #include <script/context.hpp>
#include <script/dependency.hpp>
#include <util/tagged_string.hpp> #include <util/tagged_string.hpp>
#include <data/set.hpp> #include <data/set.hpp>
#include <wx/regex.h> #include <wx/regex.h>
...@@ -421,18 +422,37 @@ int position_in_vector(const ScriptValueP& of, const ScriptValueP& in, const Scr ...@@ -421,18 +422,37 @@ int position_in_vector(const ScriptValueP& of, const ScriptValueP& in, const Scr
} }
// finding positions, also of substrings // finding positions, also of substrings
SCRIPT_FUNCTION(position_of) { SCRIPT_FUNCTION_DEP(position_of) {
ScriptValueP of = ctx.getVariable(_("of")); ScriptValueP of = ctx.getVariable(_("of"));
ScriptValueP in = ctx.getVariable(_("in")); ScriptValueP in = ctx.getVariable(_("in"));
ScriptValueP order_by = ctx.getVariableOpt(_("order by")); ScriptValueP order_by = ctx.getVariableOpt(_("order by"));
SCRIPT_RETURN(position_in_vector(of, in, order_by)); SCRIPT_RETURN(position_in_vector(of, in, order_by));
} }
ScriptValueP ScriptBuildin_position_of::dependencies(Context& ctx, const Dependency& dep) const {
ScriptValueP of = ctx.getVariable(_("of"));
ScriptValueP in = ctx.getVariable(_("in"));
ScriptValueP order_by = ctx.getVariableOpt(_("order by"));
ScriptObject<Set*>* s = dynamic_cast<ScriptObject<Set*>* >(in.get());
ScriptObject<CardP>* c = dynamic_cast<ScriptObject<CardP>*>(of.get());
if (s && c) {
// dependency on cards
mark_dependency_member(s->getValue(), _("cards"), dep);
if (order_by) {
// dependency on order_by function
order_by->dependencies(ctx, dep.makeCardIndependend());
}
}
return dependency_dummy;
};
// finding sizes // finding sizes
SCRIPT_FUNCTION(number_of_items) { SCRIPT_FUNCTION(number_of_items) {
SCRIPT_RETURN(ctx.getVariable(_("in"))->itemCount()); SCRIPT_RETURN(ctx.getVariable(_("in"))->itemCount());
} }
// ----------------------------------------------------------------------------- : Initialize functions // ----------------------------------------------------------------------------- : Initialize functions
void init_script_functions(Context& ctx) { void init_script_functions(Context& ctx) {
......
...@@ -99,6 +99,7 @@ void SetScriptManager::onInit(const StyleSheetP& stylesheet, Context* ctx) { ...@@ -99,6 +99,7 @@ void SetScriptManager::onInit(const StyleSheetP& stylesheet, Context* ctx) {
initDependencies(*ctx, *set.game); initDependencies(*ctx, *set.game);
initDependencies(*ctx, *stylesheet); initDependencies(*ctx, *stylesheet);
// apply scripts to everything // apply scripts to everything
// TODO : don't updateAll here, it will be done repeatedly
updateAll(); updateAll();
} catch (Error e) { } catch (Error e) {
handle_error(e, false, false); handle_error(e, false, false);
...@@ -195,7 +196,7 @@ void SetScriptManager::updateAllDependend(const vector<Dependency>& dependent_sc ...@@ -195,7 +196,7 @@ void SetScriptManager::updateAllDependend(const vector<Dependency>& dependent_sc
} }
void SetScriptManager::updateRecursive(deque<ToUpdate>& to_update, Age starting_age) { void SetScriptManager::updateRecursive(deque<ToUpdate>& to_update, Age starting_age) {
// set->order_cache.clear(); // clear caches before evaluating a round of scripts set.clearOrderCache(); // clear caches before evaluating a round of scripts
while (!to_update.empty()) { while (!to_update.empty()) {
updateToUpdate(to_update.front(), to_update, starting_age); updateToUpdate(to_update.front(), to_update, starting_age);
to_update.pop_front(); to_update.pop_front();
...@@ -226,8 +227,10 @@ void SetScriptManager::alsoUpdate(deque<ToUpdate>& to_update, const vector<Depen ...@@ -226,8 +227,10 @@ void SetScriptManager::alsoUpdate(deque<ToUpdate>& to_update, const vector<Depen
if (card) { if (card) {
ValueP value = card->data.at(d.index); ValueP value = card->data.at(d.index);
to_update.push_back(ToUpdate(value.get(), card)); to_update.push_back(ToUpdate(value.get(), card));
break;
} else {
// There is no card, so the update should affect all cards (fall through).
} }
break;
} case DEP_CARDS_FIELD: { } case DEP_CARDS_FIELD: {
// something invalidates a card value for all cards, so all cards need updating // something invalidates a card value for all cards, so all cards need updating
FOR_EACH(card, set.cards) { FOR_EACH(card, set.cards) {
......
...@@ -314,16 +314,16 @@ inline ScriptValueP toScript(const shared_ptr<T>& v) { return new_intrusive1<Scr ...@@ -314,16 +314,16 @@ inline ScriptValueP toScript(const shared_ptr<T>& v) { return new_intrusive1<Scr
* context.setVariable("my_function", script_my_function); * context.setVariable("my_function", script_my_function);
* @endcode * @endcode
*/ */
#define SCRIPT_FUNCTION(name) SCRIPT_FUNCTION_AUX(name,virtual) #define SCRIPT_FUNCTION(name) SCRIPT_FUNCTION_AUX(name,;)
/// Macro to declare a new script function with custom dependency handling /// Macro to declare a new script function with custom dependency handling
#define SCRIPT_FUNCTION_DEP(name) SCRIPT_FUNCTION_AUX(name,virtual) //TODO #define SCRIPT_FUNCTION_DEP(name) SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP dependencies(Context&, const Dependency&) const;)
// helper for SCRIPT_FUNCTION and SCRIPT_FUNCTION_DEP // helper for SCRIPT_FUNCTION and SCRIPT_FUNCTION_DEP
#define SCRIPT_FUNCTION_AUX(name,dep) \ #define SCRIPT_FUNCTION_AUX(name,dep) \
class ScriptBuildin_##name : public ScriptValue { \ class ScriptBuildin_##name : public ScriptValue { \
dep \ dep \
/* virtual */ ScriptType type() const \ virtual ScriptType type() const \
{ return SCRIPT_FUNCTION; } \ { return SCRIPT_FUNCTION; } \
virtual String typeName() const \ virtual String typeName() const \
{ return _("build in function '") _(#name) _("'"); } \ { return _("build in function '") _(#name) _("'"); } \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment