QTfrontend/net/hwmap.cpp
author unc0rr
Sun, 02 Dec 2012 01:25:11 +0400
changeset 8178 8bd087478b48
parent 8070 66bc20d089fc
child 8092 08960209db8c
child 8325 ecd51650d5d8
permissions -rw-r--r--
Fix QSettings problems: - Reopen file in ReadOnly mode if it was open in ReadWrite mode and is being read. This is needed for stupid QSettings which opens file in ReadWrite mode just to call readAll() on it. - Implement setSize(0)
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"
1797
fedd8649fdd9 Templates filter by nemo
unc0rr
parents: 1791
diff changeset
    22
8070
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
    23
HWMap::HWMap(QObject * parent) :
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
    24
    TCPBase(false, parent)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    25
{
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    26
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    27
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    28
HWMap::~HWMap()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    29
{
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    30
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    31
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    32
bool HWMap::couldBeRemoved()
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    33
{
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    34
    return !m_hasStarted;
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    35
}
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    36
4534
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
    37
void HWMap::getImage(const QString & seed, int filter, MapGenerator mapgen, int maze_size, const QByteArray & drawMapData)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    38
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    39
    m_seed = seed;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    40
    templateFilter = filter;
3133
1ab5f18f4df8 prg's maze generator
nemo
parents: 2948
diff changeset
    41
    m_mapgen = mapgen;
1ab5f18f4df8 prg's maze generator
nemo
parents: 2948
diff changeset
    42
    m_maze_size = maze_size;
4572
087fdb85e2ef fix typo in hand drawn maps
nemo
parents: 4562
diff changeset
    43
    if(mapgen == MAPGEN_DRAWN) m_drawMapData = drawMapData;
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    44
    Start(true);
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    45
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    46
5213
a86768368309 make the associate button use the user's settings for loading demos/saves
nemo
parents: 4976
diff changeset
    47
QStringList HWMap::getArguments()
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    48
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    49
    QStringList arguments;
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    50
    arguments << cfgdir->absolutePath();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    51
    arguments << QString("%1").arg(ipc_port);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    52
    arguments << "landpreview";
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    53
    return arguments;
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    54
}
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    55
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    56
void HWMap::onClientDisconnect()
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    57
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    58
    if (readbuffer.size() == 128 * 32 + 1)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    59
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    60
        quint8 *buf = (quint8*) readbuffer.constData();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    61
        QImage im(buf, 256, 128, QImage::Format_Mono);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    62
        im.setNumColors(2);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    63
        emit HHLimitReceived(buf[128 * 32]);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    64
        emit ImageReceived(im);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    65
    }
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    66
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    67
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    68
void HWMap::SendToClientFirst()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    69
{
4534
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
    70
    SendIPC(QString("eseed %1").arg(m_seed).toUtf8());
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
    71
    SendIPC(QString("e$template_filter %1").arg(templateFilter).toUtf8());
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
    72
    SendIPC(QString("e$mapgen %1").arg(m_mapgen).toUtf8());
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    73
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    74
    switch (m_mapgen)
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    75
    {
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    76
        case MAPGEN_MAZE:
4534
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
    77
            SendIPC(QString("e$maze_size %1").arg(m_maze_size).toUtf8());
4494
9585435e20f7 Pass hardcoded drawn map from frontend into engine \o/
unc0rr
parents: 4489
diff changeset
    78
            break;
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    79
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    80
        case MAPGEN_DRAWN:
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    81
        {
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    82
            QByteArray data = m_drawMapData;
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    83
            while(data.size() > 0)
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    84
            {
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    85
                QByteArray tmp = data;
4494
9585435e20f7 Pass hardcoded drawn map from frontend into engine \o/
unc0rr
parents: 4489
diff changeset
    86
                tmp.truncate(200);
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    87
                SendIPC("edraw " + tmp);
4494
9585435e20f7 Pass hardcoded drawn map from frontend into engine \o/
unc0rr
parents: 4489
diff changeset
    88
                data.remove(0, 200);
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    89
            }
4494
9585435e20f7 Pass hardcoded drawn map from frontend into engine \o/
unc0rr
parents: 4489
diff changeset
    90
            break;
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    91
        }
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    92
        default:
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
    93
            ;
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    94
    }
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
    95
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    96
    SendIPC("!");
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    97
}