File: unit.c

package info (click to toggle)
ogamesim 20080831-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 772 kB
  • ctags: 213
  • sloc: ansic: 1,620; perl: 1,041; makefile: 185
file content (143 lines) | stat: -rw-r--r-- 4,075 bytes parent folder | download | duplicates (2)
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
/*
Copyright (C) by Dmitry E. Oboukhov 2006, 2007

  This package is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This package is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this package; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include "unit.h"
#include "units_info.h"
#include "random.h"

#define DEBUG_FIRE 0

//===================================================================
// производит симуляцию одного юнита (против одного юнита)
// возвращает истину если можно еще раз атаковать
//===================================================================
int attack_unit(const unit * attacker, unit * defender)
{
  int power=attacker->attack;
  
  if (DEBUG_FIRE)
  {
    printf("'%s' attack (power %d) to\n\t"
        "'%s' (shield %d/%d, armory %d/%d)", 
        units[attacker->id].name,
        attacker->attack,
        units[defender->id].name, 
        defender->shield,
        defender->max.shield,
        defender->armory,
        defender->max.armory        
    );
  }


  // если щиты выстрел не отражают
  if (defender->shield<power*100)
  {
    // вся мощность ушла в щиты
    if (defender->shield && defender->shield>=power)
    {
      defender->shield-=power;
      power=0;
    }
    // часть мощности ушла в щиты
    else
    {
      power-=defender->shield;
      defender->shield=0;
    }

    // расчитываем урон в структуре

    // убит выстрелом
    if (power>=defender->armory)
    {
      defender->armory=0;
    }
    // мощность выстрела меньше мощности структуры
    else if (power)
    {
      defender->armory-=power;


      if (((defender->max.armory*70)/100)>=defender->armory)
      {
        int level_boom=100*defender->armory/defender->max.armory;
        if (random()%100>level_boom)
        {
          // boom!
          defender->armory=0;
        }
      }
    }
  }
  
  if (DEBUG_FIRE)
  {
    if (defender->armory) printf("\n");
    else printf (", killed!\n");
  }
  
  // вычисляем можно ли стрелять еще раз
  
  int rid=units[attacker->id].rapid;
  int i;

  if (rid==0) return 0;
  
  for (i=0; rapids[rid][i].rf; i++)
  {
    // если стреляли по чуду против которого у нас есть скорострел
    // то вычисляем вероятность следующего выстрела
    if (rapids[rid][i].id==defender->id)
    {
      return random()<rapids[rid][i].chance*RAND_MAX;
    }
  }


  return 0;
}
//===================================================================
// возвращает юнит приведенный к характеристикам игрока
//===================================================================
unit create_unit(int id, int gattack, int gshield, int garmory)
{
  unit u;

  if (id>=sizeof(units)/sizeof(unit_info))
  {
    fprintf(stderr, 
      "error id unit '%d' in function create_unit", id);
    exit(-1);
  }

  u.id=id;
  u.max.shield=units[id].shield;
  u.max.armory=units[id].structure/10;
  u.attack=units[id].attack;

  u.max.shield+=u.max.shield*gshield/10;
  u.max.armory+=u.max.armory*garmory/10;
  u.attack+=u.attack*gattack/10;
  
  u.shield=u.max.shield;
  u.armory=u.max.armory;
  return u;
}
//===================================================================