File: edit.c

package info (click to toggle)
xbattle 5.4.1-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 736 kB
  • ctags: 1,086
  • sloc: ansic: 9,102; sh: 845; makefile: 493
file content (306 lines) | stat: -rw-r--r-- 7,144 bytes parent folder | download | duplicates (7)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <stdio.h>

#include "constant.h"
  
/**** x include files ****/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>

#if USE_LONGJMP
#include <setjmp.h>
#endif

#include "extern.h"
#include "edit.h"

/******************************************************************************
  edit_board ()

  Allows the user to interactively edit the board, placing troops, terrain,
  bases, and towns as desired.
******************************************************************************/

edit_board ()
{
  XEvent event;

  int player,
      current_side,
      base_side,
      value,
      textcount,
      tdir[MAX_DIRECTIONS];

  short max_value;

  char text[20];

  KeySym key;

  cell_type *cell;

  player = 0;
  current_side = Config->player_to_side[player];
  base_side = current_side;

  /** Draw the baseline board **/

  draw_board (player, TRUE);

  /** Loop forever **/

  while (TRUE)
  {
    XNextEvent (XWindow[player]->display, &event);

    cell = get_cell (event.xbutton.x, event.xbutton.y, tdir,
			base_side, FALSE);

    /** Find out what type of action to take **/

    switch (event.type)
    {
      /** Redraw whole board **/

      case Expose:
        draw_board (player, TRUE);
        break;

      /** Add terrain **/

      case ButtonPress:
        /** Decrement/increment terrain with left and middle buttons **/

        if (event.xbutton.button == Button1)
          cell->level -= 1;
        else if (event.xbutton.button == Button2)
          cell->level += 1;
        else
        {
          /** Else clear to default terrain with right button **/

          cell->level = 0;
        }

        /** Make sure terrain level within proper bounds **/

        if (cell->level < Config->level_min)
          cell->level = Config->level_min;
        else if (cell->level > Config->level_max)
          cell->level = Config->level_max;

        if (cell->level < 0)
        {
          if (cell->side != SIDE_NONE)
          {
            cell->value[cell->side] = 0;
            cell->side = SIDE_NONE;
          }

          cell->growth = 0;
          cell->old_growth = 0;
          cell->angle = 0;
        }

        /** Need to redraw entire polygon, not just erase contents **/

        cell->redraw_status = REDRAW_FULL;

        /** Draw the newly altered cell **/

        draw_cell (cell, player, TRUE);

        cell->redraw_status = REDRAW_NORMAL;
        break;

      case KeyPress:
        
        textcount = XLookupString((XKeyEvent *)&event, text, 10, &key, NULL);
        if (textcount == 0)
          break;

        switch (text[0])
        {
          /** Create city, town, or village **/

          case EDIT_CREATE_CITY:
          case EDIT_CREATE_TOWN:
          case EDIT_CREATE_VILLAGE:

            if (cell->level < 0)
              break;
          
            if (text[0] == EDIT_CREATE_CITY)
              cell->growth = TOWN_MAX;
            else if (text[0] == EDIT_CREATE_TOWN)
              cell->growth = (TOWN_MAX-TOWN_MIN)/2 + TOWN_MIN;
            else
              cell->growth = TOWN_MIN;

            cell->old_growth = cell->growth;
            cell->angle = ANGLE_FULL;
            break;

          /** Partially scuttle a town **/

          case EDIT_SCUTTLE_TOWN:

            if (cell->old_growth > TOWN_MIN)
            {
              cell->growth = 0;
              cell->angle -= EDIT_SCUTTLE_STEP;

              if (cell->angle < ANGLE_ROUND_DOWN)
              {
                cell->growth = 0;
                cell->old_growth = 0;
                cell->angle = 0;
              }
            }
            break;

          /** Partially build a town **/

          case EDIT_BUILD_TOWN:

            if (cell->old_growth > TOWN_MIN)
            {
              cell->angle += EDIT_BUILD_STEP;

              if (cell->angle > ANGLE_ROUND_UP)
              {
                cell->growth = cell->old_growth;
                cell->angle = ANGLE_FULL;
              }
            }
            break;

          /** Shrink a town **/

          case EDIT_SHRINK_TOWN:

            if (cell->growth > TOWN_MIN)
            {
              cell->growth *= EDIT_SHRINK_FACTOR;
              cell->old_growth = cell->growth;
            }

            if (cell->growth < TOWN_MIN)
            {
              cell->growth = 0;
              cell->old_growth = 0;
              cell->angle = 0;
            }
            break;

          /** Enlarge a town **/

          case EDIT_ENLARGE_TOWN:

            if (cell->growth > TOWN_MIN)
            {
              cell->growth *= EDIT_ENLARGE_FACTOR;
              cell->old_growth = cell->growth;
            }

            if (cell->growth > TOWN_MAX)
            {
              cell->growth = TOWN_MAX;
              cell->old_growth = TOWN_MAX;
            }
            break;

          /** Rotate active side to next side **/

          case EDIT_ROTATE_SIDE:

            current_side += 1;
            if (current_side >= Config->side_count)
              current_side = 0;
            break;

          /** Rotate troop in cell to next side **/

          case EDIT_ROTATE_TROOPS:

            if (cell->side != SIDE_NONE)
            {
              value = cell->value[cell->side];
              cell->value[cell->side] = 0;
              cell->side += 1;
              if (cell->side >= Config->side_count)
                cell->side = 0;
              cell->value[cell->side] = value;
            }
            break;

          /** Add (X-'0') troops of current_side to cell **/

          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
          case '0':
          case EDIT_INCREMENT_TROOPS:
          case EDIT_DECREMENT_TROOPS:

            if (cell->level < 0)
              break;

            if (cell->side == SIDE_NONE)
              cell->side = current_side;

            max_value =
		Board->shapes[cell->side][cell->shape_index]->max_value;

            if (text[0] == EDIT_INCREMENT_TROOPS)
              cell->value[cell->side] += 1;
            else if (text[0] == EDIT_DECREMENT_TROOPS)
            {
              if (cell->value[cell->side] > 0)
                cell->value[cell->side] -= 1;
            }
            else
              cell->value[cell->side] =
		(text[0] - '0')*max_value/9;


            if (cell->value[cell->side] == 0)
              cell->side = SIDE_NONE;
            else if (cell->value[cell->side] > max_value)
                cell->value[cell->side] = max_value;
            break;

          /** Dump the board to a file **/

          case EDIT_DUMP_BOARD:

            dump_board (Config->file_store_map, Config->use_brief_load);
            draw_board (player, TRUE);
            break;

          /** Exit **/

          case EDIT_EXIT:

            exit (0);
            break;
        }

        /** Draw the newly altered cell **/

        if (cell != NULL)
          draw_cell (cell, player, TRUE);

        break;
    }
  }
}