File: groups.c

package info (click to toggle)
ale-clone 1.15pre16.3-1
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 2,432 kB
  • ctags: 3,217
  • sloc: ansic: 37,483; sh: 1,105; makefile: 343
file content (143 lines) | stat: -rw-r--r-- 2,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
**	A clone of a famous game.
*/
/**@name groups.c	-	The units' groups handling. */
/*
**	(c) Copyright 1999 by Patrice Fortier
**
**	$Id: groups.c,v 1.1 1999/06/18 00:55:57 root Exp $
*/

//@{

/*----------------------------------------------------------------------------
--	Includes
----------------------------------------------------------------------------*/

#include <stdio.h>

#include "clone.h"
#include "video.h"
#include "sound_id.h"
#include "unitsound.h"
#include "unittype.h"
#include "player.h"
#include "unit.h"
#include "interface.h"
#include "tileset.h"
#include "map.h"


/*----------------------------------------------------------------------------
--	Variables
----------------------------------------------------------------------------*/

// FIXME?: wanted to use 'SelectionGroup' but it is already used
// in the unitsound.c file...
typedef struct _group_ {
    Unit *units[NUM_UNITS_PER_GROUP];
    int num_units;
} UnitGroup;	/// group of units

global UnitGroup Groups[NUM_GROUPS];	/// Number of groups predefined

/*----------------------------------------------------------------------------
--	Functions
----------------------------------------------------------------------------*/

/**
 **	Initialize group part.
 */
global void InitGroups(void)
{
    int i;

    for( i=0; i<NUM_GROUPS; i++ ) {
        Groups[i].num_units=0;
    }
}

/**
 **	Return the number of units of group #num
 */
global int GetNumberUnitsOfGroup(int num)
{
    return Groups[num].num_units;
}

/**
 **	Return the units of group #num
 */
global Unit** GetUnitsOfGroup(int num)
{
    return Groups[num].units;
}

/**
 **	Clear contents of group #num
 */
global void ClearGroup(int num)
{
    UnitGroup *group;
    int i;

    group=&Groups[num];
    for( i=0; i<group->num_units; i++ ) {
	group->units[i]->GroupId=-1;
	group->units[i]=NoUnitP;
    }
    group->num_units=0;
}

/**
 **	Set group #num contents to unit array "units"
 */
global void SetGroup(Unit **units,int nunits,int num)
{
    UnitGroup *group;
    int i;

    DebugCheck(num>NUM_GROUPS);

    ClearGroup(num);

    group=&Groups[num];
    for( i=0; i<nunits; i++ ) {
        group->units[i]=units[i];
	group->units[i]->GroupId=num;
    }
    group->num_units=nunits;
}

/**
 **	Remove unit from its group
 */
global void RemoveUnitFromGroup(Unit *unit)
{
    UnitGroup *group;
    int num;
    int i;

    // unit doesn't belong to a group
    if( (num=unit->GroupId)==-1 ) {
        return;
    }

    group=&Groups[num];
    for( i=0; group->units[i]!=unit; i++ ) {
	;
    }

    DebugCheck( i>=group->num_units );

    // This is a clean way that will allow us to add a unit
    // to a group easily, or make an easy array walk...
    if( i<--group->num_units ) {
        group->units[i]=group->units[group->num_units];
    }
    group->units[group->num_units]=NoUnitP;

    unit->GroupId=-1;
}

//@}