More work on debug_string
This commit is contained in:
@@ -324,14 +324,15 @@ bool AnimStep::from_string(const std::string &config) {
|
||||
}
|
||||
|
||||
AnimQueue::AnimQueue(util::WorldType wt) {
|
||||
world_type_ = wt;
|
||||
version_autoinc_ = (wt == util::WORLD_TYPE_MASTER);
|
||||
size_limit_ = 10; // Default size limit.
|
||||
clear_steps();
|
||||
version_number_ = (wt == util::WORLD_TYPE_MASTER) ? 1 : 0;
|
||||
steps_.emplace_back();
|
||||
steps_.front().keep_state_only();
|
||||
version_number_ = version_autoinc_ ? 1 : 0;
|
||||
}
|
||||
|
||||
void AnimQueue::mutated() {
|
||||
if (world_type_ == util::WORLD_TYPE_MASTER) {
|
||||
if (version_autoinc_) {
|
||||
version_number_ += 1;
|
||||
} else {
|
||||
version_number_ = 0;
|
||||
@@ -353,30 +354,23 @@ bool AnimQueue::size_and_steps_equal(const AnimQueue &other) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void AnimQueue::clear_steps() {
|
||||
steps_.clear();
|
||||
steps_.emplace_back();
|
||||
AnimStep &init = steps_.back();
|
||||
init.bits_ = AnimStep::HAS_EVERYTHING;
|
||||
init.action_ = "";
|
||||
init.graphic_ = "";
|
||||
init.plane_ = "";
|
||||
mutated();
|
||||
}
|
||||
|
||||
void AnimQueue::full_clear_and_set_limit(int n) {
|
||||
assert(n >= 1);
|
||||
clear_steps();
|
||||
steps_.clear();
|
||||
steps_.emplace_back();
|
||||
steps_.front().keep_state_only();
|
||||
size_limit_ = n;
|
||||
version_number_ = 1;
|
||||
version_number_ = version_autoinc_ ? 1 : 0;
|
||||
}
|
||||
void AnimQueue::set_limit(int n) {
|
||||
assert(n >= 1);
|
||||
size_limit_ = n;
|
||||
while (int(steps_.size()) > n) {
|
||||
steps_.pop_front();
|
||||
if (int(steps_.size()) > n) {
|
||||
while (int(steps_.size()) > n) {
|
||||
steps_.pop_front();
|
||||
}
|
||||
steps_.front().keep_state_only();
|
||||
}
|
||||
steps_.front().keep_state_only();
|
||||
mutated();
|
||||
}
|
||||
|
||||
@@ -549,12 +543,23 @@ const AnimStep &AnimQueue::back() const {
|
||||
return steps_.back();
|
||||
}
|
||||
|
||||
static bool diff_works(const AnimQueue &master, AnimQueue &sync) {
|
||||
StreamBuffer sb;
|
||||
sync.make_patch(master, &sb);
|
||||
sync.apply_patch(&sb);
|
||||
return sync.size_and_steps_equal(master);
|
||||
}
|
||||
|
||||
LuaDefine(unittests_animqueue, "c") {
|
||||
// Check initial state.
|
||||
// Useful objects.
|
||||
AnimStep stp;
|
||||
AnimQueue aq(util::WORLD_TYPE_MASTER);
|
||||
StreamBuffer sb;
|
||||
AnimQueue aqds(util::WORLD_TYPE_S_SYNC);
|
||||
StreamBuffer sb;
|
||||
|
||||
// Debug string of a newly initialized queue
|
||||
LuaAssert(L, aq.valid());
|
||||
LuaAssertStrEq(L, aq.debug_string(), "version=1; limit=10; id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=");
|
||||
|
||||
// Test the step setters.
|
||||
stp.clear();
|
||||
@@ -572,11 +577,11 @@ LuaDefine(unittests_animqueue, "c") {
|
||||
stp.set_graphic("something");
|
||||
LuaAssertStrEq(L, stp.debug_string(), "id=0 action= plane=somewhere x=3 y=4 z=5 facing=180 graphic=something");
|
||||
|
||||
// Test the step parser.
|
||||
// Test the step debug string parser.
|
||||
LuaAssert(L, stp.from_string("id=123 action=walk x=1 y=2 z=3 facing=4 plane=p graphic=g"));
|
||||
LuaAssertStrEq(L, stp.debug_string(), "id=123 action=walk plane=p x=1 y=2 z=3 facing=4 graphic=g");
|
||||
|
||||
// Test a blank queue.
|
||||
// Test that we can clear a queue.
|
||||
aq.full_clear_and_set_limit(3);
|
||||
LuaAssert(L, aq.valid());
|
||||
LuaAssertStrEq(L, aq.debug_string(), "version=1; limit=3; id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=");
|
||||
@@ -630,28 +635,16 @@ LuaDefine(unittests_animqueue, "c") {
|
||||
// Add a single action to the queue and DT
|
||||
LuaAssert(L, stp.from_string("action=walk x=3 y=4 z=5"));
|
||||
aq.add(12345, stp);
|
||||
sb.clear();
|
||||
LuaAssert(L, aqds.make_patch(aq, &sb));
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
LuaAssert(L, diff_works(aq, aqds));
|
||||
|
||||
// Add another action and DT
|
||||
LuaAssert(L, stp.from_string("action=fnord plane=where facing=123"));
|
||||
aq.add(232, stp);
|
||||
sb.clear();
|
||||
LuaAssert(L, aqds.make_patch(aq, &sb));
|
||||
sb.write_uint32(0);
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, sb.total_writes() - sb.total_reads() == 4);
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
LuaAssert(L, diff_works(aq, aqds));
|
||||
|
||||
// Change the queue size limit.
|
||||
aq.set_limit(13);
|
||||
sb.clear();
|
||||
LuaAssert(L, !aqds.size_and_steps_equal(aq));
|
||||
LuaAssert(L, aqds.make_patch(aq, &sb));
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
LuaAssert(L, diff_works(aq, aqds));
|
||||
|
||||
// compare again, should be no differences.
|
||||
sb.clear();
|
||||
@@ -661,11 +654,7 @@ LuaDefine(unittests_animqueue, "c") {
|
||||
|
||||
// Discard all but the last action.
|
||||
aq.set_limit(1);
|
||||
|
||||
sb.clear();
|
||||
LuaAssert(L, aqds.make_patch(aq, &sb));
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
LuaAssert(L, diff_works(aq, aqds));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user