source: abuse/trunk/src/demo.cpp @ 676

Last change on this file since 676 was 676, checked in by Sam Hocevar, 8 years ago

game: prefix a few class members with m_ to avoid confusion.

File size: 6.4 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "common.h"
16
17#include "game.h"
18
19#include "demo.h"
20#include "specs.h"
21#include "jwindow.h"
22#include "dprint.h"
23#include "dev.h"
24#include "jrand.h"
25#include "lisp.h"
26#include "clisp.h"
27#include "netface.h"
28
29
30demo_manager demo_man;
31vec2i last_demo_mpos;
32int last_demo_mbut;
33extern base_memory_struct *base;   // points to shm_addr
34extern int idle_ticks;
35
36extern void net_receive();
37extern void net_send(int force);
38extern void fade_in(image *im, int steps);
39extern void fade_out(int steps);
40
41void get_event(Event &ev)
42{ wm->get_event(ev);
43  switch (ev.type)
44  {
45    case EV_KEY :
46    {
47      if (demo_man.state==demo_manager::PLAYING)
48        demo_man.set_state(demo_manager::NORMAL);
49      else if (ev.key==JK_ENTER && demo_man.state==demo_manager::RECORDING)
50      {
51        demo_man.set_state(demo_manager::NORMAL);
52    the_game->show_help("Finished recording");
53      }
54    } break;
55  }
56
57  last_demo_mpos = ev.mouse_move;
58  last_demo_mbut = ev.mouse_button;
59  idle_ticks=0;
60}
61
62int event_waiting()
63{ return wm->IsPending(); }
64
65
66int demo_manager::start_recording(char *filename)
67{
68  if (!current_level) return 0;
69
70  record_file=open_file(filename,"wb");
71  if (record_file->open_failure()) { delete record_file; return 0; }
72
73  char name[100];
74  strcpy(name,current_level->name());
75
76  the_game->load_level(name);
77  record_file->write((void *)"DEMO,VERSION:2",14);
78  record_file->write_uint8(strlen(name)+1);
79  record_file->write(name,strlen(name)+1);
80
81
82  if (DEFINEDP(symbol_value(l_difficulty)))
83  {
84    if (symbol_value(l_difficulty)==l_easy) record_file->write_uint8(0);
85    else if (symbol_value(l_difficulty)==l_medium) record_file->write_uint8(1);
86    else if (symbol_value(l_difficulty)==l_hard) record_file->write_uint8(2);
87    else record_file->write_uint8(3);
88  } else record_file->write_uint8(3);
89
90
91  state=RECORDING;
92
93  reset_game();
94
95  return 1;
96}
97
98void demo_manager::do_inputs()
99{
100  switch (state)
101  {
102    case RECORDING :
103    {
104      base->packet.packet_reset();       // reset input buffer
105      view *p=player_list;               // get current inputs
106      for (; p; p=p->next)
107        if (p->local_player())
108          p->get_input();
109
110      base->packet.write_uint8(SCMD_SYNC);
111      base->packet.write_uint16(make_sync());
112      demo_man.save_packet(base->packet.packet_data(),base->packet.packet_size());
113      process_packet_commands(base->packet.packet_data(),base->packet.packet_size());
114
115    } break;
116    case PLAYING :
117    {
118      uint8_t buf[1500];
119      int size;
120      if (get_packet(buf,size))              // get starting inputs
121      {
122        process_packet_commands(buf, size);
123        vec2i mouse = the_game->GameToMouse(vec2i(player_list->pointer_x,
124                                                  player_list->pointer_y),
125                                            player_list);
126        wm->SetMousePos((small_render ? 2 : 1) * mouse);
127      }
128      else
129      {
130    set_state(NORMAL);
131    return ;
132      }
133    } break;
134    default :
135      break;
136  }
137}
138
139void demo_manager::reset_game()
140{
141  if (dev&EDIT_MODE) toggle_edit_mode();
142  the_game->set_state(RUN_STATE);
143  rand_on=0;
144
145  view *v=player_list;
146  for (; v; v=v->next) { if (v->m_focus) v->reset_player(); }
147
148  last_demo_mpos = vec2i(0, 0);
149  last_demo_mbut = 0;
150  current_level->set_tick_counter(0);
151
152}
153
154int demo_manager::start_playing(char *filename)
155{
156  uint8_t sig[15];
157  record_file=open_file(filename,"rb");
158  if (record_file->open_failure()) { delete record_file; return 0; }
159  char name[100],nsize,diff;
160  if (record_file->read(sig,14)!=14        ||
161      memcmp(sig,"DEMO,VERSION:2",14)!=0   ||
162      record_file->read(&nsize,1)!=1       ||
163      record_file->read(name,nsize)!=nsize ||
164      record_file->read(&diff,1)!=1)
165  { delete record_file; return 0; }
166
167  char tname[100],*c;
168  strcpy(tname,name);
169  c=tname;
170  while (*c) { if (*c=='\\') *c='/'; c++; }
171
172  bFILE *probe=open_file(tname,"rb");   // see if the level still exists?
173  if (probe->open_failure()) { delete record_file; delete probe; return 0; }
174  delete probe;
175
176  the_game->load_level(tname);
177  initial_difficulty = l_difficulty;
178
179  switch (diff)
180  {
181    case 0: l_difficulty->SetValue(l_easy); break;
182    case 1: l_difficulty->SetValue(l_medium); break;
183    case 2: l_difficulty->SetValue(l_hard); break;
184    case 3: l_difficulty->SetValue(l_extreme); break;
185  }
186
187  state=PLAYING;
188  reset_game();
189
190
191
192  return 1;
193}
194
195int demo_manager::set_state(demo_state new_state, char *filename)
196{
197  if (new_state==state) return 1;
198
199  switch (state)
200  {
201    case RECORDING :
202    { delete record_file; } break;
203    case PLAYING :
204    {
205/*
206      fade_in(cache.img(cache.reg("art/help.spe","sell6",SPEC_IMAGE,1)),8);
207      Timer now; now.WaitMs(2000);
208      fade_out(8);
209*/
210      delete record_file;
211      l_difficulty = initial_difficulty;
212      the_game->set_state(MENU_STATE);
213      wm->Push(new Event(ID_NULL,NULL));
214
215      view *v=player_list;
216      for (; v; v=v->next)  // reset all the players
217      { if (v->m_focus) { v->reset_player(); v->m_focus->set_aistate(0); } }
218      delete current_level;
219      current_level=NULL;
220      the_game->reset_keymap();
221      base->input_state=INPUT_PROCESSING;
222
223
224    } break;
225    default :
226      break;
227  }
228
229  switch (new_state)
230  {
231    case RECORDING :
232    { return start_recording(filename); } break;
233    case PLAYING :
234    { return start_playing(filename); } break;
235    case NORMAL :
236    { state=NORMAL; } break;
237  }
238
239  return 1;
240}
241
242int demo_manager::save_packet(void *packet, int packet_size)   // returns non 0 if actually saved
243{
244  if (state==RECORDING)
245  {
246    uint16_t ps=lstl(packet_size);
247    if (record_file->write(&ps,2)!=2 ||
248    record_file->write(packet,packet_size)!=packet_size)
249    {
250      set_state(NORMAL);
251      return 0;
252    }
253    return 1;
254  } else return 0;
255}
256
257int demo_manager::get_packet(void *packet, int &packet_size)   // returns non 0 if actually loaded
258{
259  if (state==PLAYING)
260  {
261    uint16_t ps;
262    if (record_file->read(&ps,2)!=2)
263    {
264      set_state(NORMAL);
265      return 0;
266    }
267    ps=lstl(ps);
268
269    if (record_file->read(packet,ps)!=ps)
270    {
271      set_state(NORMAL);
272      return 0;
273    }
274
275    packet_size=ps;
276    return 1;
277  }
278  return 0;
279}
280
Note: See TracBrowser for help on using the repository browser.