7283c0c320a09604e6098f3db31fee002379d471
[tagua/yd.git] / src / variants / crazyhouse / crazyhouse.cpp
blob7283c0c320a09604e6098f3db31fee002379d471
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 */
10 #include <core/board.h>
11 #include <core/color.h>
12 #include <core/repository.h>
13 #include <core/type.h>
14 #include <core/variantloader.h>
16 #include "animator.h"
17 #include "behaviour.h"
18 #include "moveserializer.h"
19 #include "state.h"
20 #include "validator.h"
22 #include <KDebug>
24 using namespace Crazyhouse;
26 #define LOAD_TYPE(NAME) \
27 NAME = requestInterface<IType>(chess->getComponent("type/" #NAME))
29 namespace Crazyhouse {
30 const IType* pawn;
31 const IType* king;
32 const IType* queen;
33 const IType* bishop;
34 const IType* rook;
35 const IType* knight;
36 const IColor* players[2];
39 extern "C" KDE_EXPORT Repository*
40 taguacrazyhouse_initrepo(IVariantLoader* loader) {
41 Repository* repo = new Repository;
42 Repository* chess = loader->getRepository("chess");
43 if (!chess) return 0;
44 repo->setProxy(chess);
46 // get chess state factory
47 Component* chess_state_comp = chess->getComponent("state");
48 IState* chess_state = requestInterface<IState>(chess_state_comp);
50 // create crazyhouse behaviour
51 const IBehaviour* behaviour = new Behaviour(chess_state->behaviour());
53 // create crazyhouse state factory
54 Component* state_component = 0;
55 bool ok = QMetaObject::invokeMethod(chess_state_comp, "clone",
56 Q_RETURN_ARG(Component*, state_component),
57 Q_ARG(const IBehaviour*, behaviour),
58 Q_ARG(Component*, chess->getComponent("extra/castling_rules")),
59 Q_ARG(Point, chess_state->board()->size()));
60 kDebug() << "cloning prototype state" << ok;
61 State* state = new State(requestInterface<IState>(state_component));
63 // set state factory
64 repo->addComponent("state", state);
66 // set animator factory
67 IAnimatorFactory* chess_animator_factory =
68 requestInterface<IAnimatorFactory>(chess->getComponent("animator_factory"));
69 repo->addComponent("animator_factory", new AnimatorFactory(chess_animator_factory));
71 // set validator
72 IValidator* validator = requestInterface<IValidator>(chess->getComponent("validator"));
73 repo->addComponent("validator", new Validator(validator));
75 // set move serializers
76 Repository::ComponentMap serializers = chess->listComponents("move_serializer");
77 for (Repository::ComponentMap::const_iterator it = serializers.begin(),
78 end = serializers.end(); it != end; ++it) {
79 IMoveSerializer* s = requestInterface<IMoveSerializer>(it.value());
80 if (s) repo->addComponent("move_serializer/" + it.key(), new MoveSerializer(s));
83 LOAD_TYPE(pawn);
84 LOAD_TYPE(king);
85 LOAD_TYPE(queen);
86 LOAD_TYPE(bishop);
87 LOAD_TYPE(rook);
88 LOAD_TYPE(knight);
89 players[0] = White::self();
90 players[1] = Black::self();
92 return repo;