Finalize support for delegating behaviours.
[tagua/yd.git] / src / variants / crazyhouse / crazyhouse.cpp
blob8674449c6557c98c1f42b91d0fb47e064d406cb1
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 based on chess
51 Component* chess_behaviour_comp = chess->getComponent("behaviour");
52 Q_ASSERT(chess_behaviour_comp);
53 IBehaviour* behaviour_clone = NULL;
54 Q_ASSERT(QMetaObject::invokeMethod(chess_behaviour_comp, "clone",
55 Q_RETURN_ARG(IBehaviour*, behaviour_clone)));
56 Q_ASSERT(behaviour_clone);
57 const Behaviour* behaviour = new Behaviour(behaviour_clone);
59 // create crazyhouse state factory
60 Component* state_component = 0;
61 bool ok = QMetaObject::invokeMethod(chess_state_comp, "clone",
62 Q_RETURN_ARG(Component*, state_component),
63 Q_ARG(const IBehaviour*, behaviour),
64 Q_ARG(Component*, chess->getComponent("extra/castling_rules")),
65 Q_ARG(Point, chess_state->board()->size()));
66 kDebug() << "cloning prototype state" << ok;
67 State* state = new State(requestInterface<IState>(state_component));
69 // set state factory
70 repo->addComponent("state", state);
72 // set animator factory
73 IAnimatorFactory* chess_animator_factory =
74 requestInterface<IAnimatorFactory>(chess->getComponent("animator_factory"));
75 repo->addComponent("animator_factory", new AnimatorFactory(chess_animator_factory));
77 // set validator
78 IValidator* validator = requestInterface<IValidator>(chess->getComponent("validator"));
79 repo->addComponent("validator", new Validator(validator));
81 // set move serializers
82 Repository::ComponentMap serializers = chess->listComponents("move_serializer");
83 for (Repository::ComponentMap::const_iterator it = serializers.begin(),
84 end = serializers.end(); it != end; ++it) {
85 IMoveSerializer* s = requestInterface<IMoveSerializer>(it.value());
86 if (s) repo->addComponent("move_serializer/" + it.key(), new MoveSerializer(s));
89 LOAD_TYPE(pawn);
90 LOAD_TYPE(king);
91 LOAD_TYPE(queen);
92 LOAD_TYPE(bishop);
93 LOAD_TYPE(rook);
94 LOAD_TYPE(knight);
95 players[0] = White::self();
96 players[1] = Black::self();
98 return repo;