QTfrontend/team.cpp
author unc0rr
Wed, 01 Feb 2006 20:42:53 +0000
changeset 57 e1a77ae57065
parent 26 e32fa14529f8
child 86 664b536a1c27
permissions -rw-r--r--
- Fixed compiling .) - Fixed camera following gears
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     1
/*
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     2
 * Hedgewars, a worms-like game
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     3
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     4
 *
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     6
 *
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
     9
 * with the Software without restriction, including without limitation the
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    12
 * furnished to do so, subject to the following conditions:
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    13
 *
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    15
 *    this list of conditions and the following disclaimer.
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    18
 *    and/or other materials provided with the distribution.
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    20
 *    derived from this software without specific prior written permission.
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    21
 *
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    32
 */
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    33
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    34
#include <QFile>
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    35
#include <QTextStream>
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    36
#include "team.h"
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    37
#include "hwform.h"
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    38
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    39
HWTeam::HWTeam(const QString & teamname)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    40
{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    41
	TeamName = teamname;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    42
	for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    43
	Grave = "Simple";
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    44
	Fort = "Barrelhouse";
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    45
	for(int i = 0; i < BINDS_NUMBER; i++)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    46
	{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    47
		binds[i].action = cbinds[i].action;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    48
		binds[i].strbind = cbinds[i].strbind;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    49
	}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    50
	dir = "";
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    51
}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    52
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    53
bool HWTeam::LoadFromFile()
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    54
{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    55
	QFile cfgfile(dir + "/" + TeamName + ".cfg");
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    56
	if (!cfgfile.open(QIODevice::ReadOnly)) return false;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    57
	QTextStream stream(&cfgfile);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    58
	stream.setCodec("UTF-8");
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    59
	QString str;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    60
	QString action;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    61
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    62
	while (!stream.atEnd())
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    63
	{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    64
		str = stream.readLine();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    65
		if (str.startsWith(";")) continue;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    66
		if (str.startsWith("name team "))
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    67
		{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    68
			str.remove(0, 10);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    69
			TeamName = str;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    70
		} else
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    71
		if (str.startsWith("name hh"))
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    72
		{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    73
			str.remove(0, 7);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    74
			long i = str.left(1).toLong();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    75
			if ((i < 0) || (i > 7)) continue;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    76
			str.remove(0, 2);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    77
			HHName[i] = str;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    78
		} else
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    79
		if (str.startsWith("grave "))
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    80
		{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    81
			str.remove(0, 6);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    82
			Grave = str;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    83
		} else
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    84
		if (str.startsWith("fort "))
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    85
		{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    86
			str.remove(0, 5);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    87
			Fort = str;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    88
		} else
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    89
		if (str.startsWith("bind "))
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    90
		{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    91
			str.remove(0, 5);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    92
			action = str.section(' ', 1);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    93
			str = str.section(' ', 0, 0);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    94
			str.truncate(15);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    95
			for (int i = 0; i < BINDS_NUMBER; i++)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    96
				if (action == binds[i].action)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    97
				{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    98
					binds[i].strbind = str;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    99
					break;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   100
				}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   101
		}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   102
	}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   103
	cfgfile.close();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   104
	return true;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   105
}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   106
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   107
bool HWTeam::SaveToFile()
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   108
{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   109
	QFile cfgfile(dir + "/" + TeamName + ".cfg");
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   110
	if (!cfgfile.open(QIODevice::WriteOnly)) return false;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   111
	QTextStream stream(&cfgfile);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   112
	stream.setCodec("UTF-8");
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   113
	stream << "; Generated by Hedgewars, do not modify" << endl;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   114
	stream << "name team " << TeamName << endl;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   115
	for (int i = 0; i < 8; i++)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   116
		stream << "name hh" << i << " " << HHName[i] << endl;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   117
	stream << "grave " << Grave << endl;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   118
	stream << "fort " << Fort << endl;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   119
	for(int i = 0; i < BINDS_NUMBER; i++)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   120
	{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   121
		stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   122
	}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   123
	cfgfile.close();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   124
	return true;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   125
}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   126
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   127
void HWTeam::SetToPage(HWForm * hwform)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   128
{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   129
	hwform->TeamNameEdit->setText(TeamName);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   130
	for(int i = 0; i < 8; i++)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   131
	{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   132
		hwform->HHNameEdit[i]->setText(HHName[i]);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   133
	}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   134
	hwform->ui.CBGrave->setCurrentIndex(hwform->ui.CBGrave->findText(Grave));
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   135
	hwform->CBGrave_activated(Grave);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   136
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   137
	hwform->ui.CBFort->setCurrentIndex(hwform->ui.CBFort->findText(Fort));
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   138
	hwform->CBFort_activated(Fort);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   139
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   140
	for(int i = 0; i < BINDS_NUMBER; i++)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   141
	{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   142
		hwform->CBBind[i]->setCurrentIndex(hwform->CBBind[i]->findText(binds[i].strbind));
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   143
	}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   144
}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   145
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   146
void HWTeam::GetFromPage(HWForm * hwform)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   147
{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   148
	TeamName  = hwform->TeamNameEdit->text();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   149
	for(int i = 0; i < 8; i++)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   150
	{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   151
		HHName[i] = hwform->HHNameEdit[i]->text();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   152
	}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   153
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   154
	Grave = hwform->ui.CBGrave->currentText();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   155
	Fort = hwform->ui.CBFort->currentText();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   156
	for(int i = 0; i < 8; i++)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   157
	{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   158
		binds[i].strbind = hwform->CBBind[i]->currentText();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   159
	}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   160
}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   161
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   162
void HWTeam::SetCfgDir(const QString & dir)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   163
{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   164
	this->dir = dir;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   165
}