source: golgotha/src/golg/human.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 6.2 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "human.hh"
10#include "resources.hh"
11#include "g1_speed.hh"
12#include "input.hh"
13#include "objs/stank.hh"
14#include "map.hh"
15#include "map_man.hh"
16#include "controller.hh"
17#include "cwin_man.hh"
18#include "time/profile.hh"
19#include "player.hh"
20#include "sfx_id.hh"
21#include "lisp/lisp.hh"
22#include "objs/path_object.hh"
23
24g1_human_class *g1_human = 0;
25
26void g1_human_class::load(g1_loader_class *fp)
27{
28}
29
30class g1_human_def_class : public g1_team_api_definition_class
31{
32public:
33  g1_human_def_class()
34    : g1_team_api_definition_class("human") {}
35
36  virtual g1_team_api_class *create(g1_loader_class *f)
37    {
38    if (!g1_human)
39      own_team_api(new g1_human_class(f));
40
41    return g1_human;
42  }
43} g1_human_def;
44
45g1_human_class::g1_human_class(g1_loader_class *f)
46{
47  mouse_look_increment_x = 0;
48  mouse_look_increment_y = 0;
49  g1_human = this;
50}
51
52g1_human_class::~g1_human_class()
53{
54  g1_human = 0;
55}
56
57void g1_human_class::send_selected_units(i4_float x, i4_float y)
58{   
59  if (selected_object.get())
60    deploy_unit(selected_object->global_id, x,y);
61}
62
63
64static li_symbol_ref allow_follow_mode("allow_follow_mode");
65static li_symbol_ref supergun("supergun");
66
67void g1_human_class::clicked_on_object(g1_object_class *o)
68{
69  if (o && g1_path_object_class::cast(o))
70    set_current_target(o->global_id);
71}
72
73w8 g1_human_class::determine_cursor(g1_object_class *object_mouse_is_on)
74{
75  if (selected_object.valid())
76  {
77    if (selected_object->id == g1_get_object_type(supergun.get()))
78      return G1_TARGET_CURSOR;
79    else
80      return G1_MOVE_CURSOR;
81  }
82  else if (object_mouse_is_on)
83  {   
84    if (g1_path_object_class::cast(object_mouse_is_on))
85        //        g1_path_object_class::cast(object_mouse_is_on)->valid_destination)
86      return G1_MOVE_CURSOR;
87    else
88      return G1_DEFAULT_CURSOR;
89  }
90  else
91    return G1_DEFAULT_CURSOR;
92}
93
94void g1_human_class::player_clicked(g1_object_class *obj, float gx, float gy)
95{
96  if (obj && obj->player_num==team())
97  {
98    if (selected_object.valid())
99      selected_object->mark_as_unselected();
100
101    if (obj == commander())
102      selected_object = 0;
103    else
104    {
105      selected_object = obj;
106      selected_object->mark_as_selected();
107    }
108  }
109  else if (selected_object.valid())
110    send_selected_units(gx,gy);
111  else if (gx>=0 && gy>=0 && gx<g1_get_map()->width() && gy<g1_get_map()->height())
112  {
113    g1_path_object_class *best=0, *best2=0;
114    float best_dist=10000;
115    for (g1_path_object_class *o=g1_path_object_list.first(); o; o=o->next)
116    {
117      int t=o->total_links(player->get_team());
118      for (int i=0; i<t; i++)
119      {
120        g1_path_object_class *o2=o->get_link(player->get_team(), i);
121        if (o2 && !o2->get_flag(g1_object_class::SCRATCH_BIT))
122        {
123          i4_2d_vector p1(o->x, o->y), p2(o2->x, o2->y);
124          i4_2d_vector a=p2-p1;
125          i4_2d_vector x=i4_2d_vector(gx,gy);
126
127          g1_path_object_class *use1=0, *use2=0;
128          float dist;
129
130          float alen=a.length();
131          float t=(x-p1).dot(a)/(alen*alen);
132                               
133          if (t<0)
134            dist=(x-p1).length();
135          else if (t>1)
136            dist=(x-p2).length();
137          else
138            dist=i4_fabs(a.perp_dot(x-p1)/a.length());
139
140          if (dist<best_dist)
141          {
142            best_dist=dist;
143            best=o;
144            best2=o2;
145          }
146        }             
147      }
148    }       
149
150    if (best)
151    {
152      set_current_target(best->global_id);
153      if (best2)
154        set_current_target(best2->global_id);
155    }
156  }
157}
158
159int g1_human_class::build_unit(g1_object_type type)
160{
161  return (playback)? G1_BUILD_PLAYBACK : g1_team_api_class::build_unit(type);
162}
163
164void g1_human_class::think()
165{
166  i4_bool process_input = i4_T;
167
168  if (playback_think())
169    process_input=i4_F;
170
171
172  g1_player_piece_class *stank = commander(); 
173  if ((g1_current_controller->view.get_view_mode()!=G1_ACTION_MODE &&
174       g1_current_controller->view.get_view_mode()!=G1_FOLLOW_MODE) ||
175      !stank || stank->health<=0)
176    process_input=i4_F;
177 
178  if (!process_input)
179  {
180    g1_input.deque_time(g1_input_class::LEFT);
181    g1_input.deque_time(g1_input_class::RIGHT);
182    g1_input.deque_time(g1_input_class::UP);
183    g1_input.deque_time(g1_input_class::DOWN);
184    g1_input.deque_time(g1_input_class::STRAFE_LEFT);
185    g1_input.deque_time(g1_input_class::STRAFE_RIGHT);
186  }
187  else
188  {
189    //keys are buffered in the order pressed, so do a reversed comparison
190    if (memcmp(g1_input.grab_cheat_keys(),"DOG",3)==0)
191    {
192      g1_input.clear_cheat_keys();
193
194      stank->toggle_stank_flag(g1_player_piece_class::ST_GODMODE);
195    }
196   
197    look(mouse_look_increment_x, mouse_look_increment_y);
198    mouse_look_increment_y = mouse_look_increment_x = 0;
199   
200    sw32
201      left_ms=g1_input.deque_time(g1_input_class::LEFT),
202      right_ms=g1_input.deque_time(g1_input_class::RIGHT),
203      up_ms=g1_input.deque_time(g1_input_class::UP),
204      down_ms=g1_input.deque_time(g1_input_class::DOWN),
205      sleft_ms=g1_input.deque_time(g1_input_class::STRAFE_LEFT),
206      sright_ms=g1_input.deque_time(g1_input_class::STRAFE_RIGHT);
207
208    turn( g1_resources.player_turn_speed*(left_ms-right_ms)*G1_HZ/1000.0 );
209    accelerate( (up_ms-down_ms)*G1_HZ/1000.0 );
210    strafe( (sright_ms-sleft_ms)*G1_HZ/1000.0 );
211
212    if (sright_ms>0)
213      sright_ms=sright_ms+1;
214
215    if (g1_input.button_1()) fire0();
216    if (g1_input.button_2()) fire1();
217    if (g1_input.button_3()) fire2();
218  }
219
220
221  if (g1_input.key_pressed) 
222    continue_game();
223
224  g1_input.key_pressed=i4_F;
225}
226
227//{{{ Emacs Locals
228// Local Variables:
229// folded-file: t
230// End:
231//}}}
Note: See TracBrowser for help on using the repository browser.