QTfrontend/net/hwmap.cpp
author nemo
Sun, 25 Jan 2015 14:48:02 -0500 (2015-01-25)
changeset 10808 1e39630d7c2e
parent 10472 8d04cebedb16
child 10959 1225f42f61e2
permissions -rw-r--r--
Add a couple of more variable bindings, make all the set visual gear values optional, to allow skipping trailing params or specifying ones in the middle as nil to not set. Should make using it more convenient in simple cases.
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>
9998
736015b847e3 update copyright to 2014
sheepluva
parents: 9080
diff changeset
     4
 * Copyright (c) 2004-2014 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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 9998
diff changeset
    17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4572
diff changeset
    18
 */
172
5294ada3910b working map preview
displacer
parents: 171
diff changeset
    19
10161
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    20
#include <QPainter>
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    21
#include <QBitmap>
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    22
#include <QLinearGradient>
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    23
497
adf1aee202c6 merge fixes from branch
displacer
parents: 486
diff changeset
    24
#include "hwconsts.h"
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    25
#include "hwmap.h"
1797
fedd8649fdd9 Templates filter by nemo
unc0rr
parents: 1791
diff changeset
    26
8070
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
    27
HWMap::HWMap(QObject * parent) :
66bc20d089fc Okay, remove previous request only if it has same parent
unc0rr
parents: 8069
diff changeset
    28
    TCPBase(false, parent)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    29
{
10248
7b9b44a051f8 Fix some of issues found by coverity
unc0rr
parents: 10166
diff changeset
    30
    templateFilter = 0;
7b9b44a051f8 Fix some of issues found by coverity
unc0rr
parents: 10166
diff changeset
    31
    m_mapgen = MAPGEN_REGULAR;
7b9b44a051f8 Fix some of issues found by coverity
unc0rr
parents: 10166
diff changeset
    32
    m_maze_size = 0;
10472
8d04cebedb16 Partially hook up feature size so horrorcat has something to test.
nemo
parents: 10391
diff changeset
    33
    m_feature_size = 50;
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    34
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    35
168
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    36
HWMap::~HWMap()
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    37
{
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    38
}
679e9b8912f5 multiple server starting improved
displacer
parents: 164
diff changeset
    39
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    40
bool HWMap::couldBeRemoved()
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    41
{
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    42
    return !m_hasStarted;
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    43
}
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    44
10472
8d04cebedb16 Partially hook up feature size so horrorcat has something to test.
nemo
parents: 10391
diff changeset
    45
void HWMap::getImage(const QString & seed, int filter, MapGenerator mapgen, int maze_size, const QByteArray & drawMapData, QString & script, int feature_size)
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    46
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    47
    m_seed = seed;
10150
fa5c83fd0ad9 Allow lua drawn maps (shoppamap, tunnels, diagonal maze etc) to generate previews.
nemo
parents: 10108
diff changeset
    48
    m_script = script;
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    49
    templateFilter = filter;
3133
1ab5f18f4df8 prg's maze generator
nemo
parents: 2948
diff changeset
    50
    m_mapgen = mapgen;
10472
8d04cebedb16 Partially hook up feature size so horrorcat has something to test.
nemo
parents: 10391
diff changeset
    51
    m_maze_size = maze_size; // TODO replace with feature_size
8d04cebedb16 Partially hook up feature size so horrorcat has something to test.
nemo
parents: 10391
diff changeset
    52
    m_feature_size = feature_size;
4572
087fdb85e2ef fix typo in hand drawn maps
nemo
parents: 4562
diff changeset
    53
    if(mapgen == MAPGEN_DRAWN) m_drawMapData = drawMapData;
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 6952
diff changeset
    54
    Start(true);
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    55
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
    56
5213
a86768368309 make the associate button use the user's settings for loading demos/saves
nemo
parents: 4976
diff changeset
    57
QStringList HWMap::getArguments()
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    58
{
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    59
    QStringList arguments;
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    60
    arguments << "--internal";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    61
    arguments << "--port";
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    62
    arguments << QString("%1").arg(ipc_port);
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    63
    arguments << "--user-prefix";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    64
    arguments << cfgdir->absolutePath();
10166
4be4a3302ff8 pass --prefix to previews too
nemo
parents: 10162
diff changeset
    65
    arguments << "--prefix";
4be4a3302ff8 pass --prefix to previews too
nemo
parents: 10162
diff changeset
    66
    arguments << datadir->absolutePath();
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8070
diff changeset
    67
    arguments << "--landpreview";
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    68
    return arguments;
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    69
}
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    70
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 172
diff changeset
    71
void HWMap::onClientDisconnect()
10161
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    72
{    
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    73
    QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128));
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    74
    linearGrad.setColorAt(1, QColor(0, 0, 192));
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    75
    linearGrad.setColorAt(0, QColor(66, 115, 225));
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    76
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    77
    if (readbuffer.size() == 128 * 32 + 1)
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    78
    {
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    79
        quint8 *buf = (quint8*) readbuffer.constData();
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    80
        QImage im(buf, 256, 128, QImage::Format_Mono);
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    81
        im.setNumColors(2);
10161
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    82
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    83
        QPixmap px(QSize(256, 128));
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    84
        QPixmap pxres(px.size());
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    85
        QPainter p(&pxres);
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    86
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    87
        px.fill(Qt::yellow);
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    88
        QBitmap bm = QBitmap::fromImage(im);
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    89
        px.setMask(bm);
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    90
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    91
        p.fillRect(pxres.rect(), linearGrad);
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    92
        p.drawPixmap(0, 0, px);
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    93
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
    94
        emit HHLimitReceived(buf[128 * 32]);
10161
c092f92aee23 Move preview rendering into HWMap
unc0rr
parents: 10159
diff changeset
    95
        emit ImageReceived(px);
10162
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
    96
    } else if (readbuffer.size() == 128 * 256 + 1)
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
    97
    {
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
    98
        QVector<QRgb> colorTable;
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
    99
        colorTable.resize(256);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   100
        for(int i = 0; i < 256; ++i)
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   101
            colorTable[i] = qRgba(255, 255, 0, i);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   102
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   103
        const quint8 *buf = (const quint8*) readbuffer.constData();
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   104
        QImage im(buf, 256, 128, QImage::Format_Indexed8);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   105
        im.setColorTable(colorTable);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   106
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   107
        QPixmap px = QPixmap::fromImage(im, Qt::ColorOnly);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   108
        QPixmap pxres(px.size());
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   109
        QPainter p(&pxres);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   110
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   111
        p.fillRect(pxres.rect(), linearGrad);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   112
        p.drawPixmap(0, 0, px);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   113
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   114
        emit HHLimitReceived(buf[128 * 256]);
38dbf26475d8 Add alpha channel to preview
unc0rr
parents: 10161
diff changeset
   115
        emit ImageReceived(px);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
   116
    }
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   117
}
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   118
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   119
void HWMap::SendToClientFirst()
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   120
{
4534
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
   121
    SendIPC(QString("eseed %1").arg(m_seed).toUtf8());
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
   122
    SendIPC(QString("e$template_filter %1").arg(templateFilter).toUtf8());
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
   123
    SendIPC(QString("e$mapgen %1").arg(m_mapgen).toUtf8());
10472
8d04cebedb16 Partially hook up feature size so horrorcat has something to test.
nemo
parents: 10391
diff changeset
   124
    SendIPC(QString("e$feature_size %1").arg(m_feature_size).toUtf8());
10159
5848ed144e0b move the freeModule too, couple of style tweaks
nemo
parents: 10150
diff changeset
   125
    if (!m_script.isEmpty())
10150
fa5c83fd0ad9 Allow lua drawn maps (shoppamap, tunnels, diagonal maze etc) to generate previews.
nemo
parents: 10108
diff changeset
   126
    {
fa5c83fd0ad9 Allow lua drawn maps (shoppamap, tunnels, diagonal maze etc) to generate previews.
nemo
parents: 10108
diff changeset
   127
        SendIPC(QString("escript Scripts/Multiplayer/%1.lua").arg(m_script).toUtf8());
fa5c83fd0ad9 Allow lua drawn maps (shoppamap, tunnels, diagonal maze etc) to generate previews.
nemo
parents: 10108
diff changeset
   128
    }
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   129
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   130
    switch (m_mapgen)
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   131
    {
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   132
        case MAPGEN_MAZE:
10391
ce3ccc45d790 Add separate option for perlin gen
unc0rr
parents: 10248
diff changeset
   133
        case MAPGEN_PERLIN:
4534
395278890955 Don't use std::string
unc0rr
parents: 4494
diff changeset
   134
            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
   135
            break;
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   136
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   137
        case MAPGEN_DRAWN:
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   138
        {
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   139
            QByteArray data = m_drawMapData;
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   140
            while(data.size() > 0)
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   141
            {
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   142
                QByteArray tmp = data;
4494
9585435e20f7 Pass hardcoded drawn map from frontend into engine \o/
unc0rr
parents: 4489
diff changeset
   143
                tmp.truncate(200);
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   144
                SendIPC("edraw " + tmp);
4494
9585435e20f7 Pass hardcoded drawn map from frontend into engine \o/
unc0rr
parents: 4489
diff changeset
   145
                data.remove(0, 200);
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   146
            }
4494
9585435e20f7 Pass hardcoded drawn map from frontend into engine \o/
unc0rr
parents: 4489
diff changeset
   147
            break;
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   148
        }
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   149
        default:
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6060
diff changeset
   150
            ;
4489
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   151
    }
c4ca9ced258b - Pass map drawing data to engine
unc0rr
parents: 3133
diff changeset
   152
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 1797
diff changeset
   153
    SendIPC("!");
164
92cff18a3ab6 first map preview added (still experimental)
displacer
parents:
diff changeset
   154
}