Renamed AnimationSettings -> AnimationManager.
[tagua/yd.git] / src / game.cpp
bloba2b991cec3312884966aaa9d25c61780a04179a4
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include <iostream>
12 #include <map>
13 #ifdef Q_CC_MSVC
14 #pragma warning( push )
15 #pragma warning( disable : 4100 )
16 #include <boost/variant.hpp>
17 #pragma warning( pop )
18 #else
19 #include <boost/variant.hpp>
20 #endif
22 #include <core/moveserializer.h>
23 #include <core/state.h>
24 #include <core/statefactory.h>
25 #include <core/validator.h>
26 #include "variant.h"
28 #include "game.h"
29 #include "game_p.h"
30 #include "pgnparser.h"
31 #include "components.h"
32 #include "variants.h"
34 using namespace GamePrivate;
37 Game::Game(Components* components)
38 : current(-1)
39 , undo_pos(0)
40 , m_components(components) { }
42 Game::~Game() {
45 void Game::onAdded(const Index&) {
48 void Game::onRemoved(const Index&) {
51 void Game::onEntryChanged(const Index&, int) {
54 void Game::onPromoteVariation(const Index&, int) {
57 void Game::onSetComment(const Index&, const QString&) {
60 void Game::onSetVComment(const Index&, int, const QString&) {
63 void Game::onCurrentIndexChanged(const Index&) {
66 void Game::onAvailableUndo(bool) {
69 void Game::onAvailableRedo(bool) {
72 Entry* Game::fetch(const Index& ix) {
73 int at;
74 History *vec = fetchRef(ix, &at);
75 return vec ? &(*vec)[at] : NULL;
78 const Entry* Game::fetch(const Index& ix) const {
79 int at;
80 const History *vec = fetchRef(ix, &at);
81 return vec ? &(*vec)[at] : NULL;
84 History* Game::fetchRef(const Index& ix, int* idx) {
85 if(ix.num_moves >= (int)history.size() || ix.num_moves < 0 )
86 return NULL;
88 History* aretv = &history;
89 Entry* retv = &history[ix.num_moves];
90 if(idx) *idx = ix.num_moves;
92 for(int i=0; i<(int)ix.nested.size();i++) {
93 Variations::iterator it = retv->variations.find(ix.nested[i].variation);
94 if(it == retv->variations.end() || ix.nested[i].num_moves >= (int)it->second.size()
95 || ix.nested[i].num_moves < 0 )
96 return NULL;
98 aretv = &it->second;
99 retv = &it->second[ix.nested[i].num_moves];
100 if(idx) *idx = ix.nested[i].num_moves;
102 return aretv;
105 const History* Game::fetchRef(const Index& ix, int* idx) const {
106 return const_cast<const History*>(const_cast<Game*>(this)->fetchRef(ix, idx));
109 void Game::testMove(const Index& ix) {
110 if (ix != Index(0)) {
111 Entry *e1 = fetch(ix.prev());
112 Entry *e2 = fetch(ix);
113 if (!e1 || !e2 || !e1->position || e2->move == Move())
114 return;
116 if (!m_components->validator()->legal(e1->position.get(), e2->move))
117 ERROR("invalid move added to game history!");
121 void Game::testMove() {
122 testMove(current);
125 void Game::saveUndo(const UndoOp& op) {
126 bool redo = undo_pos < (int)undo_history.size();
128 while(undo_pos < (int)undo_history.size())
129 undo_history.pop_back();
130 undo_history.push_back(op);
131 undo_pos++;
133 if(undo_pos == 1)
134 onAvailableUndo(true);
135 if(redo)
136 onAvailableRedo(false);
140 Index Game::index() const {
141 return current;
144 Index Game::lastMainlineIndex() const {
145 return Index(history.size()-1);
148 bool Game::containsIndex(const Index& index) const {
149 return !!fetch(index);
152 Move Game::move() const {
153 return move(current);
156 Move Game::move(const Index& index) const {
157 Entry *e = (Entry*)fetch(index);
158 if(!e) {
159 ERROR("Index out of range!");
160 return Move();
162 return e->move;
165 StatePtr Game::position() const {
166 return position(current);
169 StatePtr Game::position(const Index& index) const {
170 Entry *e = (Entry*)fetch(index);
171 if(!e) {
172 ERROR("Index " << index << " out of range!");
173 return StatePtr();
175 return e->position;
178 QString Game::comment() const {
179 return comment(current);
182 QString Game::comment(const Index& index) const {
183 const Entry *e = fetch(index);
184 if(!e) {
185 ERROR("Index out of range!");
186 return QString();
188 return e->comment;
191 void Game::reset(StatePtr pos) {
192 Q_ASSERT(pos);
194 undo_pos = 0;
195 undo_history.clear();
196 history.clear();
197 history.push_back( Entry(Move(), pos) );
198 current = Index(0);
199 onCurrentIndexChanged();
202 void Game::undo() {
203 if(undo_pos <= 0) {
204 ERROR("Cannot undo at the beginning of the undo history!");
205 return;
208 bool last_undo = undo_pos == 1;
209 bool now_redo = undo_pos == (int)undo_history.size();
211 undo_pos--;
212 UndoOp* op = &(undo_history[undo_pos]);
214 if(boost::get<UndoAdd>(op)) {
215 UndoAdd *a = boost::get<UndoAdd>(op);
217 if(a->index.atVariationStart() ) {
218 Entry* e = fetch(a->index.prev());
219 Q_ASSERT(e);
221 int v = a->index.nested[a->index.nested.size()-1].variation;
222 Q_ASSERT(e->variations.count(v) == 1);
223 Q_ASSERT(e->variations[v].size() == 1);
225 e->variations.erase(v);
227 else {
228 int at;
229 std::vector<Entry>* vec = fetchRef(a->index, &at);
230 Q_ASSERT(vec);
231 Q_ASSERT((int)vec->size() == at+1);
233 vec->pop_back();
236 if(current == a->index) {
237 current = current.prev();
238 onCurrentIndexChanged();
241 onRemoved(a->index);
243 else if(boost::get<UndoPromote>(op)) {
244 UndoPromote *p = boost::get<UndoPromote>(op);
246 int at;
247 std::vector<Entry>* vec = fetchRef(p->index, &at);
248 Q_ASSERT(vec);
249 Q_ASSERT((*vec)[at].variations.count(p->variation)==1);
251 History vold = (*vec)[at].variations[p->variation];
252 History vnew;
253 for(int i=at+1; i<(int)vec->size(); i++)
254 vnew.push_back((*vec)[i]);
255 while((int)vec->size()>at+1)
256 vec->pop_back();
257 for(int i=0; i<(int)vold.size(); i++)
258 vec->push_back(vold[i]);
259 (*vec)[at].variations[p->variation] = vnew;
261 current = current.flipVariation(p->index, p->variation);
262 onPromoteVariation(p->index, p->variation);
263 //onCurrentIndexChanged();
265 else if(boost::get<UndoTruncate>(op)) {
266 UndoTruncate *t = boost::get<UndoTruncate>(op);
268 int at;
269 std::vector<Entry>* vec = fetchRef(t->index, &at);
270 Q_ASSERT(vec);
271 Q_ASSERT((int)vec->size() == at+1);
272 Q_ASSERT((*vec)[at].variations.empty());
274 for(int i=0;i<(int)t->history.size();i++)
275 vec->push_back(t->history[i]);
276 (*vec)[at].variations = t->variations;
277 (*vec)[at].vcomments = t->vcomments;
279 if(t->history.size())
280 onAdded(t->index.next());
281 for(Variations::iterator it = t->variations.begin(); it != t->variations.end(); ++it)
282 onAdded(t->index.next(it->first));
283 for(VComments::iterator it = t->vcomments.begin(); it != t->vcomments.end(); ++it)
284 onSetVComment(t->index, it->first, it->second);
286 else if(boost::get<UndoRemove>(op)) {
287 UndoRemove *r = boost::get<UndoRemove>(op);
289 Entry *e = fetch(r->index);
290 e->variations[r->variation] = r->history;
291 onAdded(r->index.next(r->variation));
292 if(!r->vcomment.isEmpty()) {
293 e->vcomments[r->variation] = r->vcomment;
294 onSetVComment(r->index, r->variation, r->vcomment);
297 else if(boost::get<UndoClear>(op)) {
298 UndoClear *c = boost::get<UndoClear>(op);
300 Entry *e = fetch(c->index);
301 e->variations = c->variations;
302 e->vcomments = c->vcomments;
303 for(Variations::iterator it = c->variations.begin(); it != c->variations.end(); ++it)
304 onAdded(c->index.next(it->first));
305 for(VComments::iterator it = c->vcomments.begin(); it != c->vcomments.end(); ++it)
306 onSetVComment(c->index, it->first, it->second);
308 else if(boost::get<UndoSetComment>(op)) {
309 UndoSetComment *sc = boost::get<UndoSetComment>(op);
310 Entry *e = fetch(sc->index);
311 Q_ASSERT(e);
313 if(sc->variation == -1) {
314 e->comment = sc->old_comment;
315 onSetComment(sc->index, sc->old_comment);
317 else {
318 if(sc->old_comment.isEmpty())
319 e->vcomments.erase(sc->variation);
320 else
321 e->vcomments[sc->variation] = sc->old_comment;
322 onSetVComment(sc->index, sc->variation, sc->old_comment);
326 if(last_undo)
327 onAvailableUndo(false);
328 if(now_redo)
329 onAvailableRedo(true);
332 void Game::redo() {
333 if(undo_pos >= (int)undo_history.size()) {
334 ERROR("Cannot redo at the end of the undo history!");
335 return;
338 bool now_undo = undo_pos == 0;
339 bool last_redo = undo_pos == (int)undo_history.size()-1;
341 UndoOp* op = &(undo_history[undo_pos]);
342 undo_pos++;
344 if(boost::get<UndoAdd>(op)) {
345 UndoAdd *a = boost::get<UndoAdd>(op);
347 if(a->index.atVariationStart() ) {
348 Entry* e = fetch(a->index.prev());
349 Q_ASSERT(e);
351 int v = a->index.nested[a->index.nested.size()-1].variation;
352 Q_ASSERT(e->variations.count(v) == 0);
354 History h;
355 h.push_back(a->entry);
356 e->variations[v] = h;
358 else {
359 int at;
360 std::vector<Entry>* vec = fetchRef(a->index.prev(), &at);
361 Q_ASSERT(vec);
362 Q_ASSERT((int)vec->size() == at+1);
364 vec->push_back(a->entry);
367 onAdded(a->index);
369 else if(boost::get<UndoPromote>(op)) {
370 UndoPromote *p = boost::get<UndoPromote>(op);
372 int at;
373 std::vector<Entry>* vec = fetchRef(p->index, &at);
375 Q_ASSERT(vec);
376 Q_ASSERT((*vec)[at].variations.count(p->variation)==1);
377 History vold = (*vec)[at].variations[p->variation];
378 History vnew;
379 for(int i=at+1; i<(int)vec->size(); i++)
380 vnew.push_back((*vec)[i]);
381 while((int)vec->size()>at+1)
382 vec->pop_back();
383 for(int i=0; i<(int)vold.size(); i++)
384 vec->push_back(vold[i]);
385 (*vec)[at].variations[p->variation] = vnew;
387 current = current.flipVariation(p->index, p->variation);
388 onPromoteVariation(p->index, p->variation);
389 //onCurrentIndexChanged();
391 else if(boost::get<UndoTruncate>(op)) {
392 UndoTruncate *t = boost::get<UndoTruncate>(op);
394 int at;
395 std::vector<Entry>* vec = fetchRef(t->index, &at);
396 Q_ASSERT(vec);
397 Q_ASSERT((int)vec->size() == at+1+(int)t->history.size());
399 while((int)vec->size() > at+1)
400 vec->pop_back();
401 (*vec)[at].variations.clear();
402 (*vec)[at].vcomments.clear();
404 if(current > t->index) {
405 current = t->index;
406 onCurrentIndexChanged();
409 if(t->history.size())
410 onRemoved(t->index.next());
411 for(Variations::iterator it = t->variations.begin(); it != t->variations.end(); ++it)
412 onRemoved(t->index.next(it->first));
414 else if(boost::get<UndoRemove>(op)) {
415 UndoRemove *r = boost::get<UndoRemove>(op);
417 Entry *e = fetch(r->index);
418 e->variations.erase(r->variation);
419 e->vcomments.erase(r->variation);
420 onRemoved(r->index.next(r->variation));
422 else if(boost::get<UndoClear>(op)) {
423 UndoClear *c = boost::get<UndoClear>(op);
425 Entry *e = fetch(c->index);
426 e->variations.clear();
427 e->vcomments.clear();
428 for(Variations::iterator it = c->variations.begin(); it != c->variations.end(); ++it)
429 onRemoved(c->index.next(it->first));
431 else if(boost::get<UndoSetComment>(op)) {
432 UndoSetComment *sc = boost::get<UndoSetComment>(op);
433 Entry *e = fetch(sc->index);
434 Q_ASSERT(e);
436 if(sc->variation == -1) {
437 e->comment = sc->new_comment;
438 onSetComment(sc->index, sc->new_comment);
440 else {
441 if(sc->new_comment.isEmpty())
442 e->vcomments.erase(sc->variation);
443 else
444 e->vcomments[sc->variation] = sc->new_comment;
445 onSetVComment(sc->index, sc->variation, sc->new_comment);
449 if(now_undo)
450 onAvailableUndo(true);
451 if(last_redo)
452 onAvailableRedo(false);
455 void Game::setComment(const QString& c) {
456 setComment(current, c);
459 void Game::setComment(const Index& ix, const QString& c) {
460 Entry* e = fetch(ix);
461 if(!e) {
462 ERROR("Invalid index!");
463 return;
465 if(e->comment == c)
466 return;
468 saveUndo(UndoSetComment(ix, -1, e->comment, c));
469 e->comment = c;
470 onSetComment(ix, c);
473 void Game::setVComment(const Index& ix, int v, const QString& c) {
474 Entry* e = fetch(ix);
475 if(!e) {
476 ERROR("Invalid index!");
477 return;
479 QString oc = e->vcomments.count(v) ? e->vcomments[v] : QString();
480 if(oc == c)
481 return;
483 saveUndo(UndoSetComment(ix, v, oc, c));
484 if(c.isEmpty())
485 e->vcomments.erase(v);
486 else
487 e->vcomments[v] = c;
488 onSetVComment(ix, v, c);
491 void Game::promoteVariation() {
492 promoteVariation(current);
495 void Game::promoteVariation(const Index& _ix) {
496 if(_ix.nested.size()==0) {
497 ERROR("Cannot promote main line!");
498 return;
500 Index ix = _ix;
501 int v = ix.nested[ix.nested.size()-1].variation;
502 ix.nested.pop_back();
504 promoteVariation(ix, v);
507 void Game::promoteVariation(const Index& ix, int v) {
508 int at;
509 std::vector<Entry>* vec = fetchRef(ix, &at);
510 Q_ASSERT(vec);
511 Q_ASSERT((*vec)[at].variations.count(v)==1);
513 History vold = (*vec)[at].variations[v];
514 History vnew;
515 for(int i=at+1; i<(int)vec->size(); i++)
516 vnew.push_back((*vec)[i]);
517 while((int)vec->size()>at+1)
518 vec->pop_back();
519 for(int i=0; i<(int)vold.size(); i++)
520 vec->push_back(vold[i]);
521 (*vec)[at].variations[v] = vnew;
523 saveUndo(UndoPromote(ix, v));
524 current = current.flipVariation(ix, v);
525 onPromoteVariation(ix, v);
526 //don't call onCurrentIndexChanged(), as the position did not change actually
529 void Game::removeVariation(int v) {
530 removeVariation(current, v);
533 void Game::removeVariation(const Index& _ix) {
534 if(_ix.nested.size()==0) {
535 ERROR("Cannot remove main line!");
536 return;
538 Index ix = _ix;
539 int v = ix.nested[ix.nested.size()-1].variation;
540 ix.nested.pop_back();
542 removeVariation(ix, v);
545 void Game::removeVariation(const Index& ix, int v) {
546 Entry* e = fetch(ix);
548 saveUndo(UndoRemove(ix, v, e->variations[v],
549 e->vcomments.count(v) ? e->vcomments[v] : QString() ));
550 e->variations.erase(v);
551 e->vcomments.erase(v);
553 onRemoved(ix.next(v));
554 if(current >= ix.next(v)) {
555 current = ix;
556 onCurrentIndexChanged();
560 void Game::clearVariations() {
561 clearVariations(current);
564 void Game::clearVariations(const Index& ix) {
565 Entry* e = fetch(ix);
567 UndoClear uc(ix, e->variations, e->vcomments);
568 saveUndo(uc);
569 e->variations.clear();
570 e->vcomments.clear();
572 for(Variations::iterator it = uc.variations.begin(); it != uc.variations.end(); ++it)
573 onRemoved(ix.next(it->first));
574 if(current > ix && !(current >= ix.next())) {
575 current = ix;
576 onCurrentIndexChanged();
580 void Game::truncate() {
581 truncate(current);
584 void Game::truncate(const Index& ix) {
585 int at;
586 History* vec = fetchRef(ix, &at);
587 if(!vec) {
588 ERROR("Truncating at an unexisting index!");
589 return;
592 Entry *e = &(*vec)[at];
593 UndoTruncate undo(ix);
594 for(int i=at+1; i<(int)vec->size();i++)
595 undo.history.push_back((*vec)[i]);
596 while((int)vec->size()>at+1)
597 vec->pop_back();
599 undo.variations = e->variations;
600 undo.vcomments = e->vcomments;
601 saveUndo(undo);
602 e->variations.clear();
603 e->vcomments.clear();
605 if(undo.history.size())
606 onRemoved(undo.index.next());
607 for(Variations::iterator it = undo.variations.begin(); it != undo.variations.end(); ++it)
608 onRemoved(undo.index.next(it->first));
610 if(current > ix) {
611 current = ix;
612 onCurrentIndexChanged();
616 void Game::add(const Move& m, const StatePtr& pos) {
617 Q_ASSERT(pos);
619 Index old_c = current;
620 int at;
621 std::vector<Entry>* vec = fetchRef(current, &at);
622 Q_ASSERT(vec);
624 /* add the move on the mainline */
625 if((int)vec->size() <= at+1 ) {
626 Q_ASSERT((int)vec->size() == at+1);
627 vec->push_back(Entry(m, pos));
628 current = current.next();
629 testMove();
630 saveUndo(UndoAdd(current, Entry(m, pos)));
631 onAdded(current);
632 onCurrentIndexChanged(old_c);
634 /* we are playing the move that is already next in the mainline */
635 else if( (*vec)[at+1].position && (*vec)[at+1].position->equals(pos.get())) {
636 current = current.next();
637 onCurrentIndexChanged(old_c);
638 /* no need to test the move */
640 else {
641 Entry *e = fetch(current);
642 Q_ASSERT(e);
644 /* check if a variations with this move already exists. */
645 for(Variations::iterator it = e->variations.begin(); it != e->variations.end(); ++it)
646 if(it->second.size() > 0 && it->second[0].position
647 && it->second[0].position->equals(pos.get()) ) {
648 current = current.next(it->first);
649 onCurrentIndexChanged(old_c);
651 return;
654 int var_id = e->last_var_id++;
655 e->variations[var_id].push_back(Entry(m, pos));
656 current = current.next(var_id);
657 testMove();
658 saveUndo(UndoAdd(current, Entry(m, pos)));
659 onAdded(current);
660 onCurrentIndexChanged(old_c);
664 bool Game::insert(const Move& m, const StatePtr& pos, const Index& at) {
665 Entry *e = fetch(at);
667 if(!e) {
668 if(at.nested.size() == 0) {
669 if(undo_history.size()) {
670 undo_pos = 0;
671 undo_history.clear();
673 int hs = history.size();
674 history.resize(at.num_moves + 1);
675 history[at.num_moves] = Entry(m, pos);
676 testMove(at);
677 onAdded(Index(hs));
678 return true;
680 else {
681 ERROR("Index out if range!");
682 return false;
686 if(undo_history.size()) {
687 undo_pos = 0;
688 undo_history.clear();
690 bool res = e->position && e->position->equals(pos.get());
691 e->move = m;
692 e->position = pos;
693 testMove(at);
694 testMove(at.next());
695 for (Variations::const_iterator it = e->variations.begin();
696 it != e->variations.end(); ++it)
697 testMove(at.next(it->first));
698 onEntryChanged(at);
699 return res;
702 bool Game::lastPosition() const {
703 return !fetch(current.next());
706 bool Game::back() {
707 if (current <= 0) return false; // first entry or uninitialized
708 Index old_c = current;
709 Index new_c = current.prev();
711 Entry *e = fetch(new_c);
712 if(!e || e->position == 0) return false; // gap immediately before current
713 current = new_c;
714 onCurrentIndexChanged(old_c);
716 return true;
719 bool Game::forward() {
720 Index old_c = current;
721 Index new_c = current.next();
723 Entry *e = fetch(new_c);
724 if(!e || e->position == 0) {
725 return false; // gap immediately before current
727 current = new_c;
728 onCurrentIndexChanged(old_c);
730 return true;
733 void Game::gotoFirst() {
734 Index old_c = current;
735 current = Index(0);
736 onCurrentIndexChanged(old_c);
739 void Game::gotoLast() {
740 int at;
741 std::vector<Entry>* vec = fetchRef(current, &at);
742 Q_ASSERT(vec);
743 Q_ASSERT((int)vec->size() > at);
745 if((int)vec->size() > at+1) {
746 Index old_c = current;
747 current = current.next(-1, vec->size()-1-at);
748 onCurrentIndexChanged(old_c);
752 bool Game::goTo(const Index& index) {
753 if (fetch(index)) {
754 Index old_c = current;
755 current = index;
756 onCurrentIndexChanged(old_c);
757 return true;
759 return false;
762 QString Game::variationPgn(const History& vec, const Entry& e,
763 int start, const Index& _ix) const {
764 Index ix = _ix;
765 QString res;
767 for (int i = start; i < static_cast<int>(vec.size()); i++) {
768 const Entry& preve = (i > start) ? vec[i-1] : e;
771 QString mv = (vec[i].move != Move() && preve.position)
772 ? m_components->moveSerializer("compact")->
773 serialize(vec[i].move, preve.position.get())
774 : "???";
775 #if 0
776 if (ix == current)
777 mv = "[[" + mv + "]]";
778 #endif
780 int n = ix.totalNumMoves()+1;
781 if(i==start || n%2==0)
782 mv = QString::number(n/2)+(n%2==1 ? ". ... " : ". ") + mv;
783 if (i > start)
784 mv = " " + mv;
786 res += mv;
788 if(!vec[i].comment.isEmpty())
789 res += " {" + vec[i].comment + "}";
791 if(i > 0) {
792 for(Variations::const_iterator it = vec[i-1].variations.begin();
793 it != vec[i-1].variations.end(); ++it) {
794 res += " (";
795 if(vec[i-1].vcomments.count(it->first))
796 res += "{" + vec[i-1].vcomments.find(it->first)->second + "} ";
797 res += variationPgn(it->second, vec[i - 1], 0,
798 ix.prev().next(it->first)) + ")";
802 ix = ix.next();
804 return res;
807 QString Game::pgn() const {
808 return variationPgn(history, history[0], 1, Index(1));
811 void Game::load(const PGN& pgn) {
812 std::map<QString, QString>::const_iterator var = pgn.m_tags.find("Variant");
813 Variant* vi;
815 // FIXME do not create a variant here
816 if (var == pgn.m_tags.end()) {
817 vi = Variants::self().create("chess");
819 else if (!(vi = Variants::self().create(var->second))) {
820 ERROR("No such variant " << var->second);
821 return;
824 std::map<QString, QString>::const_iterator fen = pgn.m_tags.find("FEN");
825 StatePtr pos;
827 if(var == pgn.m_tags.end()) {
828 pos = StatePtr(m_components->createState());
829 pos->setup();
831 #if 0 // BROKEN
832 else if( !(pos = vi->createPositionFromFEN(fen->second))) {
833 ERROR("Wrong fen " << fen->second);
834 return;
836 #endif
838 //TODO: what about options? FEN rules?
840 load(pos, pgn);
843 void Game::load(StatePtr pos, const PGN& pgn) {
844 current = Index(0);
845 undo_history.clear();
846 undo_pos = 0;
848 if(history.size()) {
849 Entry* fe = &history[0];
850 int old_history_size = history.size();
851 std::vector<int> v_ids;
853 while(history.size()>1)
854 history.pop_back();
855 for(Variations::const_iterator it = fe->variations.begin();
856 it != fe->variations.end(); ++it)
857 v_ids.push_back(it->first);
858 fe->variations.clear();
859 fe->vcomments.clear();
861 for(int i=0;i<(int)v_ids.size();i++)
862 onRemoved(Index(0).next(v_ids[i]));
863 if(old_history_size>1)
864 onRemoved(Index(1));
865 v_ids.clear();
866 history[0].position = pos;
868 else
869 history.push_back( Entry(Move(), pos) );
871 QString vcomment;
872 std::vector<Index> var_stack;
873 bool var_start = false;
875 for (uint i = 0; i < pgn.m_entries.size(); i++) {
876 if(boost::get<QString>(pgn[i])) {
877 if(var_start)
878 vcomment += *boost::get<QString>(pgn[i]);
879 else {
880 Entry *e = fetch(current);
881 Q_ASSERT(e);
883 e->comment += *boost::get<QString>(pgn[i]);
886 else if(boost::get<PGN::BeginVariation>(pgn[i])) {
887 var_stack.push_back(current);
888 var_start = true;
890 else if(boost::get<PGN::EndVariation>(pgn[i])) {
891 if(var_stack.size() == 0) {
892 ERROR("Unexpected end variation!");
893 break;
895 current = var_stack[var_stack.size()-1];
896 var_stack.pop_back();
898 else if(boost::get<PGN::Move>(pgn[i])) {
899 const PGN::Move *pm = boost::get<PGN::Move>(pgn[i]);
901 int n = current.totalNumMoves()+1;
902 if(var_start) {
903 if(!pm->m_number)
904 current = current.prev();
905 else if(pm->m_number>n+1)
906 ERROR("Too far variation!");
907 else {
908 if(pm->m_number<n)
909 ERROR("Too near variation!");
910 current = current.prev(n + 1 - pm->m_number);
913 else if(pm->m_number && pm->m_number!=n+1)
914 ERROR("Move number mismatch!");
916 StatePtr pos = position();
917 Move m = m_components->moveSerializer("compact")->
918 deserialize(pm->m_move, pos.get());
920 if (m == Move() || !m_components->validator()->legal(pos.get(), m))
921 break;
923 StatePtr newPos(pos->clone());
924 newPos->move(m);
926 int at;
927 History *vec = fetchRef(current, &at);
928 Q_ASSERT(vec);
930 if(var_start) {
931 Entry *e = &(*vec)[at];
932 int var_id = e->last_var_id++;
933 e->variations[var_id].push_back(Entry(m, newPos));
934 if(!vcomment.isEmpty()) {
935 e->vcomments[var_id] = vcomment;
936 vcomment = QString();
938 /* this is a hack, but the mainline should NEVER
939 be empty if there is a variation*/
940 if((int)vec->size() - 1 == at)
941 vec->push_back(Entry(m, newPos));
943 current = current.next(var_id);
945 else {
946 if((int)vec->size() - 1 == at)
947 vec->push_back(Entry(m, newPos));
948 else
949 (*vec)[at] = Entry(m, newPos);
951 current = current.next();
954 var_start = false;
958 if(history.size()>1)
959 onAdded(Index(1));
960 Entry* e = fetch(Index(0));
961 for(Variations::const_iterator it = e->variations.begin();
962 it != e->variations.end(); ++it)
963 onAdded(Index(0).next(it->first));
964 for(VComments::const_iterator it = e->vcomments.begin();
965 it != e->vcomments.end(); ++it)
966 onSetVComment(Index(0), it->first, it->second);
968 current = Index(0);
969 onCurrentIndexChanged();