This file is indexed.

/usr/include/osl/ntesuki/ntesukiSimulationSearcherProof.tcc is in libosl-dev 0.8.0-1.4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/* ntesukiSimulationProof.tcc
 */
#include "osl/ntesuki/ntesukiSimulationSearcher.h"
#include "osl/ntesuki/oracleProverLight.h"
#include "osl/ntesuki/ntesukiExceptions.h"
#include "osl/ntesuki/ntesukiRecord.h"
#include "osl/container/moveVector.h"
#include "osl/move_classifier/safeMove.h"
#include "osl/apply_move/applyMoveWithPath.h"
#include "osl/checkmate/immediateCheckmate.h"
#include "osl/effect_util/effectUtil.h"
#ifdef NDEBUG
# include "osl/ntesuki/ntesukiMove.tcc"
# include "osl/ntesuki/ntesukiRecord.tcc"
//# include "osl/move_generator/escape.tcc"
#endif

using namespace osl;
using namespace osl::ntesuki;

#ifndef RETURN
#ifndef NDEBUG
#define RETURN \
  ntesuki_assert(result.isCheckmateSuccess() ==\
   record->getValueWithPath<A>(pass_left, path).isCheckmateSuccess());\
  if (record->getValueWithPath<A>(pass_left, path).proof() == 0)\
    ntesuki_assert(record->getValueWithPath<A>(pass_left, path).disproof() > ProofDisproof::DISPROOF_LIMIT);\
  if (record->getValueWithPath<A>(pass_left, path).disproof() == 0)\
    ntesuki_assert(record->getValueWithPath<A>(pass_left, path).proof() > ProofDisproof::PROOF_LIMIT);\
  return
#else
#define RETURN return
#endif
#endif


/* Helper classes
 */
template <class Searcher, Player P>
class
NtesukiSimulationSearcher::
AttackHelperProof
{
  Searcher* searcher;
  NtesukiRecord *record;
  const NtesukiRecord *record_orig;
  unsigned int pass_left;
  const Move last_move;
public:
  AttackHelperProof(Searcher* searcher,
		    NtesukiRecord* record,
		    const NtesukiRecord* record_orig,
		    unsigned int pass_left,
		    const Move last_move)
    : searcher(searcher),
      record(record), record_orig(record_orig),
      pass_left(pass_left),
      last_move(last_move)
  {}

  void operator()(Square p)
  {
    (*searcher).template defenseForProof<PlayerTraits<P>::opponent>
      (record, record_orig, pass_left, last_move);
  }
};
      
template <class Searcher, Player P>
class NtesukiSimulationSearcher::
DefenseHelperProof
{
  Searcher* searcher;
  NtesukiRecord *record;
  const NtesukiRecord *record_orig;
  unsigned int pass_left;
  const Move last_move;
public:
  DefenseHelperProof(Searcher* searcher,
		     NtesukiRecord* record,
		     const NtesukiRecord* record_orig,
		     unsigned int pass_left,
		     const Move last_move)
    : searcher(searcher),
      record(record), record_orig(record_orig),
      pass_left(pass_left),
      last_move(last_move)
  {}

  void operator()(Square p)
  {
    (*searcher).template attackForProof<PlayerTraits<P>::opponent>
      (record, record_orig, pass_left, last_move);
  }
};

/* ===================
 * Count the increase of child nodes
 */
class CountChildLock
{
public:
  CountChildLock(NtesukiRecord* r,
		 const NtesukiTable& t)
    : record(r), table(t)
  {
    size_start = table.size();
  }

  ~CountChildLock()
  {
    record->addChildCount(table.size() - size_start);
  }
private:
  osl::ntesuki::NtesukiRecord* record;
  const osl::ntesuki::NtesukiTable& table;
  unsigned int size_start;
};

/*======================================================================
 * Proof
 *======================================================================
 */

/**
 * Still cannot see if is safe move
 */
template <Player P>
bool
NtesukiSimulationSearcher::
isSafeMove(const Move move,
	   int pass_left)
{
  if (!state.isValidMove(move, false)) return false;

  if (move.isDrop()) return true;

  return move_classifier::SafeMove<P>::isMember(state, move.ptype(), move.from(), move.to());
}

