src/helpers.cpp
author celestar
Mon, 19 Mar 2007 09:33:17 +0000
branchgamebalance
changeset 9894 70d78ac95d6c
parent 6505 abcb0580d976
child 6308 646711c5feaa
permissions -rw-r--r--
(svn r9310) [gamebalance] -Feature: Player performance now influences the wealth level of a town (albeit only on a small scale). This is the first feedback effect that the player has on the local and global economy. Please refrain from using the AI too much for the time being because it'll trash the ratings most likely.
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     1
/* $Id$ */
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
     2
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
     3
/** @file helpers.cpp */
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
     4
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     5
#include "stdafx.h"
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     6
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     7
#include "openttd.h"
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     8
#include "engine.h"
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     9
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    10
#include <new>
5884
be0c8467aeb4 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents: 5838
diff changeset
    11
#include "misc/blob.hpp"
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    12
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    13
/* Engine list manipulators - current implementation is only C wrapper around CBlobT<EngineID> (see yapf/blob.hpp) */
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    14
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    15
/* we cannot expose CBlobT directly to C so we must cast EngineList* to CBlobT<EngineID>* always when we are called from C */
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    16
#define B (*(CBlobT<EngineID>*)el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    17
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    18
/** Create Engine List (and initialize it to empty)
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    19
 * @param el list to be created
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    20
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    21
void EngList_Create(EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    22
{
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    23
	/* call CBlobT constructor explicitly */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    24
	new (&B) CBlobT<EngineID>();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    25
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    26
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    27
/** Destroy Engine List (and free its contents)
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    28
 * @param el list to be destroyed
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    29
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    30
void EngList_Destroy(EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    31
{
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    32
	/* call CBlobT destructor explicitly */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    33
	B.~CBlobT<EngineID>();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    34
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    35
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    36
/** Return number of items stored in the Engine List
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    37
 * @param el list for count inquiry
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    38
 * @return the desired count
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    39
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    40
uint EngList_Count(const EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    41
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    42
	return B.Size();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    43
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    44
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    45
/** Add new item at the end of Engine List
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    46
 * @param el list o which to add an engine
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    47
 * @param eif engine to add to the list
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    48
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    49
void EngList_Add(EngineList *el, EngineID eid)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    50
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    51
	B.Append(eid);
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    52
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    53
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    54
/** Return pointer to the items array held by Engine List
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    55
 * @param el list from which the array pointer has to be returned
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    56
 * @return the pointer required
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    57
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    58
EngineID* EngList_Items(EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    59
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    60
	return B.Data();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    61
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    62
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    63
/** Clear the Engine List (by invalidating all its items == reseting item count to zero)
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    64
 * @param el list to be cleared
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    65
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    66
void EngList_RemoveAll(EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    67
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    68
	B.Clear();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    69
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    70
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    71
/** Sort all items using qsort() and given 'CompareItems' function
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    72
 * @param el list to be sorted
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    73
 * @param compare function for evaluation of the quicksort
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    74
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    75
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    76
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    77
	qsort(B.Data(), B.Size(), sizeof(**el), compare);
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    78
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    79
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    80
/** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    81
 * @param el list to be sorted
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    82
 * @param compare function for evaluation of the quicksort
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    83
 * @param begin start of sorting
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    84
 * @param count of items to be sorted
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    85
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    86
void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    87
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    88
	assert(begin <= (uint)B.Size());
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    89
	assert(begin + num_items <= (uint)B.Size());
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    90
	qsort(B.Data() + begin, num_items, sizeof(**el), compare);
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    91
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    92
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    93
#undef B
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    94