QTfrontend/net/hwmap.cpp
author unc0rr
Fri, 04 Jan 2013 21:44:40 +0400
branchflibqtfrontend
changeset 8363 0b4ac686fc44
parent 8304 620560c89284
parent 8325 ecd51650d5d8
permissions -rw-r--r--
Merge from default
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()
8094
6c5b4e69f03d Fix some frontlib bugs
unc0rr
parents: 8092
diff changeset
    51
                    , ""
8092
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
    }
8094
6c5b4e69f03d Fix some frontlib bugs
unc0rr
parents: 8092
diff changeset
    71
    qDebug(m_map->seed);
8092
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;
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    82
    arguments << "--internal";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    83
    arguments << "--port";
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    84
    arguments << QString("%1").arg(ipc_port);
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    85
    arguments << "--user-prefix";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    86
    arguments << cfgdir->absolutePath();
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    87
    arguments << "--landpreview";
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    88
    return arguments;
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    89
}
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    90
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    91
void HWMap::onSuccess(void *context, const uint8_t *bitmap, int numHedgehogs)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    92
{
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    93
    HWMap * hwMap = (HWMap *)context;
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    94
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    95
    QImage im(bitmap, MAPIMAGE_WIDTH, MAPIMAGE_HEIGHT, QImage::Format_Mono);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    96
    im.setNumColors(2);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    97
    emit hwMap->HHLimitReceived(numHedgehogs);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    98
    emit hwMap->ImageReceived(im);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
    99
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   100
    hwMap->clientDisconnected();
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   101
}
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
void HWMap::onFailure(void *context, const char *errormessage)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   104
{
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   105
    HWMap * hwMap = (HWMap *)context;
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   106
8304
620560c89284 Merge from default
unc0rr
parents: 8097
diff changeset
   107
    qWarning(errormessage);
620560c89284 Merge from default
unc0rr
parents: 8097
diff changeset
   108
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   109
    hwMap->clientDisconnected();
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   110
}
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   111
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   112
void HWMap::onEngineStart()
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   113
{
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   114
    m_conn = flib_mapconn_create(m_map);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   115
    flib_mapconn_onSuccess(m_conn, onSuccess, this);
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   116
    flib_mapconn_onFailure(m_conn, onFailure, this);
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   117
8092
08960209db8c Try to use map preview facilities:
unc0rr
parents: 8070
diff changeset
   118
    new FrontLibPoller((void (*)(void *))flib_mapconn_tick, m_conn, this);
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   119
}