template <Player P>
void
NtesukiSimulationSearcher::
attackForProof(NtesukiRecord* record,
	       const NtesukiRecord* record_orig,
	       const unsigned int pass_left,
	       const Move last_move)
{
  CountChildLock cclock(record, table);
  const Player A = P;
#ifndef NDEBUG
  const Player O = PlayerTraits<P>::opponent;
#endif
  ++node_count;
  ntesuki_assert(P == state.turn());

  ntesuki_assert(record);
  ntesuki_assert(!record->getValueWithPath<A>(pass_left, path).isFinal());
  ntesuki_assert(record->getBestMove<A>(pass_left).isInvalid());

  ntesuki_assert(record_orig);
  ntesuki_assert(record_orig->getValueWithPath<A>(pass_left, path).isCheckmateSuccess());
  ntesuki_assert(record_orig->getBestMove<A>(pass_left).isValid());

  ntesuki_assert(!state.inCheck(O));

  if (record->isVisited())
  {
    result = ProofDisproof::LoopDetection();
    RETURN;
  }

  /* 深さ固定 checkmate searcher を呼び出す */
  if (record->setUpNode<P>())
  {
    const NtesukiResult result_cur = record->getValueWithPath<A>(pass_left, path);
    if (result_cur.isCheckmateSuccess())
    {
      /* Immediate Checkmate */
      result = ProofDisproof::Checkmate();  
      RETURN;
    }
    else if (result_cur.isFinal())
    {
      result = result_cur;
      RETURN;
    }
  }

  /* Simulation 元が immediate checkmate ならこの先は simulate できない */
  const NtesukiMove best_move_orig = record_orig->getBestMove<A>(pass_left);
  if (best_move_orig.isImmediateCheckmate())
  {
    result = ProofDisproof::NoCheckmate();
    RETURN;
  }

  NtesukiRecord::VisitLock visitLock(record);
  /* n が少ないときの結果を参照 */
  if ((pass_left > 0) && 
      record_orig->getValueWithPath<A>(pass_left - 1, path)
      .isCheckmateSuccess())
  {
    if (record->getValueWithPath<A>(pass_left - 1, path)
	.isCheckmateFail())
    {
      result = ProofDisproof::NoCheckmate();
      RETURN;
    }
    
    ntesuki_assert(!record->getValueWithPath<A>(pass_left - 1, path)
		   .isFinal());
    NtesukiRecord::UnVisitLock unVisitLock(record);

    TRY_DFPN;
    attackForProof<P>(record, record_orig, pass_left - 1, last_move);
    CATCH_DFPN;
    RETURN;
  }

  const Move move = adjustMove<P>(best_move_orig.getMove());

  /* invalid move となってしまった */
  if (!move.isValid() ||
      !isSafeMove<P>(move, pass_left))
  {
    result = ProofDisproof::NoCheckmate();
    RETURN;
  }

  const bool move_is_check = (move_classifier::PlayerMoveAdaptor<move_classifier::Check>::
			      isMember(state, move));
  if (0 == pass_left && !move_is_check)
  {
    result = ProofDisproof::NoCheckmate();
    RETURN;
  }
  if (move_is_check != best_move_orig.isCheck())
  {
    result = ProofDisproof::NoCheckmate();
    RETURN;
  }

  /* 以前の bestMove を実行
   */
  NtesukiRecord *record_child = table.allocateWithMove(record, move);
  if (record_child == 0)
  {
    result = ProofDisproof::NoCheckmate();
    RETURN;
  }
  const NtesukiRecord* record_child_orig = table.findWithMoveConst(record_orig,
								   best_move_orig);
  if (!record_child_orig)
  {
    result = ProofDisproof::NoCheckmate();
    RETURN;
  }

  result = record_child->getValueWithPath<A>(pass_left, path);
  if (result.isUnknown())
  {
    AttackHelperProof<NtesukiSimulationSearcher, P> helper(this,
							   record_child,
							   record_child_orig,
							   pass_left,
							   move);
    TRY_DFPN;
    ApplyMoveWithPath<P>::doUndoMove(state, path, move, helper);
    if (record->getValueWithPath<A>(pass_left, path).isFinal())
    {
      result = record->getValueWithPath<A>(pass_left, path);
      RETURN;
    }
    CATCH_DFPN;
  }

  if (result.isPawnDropFoul(move))
  {
    result = ProofDisproof::PawnCheckmate();
    RETURN;
  }
  else if (result.isCheckmateSuccess())
  {
    result = ProofDisproof::Checkmate();
    NtesukiMove best_move(move);

    TRY_DFPN;
    best_move.setCheckmateSuccess<A>(pass_left);
    record->setResult<A>(pass_left, result, best_move, true);
    CATCH_DFPN;
    RETURN;
  }
  else if (result == ProofDisproof::LoopDetection())
  {
    result = ProofDisproof::NoCheckmate();
  }

  ntesuki_assert(result.isCheckmateFail());
  RETURN;
}

