QTfrontend/net/hwmap.cpp
author unc0rr
Thu, 22 Nov 2012 01:19:16 +0400
branchflibqtfrontend
changeset 8092 08960209db8c
parent 8070 66bc20d089fc
child 8094 6c5b4e69f03d
permissions -rw-r--r--
Try to use map preview facilities: - HWGame and HWRecorder are completely broken - HWMap should work, but doesn't
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
172
5294ada3910b working map preview
displacer
parents: 171
diff changeset
     1
/*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 497
diff changeset
     2
 * Hedgewars, a free turn based strategy game
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4572
diff changeset
     3
 * Copyright (c) 2006-2007 Ulyanov Igor <iulyanov@gmail.com>
6952
7f70f37bbf08 license header year range adjustments
sheepluva
parents: 6700
diff changeset
     4
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
172
5294ada3910b working map preview
displacer
parents: 171
diff changeset
     5
 *
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
172
5294ada3910b working map preview
displacer
parents: 171
diff changeset
     9
 *
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
    13
 * GNU General Public License for more details.
172
5294ada3910b working map preview
displacer
parents: 171
diff changeset
    14
 *
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
    15
 * You should have received a copy of the GNU General Public License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 180
diff changeset
    16
 * along with this program; if not, write to the Free Software
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4572
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
088d40d8aba2 Happy 2011 :)
koda
parents: 4572
diff changeset
    18
 */
172
5294ada3910b working map preview
displacer
parents: 171
diff changeset
    19
497
adf1aee202c6 merge fixes from branch
displacer
parents: 486
diff changeset
    20
#include "hwconsts.h"
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    21
#include "hwmap.h"
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    22
#include "frontlibpoller.h"
1797
fedd8649fdd9 Templates filter by nemo
unc0rr
parents: 1791
diff changeset
    23
8070
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
    24
HWMap::HWMap(QObject * parent) :
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    25
    TCPBase(parent)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    26
{
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    27
    m_conn = NULL;
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    28
    m_map = NULL;
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    29
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    30
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    31
HWMap::~HWMap()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    32
{
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    33
    if(m_conn)
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    34
        flib_mapconn_destroy(m_conn);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    35
    if(m_map)
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    36
        flib_map_destroy(m_map);
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    37
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    38
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    39
bool HWMap::couldBeRemoved()
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    40
{
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    41
    return !m_hasStarted;
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    42
}
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    43
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    44
void HWMap::getImage(const QString & seed, int filter, int mapgen, int maze_size, const QByteArray & drawMapData)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    45
{
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    46
    switch(mapgen)
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    47
    {
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    48
        case MAPGEN_REGULAR: m_map =
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    49
                flib_map_create_regular(
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    50
                    seed.toUtf8().constData()
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    51
                    , "" // theme? here?
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    52
                    , filter);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    53
                break;
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    54
        case MAPGEN_MAZE: m_map =
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    55
                flib_map_create_maze(
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    56
                    seed.toUtf8().constData()
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    57
                    , ""
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    58
                    , maze_size);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    59
                break;
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    60
        case MAPGEN_DRAWN: m_map =
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    61
                flib_map_create_drawn(
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    62
                    seed.toUtf8().constData()
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    63
                    , ""
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    64
                    , (const uint8_t*)drawMapData.constData()
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    65
                    , drawMapData.size()
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    66
                    );
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    67
                break;
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    68
        default:
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    69
            Q_ASSERT_X(false, "HWMap::getImage", "Unknown generator");
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    70
    }
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    71
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    72
    start(true);
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    73
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    74
5213
a86768368309 make the associate button use the user's settings for loading demos/saves
nemo
parents: 4976
diff changeset
    75
QStringList HWMap::getArguments()
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    76
{
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    77
    Q_ASSERT(m_conn);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    78
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    79
    int ipc_port = flib_mapconn_getport(m_conn);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    80
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    81
    QStringList arguments;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    82
    arguments << cfgdir->absolutePath();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    83
    arguments << QString("%1").arg(ipc_port);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    84
    arguments << "landpreview";
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    85
    return arguments;
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    86
}
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    87
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    88
void HWMap::onSuccess(void *context, const uint8_t *bitmap, int numHedgehogs)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    89
{
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    90
    qDebug("HWMap::onSuccess");
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    91
    HWMap * hwMap = (HWMap *)context;
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    92
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    93
    QImage im(bitmap, MAPIMAGE_WIDTH, MAPIMAGE_HEIGHT, QImage::Format_Mono);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    94
    im.setNumColors(2);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    95
    emit hwMap->HHLimitReceived(numHedgehogs);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    96
    emit hwMap->ImageReceived(im);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    97
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    98
    hwMap->clientDisconnected();
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    99
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   100
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   101
void HWMap::onFailure(void *context, const char *errormessage)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   102
{
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   103
    qDebug("HWMap::onFailure");
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   104
    HWMap * hwMap = (HWMap *)context;
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   105
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   106
    hwMap->clientDisconnected();
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   107
}
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   108
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   109
void HWMap::onEngineStart()
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   110
{
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   111
    qDebug("HWMap::onEngineStart");
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   112
    m_conn = flib_mapconn_create(m_map);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   113
    flib_mapconn_onSuccess(m_conn, onSuccess, this);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   114
    flib_mapconn_onFailure(m_conn, onFailure, this);
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   115
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   116
    new FrontLibPoller((void (*)(void *))flib_mapconn_tick, m_conn, this);
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   117
}