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

Last change on this file since 657 was 657, checked in by Sam Hocevar, 11 years ago

game: refactor the mouse/game coordinate conversion logic.

File size: 6.5 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;
31int last_demo_mx,last_demo_my,last_demo_mbut;
32extern base_memory_struct *base;   // points to shm_addr
33extern int idle_ticks;
34
35extern void net_receive();
36extern void net_send(int force);
37extern void fade_in(image *im, int steps);
38extern void fade_out(int steps);
39
40void get_event(Event &ev)
41{ wm->get_event(ev);
42  switch (ev.type)
43  {
44    case EV_KEY :
45    {
46      if (demo_man.state==demo_manager::PLAYING)
47        demo_man.set_state(demo_manager::NORMAL);
48      else if (ev.key==JK_ENTER && demo_man.state==demo_manager::RECORDING)
49      {
50        demo_man.set_state(demo_manager::NORMAL);
51    the_game->show_help("Finished recording");
52      }
53    } break;
54  }
55
56  last_demo_mx=ev.mouse_move.x;
57  last_demo_my=ev.mouse_move.y;
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->focus) v->reset_player(); }
147
148  last_demo_mx=last_demo_my=last_demo_mbut=0;
149  current_level->set_tick_counter(0);
150
151}
152
153int demo_manager::start_playing(char *filename)
154{
155  uint8_t sig[15];
156  record_file=open_file(filename,"rb");
157  if (record_file->open_failure()) { delete record_file; return 0; }
158  char name[100],nsize,diff;
159  if (record_file->read(sig,14)!=14        ||
160      memcmp(sig,"DEMO,VERSION:2",14)!=0   ||
161      record_file->read(&nsize,1)!=1       ||
162      record_file->read(name,nsize)!=nsize ||
163      record_file->read(&diff,1)!=1)
164  { delete record_file; return 0; }
165
166  char tname[100],*c;
167  strcpy(tname,name);
168  c=tname;
169  while (*c) { if (*c=='\\') *c='/'; c++; }
170
171  bFILE *probe=open_file(tname,"rb");   // see if the level still exists?
172  if (probe->open_failure()) { delete record_file; delete probe; return 0; }
173  delete probe;
174
175  the_game->load_level(tname);
176  initial_difficulty = l_difficulty;
177
178  switch (diff)
179  {
180    case 0: l_difficulty->SetValue(l_easy); break;
181    case 1: l_difficulty->SetValue(l_medium); break;
182    case 2: l_difficulty->SetValue(l_hard); break;
183    case 3: l_difficulty->SetValue(l_extreme); break;
184  }
185
186  state=PLAYING;
187  reset_game();
188
189
190
191  return 1;
192}
193
194int demo_manager::set_state(demo_state new_state, char *filename)
195{
196  if (new_state==state) return 1;
197
198  switch (state)
199  {
200    case RECORDING :
201    { delete record_file; } break;
202    case PLAYING :
203    {
204/*
205      fade_in(cache.img(cache.reg("art/help.spe","sell6",SPEC_IMAGE,1)),8);
206      Timer now; now.WaitMs(2000);
207      fade_out(8);
208*/
209      delete record_file;
210      l_difficulty = initial_difficulty;
211      the_game->set_state(MENU_STATE);
212      wm->Push(new Event(ID_NULL,NULL));
213
214      view *v=player_list;
215      for (; v; v=v->next)  // reset all the players
216      { if (v->focus) { v->reset_player(); v->focus->set_aistate(0); } }
217      delete current_level;
218      current_level=NULL;
219      the_game->reset_keymap();
220      base->input_state=INPUT_PROCESSING;
221
222
223    } break;
224    default :
225      break;
226  }
227
228  switch (new_state)
229  {
230    case RECORDING :
231    { return start_recording(filename); } break;
232    case PLAYING :
233    { return start_playing(filename); } break;
234    case NORMAL :
235    { state=NORMAL; } break;
236  }
237
238  return 1;
239}
240
241int demo_manager::save_packet(void *packet, int packet_size)   // returns non 0 if actually saved
242{
243  if (state==RECORDING)
244  {
245    uint16_t ps=lstl(packet_size);
246    if (record_file->write(&ps,2)!=2 ||
247    record_file->write(packet,packet_size)!=packet_size)
248    {
249      set_state(NORMAL);
250      return 0;
251    }
252    return 1;
253  } else return 0;
254}
255
256int demo_manager::get_packet(void *packet, int &packet_size)   // returns non 0 if actually loaded
257{
258  if (state==PLAYING)
259  {
260    uint16_t ps;
261    if (record_file->read(&ps,2)!=2)
262    {
263      set_state(NORMAL);
264      return 0;
265    }
266    ps=lstl(ps);
267
268    if (record_file->read(packet,ps)!=ps)
269    {
270      set_state(NORMAL);
271      return 0;
272    }
273
274    packet_size=ps;
275    return 1;
276  }
277  return 0;
278}
279
Note: See TracBrowser for help on using the repository browser.