template <Player P>
void NtesukiSimulationSearcher::
defenseForProof(NtesukiRecord* record,
		const NtesukiRecord* record_orig,
		const unsigned int pass_left,
		const Move last_move)
{
  CountChildLock cclock(record, table);
  const Player A = PlayerTraits<P>::opponent;
  const Player O = PlayerTraits<P>::opponent;

  ++node_count;
  ntesuki_assert(P == state.turn());

  ntesuki_assert(record);
  if (!record_orig)
  {
    result = ProofDisproof::NoCheckmate();
    return;
  }

  if (state.inCheck(O))
  {
    //the previous move was a drop move that did not resolve a check
    result = ProofDisproof::NoCheckmate();
    return;
  }

  /* 深さ固定 checkmate searcher を呼び出す */
  if (record->setUpNode<P>())
  {
    result = record->getValueWithPath<A>(pass_left, path);
    if (result.isFinal())
    {
      return;
    }
  }

  /* 元のシナリオが間違っている */
  if (!record_orig->getValueWithPath<A>(pass_left, path).isCheckmateSuccess())
  {
    result = ProofDisproof::NoCheckmate();
    return;
  }

  if (record->isVisited())
  {
    result = ProofDisproof::LoopDetection();
    RETURN;
  }
  NtesukiRecord::VisitLock visitLock(record);

  /* 攻撃側に王手がかかっていないか調べる */
  const bool invalidAttack = state.inCheck(PlayerTraits<P>::opponent);
  if (invalidAttack)
  {
    result = ProofDisproof::AttackBack();
    RETURN;
  }

  /* n が少ないときの結果を参照 */
  if (pass_left > 0 &&
      record_orig->getValueWithPath<A>(pass_left - 1, path)
      .isCheckmateSuccess())
  {
    result = (record->getValueWithPath<A>(pass_left - 1, path));
    if(result.isCheckmateFail())
    {
      RETURN;
    }
    ntesuki_assert(!record->getValueWithPath<A>(pass_left - 1, path)
		   .isFinal());
    NtesukiRecord::UnVisitLock unVisitLock(record);

    TRY_DFPN;
    defenseForProof<P>(record, record_orig, pass_left - 1, last_move);
    CATCH_DFPN;
    RETURN;
  }

  /*
   * 受ける手の実行
   */
  /*
   * 守り側の手を生成する
   *  - 王手がかかっているか調べる(王手がかかっていた場合には逃げる手を生成する)
   *  - そうでないなら,通常の手生成を行う
   */
  NtesukiMoveList moves;
  record->generateMoves<P>(moves, 0, true);
  result = ProofDisproof::Checkmate();

  if (moves.empty())
  {
    result = ProofDisproof::NoEscape();
    TRY_DFPN;
    record->setResult<A>(pass_left, result,
			 NtesukiMove::INVALID(), false);
    
    CATCH_DFPN;
    RETURN;
  }

  bool some_moves_not_generated = false;
  for (NtesukiMoveList::iterator move_it = moves.begin();
       move_it != moves.end(); move_it++)
  {
    NtesukiMove& move = *move_it;
    if (move.isCheckmateSuccess<A>(pass_left)) continue;

    /* 逆王手は読んでいない可能性がある */
    if (isscheme != NtesukiRecord::normal_is &&
	isscheme != NtesukiRecord::delay_is &&
	move.isCheck() && pass_left > 0) continue;

    /* シミュレーション元の子を探す */
    const NtesukiRecord* record_child_orig = table.findWithMoveConst(record_orig, move);
    if (!record_child_orig ||
	!record_child_orig->getValue<A>(pass_left).isCheckmateSuccess())
    {
      some_moves_not_generated = true;
      continue;
    }

    NtesukiRecord *record_child = table.allocateWithMove(record, move);
    if (record_child == 0)
    {
      result = ProofDisproof::NoCheckmate();
      RETURN;
    }

    if(record_child->isVisited())
    {
      result = ProofDisproof::LoopDetection();
      record->setLoopWithPath<A>(pass_left, path);
      TRY_DFPN;
      record->setResult<A>(pass_left, NtesukiResult(1, 1),
				  NtesukiMove::INVALID(), false);
      CATCH_DFPN;
      RETURN;
    }

    int pass_left_child = pass_left;
    if (move.isPass()) --pass_left_child;
    const PathEncoding path_child(path, move.getMove());
    result = record_child->getValueWithPath<A>(pass_left_child, path_child);
    if (result.isUnknown())
    {
      DefenseHelperProof<NtesukiSimulationSearcher, P> helper(this,
							      record_child,
							      record_child_orig,
							      pass_left_child,
							      move.getMove());
      TRY_DFPN;
      ApplyMoveWithPath<P>::doUndoMoveOrPass(state, path,
					     move.getMove(),
					     helper);
      CATCH_DFPN;
      if (record->getValueWithPath<A>(pass_left, path).isFinal())
      {
	result = record->getValueWithPath<A>(pass_left, path);
	RETURN;
      }
    }

    if (result.isCheckmateFail())
    {
      RETURN;
    }
  }

  ntesuki_assert(result.isCheckmateSuccess());

  if (some_moves_not_generated)
  {
    result = ProofDisproof::NoCheckmate();
  }
  else
  {
    TRY_DFPN;
    record->setResult<A>(pass_left, result,
			 NtesukiMove::INVALID(), true);
    CATCH_DFPN;
  }
  RETURN;
}

