made dependency analysis work without errors for magic-new (except for a few script functions); implemented the rest of the ScriptManager

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@71 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-11-01 16:41:50 +00:00
parent 2dd93a91fb
commit f18bdafab1
17 changed files with 280 additions and 45 deletions
+138 -5
View File
@@ -10,14 +10,19 @@
#include <data/set.hpp>
#include <data/stylesheet.hpp>
#include <data/game.hpp>
#include <data/card.hpp>
#include <data/field.hpp>
#include <util/error.hpp>
typedef map<const StyleSheet*,Context*> Contexts;
typedef IndexMap<FieldP,StyleP> IndexMap_FieldP_StyleP;
typedef IndexMap<FieldP,ValueP> IndexMap_FieldP_ValueP;
DECLARE_TYPEOF(Contexts);
DECLARE_TYPEOF_COLLECTION(CardP);
DECLARE_TYPEOF_COLLECTION(FieldP);
DECLARE_TYPEOF_COLLECTION(Dependency);
DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_StyleP);
DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_ValueP);
// initialize functions, from functions.cpp
void init_script_functions(Context& ctx);
@@ -58,9 +63,9 @@ Context& ScriptManager::getContext(const StyleSheetP& stylesheet) {
ctx->setVariable(_("card"), set.cards.empty() ? script_nil : toScript(set.cards.front())); // dummy value
//ctx->setVariable(_("styling"), toScript(set->extraStyleData(style)));
try {
// perform init scripts
set.game ->init_script.invoke(*ctx);
stylesheet->init_script.invoke(*ctx);
// perform init scripts, don't use a scope, variables stay bound in the context
set.game ->init_script.invoke(*ctx, false);
stylesheet->init_script.invoke(*ctx, false);
// find script dependencies
initDependencies(*ctx, *set.game);
initDependencies(*ctx, *stylesheet);
@@ -97,12 +102,140 @@ void ScriptManager::initDependencies(Context& ctx, StyleSheet& stylesheet) {
}
}
// ----------------------------------------------------------------------------- : ScriptManager : dependency handling
// ----------------------------------------------------------------------------- : ScriptManager : updating
void ScriptManager::onAction(const Action& action, bool undone) {
// TODO
// TYPE_CASE(action, ValueAction) {
// }
// TYPE_CASE(action, CardListAction) {
// }
}
void ScriptManager::updateStyles(const CardP& card) {
// lastUpdatedCard = card;
StyleSheetP stylesheet = set.stylesheetFor(card);
Context& ctx = getContext(stylesheet);
// update all styles
FOR_EACH(s, stylesheet->card_style) {
if (s->update(ctx)) {
// style has changed, tell listeners
// ScriptStyleEvent change(s.get());
// set->actions.tellListeners(change);
}
}
}
void ScriptManager::updateValue(Value& value, const CardP& card) {
Age starting_age; // the start of the update process
deque<ToUpdate> to_update;
// execute script for initial changed value
value.update(getContext(set.stylesheetFor(card)));
// update dependent scripts
alsoUpdate(to_update, value.fieldP->dependent_scripts, card);
updateRecursive(to_update, starting_age);
}
void ScriptManager::updateAll() {
// TODO
// update set data
Context& ctx = getContext(set.stylesheet);
FOR_EACH(v, set.data) {
v->update(ctx);
}
// update card data of all cards
FOR_EACH(card, set.cards) {
Context& ctx = getContext(set.stylesheetFor(card));
FOR_EACH(v, card->data) {
v->update(ctx);
}
}
// update things that depend on the card list
updateAllDependend(set.game->dependent_scripts_cards);
}
void ScriptManager::updateAllDependend(const vector<Dependency>& dependent_scripts) {
deque<ToUpdate> to_update;
Age starting_age;
alsoUpdate(to_update, dependent_scripts, CardP());
updateRecursive(to_update, starting_age);
}
void ScriptManager::updateRecursive(deque<ToUpdate>& to_update, Age starting_age) {
// set->order_cache.clear(); // clear caches before evaluating a round of scripts
while (!to_update.empty()) {
updateToUpdate(to_update.front(), to_update, starting_age);
to_update.pop_front();
}
}
void ScriptManager::updateToUpdate(const ToUpdate& u, deque<ToUpdate>& to_update, Age starting_age) {
Age age = u.value->last_script_update;
if (starting_age < age) return; // this value was already updated
Context& ctx = getContext(set.stylesheetFor(u.card));
if (u.value->update(ctx)) {
// changed, send event
// ScriptValueEvent change(&*u.card, u.value);
// set.actions.tellListeners(change);
// u.value has changed, also update values with a dependency on u.value
alsoUpdate(to_update, u.value->fieldP->dependent_scripts, u.card);
}
}
void ScriptManager::alsoUpdate(deque<ToUpdate>& to_update, const vector<Dependency>& deps, const CardP& card) {
FOR_EACH_CONST(d, deps) {
switch (d.type) {
case DEP_SET_FIELD: {
break;
} case DEP_CARD_FIELD: {
break;
} case DEP_CARDS_FIELD: {
break;
} case DEP_STYLE: {
break;
} case DEP_CARD_COPY_DEP: {
break;
} case DEP_SET_COPY_DEP: {
break;
} default:
assert(false);
}
/*
if (d.type == DependendScript.setField) {
// from set data
ValueP value = set->data.at(ds.index);
toUpdate.push_back(ToUpdate(&*value));
} else if (ds.type == DependendScript.cardField) {
// from the same card's data
assert(card);
ValueP value = card->data.at(ds.index);
toUpdate.push_back(ToUpdate(&*value, card));
} else if (ds.type == DependendScript.cardsField) {
// something invalidates a card value for all cards, so all cards need updating
FOR_EACH(card, set)->cards {
ValueP value = card->data.at(ds.index);
toUpdate.push_back(ToUpdate(&*value, card));
}
} else if (ds.type >= DependendScript.choiceImage) {
// a generated image has become invalid, there is not much we can do
// because the index is not exact enough, it only gives the field
// TODO : Indicate what style
//CardStyleP style = set->styleOf(card) // WRONG?
CardStyle* style = CardStyle.getByIndex(ds.type - DependendScript.choiceImage);
StyleP s = style->cardStyle.at(ds.index);
s->invalidate();
// something changed, send event
ScriptStyleEvent change(&*s);
set->actions.tellListeners(change);
} else if (ds.type == DependendScript.cardCopyDep) {
// propagate dependencies from another field
FieldP f = game->cardFields#(ds.index);
alsoUpdate(toUpdate, f->dependendScripts, card);
} else if (ds.type == DependendScript.setCopyDep) {
// propagate dependencies from another field
FieldP f = game->setFields#(ds.index);
alsoUpdate(toUpdate, f->dependendScripts, card);
} else {
assert(false); // only setField, cardField and cardsField should be possible
}*/
}
}