Prepare 1.0 alpha3 release.
[tagua/yd.git] / src / game.cpp
blobe70fb380cf31b2d212c8bc07eb4b51c575c3cc36
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 <map>
12 #include <KDebug>
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
21 #include "variants.h"
22 #include "pgnparser.h"
23 #include "tagua.h"
24 #include "game.h"
25 #include "game_p.h"
28 using namespace GamePrivate;
31 Game::Game()
32 : current(-1)
33 , undo_pos(0) {
36 Game::~Game() {
39 void Game::onAdded(const Index&) {
42 void Game::onRemoved(const Index&) {
45 void Game::onEntryChanged(const Index&, int) {
48 void Game::onPromoteVariation(const Index&, int) {
51 void Game::onSetComment(const Index&, const QString&) {
54 void Game::onSetVComment(const Index&, int, const QString&) {
57 void Game::onCurrentIndexChanged(const Index&) {
60 void Game::onAvailableUndo(bool) {
63 void Game::onAvailableRedo(bool) {
66 Entry* Game::fetch(const Index& ix) {
67 int at;
68 History *vec = fetchRef(ix, &at);
69 return vec ? &(*vec)[at] : NULL;
72 const Entry* Game::fetch(const Index& ix) const {
73 int at;
74 const History *vec = fetchRef(ix, &at);
75 return vec ? &(*vec)[at] : NULL;
78 History* Game::fetchRef(const Index& ix, int* idx) {
79 if(ix.num_moves >= (int)history.size() || ix.num_moves < 0 )
80 return NULL;
82 History* aretv = &history;
83 Entry* retv = &history[ix.num_moves];
84 if(idx) *idx = ix.num_moves;
86 for(int i=0; i<(int)ix.nested.size();i++) {
87 Variations::iterator it = retv->variations.find(ix.nested[i].variation);
88 if(it == retv->variations.end() || ix.nested[i].num_moves >= (int)it->second.size()
89 || ix.nested[i].num_moves < 0 )
90 return NULL;
92 aretv = &it->second;
93 retv = &it->second[ix.nested[i].num_moves];
94 if(idx) *idx = ix.nested[i].num_moves;
96 return aretv;
99 const History* Game::fetchRef(const Index& ix, int* idx) const {
100 return const_cast<const History*>(const_cast<Game*>(this)->fetchRef(ix, idx));
103 void Game::testMove(const Index& ix) {
104 if (ix != Index(0)) {
105 Entry *e1 = fetch(ix.prev());
106 Entry *e2 = fetch(ix);
107 if(!e1 || !e2 || !e1->position || !e2->move)
108 return;
110 if (!e1->position->testMove(e2->move))
111 kError() << "invalid move added to game history";
115 void Game::testMove() {
116 testMove(current);
119 void Game::saveUndo(const UndoOp& op) {
120 bool redo = undo_pos < (int)undo_history.size();
122 while(undo_pos < (int)undo_history.size())
123 undo_history.pop_back();
124 undo_history.push_back(op);
125 undo_pos++;
127 if(undo_pos == 1)
128 onAvailableUndo(true);
129 if(redo)
130 onAvailableRedo(false);
134 Index Game::index() const {
135 return current;
138 Index Game::lastMainlineIndex() const {
139 return Index(history.size()-1);
142 bool Game::containsIndex(const Index& index) const {
143 return !!fetch(index);
146 MovePtr Game::move() const {
147 return move(current);
150 MovePtr Game::move(const Index& index) const {
151 Entry *e = (Entry*)fetch(index);
152 if(!e) {
153 kError() << "Index out of range";
154 return MovePtr();
156 return e->move;
159 PositionPtr Game::position() const {
160 return position(current);
163 PositionPtr Game::position(const Index& index) const {
164 Entry *e = (Entry*)fetch(index);
165 if(!e) {
166 kError() << "Index" << index << "out of range";
167 return PositionPtr();
169 return e->position;
172 QString Game::comment() const {
173 return comment(current);
176 QString Game::comment(const Index& index) const {
177 const Entry *e = fetch(index);
178 if(!e) {
179 kError() << "Index out of range";
180 return QString();
182 return e->comment;
185 void Game::reset(PositionPtr pos) {
186 Q_ASSERT(pos);
188 undo_pos = 0;
189 undo_history.clear();
190 history.clear();
191 history.push_back( Entry(MovePtr(), pos) );
192 current = Index(0);
193 onCurrentIndexChanged();
196 void Game::undo() {
197 if(undo_pos <= 0) {
198 kError() << "Nothing to undo";
199 return;
202 bool last_undo = undo_pos == 1;
203 bool now_redo = undo_pos == (int)undo_history.size();
205 undo_pos--;
206 UndoOp* op = &(undo_history[undo_pos]);
208 if(boost::get<UndoAdd>(op)) {
209 UndoAdd *a = boost::get<UndoAdd>(op);
211 if(a->index.atVariationStart() ) {
212 Entry* e = fetch(a->index.prev());
213 Q_ASSERT(e);
215 int v = a->index.nested.back().variation;
216 Q_ASSERT(e->variations.count(v) == 1);
217 Q_ASSERT(e->variations[v].size() == 1);
219 e->variations.erase(v);
221 else {
222 int at;
223 std::vector<Entry>* vec = fetchRef(a->index, &at);
224 Q_ASSERT(vec);
225 Q_ASSERT((int)vec->size() == at+1);
227 vec->pop_back();
230 if(current == a->index) {
231 current = current.prev();
232 onCurrentIndexChanged();
235 onRemoved(a->index);
237 else if(boost::get<UndoPromote>(op)) {
238 UndoPromote *p = boost::get<UndoPromote>(op);
240 int at;
241 std::vector<Entry>* vec = fetchRef(p->index, &at);
242 Q_ASSERT(vec);
243 Q_ASSERT((*vec)[at].variations.count(p->variation)==1);
245 History vold = (*vec)[at].variations[p->variation];
246 History vnew;
247 for(int i=at+1; i<(int)vec->size(); i++)
248 vnew.push_back((*vec)[i]);
249 while((int)vec->size()>at+1)
250 vec->pop_back();
251 for(int i=0; i<(int)vold.size(); i++)
252 vec->push_back(vold[i]);
253 (*vec)[at].variations[p->variation] = vnew;
255 current = current.flipVariation(p->index, p->variation);
256 onPromoteVariation(p->index, p->variation);
257 //onCurrentIndexChanged();
259 else if(boost::get<UndoTruncate>(op)) {
260 UndoTruncate *t = boost::get<UndoTruncate>(op);
262 int at;
263 std::vector<Entry>* vec = fetchRef(t->index, &at);
264 Q_ASSERT(vec);
265 Q_ASSERT((int)vec->size() == at+1);
266 Q_ASSERT((*vec)[at].variations.empty());
268 for(int i=0;i<(int)t->history.size();i++)
269 vec->push_back(t->history[i]);
270 (*vec)[at].variations = t->variations;
271 (*vec)[at].vcomments = t->vcomments;
273 if(t->history.size())
274 onAdded(t->index.next());
275 for(Variations::iterator it = t->variations.begin(); it != t->variations.end(); ++it)
276 onAdded(t->index.next(it->first));
277 for(VComments::iterator it = t->vcomments.begin(); it != t->vcomments.end(); ++it)
278 onSetVComment(t->index, it->first, it->second);
280 else if(boost::get<UndoRemove>(op)) {
281 UndoRemove *r = boost::get<UndoRemove>(op);
283 Entry *e = fetch(r->index);
284 e->variations[r->variation] = r->history;
285 onAdded(r->index.next(r->variation));
286 if(!r->vcomment.isEmpty()) {
287 e->vcomments[r->variation] = r->vcomment;
288 onSetVComment(r->index, r->variation, r->vcomment);
291 else if(boost::get<UndoClear>(op)) {
292 UndoClear *c = boost::get<UndoClear>(op);
294 Entry *e = fetch(c->index);
295 e->variations = c->variations;
296 e->vcomments = c->vcomments;
297 for(Variations::iterator it = c->variations.begin(); it != c->variations.end(); ++it)
298 onAdded(c->index.next(it->first));
299 for(VComments::iterator it = c->vcomments.begin(); it != c->vcomments.end(); ++it)
300 onSetVComment(c->index, it->first, it->second);
302 else if(boost::get<UndoSetComment>(op)) {
303 UndoSetComment *sc = boost::get<UndoSetComment>(op);
304 Entry *e = fetch(sc->index);
305 Q_ASSERT(e);
307 if(sc->variation == -1) {
308 e->comment = sc->old_comment;
309 onSetComment(sc->index, sc->old_comment);
311 else {
312 if(sc->old_comment.isEmpty())
313 e->vcomments.erase(sc->variation);
314 else
315 e->vcomments[sc->variation] = sc->old_comment;
316 onSetVComment(sc->index, sc->variation, sc->old_comment);
319 else
320 kError() << "Unexpected type in boost::variant";
322 if(last_undo)
323 onAvailableUndo(false);
324 if(now_redo)
325 onAvailableRedo(true);
328 void Game::redo() {
329 if(undo_pos >= (int)undo_history.size()) {
330 kError() << "Nothing to redo";
331 return;
334 bool now_undo = undo_pos == 0;
335 bool last_redo = undo_pos == (int)undo_history.size()-1;
337 UndoOp* op = &(undo_history[undo_pos]);
338 undo_pos++;
340 if(boost::get<UndoAdd>(op)) {
341 UndoAdd *a = boost::get<UndoAdd>(op);
343 if(a->index.atVariationStart() ) {
344 Entry* e = fetch(a->index.prev());
345 Q_ASSERT(e);
347 int v = a->index.nested.back().variation;
348 Q_ASSERT(e->variations.count(v) == 0);
350 History h;
351 h.push_back(a->entry);
352 e->variations[v] = h;
354 else {
355 int at;
356 std::vector<Entry>* vec = fetchRef(a->index.prev(), &at);
357 Q_ASSERT(vec);
358 Q_ASSERT((int)vec->size() == at+1);
360 vec->push_back(a->entry);
363 onAdded(a->index);
364 current = a->index;
365 onCurrentIndexChanged();
367 else if(boost::get<UndoPromote>(op)) {
368 UndoPromote *p = boost::get<UndoPromote>(op);
370 int at;
371 std::vector<Entry>* vec = fetchRef(p->index, &at);
373 Q_ASSERT(vec);
374 Q_ASSERT((*vec)[at].variations.count(p->variation)==1);
375 History vold = (*vec)[at].variations[p->variation];
376 History vnew;
377 for(int i=at+1; i<(int)vec->size(); i++)
378 vnew.push_back((*vec)[i]);
379 while((int)vec->size()>at+1)
380 vec->pop_back();
381 for(int i=0; i<(int)vold.size(); i++)
382 vec->push_back(vold[i]);
383 (*vec)[at].variations[p->variation] = vnew;
385 current = current.flipVariation(p->index, p->variation);
386 onPromoteVariation(p->index, p->variation);
387 //onCurrentIndexChanged();
389 else if(boost::get<UndoTruncate>(op)) {
390 UndoTruncate *t = boost::get<UndoTruncate>(op);
392 int at;
393 std::vector<Entry>* vec = fetchRef(t->index, &at);
394 Q_ASSERT(vec);
395 Q_ASSERT((int)vec->size() == at+1+(int)t->history.size());
397 while((int)vec->size() > at+1)
398 vec->pop_back();
399 (*vec)[at].variations.clear();
400 (*vec)[at].vcomments.clear();
402 if(current > t->index) {
403 current = t->index;
404 onCurrentIndexChanged();
407 if(t->history.size())
408 onRemoved(t->index.next());
409 for(Variations::iterator it = t->variations.begin(); it != t->variations.end(); ++it)
410 onRemoved(t->index.next(it->first));
412 else if(boost::get<UndoRemove>(op)) {
413 UndoRemove *r = boost::get<UndoRemove>(op);
415 Entry *e = fetch(r->index);
416 e->variations.erase(r->variation);
417 e->vcomments.erase(r->variation);
418 onRemoved(r->index.next(r->variation));
420 else if(boost::get<UndoClear>(op)) {
421 UndoClear *c = boost::get<UndoClear>(op);
423 Entry *e = fetch(c->index);
424 e->variations.clear();
425 e->vcomments.clear();
426 for(Variations::iterator it = c->variations.begin(); it != c->variations.end(); ++it)
427 onRemoved(c->index.next(it->first));
429 else if(boost::get<UndoSetComment>(op)) {
430 UndoSetComment *sc = boost::get<UndoSetComment>(op);
431 Entry *e = fetch(sc->index);
432 Q_ASSERT(e);
434 if(sc->variation == -1) {
435 e->comment = sc->new_comment;
436 onSetComment(sc->index, sc->new_comment);
438 else {
439 if(sc->new_comment.isEmpty())
440 e->vcomments.erase(sc->variation);
441 else
442 e->vcomments[sc->variation] = sc->new_comment;
443 onSetVComment(sc->index, sc->variation, sc->new_comment);
446 else
447 kError() << "Unexpected type in boost::variant";
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 kError() << "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 kError() << "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 kError() << "Cannot promote main line";
498 return;
500 Index ix = _ix;
501 int v = ix.nested.back().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 kError() << "Cannot remove main line";
536 return;
538 Index ix = _ix;
539 int v = ix.nested.back().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 kError() << "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(MovePtr m, PositionPtr 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)) {
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) ) {
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(MovePtr m, PositionPtr 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 kError() << "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);
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;
770 QString mv = (vec[i].move && preve.position) ?
771 vec[i].move->toString("compact", preve.position ) : "???";
772 #if 0
773 if (ix == current)
774 mv = "[[" + mv + "]]";
775 #endif
777 int n = ix.totalNumMoves()+1;
778 if(i==start || n%2==0)
779 mv = QString::number(n/2)+(n%2==1 ? ". ... " : ". ") + mv;
780 if (i > start)
781 mv = " " + mv;
783 res += mv;
785 if(!vec[i].comment.isEmpty())
786 res += " {" + vec[i].comment + "}";
788 if(i > 0) {
789 for(Variations::const_iterator it = vec[i-1].variations.begin();
790 it != vec[i-1].variations.end(); ++it) {
791 res += " (";
792 if(vec[i-1].vcomments.count(it->first))
793 res += "{" + vec[i-1].vcomments.find(it->first)->second + "} ";
794 res += variationPgn(it->second, vec[i - 1], 0,
795 ix.prev().next(it->first)) + ")";
799 ix = ix.next();
801 return res;
804 QString Game::pgn() const {
805 return variationPgn(history, history[0], 1, Index(1));
808 void Game::load(const PGN& pgn) {
809 std::map<QString, QString>::const_iterator var = pgn.m_tags.find("Variant");
810 VariantPtr vi;
812 if (var == pgn.m_tags.end()) {
813 vi = Variants::instance().get("chess");
815 else if (!(vi = Variants::instance().get(var->second))) {
816 kError() << "No such variant" << var->second;
817 return;
820 std::map<QString, QString>::const_iterator fen = pgn.m_tags.find("FEN");
821 PositionPtr pos;
823 //if(var == pgn.m_tags.end()) {
824 pos = vi->createPosition();
825 pos->setup();
827 #if 0 // BROKEN
828 else if( !(pos = vi->createPositionFromFEN(fen->second))) {
829 kError() << "Wrong fen " << fen->second;
830 return;
832 #endif
834 //TODO: what about options? FEN rules?
836 load(pos, pgn);
839 void Game::load(PositionPtr pos, const PGN& pgn) {
840 current = Index(0);
841 undo_history.clear();
842 undo_pos = 0;
844 // setup an empty history, clear as needed
846 if(history.size()) {
847 Entry* fe = &history[0];
848 int old_history_size = history.size();
849 std::vector<int> v_ids;
851 while(history.size()>1)
852 history.pop_back();
853 for(Variations::const_iterator it = fe->variations.begin();
854 it != fe->variations.end(); ++it)
855 v_ids.push_back(it->first);
856 fe->variations.clear();
857 fe->vcomments.clear();
859 for(int i=0;i<(int)v_ids.size();i++)
860 onRemoved(Index(0).next(v_ids[i]));
861 if(old_history_size>1)
862 onRemoved(Index(1));
863 v_ids.clear();
864 history[0].position = pos;
866 else
867 history.push_back( Entry(MovePtr(), pos) );
869 // apply moves from PGN, one by one
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 kError() << "Unexpected end variation";
893 break;
895 current = var_stack.back();
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) // not all moves get numbered in PGN, usually only 1st player ones
904 current = current.prev();
905 else if(pm->m_number>n+1)
906 kError() << "Too far variation";
907 else {
908 if(pm->m_number<n)
909 kError() << "Too close variation";
910 current = current.prev(n + 1 - pm->m_number);
913 else if(pm->m_number && pm->m_number!=n+1)
914 kError() << "Move number mismatch";
916 PositionPtr pos = position();
917 MovePtr m = pos->getMove(pm->m_move);
919 if(!m || !pos->testMove(m))
920 break;
922 PositionPtr newPos = pos->clone();
923 newPos->move(m);
925 int at;
926 History *vec = fetchRef(current, &at);
927 Q_ASSERT(vec);
929 if(var_start) {
930 Entry *e = &(*vec)[at];
931 int var_id = e->last_var_id++;
932 e->variations[var_id].push_back(Entry(m, newPos));
933 if(!vcomment.isEmpty()) {
934 e->vcomments[var_id] = vcomment;
935 vcomment = QString();
937 /* this is a hack, but the mainline should NEVER
938 be empty if there is a variation*/
939 if((int)vec->size() - 1 == at)
940 vec->push_back(Entry(m, newPos));
942 current = current.next(var_id);
944 else {
945 if((int)vec->size() - 1 == at)
946 vec->push_back(Entry(m, newPos));
947 else
948 (*vec)[at] = Entry(m, newPos);
950 current = current.next();
953 var_start = false;
955 else
956 kError() << "Unexpected type in boost::variant";
959 if(history.size()>1)
960 onAdded(Index(1));
961 Entry* e = fetch(Index(0));
962 for(Variations::const_iterator it = e->variations.begin();
963 it != e->variations.end(); ++it)
964 onAdded(Index(0).next(it->first));
965 for(VComments::const_iterator it = e->vcomments.begin();
966 it != e->vcomments.end(); ++it)
967 onSetVComment(Index(0), it->first, it->second);
969 current = Index(0);
970 onCurrentIndexChanged();