/* Public interface
 */
template <Player P>
bool
NtesukiSimulationSearcher::
startFromAttackProof(NtesukiRecord* record,
		     const NtesukiRecord* record_orig,
		     const unsigned int pass_left,
		     const Move last_move)
{
  assert(P == state.turn());

  const Player A = P;
  ntesuki_assert(record);
  if (!record_orig)
  {
    return false;
  }

  if (!record_orig->getValueWithPath<A>(pass_left, path).isCheckmateSuccess())
  {
    return false;
  }

  if (!record->isDominatedByProofPieces<A>(record_orig, pass_left))
  {
    return false;
  }

  TRY_DFPN;
  if (record->setUpNode<P>())
  {
    if (record->getValueWithPath<A>(pass_left, path).isCheckmateSuccess())
    {
      /* Immediate Checkmate */
      result = ProofDisproof::Checkmate();  
      return true;
    }
    else if (record->getValueWithPath<A>(pass_left, path).isCheckmateFail())
    {
      result = ProofDisproof::NoCheckmate();  
      return false;
    }
  }
  CATCH_DFPN;

  TRY_DFPN;
  const NtesukiMove m = (record_orig->getBestMove<A>(pass_left));
  if (m.isImmediateCheckmate()) return false;
  CATCH_DFPN;

  ++proof_count;

  TRY_DFPN;
  OracleProverLight light(state, mg, path, table,isscheme);
  if (light.startFromAttack<P>(record, record_orig, pass_left))
  {
    ++proof_success_count;
    ++light_proof_success_count;
    ntesuki_assert(record->getValueWithPath<A>(pass_left, path)
		   .isCheckmateSuccess());
    return true;
  }
  else if (record->getValueWithPath<A>(pass_left, path).isCheckmateFail())
  {
    result = ProofDisproof::NoCheckmate();  
    return false;
  }
  CATCH_DFPN;

  TRY_DFPN;
  attackForProof<P>(record, record_orig, pass_left, last_move);
  CATCH_DFPN;
  if (result.isCheckmateSuccess())
  {
    ++proof_success_count;
    return true;
  }
  return false;
}

template <Player P>
bool
NtesukiSimulationSearcher::
startFromDefenseProof(NtesukiRecord* record,
		      const NtesukiRecord* record_orig,
		      const unsigned int pass_left,
		      const Move last_move)
{
  assert(P == state.turn());

  const Player A = PlayerTraits<P>::opponent;
  ntesuki_assert(record);
  if (!record_orig ||
      !record_orig->getValueWithPath<A>(pass_left, path).
      isCheckmateSuccess())
  {
    return false;
  }

  if (!record->isDominatedByProofPieces<A>(record_orig, pass_left))
  {
    return false;
  }

  if (record->setUpNode<P>())
  {
    if (record->getValueWithPath<A>(pass_left, path).isCheckmateSuccess())
    {
      /* Immediate Checkmate */
      result = ProofDisproof::Checkmate();  
      return true;
    }
    else if (record->getValueWithPath<A>(pass_left, path).isCheckmateFail())
    {
      result = ProofDisproof::NoCheckmate();  
      return false;
    }
  }

  const NtesukiMove m = (record_orig->getBestMove<A>(pass_left));
  if (m.isImmediateCheckmate()) return false;

  ++proof_count;

  OracleProverLight light(state, mg, path, table, isscheme);
  TRY_DFPN;
  if (light.startFromDefense<P>(record, record_orig, pass_left))
  {
    ++proof_success_count;
    ++light_proof_success_count;
    ntesuki_assert(record->getValueWithPath<A>(pass_left, path).
		   isCheckmateSuccess());
    return true;
  }
  else if (record->getValueWithPath<A>(pass_left, path).isCheckmateFail())
  {
    result = ProofDisproof::NoCheckmate();  
    return false;
  }
  CATCH_DFPN;

  TRY_DFPN;
  defenseForProof<P>(record, record_orig, pass_left, last_move);
  CATCH_DFPN;

  if (result.isCheckmateSuccess())
  {
    ++proof_success_count;
    return true;
  }
  return false;
}

#undef RETURN

// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: